PageRenderTime 18ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/cvs/lib/mkdir.c

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