home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / port.c < prev    next >
C/C++ Source or Header  |  1993-01-14  |  34KB  |  1,518 lines

  1. /*****************************************************************************
  2.  * $Id: port.c,v 1.9 1993/01/14 21:50:27 ak Exp $
  3.  *****************************************************************************
  4.  * $Log: port.c,v $
  5.  * Revision 1.9  1993/01/14  21:50:27  ak
  6.  * EMX 0.8f fixes and enhancements by Kai Uwe Rommel.
  7.  *
  8.  * Revision 1.8  1993/01/12  18:24:21  ak
  9.  * E.Mattes: Delete files (EAs) before extract, else EAs accumulate.
  10.  *
  11.  * Revision 1.7  1992/12/13  09:39:51  ak
  12.  * K.U.R: f_archive/f_reset_archive added to support the "archived" bit.
  13.  *
  14.  * Revision 1.6  1992/10/24  15:21:11  ak
  15.  * Bugfix. mkos2file should use current time if time stamp is 0. Time
  16.  * stamp is 0 if the file was archived without -g or -p.
  17.  *
  18.  * Revision 1.5  1992/09/29  09:56:04  ak
  19.  * K.U.R., once again :-)
  20.  * - removed a dup() in buffer.c
  21.  * - EMX opendir with hidden/system test in gnu.c (...dotdot)
  22.  *
  23.  * Revision 1.4  1992/09/12  15:57:48  ak
  24.  * - Usenet patches for GNU TAR 1.10
  25.  * - Bugfixes and patches of Kai Uwe Rommel:
  26.  *         filename conversion for FAT
  27.  *         EMX 0.8e
  28.  *         -0..1 alias for a: b:
  29.  *         -2..7 alias for +++TAPE$x
  30.  *
  31.  * Revision 1.3  1992/09/09  14:25:47  ak
  32.  * K.U.R: save atime/mtime when -p is given
  33.  *
  34.  * Revision 1.2  1992/09/02  20:08:31  ak
  35.  * Version AK200
  36.  * - Tape access
  37.  * - Quick file access
  38.  * - OS/2 extended attributes
  39.  * - Some OS/2 fixes
  40.  * - Some fixes of Kai Uwe Rommel
  41.  *
  42.  * Revision 1.1.1.1  1992/09/02  19:22:15  ak
  43.  * Original GNU Tar 1.10 with some filenames changed for FAT compatibility.
  44.  *
  45.  * Revision 1.1  1992/09/02  19:22:12  ak
  46.  * Initial revision
  47.  *
  48.  *****************************************************************************/
  49.  
  50. static char *rcsid = "$Id: port.c,v 1.9 1993/01/14 21:50:27 ak Exp $";
  51.  
  52. /*
  53.  * Modified by Andreas Kaiser July 92.
  54.  * See CHANGES.AK for info.
  55.  */
  56.  
  57. /* Modified 9/2/91 to support buffer_length operation on buffers to
  58.    support a bugfix in mangle.c, which see.  Note that I intentionally
  59.    wrote this routine in the same strange style as the others, in case
  60.    there was a reason for it.  -Max Hailperin <max@nic.gac.edu> */
  61.  
  62. /* Supporting routines which may sometimes be missing.
  63.    Copyright (C) 1988 Free Software Foundation
  64.  
  65. This file is part of GNU Tar.
  66.  
  67. GNU Tar is free software; you can redistribute it and/or modify
  68. it under the terms of the GNU General Public License as published by
  69. the Free Software Foundation; either version 1, or (at your option)
  70. any later version.
  71.  
  72. GNU Tar is distributed in the hope that it will be useful,
  73. but WITHOUT ANY WARRANTY; without even the implied warranty of
  74. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  75. GNU General Public License for more details.
  76.  
  77. You should have received a copy of the GNU General Public License
  78. along with GNU Tar; see the file COPYING.  If not, write to
  79. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  80.  
  81. /*
  82.  * @(#)port.c 1.15    87/11/05    by John Gilmore, 1986
  83.  *
  84.  * These are routines not available in all environments.
  85.  *
  86.  * I know this introduces an extra level of subroutine calls and is
  87.  * slightly slower.  Frankly, my dear, I don't give a damn.  Let the
  88.  * Missed-Em Vee losers suffer a little.  This software is proud to
  89.  * have been written on a BSD system.
  90.  */
  91. #include <stdio.h>
  92. #include <sys/types.h>
  93. #include <sys/stat.h>
  94. #include <signal.h>
  95. #include <errno.h>
  96.  
  97. #if    defined(MSDOS) || defined(USG)
  98. #include <fcntl.h>
  99. #else
  100. #include <sys/file.h>
  101. #endif
  102.  
  103. #include "tar.h"
  104. #include "port.h"
  105.  
  106. #ifdef USG
  107. #include <string.h>
  108. #else
  109. extern size_t strlen();
  110. #endif
  111.  
  112. extern long baserec;
  113. /*
  114.  * Some people (e.g. V7) don't have a #define for these.
  115.  */
  116. #ifndef    O_BINARY
  117. #define    O_BINARY    0
  118. #endif
  119. #ifndef    O_RDONLY
  120. #define    O_RDONLY    0
  121. #endif
  122. #ifndef NULL
  123. #define NULL 0
  124. #endif
  125.  
  126. /* JF: modified so all configuration information can appear here, instead of
  127.    being scattered through the file.  Add all the machine-dependent #ifdefs
  128.    here */
  129. #undef WANT_DUMB_GET_DATE/* WANT_DUMB_GET_DATE --> get_date() */
  130. #undef WANT_VALLOC    /* WANT_VALLOC --> valloc() */
  131. #undef WANT_MKDIR    /* WANT_MKDIR --> mkdir() rmdir() */
  132. #undef WANT_STRING    /* WANT_STRING --> index() bcopy() bzero() bcmp() */
  133. #undef WANT_BZERO    /* WANT_BZERO --> bzero() bcmp() execlp() */
  134.             /* EMUL_OPEN3 --> open3() */
  135. #undef WANT_MKNOD    /* WANT_MKNOD --> mknod() link() chown() geteuid() */
  136. #undef WANT_UTILS    /* WANT_UTILS --> panic() ck_*() *_buffer()
  137.                merge_sort() quote_copy_string() un_quote_string() */
  138. #undef WANT_CK_PIPE    /* WANT_CK_PIPE --> ck_pipe() */
  139. #undef WANT_GETWD    /* WANT_GETWD --> getwd() */
  140. #undef WANT_STRSTR    /* WANT_STRSTR --> strstr() */
  141. #undef WANT_FTRUNCATE    /* WANT_FTRUNCATE --> frtruncate() */
  142.  
  143. /* Define only ONE of these four . . . */
  144. /* #undef DOPRNT_MSG    /* Define this one if you have _doprnt() and
  145.                no varargs support */
  146. /* #undef VARARGS_MSG    /* Define this one if you have varargs.h and
  147.                vfprintf() */
  148. /* #undef STDC_MSG     /* Define this one if you are using ANSI C and
  149.                and have vfprintf() */
  150. /* #undef LOSING_MSG    /* Define this one if you don't have any of the
  151.                above */
  152. #ifdef USG
  153. #define WANT_STRING
  154. #define WANT_VALLOC
  155.  
  156. #if defined(sgi) && defined(mips)
  157. #define WANT_GETWD
  158. #endif
  159.  
  160. #if defined(i386)
  161. #define WANT_FTRUNCATE
  162. #endif
  163.  
  164. #endif
  165.  
  166. #ifdef hpux
  167. #define WANT_VALLOC
  168. #endif
  169.  
  170. #ifdef MINIX
  171. #define WANT_BZERO
  172. #endif
  173.  
  174. #ifdef MSDOS
  175. char TTY_NAME[] = "con";
  176.  
  177. #define WANT_STRING
  178. #define WANT_MKNOD
  179. #define WANT_UTILS
  180. #define WANT_VALLOC
  181. #define STDC_MSG
  182.  
  183. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  184. #ifdef __STDC__
  185. #define STDC_MSG
  186. #else
  187. #define LOSING_MSG
  188. #endif
  189. #endif
  190.  
  191. #else /* not MSDOS */
  192. char TTY_NAME[] ="/dev/tty";
  193.  
  194. #define WANT_UTILS
  195. #define WANT_CK_PIPE
  196. #ifndef HAVE_STRSTR
  197. #define WANT_STRSTR
  198. #endif
  199.  
  200. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  201. #ifdef BSD42
  202. /* BSD systems should do this even if __STDC__, because
  203.    we might be using an ANSI compiler without an ANSI library. (sigh) */
  204. #ifdef sparc
  205. #define LOSING_MSG
  206. #else
  207. #define DOPRNT_MSG
  208. #endif
  209. #else /* not BSD */
  210. #ifdef __STDC__
  211. #define STDC_MSG
  212. #else /* not ANSI C */
  213. #define LOSING_MSG
  214. #endif /* not ANSI C */
  215. #endif /* not BSD */
  216. #endif /* Need to define some form of _MSG */
  217. #endif /* not MSDOS */
  218.  
  219. /* End of system-dependent #ifdefs */
  220.  
  221. #ifdef WANT_DUMB_GET_DATE
  222. /* JF a get_date() routine takes a date/time/etc and turns it into a time_t */
  223. /* This one is a quick hack I wrote in about five minutes to see if the N
  224.    option works.  Someone should replace it with one that works */
  225.  
  226. /* This get_date takes an arg of the form mm/dd/yyyy hh:mm:ss and turns it
  227.    into a time_t .  Its not well tested or anything. . .  */
  228. /* In general, you should use the get_date() supplied in getdate.y */
  229.  
  230. #define OFF_FROM_GMT 0        /* Change for your time zone! */
  231.  
  232. time_t
  233. get_date(str)
  234. char *str;
  235. {
  236.     int month,day,year,hour,minute,second;
  237.     time_t ret;
  238.     int    n;
  239.  
  240. #define SECS_PER_YEAR (365L*SECS_PER_DAY)
  241. #define SECS_PER_LEAP_YEAR (366L*SECS_PER_DAY)
  242.  
  243. #define SECS_PER_DAY (24L*60*60)
  244.     static int days_per_month[2][12] = {
  245.         31,28,31,30,31,30,31,31,30,31,30,31,
  246.         31,29,31,30,31,30,31,31,30,31,30,31
  247.     };
  248.  
  249.     static int days_per_year[2]={365,366};
  250.  
  251.     month=day=year=hour=minute=second=0;
  252.     n=sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second);
  253.     if(n<3)
  254.         return 0;
  255.     if(year<100)
  256.         year+=1900;
  257.     if(year<1970)
  258.         return 0;
  259.     ret=0;
  260.  
  261.     ret+=OFF_FROM_GMT;
  262.  
  263.     for(n=1970;n<year;n++)
  264.         if(n%4==0 && n%400!=0)
  265.             ret+=SECS_PER_LEAP_YEAR;
  266.         else
  267.             ret+=SECS_PER_YEAR;
  268.  
  269.     month--;
  270.     for(n=0;n<month;n++) {
  271.         if(year%4==0 && year%400!=0)
  272.             ret+=SECS_PER_DAY*days_per_month[1][n];
  273.         else
  274.             ret+=SECS_PER_DAY*days_per_month[0][n];
  275.     }
  276.     ret+=SECS_PER_DAY*(day-1);
  277.     ret+=second+minute*60+hour*60*60;
  278.     return ret;
  279. }
  280. #endif
  281.  
  282. #ifdef WANT_VALLOC
  283. /*
  284.  * valloc() does a malloc() on a page boundary.  On some systems,
  285.  * this can make large block I/O more efficient.
  286.  */
  287. char *
  288. valloc (size)
  289.     unsigned size;
  290. {
  291.     extern char *malloc ();
  292.     return (malloc (size));
  293. }
  294. #endif
  295. /*
  296.  *                NMKDIR.C
  297.  *
  298.  * Written by Robert Rother, Mariah Corporation, August 1985. 
  299.  *
  300.  * I wrote this out of shear disgust with myself because I couldn't
  301.  * figure out how to do this in /bin/sh.
  302.  *
  303.  * If you want it, it's yours.  All I ask in return is that if you
  304.  * figure out how to do this in a Bourne Shell script you send me
  305.  * a copy.
  306.  *                    sdcsvax!rmr or rmr@uscd
  307. *
  308. * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  309. * subroutine.    11Mar86; hoptoad!gnu
  310. *
  311. * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  312. * subroutine didn't return EEXIST.  It does now.
  313. */
  314.  
  315. /*
  316.  * Make a directory.  Compatible with the mkdir() system call on 4.2BSD.
  317.  */
  318. #ifdef WANT_MKDIR
  319. int
  320. mkdir(dpath, dmode)
  321.     char *dpath;
  322.     int dmode;
  323. {
  324.     int cpid, status;
  325.     struct stat statbuf;
  326.     extern int errno;
  327.  
  328.     if (stat(dpath,&statbuf) == 0) {
  329.         errno = EEXIST;        /* Stat worked, so it already exists */
  330.         return -1;
  331.     }
  332.  
  333.     /* If stat fails for a reason other than non-existence, return error */
  334.     if (errno != ENOENT) return -1; 
  335.  
  336.     switch (cpid = fork()) {
  337.  
  338.     case -1:            /* Error in fork() */
  339.         return(-1);        /* Errno is set already */
  340.  
  341.     case 0:                /* Child process */
  342.         /*
  343.          * Cheap hack to set mode of new directory.  Since this
  344.          * child process is going away anyway, we zap its umask.
  345.          * FIXME, this won't suffice to set SUID, SGID, etc. on this
  346.          * directory.  Does anybody care?
  347.          */
  348.         status = umask(0);    /* Get current umask */
  349.         status = umask(status | (0777 & ~dmode)); /* Set for mkdir */
  350.         execl("/bin/mkdir", "mkdir", dpath, (char *)0);
  351.         _exit(-1);        /* Can't exec /bin/mkdir */
  352.     
  353.     default:            /* Parent process */
  354.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  355.     }
  356.  
  357.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  358.         errno = EIO;        /* We don't know why, but */
  359.         return -1;        /* /bin/mkdir failed */
  360.     }
  361.  
  362.     return 0;
  363. }
  364. int
  365. rmdir(dpath)
  366.     char *dpath;
  367. {
  368.     int cpid, status;
  369.     struct stat statbuf;
  370.     extern int errno;
  371.  
  372.     if (stat(dpath,&statbuf) != 0) {
  373.         /* Stat just set errno.  We don't have to */
  374.         return -1;
  375.     }
  376.  
  377.     switch (cpid = fork()) {
  378.  
  379.     case -1:            /* Error in fork() */
  380.         return(-1);        /* Errno is set already */
  381.  
  382.     case 0:                /* Child process */
  383.         execl("/bin/rmdir", "rmdir", dpath, (char *)0);
  384.         _exit(-1);        /* Can't exec /bin/mkdir */
  385.     
  386.     default:            /* Parent process */
  387.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  388.     }
  389.  
  390.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  391.         errno = EIO;        /* We don't know why, but */
  392.         return -1;        /* /bin/mkdir failed */
  393.     }
  394.  
  395.     return 0;
  396. }
  397. #endif
  398.  
  399. #ifdef WANT_STRING
  400. /*
  401.  * Translate V7 style into Sys V style.
  402.  */
  403. #include <string.h>
  404. #ifndef __MSDOS__
  405. #include <memory.h>
  406. #endif
  407.  
  408. char *
  409. index (s, c)
  410.     char *s;
  411.     int c;
  412. {
  413.     return (strchr (s, c));
  414. }
  415.  
  416. char *
  417. rindex(s,c)
  418. char *s;
  419. int c;
  420. {
  421.     return strrchr(s,c);
  422. }
  423.  
  424. void
  425. bcopy (s1, s2, n)
  426.     char *s1, *s2;
  427.     int n;
  428. {
  429.     (void) memcpy (s2, s1, n);
  430. }
  431.  
  432. void
  433. bzero (s1, n)
  434.     char *s1;
  435.     int n;
  436. {
  437.     (void) memset(s1, 0, n);
  438. }
  439.  
  440. int
  441. bcmp(s1, s2, n)
  442.     char    *s1, *s2;
  443.     int    n;
  444. {
  445.     return memcmp(s1, s2, n);
  446. }
  447. #endif
  448.  
  449. #ifdef WANT_BZERO
  450. /* Minix has bcopy but not bzero, and no memset.  Thanks, Andy. */
  451. void
  452. bzero (s1, n)
  453.     register char *s1;
  454.     register int n;
  455. {
  456.     while (n--) *s1++ = '\0';
  457. }
  458.  
  459. /* It also has no bcmp() */
  460. int
  461. bcmp (s1, s2, n) 
  462.     register char *s1,*s2;
  463.     register int n;
  464. {
  465.     for ( ; n-- ; ++s1, ++s2) {
  466.         if (*s1 != *s2) return *s1 - *s2;
  467.     }
  468.     return 0;
  469. }
  470.  
  471. /*
  472.  * Groan, Minix doesn't have execlp either!
  473.  *
  474.  * execlp(file,arg0,arg1...argn,(char *)NULL)
  475.  * exec a program, automatically searching for the program through
  476.  * all the directories on the PATH.
  477.  *
  478.  * This version is naive about variable argument lists, it assumes
  479.  * a straightforward C calling sequence.  If your system has odd stacks
  480.  * *and* doesn't have execlp, YOU get to fix it.
  481.  */
  482. int
  483. execlp(filename, arg0)
  484.     char *filename, *arg0;
  485. {
  486.     register char *p, *path;    
  487.     register char *fnbuffer;
  488.     char **argstart = &arg0;
  489.     struct stat statbuf;
  490.     extern char **environ;
  491.     extern int errno;
  492.     extern char *malloc(), *getenv(), *index();
  493.  
  494.     if ((p = getenv("PATH")) == NULL) {
  495.         /* couldn't find path variable -- try to exec given filename */
  496.         return execve(filename, argstart, environ);
  497.     }
  498.  
  499.     /*
  500.      * make a place to build the filename.  We malloc larger than we
  501.      * need, but we know it will fit in this.
  502.      */
  503.     fnbuffer = malloc( strlen(p) + 1 + strlen(filename) );
  504.     if (fnbuffer == NULL) {
  505.         errno = ENOMEM;
  506.         return -1;
  507.     }
  508.  
  509.     /*
  510.      * try each component of the path to see if the file's there
  511.      * and executable.
  512.      */
  513.     for (path = p ; path ; path = p) {
  514.         /* construct full path name to try */
  515.         if ((p = index(path,':')) == NULL) {
  516.             strcpy(fnbuffer, path);
  517.         } else {
  518.             strncpy(fnbuffer, path, p-path);
  519.             fnbuffer[p-path] = '\0';
  520.             p++;        /* Skip : for next time */
  521.         }
  522.         if (strlen(fnbuffer) != 0)
  523.             strcat(fnbuffer,"/");
  524.         strcat(fnbuffer,filename);
  525.  
  526.         /* check to see if file is there and is a normal file */
  527.         if (stat(fnbuffer, &statbuf) < 0) {
  528.             if (errno == ENOENT)
  529.                 continue; /* file not there,keep on looking */
  530.             else
  531.                 goto fail; /* failed for some reason, return */
  532.         }
  533.         if ( (statbuf.st_mode & S_IFMT) != S_IFREG) continue;
  534.  
  535.         if (execve(fnbuffer, argstart, environ) < 0
  536.             && errno != ENOENT
  537.             && errno != ENOEXEC) {
  538.             /* failed, for some other reason besides "file
  539.              * not found" or "not a.out format"
  540.              */
  541.             goto fail;
  542.         }
  543.  
  544.         /*
  545.          * If we got error ENOEXEC, the file is executable but is
  546.          * not an object file.  Try to execute it as a shell script,
  547.          * returning error if we can't execute /bin/sh.
  548.          *
  549.          * FIXME, this code is broken in several ways.  Shell
  550.          * scripts should not in general be executed by the user's
  551.          * SHELL variable program.  On more mature systems, the
  552.          * script can specify with #!/bin/whatever.  Also, this
  553.          * code clobbers argstart[-1] if the exec of the shell
  554.          * fails.
  555.          */
  556.         if (errno == ENOEXEC) {
  557.             char *shell;
  558.  
  559.             /* Try to execute command "sh arg0 arg1 ..." */
  560.             if ((shell = getenv("SHELL")) == NULL)
  561.                 shell = "/bin/sh";
  562.             argstart[-1] = shell;
  563.             argstart[0] = fnbuffer;
  564.             execve(shell, &argstart[-1], environ);
  565.             goto fail;    /* Exec didn't work */
  566.         }
  567.  
  568.         /* 
  569.          * If we succeeded, the execve() doesn't return, so we
  570.          * can only be here is if the file hasn't been found yet.
  571.          * Try the next place on the path.
  572.          */
  573.     }
  574.  
  575.     /* all attempts failed to locate the file.  Give up. */
  576.     errno = ENOENT;
  577.  
  578. fail:
  579.     free(fnbuffer);
  580.     return -1;
  581. }
  582. #endif
  583.  
  584.  
  585. #ifdef EMUL_OPEN3
  586. #include "open3.h"
  587. /*
  588.  * open3 -- routine to emulate the 3-argument open system
  589.  * call that is present in most modern Unix systems.
  590.  * This version attempts to support all the flag bits except for O_NDELAY
  591.  * and O_APPEND, which are silently ignored.  The emulation is not as efficient
  592.  * as the real thing (at worst, 4 system calls instead of one), but there's
  593.  * not much I can do about that.
  594.  *
  595.  * Written 6/10/87 by rmtodd@uokmax
  596.  *
  597.  * open3(path, flag, mode)
  598.  * Attempts to open the file specified by
  599.  * the given pathname.  The following flag bits (#defined in tar.h)
  600.  * specify options to the routine:
  601.  *    O_RDONLY    file open for read only
  602.  *    O_WRONLY    file open for write only
  603.  *    O_RDWR        file open for both read & write
  604.  * (Needless to say, you should only specify one of the above).
  605.  *     O_CREAT        file is created with specified mode if it needs to be.
  606.  *    O_TRUNC        if file exists, it is truncated to 0 bytes
  607.  *    O_EXCL        used with O_CREAT--routine returns error if file exists
  608.  * Function returns file descriptor if successful, -1 and errno if not.
  609.  */
  610.  
  611. /*
  612.  * array to give arguments to access for various modes
  613.  * FIXME, this table depends on the specific integer values of O_XXX,
  614.  * and also contains integers (args to 'access') that should be #define's.
  615.  */
  616. static int modes[] =
  617.     {
  618.         04, /* O_RDONLY */
  619.         02, /* O_WRONLY */
  620.         06, /* O_RDWR */
  621.         06, /* invalid but we'd better cope -- O_WRONLY+O_RDWR */
  622.     };
  623.  
  624. /* Shut off the automatic emulation of open(), we'll need it. */
  625. #undef open
  626.  
  627. int
  628. open3(path, flags, mode)
  629. char *path;
  630. int flags, mode;
  631. {
  632.     extern int errno;
  633.     int exists = 1;
  634.     int call_creat = 0;
  635.     int fd;
  636.     /*
  637.      * We actually do the work by calling the open() or creat() system
  638.      * call, depending on the flags.  Call_creat is true if we will use 
  639.      * creat(), false if we will use open().
  640.      */
  641.  
  642.     /*
  643.      * See if the file exists and is accessible in the requested mode. 
  644.      *
  645.      * Strictly speaking we shouldn't be using access, since access checks
  646.      * against real uid, and the open call should check against euid.
  647.      * Most cases real uid == euid, so it won't matter.   FIXME.
  648.      * FIXME, the construction "flags & 3" and the modes table depends
  649.      * on the specific integer values of the O_XXX #define's.  Foo!
  650.      */
  651.     if (access(path,modes[flags & 3]) < 0) {
  652.         if (errno == ENOENT) {
  653.             /* the file does not exist */
  654.             exists = 0;
  655.         } else {
  656.             /* probably permission violation */
  657.             if (flags & O_EXCL) {
  658.                 /* Oops, the file exists, we didn't want it. */
  659.                 /* No matter what the error, claim EEXIST. */
  660.                 errno = EEXIST;
  661.             }
  662.             return -1;
  663.         }
  664.     }
  665.  
  666.     /* if we have the O_CREAT bit set, check for O_EXCL */
  667.     if (flags & O_CREAT) {
  668.         if ((flags & O_EXCL) && exists) {
  669.             /* Oops, the file exists and we didn't want it to. */
  670.             errno = EEXIST;
  671.             return -1;
  672.         }
  673.         /*
  674.          * If the file doesn't exist, be sure to call creat() so that
  675.          * it will be created with the proper mode.
  676.          */
  677.         if (!exists) call_creat = 1;
  678.     } else {
  679.         /* If O_CREAT isn't set and the file doesn't exist, error. */
  680.         if (!exists) {
  681.             errno = ENOENT;
  682.             return -1;
  683.         }
  684.     }
  685.  
  686.     /*
  687.      * If the O_TRUNC flag is set and the file exists, we want to call
  688.      * creat() anyway, since creat() guarantees that the file will be
  689.      * truncated and open()-for-writing doesn't.
  690.      * (If the file doesn't exist, we're calling creat() anyway and the
  691.      * file will be created with zero length.)
  692.      */
  693.     if ((flags & O_TRUNC) && exists) call_creat = 1;
  694.     /* actually do the call */
  695.     if (call_creat) {
  696.         /*
  697.          * call creat.  May have to close and reopen the file if we
  698.          * want O_RDONLY or O_RDWR access -- creat() only gives
  699.          * O_WRONLY.
  700.          */
  701.         fd = creat(path,mode);
  702.         if (fd < 0 || (flags & O_WRONLY)) return fd;
  703.         if (close(fd) < 0) return -1;
  704.         /* Fall out to reopen the file we've created */
  705.     }
  706.  
  707.     /*
  708.      * calling old open, we strip most of the new flags just in case.
  709.      */
  710.     return open(path, flags & (O_RDONLY|O_WRONLY|O_RDWR|O_BINARY));
  711. }
  712. #endif
  713.  
  714. #ifdef    WANT_MKNOD
  715. #ifdef MSDOS
  716. #ifndef __EMX__
  717. typedef int dev_t;
  718. #endif
  719. #endif
  720. /* Fake mknod by complaining */
  721. int
  722. mknod(path, mode, dev)
  723.     char        *path;
  724.     unsigned short    mode;
  725.     dev_t        dev;
  726. {
  727.     extern int    errno;
  728.     int        fd;
  729.  
  730. #ifdef ENXIO
  731.     errno = ENXIO;        /* No such device or address */
  732. #else
  733.     errno = EMSDOS;        /* No such device or address */
  734. #endif    
  735.     return -1;        /* Just give an error */
  736. }
  737.  
  738. /* Fake links by copying */
  739. int
  740. link(path1, path2)
  741.     char        *path1;
  742.     char        *path2;
  743. {
  744.     char    buf[256];
  745.     int    ifd, ofd;
  746.     int    nrbytes;
  747.     int    nwbytes;
  748.  
  749.     fprintf(stderr, "%s: %s: cannot link to %s, copying instead\n",
  750.         tar, path1, path2);
  751.     if ((ifd = open(path1, O_RDONLY|O_BINARY)) < 0)
  752.         return -1;
  753.     if ((ofd = creat(path2, 0666)) < 0)
  754.         return -1;
  755.     setmode(ofd, O_BINARY);
  756.     while ((nrbytes = read(ifd, buf, sizeof(buf))) > 0) {
  757.         if ((nwbytes = write(ofd, buf, nrbytes)) != nrbytes) {
  758.             nrbytes = -1;
  759.             break;
  760.         }
  761.     }
  762.     /* Note use of "|" rather than "||" below: we want to close
  763.      * the files even if an error occurs.
  764.      */
  765.     if ((nrbytes < 0) | (0 != close(ifd)) | (0 != close(ofd))) {
  766.         unlink(path2);
  767.         return -1;
  768.     }
  769.     return 0;
  770. }
  771.  
  772. /* everyone owns everything on MS-DOS (or is it no one owns anything?) */
  773. int
  774. chown(path, uid, gid)
  775.     char    *path;
  776.     int    uid;
  777.     int    gid;
  778. {
  779.     return 0;
  780. }
  781.  
  782. int
  783. geteuid()
  784. {
  785.     return 0;
  786. }
  787. #endif    /* WANT_MKNOD */
  788.  
  789. #ifdef WANT_UTILS
  790. /* Stash argv[0] here so panic will know what the program is called */
  791. char *myname = 0;
  792.  
  793. void
  794. panic(s)
  795. char *s;
  796. {
  797.     if(myname)
  798.         fprintf(stderr,"%s:",myname);
  799.     fprintf(stderr,s);
  800.     putc('\n',stderr);
  801.     exit(12);
  802. }
  803.  
  804.  
  805. char *
  806. ck_malloc(size)
  807. size_t size;
  808. {
  809.     char *ret;
  810.     char *malloc();
  811.  
  812.     if(!size)
  813.         size++;
  814.     ret=malloc(size);
  815.     if(ret==0)
  816.         panic("Couldn't allocate memory");
  817.     return ret;
  818. }
  819.  
  820. char *
  821. ck_realloc(ptr,size)
  822. char *ptr;
  823. size_t size;
  824. {
  825.     char *ret;
  826.     char *realloc();
  827.  
  828.     if(!ptr)
  829.         ret=ck_malloc(size);
  830.     else
  831.         ret=realloc(ptr,size);
  832.     if(ret==0)
  833.         panic("Couldn't re-allocate memory");
  834.     return ret;
  835. }
  836.  
  837. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  838.    nor do we care, as long as it doesn't mind being aligned on a char boundry.
  839.  */
  840.  
  841. struct buffer {
  842.     int    allocated;
  843.     int    length;
  844.     char    *b;
  845. };
  846.  
  847. #define MIN_ALLOCATE 50
  848.  
  849. char *
  850. init_buffer()
  851. {
  852.     struct buffer *b;
  853.  
  854.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  855.     b->allocated=MIN_ALLOCATE;
  856.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  857.     b->length=0;
  858.     return (char *)b;
  859. }
  860.  
  861. int
  862. buffer_length(bb)
  863. char *bb;
  864. {
  865.   struct buffer *b;
  866.  
  867.   b = (struct buffer *)bb;
  868.   return b->length;
  869. }
  870.  
  871. void
  872. flush_buffer(bb)
  873. char *bb;
  874. {
  875.     struct buffer *b;
  876.  
  877.     b=(struct buffer *)bb;
  878.     free(b->b);
  879.     b->b=0;
  880.     b->allocated=0;
  881.     b->length=0;
  882.     free((void *)b);
  883. }
  884.  
  885. void
  886. add_buffer(bb,p,n)
  887. char *bb;
  888. char *p;
  889. int n;
  890. {
  891.     struct buffer *b;
  892.  
  893.     b=(struct buffer *)bb;
  894.     if(b->length+n>b->allocated) {
  895.         b->allocated=b->length+n+MIN_ALLOCATE;
  896.         b->b=(char *)ck_realloc(b->b,b->allocated);
  897.     }
  898.     bcopy(p,b->b+b->length,n);
  899.     b->length+=n;
  900. }
  901.  
  902. char *
  903. get_buffer(bb)
  904. char *bb;
  905. {
  906.     struct buffer *b;
  907.  
  908.     b=(struct buffer *)bb;
  909.     return b->b;
  910. }
  911.  
  912. char *
  913. merge_sort(list,n,off,cmp)
  914. char *list;
  915. int (*cmp)();
  916. unsigned n;
  917. int off;
  918. {
  919.     char *ret;
  920.  
  921.     char *alist,*blist;
  922.     unsigned alength,blength;
  923.  
  924.     char *tptr;
  925.     int tmp;
  926.     char **prev;
  927. #define NEXTOF(ptr)    (* ((char **)(((char *)(ptr))+off) ) )
  928.     if(n==1)
  929.         return list;
  930.     if(n==2) {
  931.         if((*cmp)(list,NEXTOF(list))>0) {
  932.             ret=NEXTOF(list);
  933.             NEXTOF(ret)=list;
  934.             NEXTOF(list)=0;
  935.             return ret;
  936.         }
  937.         return list;
  938.     }
  939.     alist=list;
  940.     alength=(n+1)/2;
  941.     blength=n/2;
  942.     for(tptr=list,tmp=(n-1)/2;tmp;tptr=NEXTOF(tptr),tmp--)
  943.         ;
  944.     blist=NEXTOF(tptr);
  945.     NEXTOF(tptr)=0;
  946.  
  947.     alist=merge_sort(alist,alength,off,cmp);
  948.     blist=merge_sort(blist,blength,off,cmp);
  949.     prev = &ret;
  950.     for(;alist && blist;) {
  951.         if((*cmp)(alist,blist)<0) {
  952.             tptr=NEXTOF(alist);
  953.             *prev = alist;
  954.             prev = &(NEXTOF(alist));
  955.             alist=tptr;
  956.         } else {
  957.             tptr=NEXTOF(blist);
  958.             *prev = blist;
  959.             prev = &(NEXTOF(blist));
  960.             blist=tptr;
  961.         }
  962.     }
  963.     if(alist)
  964.         *prev = alist;
  965.     else
  966.         *prev = blist;
  967.  
  968.     return ret;
  969. }
  970.  
  971. void
  972. ck_close(fd)
  973. int fd;
  974. {
  975.     if(close(fd)<0) {
  976.         msg_perror("can't close a file #%d",fd);
  977.         exit(EX_SYSTEM);
  978.     }
  979. }
  980.  
  981. #include <ctype.h>
  982.  
  983. /* Quote_copy_string is like quote_string, but instead of modifying the
  984.    string in place, it malloc-s a copy  of the string, and returns that.
  985.    If the string does not have to be quoted, it returns the NULL string.
  986.    The allocated copy can, of course, be freed with free() after the
  987.    caller is done with it.
  988.  */
  989. char *
  990. quote_copy_string(string)
  991. unsigned char *string;  /* unsigned, 8/1/91, Max Hailperin <max@nic.gac.edu> */
  992. {
  993.     unsigned char    *from_here; /* ditto Max Hailperin <max@nic.gac.edu> */
  994.     char    *to_there = 0;
  995.     char    *copy_buf = 0;
  996.     int    c;
  997.     int    copying = 0;
  998.     char    *malloc();
  999.  
  1000.     from_here=string;
  1001.     while(*from_here) {
  1002.         c= *from_here++;
  1003.         if(c=='\\') {
  1004.             if(!copying) {
  1005.                 int n;
  1006.  
  1007.                 n=(from_here-string)-1;
  1008.                 copying++;
  1009.                 copy_buf=(char *)malloc(n+5+strlen(from_here)*4);
  1010.                 if(!copy_buf)
  1011.                     return 0;
  1012.                 bcopy(string,copy_buf,n);
  1013.                 to_there=copy_buf+n;
  1014.             }
  1015.             *to_there++='\\';
  1016.             *to_there++='\\';
  1017.                 /* I added the isascii test and != ' ' test below. The former
  1018.                    fixes a latent bug -- on some systems (not mine), isprint
  1019.                    is only defined where isascii is true.  The latter fixes
  1020.                    a bug in that a symlink with a long name containing the
  1021.                    string " to " would otherwise cause trouble for
  1022.                    extract_mangle. -Max Hailperin <max@nic.gac.edu> 8/1/91 */
  1023.         } else if(isascii(c) && isprint(c) /*&& c != ' '*/) {
  1024.             if(copying)
  1025.                 *to_there++= c;
  1026.         } else {
  1027.             if(!copying) {
  1028.                 int    n;
  1029.  
  1030.                 n=(from_here-string)-1;
  1031.                 copying++;
  1032.                 copy_buf=(char *)malloc(n+5+strlen(from_here)*4);
  1033.                 if(!copy_buf)
  1034.                     return 0;
  1035.                 bcopy(string,copy_buf,n);
  1036.                 to_there=copy_buf+n;
  1037.             }
  1038.             *to_there++='\\';
  1039.             if(c=='\n') *to_there++='n';
  1040.             else if(c=='\t') *to_there++='t';
  1041.             else if(c=='\f') *to_there++='f';
  1042.             else if(c=='\b') *to_there++='b';
  1043.             else if(c=='\r') *to_there++='r';
  1044.             else if(c=='\177') *to_there++='?';
  1045.             else {
  1046.                 to_there[0]=(c>>6)+'0';
  1047.                 to_there[1]=((c>>3)&07)+'0';
  1048.                 to_there[2]=(c&07)+'0';
  1049.                 to_there+=3;
  1050.             }
  1051.         }
  1052.     }
  1053.     if(copying) {
  1054.         *to_there='\0';
  1055.         return copy_buf;
  1056.     }
  1057.     return (char *)0;
  1058. }
  1059.  
  1060.  
  1061. /* Un_quote_string takes a quoted c-string (like those produced by
  1062.    quote_string or quote_copy_string and turns it back into the
  1063.    un-quoted original.  This is done in place.
  1064.  */
  1065.  
  1066. /* There is no un-quote-copy-string.  Write it yourself */
  1067.  
  1068. char *un_quote_string(string)
  1069. char *string;
  1070. {
  1071.     char *ret;
  1072.     char *from_here;
  1073.     char *to_there;
  1074.     int    tmp;
  1075.  
  1076.     ret=string;
  1077.     to_there=string;
  1078.     from_here=string;
  1079.     while(*from_here) {
  1080.         if(*from_here!='\\') {
  1081.             if(from_here!=to_there)
  1082.                 *to_there++= *from_here++;
  1083.             else
  1084.                 from_here++,to_there++;
  1085.             continue;
  1086.         }
  1087.         switch(*++from_here) {
  1088.         case '\\':
  1089.             *to_there++= *from_here++;
  1090.             break;
  1091.         case 'n':
  1092.             *to_there++= '\n';
  1093.             from_here++;
  1094.             break;
  1095.         case 't':
  1096.             *to_there++= '\t';
  1097.             from_here++;
  1098.             break;
  1099.         case 'f':
  1100.             *to_there++= '\f';
  1101.             from_here++;
  1102.             break;
  1103.         case 'b':
  1104.             *to_there++= '\b';
  1105.             from_here++;
  1106.             break;
  1107.         case 'r':
  1108.             *to_there++= '\r';
  1109.             from_here++;
  1110.             break;
  1111.         case '?':
  1112.             *to_there++= 0177;
  1113.             from_here++;
  1114.             break;
  1115.         case '0':
  1116.         case '1':
  1117.         case '2':
  1118.         case '3':
  1119.         case '4':
  1120.         case '5':
  1121.         case '6':
  1122.         case '7':
  1123.             tmp= *from_here - '0';
  1124.             from_here++;
  1125.             if(*from_here<'0' || *from_here>'7') {
  1126.                 *to_there++= tmp;
  1127.                 break;
  1128.             }
  1129.             tmp= tmp*8 + *from_here-'0';
  1130.             from_here++;
  1131.             if(*from_here<'0' || *from_here>'7') {
  1132.                 *to_there++= tmp;
  1133.                 break;
  1134.             }
  1135.             tmp=tmp*8 + *from_here-'0';
  1136.             from_here++;
  1137.                         /* added missing ++ below 8/1/91 Max Hailperin
  1138.                            <max@nic.gac.edu> */
  1139.             *to_there++=tmp;
  1140.             break;
  1141.         default:
  1142.             ret=0;
  1143.             *to_there++='\\';
  1144.             *to_there++= *from_here++;
  1145.             break;
  1146.         }
  1147.     }
  1148.     if(*to_there)
  1149.         *to_there++='\0';
  1150.     return ret;
  1151. }
  1152. #endif
  1153.  
  1154. #ifdef WANT_CK_PIPE
  1155. void ck_pipe(pipes)
  1156. int *pipes;
  1157. {
  1158.     if(pipe(pipes)<0) {
  1159.         msg_perror("can't open a pipe");
  1160.         exit(EX_SYSTEM);
  1161.     }
  1162. }
  1163.  
  1164. #endif
  1165.  
  1166. #ifdef WANT_GETWD
  1167. char *
  1168. getwd(path)
  1169. char *path;
  1170. {
  1171.     FILE *fp;
  1172.     FILE *popen();
  1173.  
  1174.     fp=popen("pwd","r");
  1175.     if(!fp)
  1176.         return 0;
  1177.     if(!fgets(path,100,fp))
  1178.         return 0;
  1179.     if(!pclose(fp))
  1180.         return 0;
  1181.     return path;
  1182. }
  1183. #endif /* WANT_CK_PIPE */
  1184.  
  1185.  
  1186. #ifdef WANT_STRSTR
  1187.  
  1188. /*
  1189.  * strstr - find first occurrence of wanted in s
  1190.  */
  1191.  
  1192. char *                /* found string, or NULL if none */
  1193. strstr(s, wanted)
  1194. char *s;
  1195. char *wanted;
  1196. {
  1197.     register char *scan;
  1198.     register size_t len;
  1199.     register char firstc;
  1200.     extern int strcmp();
  1201.  
  1202.     if (*wanted == '\0')
  1203.       return (char *)0;
  1204.     /*
  1205.      * The odd placement of the two tests is so "" is findable.
  1206.      * Also, we inline the first char for speed.
  1207.      * The ++ on scan has been moved down for optimization.
  1208.      */
  1209.     firstc = *wanted;
  1210.     len = strlen(wanted);
  1211.     for (scan = s; *scan != firstc || strncmp(scan, wanted, len) != 0; )
  1212.         if (*scan++ == '\0')
  1213.             return (char *)0;
  1214.     return scan;
  1215. }
  1216. #endif
  1217.  
  1218. #ifdef WANT_FTRUNCATE
  1219.  
  1220. #ifdef F_FREESP
  1221. /* code courtesy of William Kucharski */
  1222.  
  1223. int
  1224. ftruncate(fd, length)
  1225. int fd;                       /* file descriptor */
  1226. off_t length;         /* length to set file to */
  1227. {
  1228.     struct flock fl;
  1229.  
  1230.     fl.l_whence = 0;
  1231.     fl.l_len = 0;
  1232.     fl.l_start = length;
  1233.     fl.l_type = F_WRLCK;    /* write lock on file space */
  1234.  
  1235.     /*
  1236.      * This relies on the UNDOCUMENTED F_FREESP argument to
  1237.      * fcntl(2), which truncates the file so that it ends at the
  1238.      * position indicated by fl.l_start.
  1239.      *
  1240.      * Will minor miracles never cease?
  1241.      */
  1242.  
  1243.     if (fcntl(fd, F_FREESP, &fl) < 0)
  1244.         return -1;
  1245.  
  1246.     return 0;
  1247. }
  1248.  
  1249.  
  1250. #else
  1251. int
  1252. ftruncate(fd, length)
  1253. int fd;
  1254. off_t length;
  1255. {
  1256.     errno = EIO;
  1257.     return -1;
  1258. }
  1259. #endif
  1260.  
  1261. #endif
  1262.  
  1263.  
  1264.  
  1265.  
  1266. extern FILE *msg_file;
  1267.  
  1268. #ifdef STDC_MSG
  1269. #include <stdarg.h>
  1270.  
  1271. void
  1272. msg(char *str,...)
  1273. {
  1274.     va_list args;
  1275.  
  1276.     va_start(args,str);
  1277.     fflush(msg_file);
  1278.     fprintf(stderr,"%s: ",tar);
  1279.     if(f_sayblock)
  1280.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1281.     vfprintf(stderr,str,args);
  1282.     va_end(args);
  1283.     putc('\n',stderr);
  1284.     fflush(stderr);
  1285. }
  1286.  
  1287. void
  1288. msg_perror(char *str,...)
  1289. {
  1290.     va_list args;
  1291.     int save_e;
  1292.     extern int errno;
  1293.  
  1294.     save_e=errno;
  1295.     fflush(msg_file);
  1296.     fprintf(stderr,"%s: ",tar);
  1297.     if(f_sayblock)
  1298.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1299.     va_start(args,str);
  1300.     vfprintf(stderr,str,args);
  1301.     va_end(args);
  1302.     errno=save_e;
  1303.     perror(" ");
  1304.     fflush(stderr);
  1305. }
  1306. #endif
  1307.  
  1308. #ifdef VARARGS_MSG
  1309. #include <varargs.h>
  1310. void msg(str,va_alist)
  1311. char *str;
  1312. va_dcl
  1313. {
  1314.     va_list args;
  1315.  
  1316.     fflush(msg_file);
  1317.     fprintf(stderr,"%s: ",tar);
  1318.     if(f_sayblock)
  1319.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1320.     va_start(args);
  1321.     vfprintf(stderr,str,args);
  1322.     va_end(args);
  1323.     putc('\n',stderr);
  1324.     fflush(stderr);
  1325. }
  1326.  
  1327. void msg_perror(str,va_alist)
  1328. char *str;
  1329. va_dcl
  1330. {
  1331.     va_list args;
  1332.     int save_e;
  1333.     extern int errno;
  1334.  
  1335.     save_e=errno;
  1336.     fflush(msg_file);
  1337.     fprintf(stderr,"%s: ",tar);
  1338.     if(f_sayblock)
  1339.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1340.     va_start(args);
  1341.     vfprintf(stderr,str,args);
  1342.     va_end(args);
  1343.     errno=save_e;
  1344.     perror(" ");
  1345.     fflush(stderr);
  1346. }
  1347. #endif
  1348.  
  1349. #ifdef DOPRNT_MSG
  1350. void msg(str,args)
  1351. char *str;
  1352. int args;
  1353. {
  1354.     fflush(msg_file);
  1355.     fprintf(stderr,"%s: ",tar);
  1356.     if(f_sayblock)
  1357.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1358.     _doprnt(str, &args, stderr);
  1359.     putc('\n',stderr);
  1360.     fflush(stderr);
  1361. }
  1362.  
  1363. void msg_perror(str,args)
  1364. char *str;
  1365. {
  1366.     int save_e;
  1367.     extern int errno;
  1368.  
  1369.     save_e=errno;
  1370.     fflush(msg_file);
  1371.     fprintf(stderr,"%s: ",tar);
  1372.     if(f_sayblock)
  1373.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1374.     _doprnt(str, &args, stderr);
  1375.     errno=save_e;
  1376.     perror(" ");
  1377.     fflush(stderr);
  1378. }
  1379.  
  1380. #endif
  1381. #ifdef LOSING_MSG
  1382. void msg(str,a1,a2,a3,a4,a5,a6)
  1383. char *str;
  1384. {
  1385.     fflush(msg_file);
  1386.     fprintf(stderr,"%s: ",tar);
  1387.     if(f_sayblock)
  1388.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1389.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1390.     putc('\n',stderr);
  1391.     fflush(stderr);
  1392. }
  1393.  
  1394. void msg_perror(str,a1,a2,a3,a4,a5,a6)
  1395. char *str;
  1396. {
  1397.     int save_e;
  1398.     extern int errno;
  1399.  
  1400.     save_e=errno;
  1401.     fflush(msg_file);
  1402.     fprintf(stderr,"%s: ",tar);
  1403.     if(f_sayblock)
  1404.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1405.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1406.     fprintf(stderr,": ");
  1407.     errno=save_e;
  1408.     perror(" ");
  1409. }
  1410.  
  1411. #endif
  1412.  
  1413. /*------------------------------------------------------------------------*/
  1414.  
  1415. #ifdef OS2
  1416.  
  1417. #define INCL_DOSFILEMGR
  1418. #define INCL_DOSQUEUES
  1419. #include <os2.h>
  1420.  
  1421. #undef opendir
  1422. #include <dirent.h>
  1423.  
  1424. void ck_pipe(pipes)
  1425. unsigned *pipes;
  1426. {
  1427.     unsigned r;
  1428.     if (r = DosCreatePipe((ULONG *)&pipes[0], (ULONG *)&pipes[1], 40 * 1024)) {
  1429.         msg("can't create a pipe (err=%u)", r);
  1430.         exit(EX_SYSTEM);
  1431.     }
  1432. }
  1433.  
  1434. void
  1435. get_fileattr(char *name, struct stat *st)
  1436. {
  1437.     ULONG attr;
  1438.     FILESTATUS3 fi;
  1439.     if (!f_use_protection)
  1440.         return;
  1441.     st->st_mode &= ~0077;
  1442. #ifdef __EMX__
  1443.     attr = st->st_attr;
  1444. #else
  1445.     if (DosQueryPathInfo(name, FIL_STANDARD, &fi, sizeof fi))
  1446.         return;
  1447.     attr = fi.attrFile;
  1448. #endif
  1449.     if (attr & A_HIDDEN)
  1450.         st->st_mode |= 0010;
  1451.     if (attr & A_SYSTEM)
  1452.         st->st_mode |= 0001;
  1453. }
  1454.  
  1455. void
  1456. put_fileattr(char *name, struct stat *st)
  1457. {
  1458.     FILESTATUS3 fi;
  1459.     if (!f_use_protection)
  1460.         return;
  1461.     if (DosQueryPathInfo(name, FIL_STANDARD, &fi, sizeof fi))
  1462.         return;
  1463.     if (st->st_mode & 0010)
  1464.         fi.attrFile |= A_HIDDEN;
  1465.     if (st->st_mode & 0001)
  1466.         fi.attrFile |= A_SYSTEM;
  1467.     DosSetPathInfo(name, FIL_STANDARD, &fi, sizeof fi, 0);
  1468. }
  1469.  
  1470. void
  1471. reset_archive(char *name)
  1472. {
  1473.     FILESTATUS3 fi;
  1474.     if (DosQueryPathInfo(name, FIL_STANDARD, &fi, sizeof fi))
  1475.         return;
  1476.     fi.attrFile &= ~A_ARCHIVE;
  1477.     DosSetPathInfo(name, FIL_STANDARD, &fi, sizeof fi, 0);
  1478. }
  1479.  
  1480. #include <time.h>
  1481. #include <sys/time.h>
  1482.  
  1483. static void mkos2time(long sec, PFTIME pftime, PFDATE pfdate)
  1484. {
  1485.     struct tm *p;
  1486.     if (sec)
  1487.         sec -= timezone;
  1488.     else
  1489.         time(&sec);
  1490.     p = gmtime (&sec);
  1491.       *(USHORT *)pftime = 
  1492.       p->tm_sec / 2 + (p->tm_min << 5) + (p->tm_hour << 11);
  1493.     *(USHORT *)pfdate = 
  1494.       p->tm_mday + ((p->tm_mon + 1) << 5) + ((p->tm_year - 80) << 9);
  1495. }
  1496.  
  1497. void
  1498. put_filetime(char *name, struct stat *st)
  1499. {
  1500.       struct tm *p;
  1501.     FILESTATUS3 fi;
  1502.     if (DosQueryPathInfo(name, FIL_STANDARD, &fi, sizeof fi))
  1503.         return;
  1504.     mkos2time(st->st_atime, &fi.ftimeLastAccess, &fi.fdateLastAccess);
  1505.     mkos2time(st->st_ctime, &fi.ftimeCreation, &fi.fdateCreation);
  1506.     mkos2time(st->st_mtime, &fi.ftimeLastWrite, &fi.fdateLastWrite);
  1507.     DosSetPathInfo(name, FIL_STANDARD, &fi, sizeof fi, 0);
  1508. }
  1509.  
  1510. void
  1511. force_delete(char *file)
  1512. {
  1513.     DosForceDelete(file);
  1514. }
  1515.  
  1516. #endif
  1517.  
  1518.