home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / apps / posix / source / PAX / PORT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-17  |  5.3 KB  |  206 lines

  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.  
  44. #ifndef lint
  45. static char *ident = "$Id: port.c,v 1.2 89/02/12 10:05:35 mark Exp $";
  46. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  47. #endif /* ! lint */
  48.  
  49.  
  50. /* Headers */
  51.  
  52. #include "pax.h"
  53. #ifdef _POSIX_SOURCE  /* Xn */
  54. #   include <sys/wait.h>  /* Xn */
  55. #endif  /* Xn */
  56.  
  57.  
  58. /*
  59.  * Some computers are not so crass as to align themselves into the BSD or USG
  60.  * camps.  If a system supplies all of the routines we fake here, add it to
  61.  * the list in the #if !defined()'s below and it'll all be skipped. 
  62.  */
  63.  
  64. #if !defined(_POSIX_SOURCE) && !defined(mc300) && !defined(mc500) && !defined(mc700) && !defined(BSD)  /* Xn */
  65.  
  66. /* mkdir - make a directory
  67.  *
  68.  * DESCRIPTION
  69.  *
  70.  *     Mkdir will make a directory of the name "dpath" with a mode of
  71.  *    "dmode".  This is consistent with the BSD mkdir() function and the
  72.  *    P1003.1 definitions of MKDIR.
  73.  *
  74.  * PARAMETERS
  75.  *
  76.  *    dpath        - name of directory to create
  77.  *    dmode        - mode of the directory
  78.  *
  79.  * RETURNS
  80.  *
  81.  *    Returns 0 if the directory was successfully created, otherwise a
  82.  *    non-zero return value will be passed back to the calling function
  83.  *    and the value of errno should reflect the error.
  84.  */
  85.  
  86. #ifdef __STDC__
  87.  
  88. int mkdir(char *dpath, int dmode)
  89.  
  90. #else
  91.     
  92. int mkdir(dpath, dmode)
  93. char           *dpath;
  94. int             dmode;
  95.  
  96. #endif
  97. {
  98.     int             cpid, status;
  99.     Stat            statbuf;
  100.     extern int      errno;
  101.  
  102. #ifdef DF_TRACE_DEBUG
  103. printf("DF_TRACE_DEBUG: int mkdir() in port.c\n");
  104. #endif
  105.     if (STAT(dpath, &statbuf) == 0) {
  106.     errno = EEXIST;        /* Stat worked, so it already exists */
  107.     return (-1);
  108.     }
  109.     /* If stat fails for a reason other than non-existence, return error */
  110.     if (errno != ENOENT)
  111.     return (-1);
  112.  
  113. puts("b fork1");
  114.     switch (cpid = fork()) {
  115.  
  116.     case -1:            /* Error in fork() */
  117.     return (-1);        /* Errno is set already */
  118.  
  119.     case 0:            /* Child process */
  120.  
  121.     status = umask(0);    /* Get current umask */
  122.     status = umask(status | (0777 & ~dmode));    /* Set for mkdir */
  123.     execl("/bin/mkdir", "mkdir", dpath, (char *) 0);
  124.     _exit(-1);        /* Can't exec /bin/mkdir */
  125.  
  126.     default:            /* Parent process */
  127.     while (cpid != wait(&status)) {
  128.         /* Wait for child to finish */
  129.     }
  130.     }
  131. puts("a fork1");
  132.  
  133.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  134.     errno = EIO;        /* We don't know why, but */
  135.     return (-1);        /* /bin/mkdir failed */
  136.     }
  137.     return (0);
  138. }
  139.  
  140.  
  141. /* rmdir - remove a directory
  142.  *
  143.  * DESCRIPTION
  144.  *
  145.  *    Rmdir will remove the directory specified by "dpath".  It is
  146.  *    consistent with the BSD and POSIX rmdir functions.
  147.  *
  148.  * PARAMETERS
  149.  *
  150.  *    dpath        - name of directory to remove
  151.  *
  152.  * RETURNS
  153.  *
  154.  *    Returns 0 if the directory was successfully deleted, otherwise a
  155.  *    non-zero return value will be passed back to the calling function
  156.  *    and the value of errno should reflect the error.
  157.  */
  158.  
  159. #ifdef __STDC__
  160.  
  161. int rmdir(char *dpath)
  162.  
  163. #else
  164.     
  165. int rmdir(dpath)
  166. char           *dpath;
  167.  
  168. #endif
  169. {
  170.     int             cpid, status;
  171.     Stat            statbuf;
  172.     extern int      errno;
  173.  
  174.     /* check to see if it exists */
  175. #ifdef DF_TRACE_DEBUG
  176. printf("DF_TRACE_DEBUG: int rmdir() in port.c\n");
  177. #endif
  178.     if (STAT(dpath, &statbuf) == -1) {
  179.     return (-1);
  180.     }
  181. puts("b fork");
  182.     switch (cpid = fork()) {
  183.  
  184.     case -1:            /* Error in fork() */
  185.     return (-1);        /* Errno is set already */
  186.  
  187.     case 0:            /* Child process */
  188.     execl("/bin/rmdir", "rmdir", dpath, (char *) 0);
  189.     _exit(-1);        /* Can't exec /bin/rmdir */
  190.  
  191.     default:            /* Parent process */
  192.     while (cpid != wait(&status)) {
  193.         /* Wait for child to finish */
  194.     }
  195.     }
  196. puts("a fork");
  197.  
  198.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  199.     errno = EIO;        /* We don't know why, but */
  200.     return (-1);        /* /bin/rmdir failed */
  201.     }
  202.     return (0);
  203. }
  204.  
  205. #endif /* MASSCOMP, BSD */
  206.