home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / DIRLIB.C < prev    next >
C/C++ Source or Header  |  1990-08-29  |  5KB  |  175 lines

  1. /* dirlib.c -- BSD compatible directory functions for System V
  2.    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <errno.h>
  21. #ifndef STDC_HEADERS
  22. extern int errno;
  23. #endif
  24.  
  25. /* mkdir and rmdir adapted from GNU tar. */
  26.  
  27. #ifdef MKDIR_MISSING
  28. /* Make directory DPATH, with permission mode DMODE.
  29.  
  30.    Written by Robert Rother, Mariah Corporation, August 1985
  31.    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
  32.  
  33.    Severely hacked over by John Gilmore to make a 4.2BSD compatible
  34.    subroutine.    11Mar86; hoptoad!gnu
  35.  
  36.    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  37.    subroutine didn't return EEXIST.  It does now. */
  38.  
  39. int
  40. mkdir (dpath, dmode)
  41.      char *dpath;
  42.      int dmode;
  43. {
  44.   int cpid, status;
  45.   struct stat statbuf;
  46.  
  47.   if (stat (dpath, &statbuf) == 0)
  48.     {
  49.       errno = EEXIST;        /* stat worked, so it already exists. */
  50.       return -1;
  51.     }
  52.  
  53.   /* If stat fails for a reason other than non-existence, return error. */
  54.   if (errno != ENOENT)
  55.     return -1;
  56.  
  57.   cpid = fork ();
  58.   switch (cpid)
  59.     {
  60.     case -1:            /* Cannot fork. */
  61.       return -1;        /* errno is set already. */
  62.  
  63.     case 0:            /* Child process. */
  64.       /* Cheap hack to set mode of new directory.  Since this child
  65.      process is going away anyway, we zap its umask.
  66.      This won't suffice to set SUID, SGID, etc. on this
  67.      directory, so the parent process calls chmod afterward. */
  68.       status = umask (0);    /* Get current umask. */
  69.       umask (status | (0777 & ~dmode));    /* Set for mkdir. */
  70.       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
  71.       _exit (1);
  72.  
  73.     default:            /* Parent process. */
  74.       while (wait (&status) != cpid) /* Wait for kid to finish. */
  75.     /* Do nothing. */ ;
  76.  
  77.       if (status & 0xFFFF)
  78.     {
  79.       errno = EIO;        /* /bin/mkdir failed. */
  80.       return -1;
  81.     }
  82.       return chmod (dpath, dmode);
  83.     }
  84. }
  85.  
  86. /* Remove directory DPATH.
  87.    Return 0 if successful, -1 if not. */
  88.  
  89. int
  90. rmdir (dpath)
  91.      char *dpath;
  92. {
  93.   int cpid, status;
  94.   struct stat statbuf;
  95.  
  96.   if (stat (dpath, &statbuf) != 0)
  97.     return -1;            /* stat set errno. */
  98.  
  99.   if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
  100.     {
  101.       errno = ENOTDIR;
  102.       return -1;
  103.     }
  104.  
  105.   cpid = fork ();
  106.   switch (cpid)
  107.     {
  108.     case -1:            /* Cannot fork. */
  109.       return -1;        /* errno is set already. */
  110.  
  111.     case 0:            /* Child process. */
  112.       execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
  113.       _exit (1);
  114.  
  115.     default:            /* Parent process. */
  116.       while (wait (&status) != cpid) /* Wait for kid to finish. */
  117.     /* Do nothing. */ ;
  118.  
  119.       if (status & 0xFFFF)
  120.     {
  121.       errno = EIO;        /* /bin/rmdir failed. */
  122.       return -1;
  123.     }
  124.       return 0;
  125.     }
  126. }
  127. #endif
  128.  
  129. #ifdef RENAME_MISSING
  130. /* Rename file FROM to file TO.
  131.    Return 0 if successful, -1 if not. */
  132.  
  133. int
  134. rename (from, to)
  135.      char *from;
  136.      char *to;
  137. {
  138.   struct stat from_stats;
  139.   int pid, status;
  140.  
  141.   if (stat (from, &from_stats) == 0)
  142.     {
  143.       if (unlink (to) && errno != ENOENT)
  144.     return -1;
  145.       if ((from_stats.st_mode & S_IFMT) == S_IFDIR)
  146.     {
  147.       /* Need a setuid root process to link and unlink directories. */
  148.       pid = fork ();
  149.       switch (pid)
  150.         {
  151.         case -1:        /* Error. */
  152.           error (1, errno, "cannot fork");
  153.  
  154.         case 0:        /* Child. */
  155.           execl (MVDIR, "mvdir", from, to, (char *) 0);
  156.           error (255, errno, "cannot run `%s'", MVDIR);
  157.  
  158.         default:        /* Parent. */
  159.           while (wait (&status) != pid)
  160.         /* Do nothing. */ ;
  161.  
  162.           errno = 0;    /* mvdir printed the system error message. */
  163.           return status != 0 ? -1 : 0;
  164.         }
  165.     }
  166.       else
  167.     {
  168.       if (link (from, to) == 0 && (unlink (from) == 0 || errno == ENOENT))
  169.         return 0;
  170.     }
  171.     }
  172.   return -1;
  173. }
  174. #endif
  175.