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

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