Subversion Repositories wimsdev

Rev

Rev 8160 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. #include "libwims.h"
  19.  
  20. /* directory manipulation routines. */
  21.  
  22. /* remove a tree */
  23. int remove_tree(char *dirname)
  24. {
  25.   DIR *sdir;
  26.   struct dirent *f;
  27.   struct stat dst;
  28.  
  29.   sdir=opendir(dirname);
  30.   if(sdir==NULL) {   /* Cannot open session directory. */
  31.     return -1;
  32.   }
  33.   while((f=readdir(sdir))!=NULL) {
  34.     char fname[MAX_LINELEN+1];
  35.     if(strcmp(".",f->d_name)==0 || strcmp("..",f->d_name)==0) continue;
  36.     snprintf(fname,sizeof(fname),"%s/%s",dirname,f->d_name);
  37.     if(lstat(fname,&dst)) continue;
  38.     if(S_ISDIR(dst.st_mode)) remove_tree(fname);
  39.     else remove(fname);
  40.   }
  41.   closedir(sdir);
  42.   if(rmdir(dirname)<0) { /* Cannot remove directory. */
  43.     return -1;
  44.   }
  45.   return 0;
  46. }
  47.  
  48. /* recursively generate a directory structure */
  49. void mkdirs(char *s)
  50. {
  51.   struct stat st;
  52.   if(stat(s,&st)==-1) {
  53.     if(strrchr(s,'/')!=NULL) {
  54.       char buf[MAX_FNAME+1];
  55.       mystrncpy(buf,s,sizeof(buf));
  56.       *strrchr(buf,'/')=0; mkdirs(buf);
  57.     }
  58.     mkdir(s,S_IRWXU|S_IRWXG|S_IRWXO);
  59.   }
  60. }
  61.