home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / lib / mkdir.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  130 lines

  1. /* mkrmdir.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 2, 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. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21.  
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #ifndef STDC_HEADERS
  26. extern int errno;
  27. #endif
  28.  
  29. /* mkdir and rmdir adapted from GNU tar. */
  30.  
  31. /* Make directory DPATH, with permission mode DMODE.
  32.  
  33.    Written by Robert Rother, Mariah Corporation, August 1985
  34.    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
  35.  
  36.    Severely hacked over by John Gilmore to make a 4.2BSD compatible
  37.    subroutine.    11Mar86; hoptoad!gnu
  38.  
  39.    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  40.    subroutine didn't return EEXIST.  It does now. */
  41.  
  42. int
  43. mkdir (dpath, dmode)
  44.      char *dpath;
  45.      int dmode;
  46. {
  47.   int cpid, status;
  48.   struct stat statbuf;
  49.  
  50.   if (stat (dpath, &statbuf) == 0)
  51.     {
  52.       errno = EEXIST;        /* stat worked, so it already exists. */
  53.       return -1;
  54.     }
  55.  
  56.   /* If stat fails for a reason other than non-existence, return error. */
  57.   if (! existence_error (errno))
  58.     return -1;
  59.  
  60.   cpid = fork ();
  61.   switch (cpid)
  62.     {
  63.     case -1:            /* Cannot fork. */
  64.       return -1;        /* errno is set already. */
  65.  
  66.     case 0:            /* Child process. */
  67.       /* Cheap hack to set mode of new directory.  Since this child
  68.      process is going away anyway, we zap its umask.
  69.      This won't suffice to set SUID, SGID, etc. on this
  70.      directory, so the parent process calls chmod afterward. */
  71.       status = umask (0);    /* Get current umask. */
  72.       umask (status | (0777 & ~dmode));    /* Set for mkdir. */
  73.       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
  74.       _exit (1);
  75.  
  76.     default:            /* Parent process. */
  77.       while (wait (&status) != cpid) /* Wait for kid to finish. */
  78.     /* Do nothing. */ ;
  79.  
  80.       if (status & 0xFFFF)
  81.     {
  82.       errno = EIO;        /* /bin/mkdir failed. */
  83.       return -1;
  84.     }
  85.       return chmod (dpath, dmode);
  86.     }
  87. }
  88.  
  89. /* Remove directory DPATH.
  90.    Return 0 if successful, -1 if not. */
  91.  
  92. int
  93. rmdir (dpath)
  94.      char *dpath;
  95. {
  96.   int cpid, status;
  97.   struct stat statbuf;
  98.  
  99.   if (stat (dpath, &statbuf) != 0)
  100.     return -1;            /* stat set errno. */
  101.  
  102.   if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
  103.     {
  104.       errno = ENOTDIR;
  105.       return -1;
  106.     }
  107.  
  108.   cpid = fork ();
  109.   switch (cpid)
  110.     {
  111.     case -1:            /* Cannot fork. */
  112.       return -1;        /* errno is set already. */
  113.  
  114.     case 0:            /* Child process. */
  115.       execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
  116.       _exit (1);
  117.  
  118.     default:            /* Parent process. */
  119.       while (wait (&status) != cpid) /* Wait for kid to finish. */
  120.     /* Do nothing. */ ;
  121.  
  122.       if (status & 0xFFFF)
  123.     {
  124.       errno = EIO;        /* /bin/rmdir failed. */
  125.       return -1;
  126.     }
  127.       return 0;
  128.     }
  129. }
  130.