home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume19 / rkive / part01 / makedir.c < prev    next >
C/C++ Source or Header  |  1989-06-29  |  4KB  |  168 lines

  1. /*
  2. **
  3. ** This software is Copyright (c) 1989 by Kent Landfield.
  4. **
  5. ** Permission is hereby granted to copy, distribute or otherwise 
  6. ** use any part of this package as long as you do not try to make 
  7. ** money from it or pretend that you wrote it.  This copyright 
  8. ** notice must be maintained in any copy made.
  9. **
  10. **
  11. **  History:
  12. **    Creation: Tue Feb 21 08:52:35 CST 1989 due to necessity.
  13. **                                                               
  14. */
  15. #ifndef lint
  16. static char SID[] = "@(#)makedir.c    1.1 6/1/89";
  17. #endif
  18.  
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <stdio.h>
  22. #include <dirent.h>
  23. #include "rkive.h"
  24.  
  25. extern char *progname;
  26.  
  27. extern int verbose;
  28. extern int test;
  29.  
  30. extern FILE *errfp;
  31. extern FILE *logfp;
  32.  
  33. int makedir(dirpath, mode, owner_id, group_id)
  34.     char *dirpath;
  35.     int mode;
  36.     int owner_id;
  37.     int group_id;
  38. {
  39. #ifndef HAVE_MKDIR
  40. # ifndef USE_SYSMKDIR
  41.  
  42.     char *strcat();
  43.     char *strncpy();
  44.     char *strcpy();
  45.  
  46.     register i;
  47.     register slen = 0;
  48.  
  49.     char parent[MAXNAMLEN];
  50.  
  51. #endif /* USE_SYSMKDIR */
  52.  
  53.     char crnt_dir[MAXNAMLEN];
  54.  
  55. #endif /* HAVE_MKDIR */
  56.     
  57.     if ((strlen(dirpath) == 0) || (dirpath[0] == NULL)) {
  58.         (void) fprintf(errfp,"%s: cannot make %s\n", progname,dirpath);
  59.         return(-1);
  60.     }
  61.  
  62.     if (verbose) {
  63.         (void) fprintf(logfp,"making\t<%s>\n",dirpath);
  64.         if (test) 
  65.             return(0);
  66.     }
  67.  
  68. #ifdef HAVE_MKDIR
  69.  
  70.     /*
  71.     ** mkdir function supplied in system library.
  72.     */
  73.  
  74.     if (mkdir(dirpath,mode) != 0)
  75.         return(-1);
  76.  
  77.     (void) chown(dirpath, owner_id, group_id);
  78.  
  79. #else
  80. # ifdef USE_SYSMKDIR
  81.  
  82.     /* 
  83.     ** Use the system mkdir executable.. why?
  84.     */
  85.  
  86.     /* build up command */
  87.  
  88.     (void) sprintf(crnt_dir, "/bin/mkdir %s", dirpath);
  89.  
  90.     if (system(crnt_dir) != 0)
  91.         return(-1);
  92.  
  93.     (void) chown(dirpath, owner_id, group_id);
  94.     (void) chmod(dirpath, mode);
  95.  
  96. # else
  97.  
  98.     /*
  99.     ** Builtin mkdir function in use ...
  100.     **
  101.     ** Save the parent path
  102.     */
  103.  
  104.     i = 0;
  105.     parent[0] = '\0';
  106.  
  107.     while (dirpath[i]) {
  108.         if (dirpath[i] == '/')
  109.             slen = i + 1;
  110.         ++i;
  111.     }
  112.  
  113.     if (slen)    /* Is there a parent string to save ? */
  114.         (void) strncpy(parent, dirpath, slen);
  115.  
  116.     (void) strcpy(parent+slen, ".");
  117.  
  118.     /* Does the parent directory exist and is it writable ? */
  119.  
  120.     if (access(parent, 02)) {
  121.         (void) fprintf(errfp,"%s: cannot access %s\n", progname,parent);
  122.         return(-1);
  123.     }
  124.  
  125.     /* make the actual directory file */
  126.  
  127.     if ((mknod(dirpath, S_IFDIR | mode, 0)) < 0) {
  128.         (void)fprintf(errfp,"%s: can't make directory %s\n",progname,dirpath);
  129.         return(-1);
  130.     }
  131.  
  132.     (void) chown(dirpath, owner_id, group_id);
  133.  
  134.     /* need to create the "." directory entry */
  135.  
  136.     (void) strcpy(crnt_dir, dirpath);
  137.     (void) strcat(crnt_dir, "/.");
  138.  
  139.     if ((link(dirpath, crnt_dir)) < 0) {
  140.         /* 
  141.         ** Could not link the  directory to it's "." entry.
  142.         ** Clean up and return.
  143.         */
  144.         (void) unlink(dirpath);
  145.         (void) fprintf(errfp, "%s: cannot link [%s]\n", progname,crnt_dir);
  146.         return(-1);
  147.     }
  148.  
  149.     /* need to create the "." directory entry */
  150.  
  151.     (void) strcat(crnt_dir, ".");
  152.  
  153.     if ((link(parent, crnt_dir)) < 0) {
  154.         /* 
  155.         ** Could not link the parent directory to the ".." entry.
  156.         ** Clean up and return.
  157.         */
  158.         crnt_dir[strlen(crnt_dir)] = '\0';
  159.         (void) unlink(crnt_dir);
  160.         (void) unlink(dirpath);
  161.         (void) fprintf(errfp, "%s: cannot link [%s]\n",progname,crnt_dir);
  162.         return(-1);
  163.     }
  164. #endif /* USE_SYSMKDIR */
  165. #endif /* HAVE_MKDIR */
  166.     return(0); 
  167. }
  168.