home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / pax / 1 / port.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-07  |  4.7 KB  |  191 lines

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