/***** * * Copyright (C) 2004, 2005 Pablo Belin * All Rights Reserved * * This file is part of the gprelude program. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * *****/ #include #include #include #include #include #include #include #include #include #include #include "gprelude-mem.h" #include "gprelude-sys.h" static char *gprelude_sys_get_working_directory ( uid_t uid ) { struct passwd passwd, *p_passwd = NULL; char *buff = NULL, *tmp; size_t size = 0; do { size += 8 * sizeof(char); tmp = (char *)realloc ( buff, size ); if ( !tmp ) { log ( LOG_ERR, "memory exhausted.\n" ); gprelude_free ( buff ); return NULL; } buff = tmp; tmp = NULL; memset ( buff, 0, size ); if ( getpwuid_r ( uid, &passwd, buff, size, &p_passwd ) == 0 ) break; } while ( errno == ERANGE || errno == EINTR ); if ( p_passwd ) tmp = gprelude_sprintf ( "%s/%s", p_passwd->pw_dir, GPRELUDE_WORKING_DIRECTORY ); free ( buff ); return tmp; } int gprelude_sys_make_directory ( const char *cwd ) { int ret; ret = mkdir ( cwd, 0700 ); if ( (ret == -1) && (errno != EEXIST) ) return -1; return 0; } char *gprelude_sys_make_working_directory ( uid_t uid ) { char *cwd; int ret; cwd = gprelude_sys_get_working_directory ( uid ); if ( !cwd ) return NULL; ret = gprelude_sys_make_directory ( cwd ); if ( ret == -1 ) { free ( cwd ); return NULL; } return cwd; } char *gprelude_sys_get_path ( uid_t uid, const char *path ) { char *str, *cwd; cwd = gprelude_sys_get_working_directory ( uid ); if ( !cwd ) return NULL; if ( !path ) return cwd; str = gprelude_sprintf ( "%s/%s", cwd, path ); free ( cwd ); return str; } char *gprelude_sys_get_path_uid ( const char *path ) { return gprelude_sys_get_path ( getuid(), path ); }