port.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* $Source: /u/mark/src/pax/RCS/port.c,v $
  2. *
  3. * $Revision: 1.2 $
  4. *
  5. * port.c - These are routines not available in all environments.
  6. *
  7. * DESCRIPTION
  8. *
  9. * The routines contained in this file are provided for portability to
  10. * other versions of UNIX or other operating systems (e.g. MSDOS).
  11. * Not all systems have the same functions or the same semantics,
  12. * these routines attempt to bridge the gap as much as possible.
  13. *
  14. * AUTHOR
  15. *
  16. * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  17. * John Gilmore (gnu@hoptoad)
  18. *
  19. * Sponsored by The USENIX Association for public distribution.
  20. *
  21. * Copyright (c) 1989 Mark H. Colburn.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms are permitted
  25. * provided that the above copyright notice is duplicated in all such
  26. * forms and that any documentation, advertising materials, and other
  27. * materials related to such distribution and use acknowledge that the
  28. * software was developed * by Mark H. Colburn and sponsored by The
  29. * USENIX Association.
  30. *
  31. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  32. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  33. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  34. *
  35. * $Log: port.c,v $
  36. * Revision 1.2 89/02/12 10:05:35 mark
  37. * 1.2 release fixes
  38. *
  39. * Revision 1.1 88/12/23 18:02:29 mark
  40. * Initial revision
  41. *
  42. */
  43. #ifndef lint
  44. static char *ident = "$Id: port.c,v 1.2 89/02/12 10:05:35 mark Exp $";
  45. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  46. #endif /* ! lint */
  47. /* Headers */
  48. #include "pax.h"
  49. /*
  50. * Some computers are not so crass as to align themselves into the BSD or USG
  51. * camps. If a system supplies all of the routines we fake here, add it to
  52. * the list in the #if !defined()'s below and it'll all be skipped.
  53. */
  54. #if !defined(mc300) && !defined(mc500) && !defined(mc700) && !defined(BSD) && !defined(_POSIX_SOURCE)
  55. /* mkdir - make a directory
  56. *
  57. * DESCRIPTION
  58. *
  59. * Mkdir will make a directory of the name "dpath" with a mode of
  60. * "dmode". This is consistent with the BSD mkdir() function and the
  61. * P1003.1 definitions of MKDIR.
  62. *
  63. * PARAMETERS
  64. *
  65. * dpath - name of directory to create
  66. * dmode - mode of the directory
  67. *
  68. * RETURNS
  69. *
  70. * Returns 0 if the directory was successfully created, otherwise a
  71. * non-zero return value will be passed back to the calling function
  72. * and the value of errno should reflect the error.
  73. */
  74. #ifdef __STDC__
  75. int mkdir(char *dpath, int dmode)
  76. #else
  77. int mkdir(dpath, dmode)
  78. char *dpath;
  79. int dmode;
  80. #endif
  81. {
  82. int cpid, status;
  83. Stat statbuf;
  84. extern int errno;
  85. if (STAT(dpath, &statbuf) == 0) {
  86. errno = EEXIST; /* Stat worked, so it already exists */
  87. return (-1);
  88. }
  89. /* If stat fails for a reason other than non-existence, return error */
  90. if (errno != ENOENT)
  91. return (-1);
  92. switch (cpid = fork()) {
  93. case -1: /* Error in fork() */
  94. return (-1); /* Errno is set already */
  95. case 0: /* Child process */
  96. status = umask(0); /* Get current umask */
  97. status = umask(status | (0777 & ~dmode)); /* Set for mkdir */
  98. execl("/bin/mkdir", "mkdir", dpath, (char *) 0);
  99. _exit(-1); /* Can't exec /bin/mkdir */
  100. default: /* Parent process */
  101. while (cpid != wait(&status)) {
  102. /* Wait for child to finish */
  103. }
  104. }
  105. if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  106. errno = EIO; /* We don't know why, but */
  107. return (-1); /* /bin/mkdir failed */
  108. }
  109. return (0);
  110. }
  111. /* rmdir - remove a directory
  112. *
  113. * DESCRIPTION
  114. *
  115. * Rmdir will remove the directory specified by "dpath". It is
  116. * consistent with the BSD and POSIX rmdir functions.
  117. *
  118. * PARAMETERS
  119. *
  120. * dpath - name of directory to remove
  121. *
  122. * RETURNS
  123. *
  124. * Returns 0 if the directory was successfully deleted, otherwise a
  125. * non-zero return value will be passed back to the calling function
  126. * and the value of errno should reflect the error.
  127. */
  128. #ifdef __STDC__
  129. int rmdir(char *dpath)
  130. #else
  131. int rmdir(dpath)
  132. char *dpath;
  133. #endif
  134. {
  135. int cpid, status;
  136. Stat statbuf;
  137. extern int errno;
  138. /* check to see if it exists */
  139. if (STAT(dpath, &statbuf) == -1) {
  140. return (-1);
  141. }
  142. switch (cpid = fork()) {
  143. case -1: /* Error in fork() */
  144. return (-1); /* Errno is set already */
  145. case 0: /* Child process */
  146. execl("/bin/rmdir", "rmdir", dpath, (char *) 0);
  147. _exit(-1); /* Can't exec /bin/rmdir */
  148. default: /* Parent process */
  149. while (cpid != wait(&status)) {
  150. /* Wait for child to finish */
  151. }
  152. }
  153. if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  154. errno = EIO; /* We don't know why, but */
  155. return (-1); /* /bin/rmdir failed */
  156. }
  157. return (0);
  158. }
  159. #endif /* MASSCOMP, BSD */