home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libc / mkdirs.c < prev    next >
C/C++ Source or Header  |  1992-12-06  |  723b  |  41 lines

  1. /*
  2.  * mkdirs - make the directories implied by `name'
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9.  
  10. #define YES 1
  11. #define NO 0
  12.  
  13. #define FNDELIM '/'
  14.  
  15. /*
  16.  * Given a/b/c/d, try to make any of a, a/b, a/b/c and a/b/c/d which are missing;
  17.  * stop on first failure.
  18.  * Returns success.
  19.  */
  20. int
  21. mkdirs(name, uid, gid)
  22. register char *name;
  23. int uid, gid;
  24. {
  25.     register char *cp;
  26.     register int isthere = YES;
  27.     struct stat stbuf;
  28.  
  29.     for (cp = name; isthere && *cp != '\0'; cp++)
  30.         if (*cp == FNDELIM) {
  31.             *cp = '\0';
  32.             isthere = stat(name, &stbuf) >= 0;
  33.             if (!isthere) {
  34.                 isthere = mkdir(name, 0777) >= 0;
  35.                 (void) chown(name, uid, gid);
  36.             }
  37.             *cp = FNDELIM;
  38.         }
  39.     return isthere;
  40. }
  41.