home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd2.bin / dosutils / gtar109 / port.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  28KB  |  1,287 lines

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