home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / creatdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  2.0 KB  |  70 lines

  1. #include "util.h"
  2. #include <sys/stat.h>
  3.  
  4. /*              create a directory
  5.  *
  6.  *      build intervening directories, if nonexistant
  7.  *      chown new directories to specified uid
  8.  *
  9.  *      some of the system calls are not checked because they can
  10.  *      fail benignly, as when an intermediate directory is executable,
  11.  *      but not readable or writeable.  the only test for ultimate
  12.  *      success is being able to create and stat the final directory.
  13.  */
  14.  
  15. creatdir (dirptr, mode, owner, group)
  16.     register char *dirptr;     /* pathname to directory                */
  17.     int mode;
  18.     int owner,                  /* non-zero uid of for dir owner        */
  19.     group;
  20. {
  21.     extern int errno;
  22.     struct stat statbuf;
  23.     int realid,
  24.     effecid;
  25.     int uid,
  26.     gid;
  27.     char shcmd[128];
  28.     register char *partpath;
  29.     register char *nptr;       /* last char in partial pathname        */
  30.  
  31.     if (dirptr == (char *) 0 || isnull (*dirptr))
  32.      return (NOTOK);      /* programming error                      */
  33.  
  34.     if (owner != 0)             /* coerced ownship requested            */
  35.     {                           /* noop it, if under requested id's     */
  36.     getwho (&realid, &effecid);
  37.     uid = realid;
  38.     getgroup (&realid, &effecid);
  39.     gid = realid;
  40.     if( uid && (uid != owner || gid != group ))
  41.         return( NOTOK );
  42.     }
  43.  
  44.     (void) strcpy (shcmd, "mkdir ");   /* initialize string with command */
  45.     partpath = &shcmd[strlen (shcmd)];
  46.  
  47.     for (nptr = partpath, *nptr++ = *dirptr++; ; *nptr++ = *dirptr++)
  48.     switch (*dirptr)
  49.     {
  50.         case '\0':
  51.         case '/':
  52.         *nptr = '\0';
  53.         if (stat (partpath, &statbuf) < 0)
  54.         {               /* should we try to creat it?           */
  55. #ifndef V4_2BSD
  56.             system (shcmd);
  57.                 /* don't check if it succeeded          */
  58. #else /* V4_2BSD */
  59.             mkdir (partpath);
  60. #endif /* V4_2BSD */
  61.             if (owner != 0)
  62.             chown (partpath, owner, group);
  63.  
  64.             chmod (partpath, mode);
  65.         }
  66.         if (isnull (*dirptr))
  67.             return ((stat (partpath, &statbuf) < 0) ? NOTOK : OK);
  68.     }
  69. }
  70.