Back to index

wims  3.65+svn20090927
dir.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* directory manipulation routines. */
00019 
00020        /* remove a tree */
00021 int remove_tree(char *dirname)
00022 {
00023     DIR *sdir;
00024     struct dirent *f;
00025     struct stat dst;
00026 
00027     sdir=opendir(dirname);
00028     if(sdir==NULL) {   /* Cannot open session directory. */
00029        return -1;
00030     }
00031     while((f=readdir(sdir))!=NULL) {
00032        char fname[MAX_LINELEN+1];
00033        if(strcmp(".",f->d_name)==0 || strcmp("..",f->d_name)==0) continue;
00034        snprintf(fname,sizeof(fname),"%s/%s",dirname,f->d_name);
00035        if(lstat(fname,&dst)) continue;
00036        if(S_ISDIR(dst.st_mode)) remove_tree(fname);
00037        else remove(fname);
00038     }
00039     closedir(sdir);
00040     if(rmdir(dirname)<0) {  /* Cannot remove directory. */
00041        return -1;
00042     }
00043     return 0;
00044 }
00045 
00046        /* recursively generate a directory structure */
00047 void mkdirs(char *s)
00048 {
00049     struct stat st;
00050     if(stat(s,&st)==-1) {
00051        if(strrchr(s,'/')!=NULL) {
00052            char buf[MAX_FNAME+1];
00053            mystrncpy(buf,s,sizeof(buf));
00054            *strrchr(buf,'/')=0; mkdirs(buf);
00055        }
00056        mkdir(s,S_IRWXU|S_IRWXG|S_IRWXO);
00057     }
00058 }
00059