/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. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details. */
  11. #ifdef HAVE_CONFIG_H
  12. #include "config.h"
  13. #endif
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <errno.h>
  17. #ifndef STDC_HEADERS
  18. extern int errno;
  19. #endif
  20. /* mkdir and rmdir adapted from GNU tar. */
  21. /* Make directory DPATH, with permission mode DMODE.
  22. Written by Robert Rother, Mariah Corporation, August 1985
  23. (sdcsvax!rmr or rmr@uscd). If you want it, it's yours.
  24. Severely hacked over by John Gilmore to make a 4.2BSD compatible
  25. subroutine. 11Mar86; hoptoad!gnu
  26. Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  27. subroutine didn't return EEXIST. It does now. */
  28. int
  29. mkdir (dpath, dmode)
  30. const char *dpath;
  31. int dmode;
  32. {
  33. int cpid, status;
  34. struct stat statbuf;
  35. if (stat (dpath, &statbuf) == 0)
  36. {
  37. errno = EEXIST; /* stat worked, so it already exists. */
  38. return -1;
  39. }
  40. /* If stat fails for a reason other than non-existence, return error. */
  41. if (! existence_error (errno))
  42. return -1;
  43. cpid = fork ();
  44. switch (cpid)
  45. {
  46. case -1: /* Cannot fork. */
  47. return -1; /* errno is set already. */
  48. case 0: /* Child process. */
  49. /* Cheap hack to set mode of new directory. Since this child
  50. process is going away anyway, we zap its umask.
  51. This won't suffice to set SUID, SGID, etc. on this
  52. directory, so the parent process calls chmod afterward. */
  53. status = umask (0); /* Get current umask. */
  54. umask (status | (0777 & ~dmode)); /* Set for mkdir. */
  55. execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
  56. _exit (1);
  57. default: /* Parent process. */
  58. while (wait (&status) != cpid) /* Wait for kid to finish. */
  59. /* Do nothing. */ ;
  60. if (status & 0xFFFF)
  61. {
  62. errno = EIO; /* /bin/mkdir failed. */
  63. return -1;
  64. }
  65. return chmod (dpath, dmode);
  66. }
  67. }
  68. /* Remove directory DPATH.
  69. Return 0 if successful, -1 if not. */
  70. int
  71. rmdir (dpath)
  72. char *dpath;
  73. {
  74. int cpid, status;
  75. struct stat statbuf;
  76. if (stat (dpath, &statbuf) != 0)
  77. return -1; /* stat set errno. */
  78. if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
  79. {
  80. errno = ENOTDIR;
  81. return -1;
  82. }
  83. cpid = fork ();
  84. switch (cpid)
  85. {
  86. case -1: /* Cannot fork. */
  87. return -1; /* errno is set already. */
  88. case 0: /* Child process. */
  89. execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
  90. _exit (1);
  91. default: /* Parent process. */
  92. while (wait (&status) != cpid) /* Wait for kid to finish. */
  93. /* Do nothing. */ ;
  94. if (status & 0xFFFF)
  95. {
  96. errno = EIO; /* /bin/rmdir failed. */
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. }