home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / dir_mgmt / nmkdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  3.0 KB  |  138 lines

  1. 24-Aug-85 04:21:37-MDT,3163;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Sat 24 Aug 85 04:21:30-MDT
  4. Received: from usenet by TGR.BRL.ARPA id a008341; 24 Aug 85 5:45 EDT
  5. From: Robert Rother <rmr@sdcsvax.uucp>
  6. Newsgroups: net.sources
  7. Subject: A new mkdir program "nmkdir".
  8. Message-ID: <1052@sdcsvax.UUCP>
  9. Date: 20 Aug 85 05:13:47 GMT
  10. To:       unix-sources@BRL-TGR.ARPA
  11.  
  12. This is a copy of a program I wrote to use in a software installation
  13. sh script.  The only difference (I hope!) between this program and the
  14. original mkdir is that given the command
  15.  
  16.         nmkdir    a/b/c
  17.     
  18. if any part of the path does not exist, it will make the appropriate
  19. directories.  In other words, just c, b/c or a/b/c.
  20.  
  21.                     Robert Rother
  22.                     Mariah Corporation
  23.  
  24. --------------------------- Cut here ------------------------------
  25. /*
  26. **                NMKDIR.C
  27. **
  28. ** Written by Robert Rother, Mariah Corporation, August 1985. 
  29. **
  30. ** I wrote this out of shear disgust with myself because I couldn't
  31. ** figure out how to do this in /bin/sh.
  32. **
  33. ** If you want it, it's yours.  All I ask in return is that if you
  34. ** figure out how to do this in a Bourne Shell script you send me
  35. ** a copy.
  36. **                    sdcsvax!rmr or rmr@uscd
  37. */
  38.  
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <signal.h>
  42. #include <stdio.h>
  43. #include <errno.h>
  44.  
  45. /*
  46. ** If you are running 4.2BSD you probably want neither of the following,
  47. ** on the other hand if your not running of 4.2BSD you probably want
  48. ** both.
  49. */
  50. #define MKDIR        /* Define this if you do not have the mkdir(2) call. */
  51. #define index    strchr  /* Define this if you do not have the index(3c) call. */
  52.  
  53. int errcnt;
  54. int vflag;
  55.  
  56. extern int errno;
  57.  
  58. main(argc, argv)
  59. int argc;
  60. register char *argv[];
  61. {
  62.  
  63.     while (*++argv) {
  64.         if (**argv == '-' && *(*argv + 1) == 'v')
  65.             vflag++;
  66.         else
  67.             mymkdir(*argv);
  68.     }
  69.     exit(errcnt != 0);
  70. }
  71.  
  72. mymkdir(path)
  73. register char *path;
  74. {
  75.     register char *pptr;
  76.     char *index();
  77.  
  78.     if (!path || !*path) return;
  79.  
  80.     pptr = *path == '/' ?  path + 1 : path;
  81.  
  82.     do
  83.     {
  84.         if (pptr = index(pptr, '/'))
  85.             *pptr = '\0';
  86.  
  87.         if (!access(path, 0)) {
  88.             struct stat buf;
  89.  
  90.             if (stat(path, &buf) < 0) {
  91.                 perror(path); 
  92.                 errcnt++;
  93.                 break;
  94.             }
  95.             if ((buf.st_mode & S_IFMT) != S_IFDIR) {
  96.                 fprintf(stderr, "%s already exists\n", path);
  97.                 errcnt++;
  98.                 break;
  99.             }
  100.         } else {
  101.             if (vflag) printf("making directory %s\n", path);
  102.             if (mkdir(path, 0777) < 0) {
  103.                 fprintf(stderr, "mkdir: ");
  104.                 perror(path);
  105.                 errcnt++;
  106.                 break;
  107.             }
  108.         }
  109.         
  110.         if (pptr) *pptr++ = '/';
  111.     } while (pptr);
  112. }
  113.  
  114. #ifdef MKDIR
  115.  
  116. mkdir(dpath, dmode)
  117. char *dpath;
  118. int dmode;
  119. {
  120.     int tmp, cpid, status;
  121.     register int (*istat)(), (*qstat)();
  122.  
  123.     if ((cpid = fork()) == 0)  {
  124.         execl("/bin/mkdir", "mkdir", dpath, (char *)0);
  125.         return(-1);
  126.     }
  127.     if (cpid != -1)  {
  128.         istat = signal(SIGINT, SIG_IGN);
  129.         qstat = signal(SIGQUIT, SIG_IGN);
  130.         while((tmp = wait(&status)) != cpid)
  131.             if (errno != EINTR) break;
  132.         (void)signal(SIGINT, istat);
  133.         (void)signal(SIGQUIT, qstat);
  134.         return (tmp == -1 ? -1 : 0);
  135.     } return(-1);
  136. }
  137. #endif
  138.