home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / rcs5ap1s.lzh / RCS5AP1S / RCSFNMS.C < prev    next >
C/C++ Source or Header  |  1991-01-30  |  34KB  |  1,227 lines

  1. /*
  2.  *                     RCS file name handling
  3.  */
  4. /****************************************************************************
  5.  *                     creation and deletion of /tmp temporaries
  6.  *                     pairing of RCS file names and working file names.
  7.  *                     Testprogram: define PAIRTEST
  8.  ****************************************************************************
  9.  */
  10.  
  11. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  12.    Copyright 1990 by Paul Eggert
  13.    Distributed under license by the Free Software Foundation, Inc.
  14.  
  15. This file is part of RCS.
  16.  
  17. RCS is free software; you can redistribute it and/or modify
  18. it under the terms of the GNU General Public License as published by
  19. the Free Software Foundation; either version 1, or (at your option)
  20. any later version.
  21.  
  22. RCS is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. GNU General Public License for more details.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with RCS; see the file COPYING.  If not, write to
  29. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  30.  
  31. Report problems and direct all questions to:
  32.  
  33.     rcs-bugs@cs.purdue.edu
  34.  
  35. */
  36.  
  37.  
  38.  
  39.  
  40. /* $Log: rcsfnms.c,v $
  41.  * Revision 5.7  1991/01/30  14:13:06  apratt
  42.  * Fixed a bug: bindex returns ptr to char PAST match if there's a match.
  43.  *
  44.  * Revision 5.7  1991/01/30  14:11:14  apratt
  45.  * Fixed a bug: bindex returns ptr to char PAST match if there's a match.
  46.  *
  47.  * Revision 5.6  91/01/29  17:45:54  apratt
  48.  * Added AKP_BUGFIXES around my bug fixes, changed headstr
  49.  * 
  50.  * Revision 5.5  91/01/16  15:45:00  apratt
  51.  * This version works passably on the ST.
  52.  * 
  53.  * Revision 5.4  90/11/01  05:03:43  eggert
  54.  * checked in with -k by apratt at 91.01.10.13.15.14.
  55.  * 
  56.  * Revision 5.4  1990/11/01  05:03:43  eggert
  57.  * Permit arbitrary data in comment leaders.
  58.  *
  59.  * Revision 5.3  1990/09/14  22:56:16  hammer
  60.  * added more filename extensions and their comment leaders
  61.  *
  62.  * Revision 5.2  1990/09/04  08:02:23  eggert
  63.  * Fix typo when !RCSSEP.
  64.  *
  65.  * Revision 5.1  1990/08/29  07:13:59  eggert
  66.  * Work around buggy compilers with defective argument promotion.
  67.  *
  68.  * Revision 5.0  1990/08/22  08:12:50  eggert
  69.  * Ignore signals when manipulating the semaphore file.
  70.  * Modernize list of file name extensions.
  71.  * Permit paths of arbitrary length.  Beware file names beginning with "-".
  72.  * Remove compile-time limits; use malloc instead.
  73.  * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  74.  * Ansify and Posixate.
  75.  * Don't use access().  Fix test for non-regular files.  Tune.
  76.  *
  77.  * Revision 4.8  89/05/01  15:09:41  narten
  78.  * changed getwd to not stat empty directories.
  79.  * 
  80.  * Revision 4.7  88/08/09  19:12:53  eggert
  81.  * Fix troff macro comment leader bug; add Prolog; allow cc -R; remove lint.
  82.  * 
  83.  * Revision 4.6  87/12/18  11:40:23  narten
  84.  * additional file types added from 4.3 BSD version, and SPARC assembler
  85.  * comment character added. Also, more lint cleanups. (Guy Harris)
  86.  * 
  87.  * Revision 4.5  87/10/18  10:34:16  narten
  88.  * Updating version numbers. Changes relative to 1.1 actually relative
  89.  * to verion 4.3
  90.  * 
  91.  * Revision 1.3  87/03/27  14:22:21  jenkins
  92.  * Port to suns
  93.  * 
  94.  * Revision 1.2  85/06/26  07:34:28  svb
  95.  * Comment leader '% ' for '*.tex' files added.
  96.  * 
  97.  * Revision 4.3  83/12/15  12:26:48  wft
  98.  * Added check for KDELIM in file names to pairfilenames().
  99.  * 
  100.  * Revision 4.2  83/12/02  22:47:45  wft
  101.  * Added csh, red, and sl file name suffixes.
  102.  * 
  103.  * Revision 4.1  83/05/11  16:23:39  wft
  104.  * Added initialization of Dbranch to InitAdmin(). Canged pairfilenames():
  105.  * 1. added copying of path from workfile to RCS file, if RCS file is omitted;
  106.  * 2. added getting the file status of RCS and working files;
  107.  * 3. added ignoring of directories.
  108.  * 
  109.  * Revision 3.7  83/05/11  15:01:58  wft
  110.  * Added comtable[] which pairs file name suffixes with comment leaders;
  111.  * updated InitAdmin() accordingly.
  112.  * 
  113.  * Revision 3.6  83/04/05  14:47:36  wft
  114.  * fixed Suffix in InitAdmin().
  115.  * 
  116.  * Revision 3.5  83/01/17  18:01:04  wft
  117.  * Added getwd() and rename(); these can be removed by defining
  118.  * V4_2BSD, since they are not needed in 4.2 bsd.
  119.  * Changed sys/param.h to sys/types.h.
  120.  *
  121.  * Revision 3.4  82/12/08  21:55:20  wft
  122.  * removed unused variable.
  123.  *
  124.  * Revision 3.3  82/11/28  20:31:37  wft
  125.  * Changed mktempfile() to store the generated file names.
  126.  * Changed getfullRCSname() to store the file and pathname, and to
  127.  * delete leading "../" and "./".
  128.  *
  129.  * Revision 3.2  82/11/12  14:29:40  wft
  130.  * changed pairfilenames() to handle file.sfx,v; also deleted checkpathnosfx(),
  131.  * checksuffix(), checkfullpath(). Semaphore name generation updated.
  132.  * mktempfile() now checks for nil path; freefilename initialized properly.
  133.  * Added Suffix .h to InitAdmin. Added testprogram PAIRTEST.
  134.  * Moved rmsema, trysema, trydiraccess, getfullRCSname from rcsutil.c to here.
  135.  *
  136.  * Revision 3.1  82/10/18  14:51:28  wft
  137.  * InitAdmin() now initializes StrictLocks=STRICT_LOCKING (def. in rcsbase.h).
  138.  * renamed checkpath() to checkfullpath().
  139.  */
  140.  
  141.  
  142. #include "rcsbase.h"
  143.  
  144. libId(fnmsId, "$Id: rcsfnms.c,v 5.7 1991/01/30 14:13:06 apratt Exp $")
  145.  
  146. const char *RCSfilename;
  147. char *workfilename;
  148. struct stat RCSstat, workstat; /* file status for RCS file and working file */
  149. int haveworkstat;
  150.  
  151. #if !USE_AKP_PAIRS
  152. static const char rcsdir[] = RCSDIR;
  153. #endif
  154.  
  155. #define TEMPNAMES 4 /* must be at least DIRTEMPNAMES (see rcsedit.c) */
  156. static char tfnames[TEMPNAMES][L_tmpnam];    /* unlink these when done */
  157. static volatile int tfmade[TEMPNAMES];        /* if these flags are set */
  158.  
  159.  
  160. struct compair {
  161.     const char *suffix, *comlead;
  162. };
  163.  
  164. static const struct compair comtable[] = {
  165. /* comtable pairs each filename suffix with a comment leader. The comment   */
  166. /* leader is placed before each line generated by the $Log keyword. This    */
  167. /* table is used to guess the proper comment leader from the working file's */
  168. /* suffix during initial ci (see InitAdmin()). Comment leaders are needed   */
  169. /* for languages without multiline comments; for others they are optional.  */
  170.     "a",   "-- ",   /* Ada         */
  171.         "c",   " * ",   /* C           */
  172.     "C",   "// ",    /* C++ in all its infinite guises */
  173.     "CC",  "// ",
  174.     "c++", "// ",
  175.     "cc",  "// ",
  176.     "cxx", "// ",
  177.     "cl",  ";;; ",  /* Common Lisp */
  178.     "cmf", "C ",    /* CM FORTRAN  */
  179.     "cs",  " * ",    /* C*          */
  180.     "el",  "; ",    /* Emacs Lisp  */
  181.     "f",   "c ",    /* Fortran     */
  182.     "for", "c ",
  183.         "h",   " * ",   /* C-header    */
  184.         "l",   " * ",   /* lex      NOTE: conflict between lex and franzlisp */
  185.     "lisp",";;; ",    /* Lucid Lisp  */
  186.         "mac", "; ",    /* macro       vms or dec-20 or pdp-11 macro */
  187.     "me",  ".\\\" ",/* me-macros   t/nroff*/
  188.     "ml",  "; ",    /* mocklisp    */
  189.     "mm",  ".\\\" ",/* mm-macros   t/nroff*/
  190.     "ms",  ".\\\" ",/* ms-macros   t/nroff*/
  191.     "p",   " * ",   /* Pascal      */
  192.     "pl",  "% ",    /* Prolog      */
  193.     "tex", "% ",    /* TeX           */
  194.         "y",   " * ",   /* yacc        */
  195.     nil,   "# "     /* default for unknown suffix; must always be last */
  196. };
  197.  
  198.  
  199.     void
  200. ffclose(fptr)
  201. FILE * fptr;
  202. /* Function: checks ferror(fptr) and aborts the program if there were
  203.  * errors; otherwise closes fptr.
  204.  */
  205. {       if (ferror(fptr) || fclose(fptr)==EOF)
  206.         IOerror();
  207. }
  208.  
  209.  
  210.  
  211.     char *
  212. maketemp(n)
  213.     int n;
  214. /* Create a unique filename using n and the process id and store it
  215.  * into the nth slot in tfnames.
  216.  * Because of storage in tfnames, tempunlink() can unlink the file later.
  217.  * Returns a pointer to the filename created.
  218.  */
  219. {
  220.     char *p = tfnames[n];
  221.  
  222.     if (!tfmade[n]) {
  223. #if has_tmpnam
  224.         if (!tmpnam(p))
  225. #else
  226.         VOID sprintf(p, "%sRCS%cXXXXXX", tmp(), 'A'+n);
  227.         if (!mktemp(p))
  228. #endif
  229.             faterror("can't make temporary file name");
  230.     }
  231.     tfmade[n] = true;
  232.     return p;
  233. }
  234.  
  235.     void
  236. tempunlink()
  237. /* Clean up maketemp() files.  May be invoked by signal handler.
  238.  */
  239. {
  240.     register int i;
  241.  
  242.     for (i = TEMPNAMES;  0 <= --i;  )
  243.         if (tfmade[i]) {
  244.         VOID unlink(tfnames[i]);
  245.         tfmade[i] = 0;
  246.         }
  247. }
  248.  
  249.  
  250.     const char *
  251. bindex(sp,ch)
  252.     register const char *sp;
  253.     int ch;
  254. /* Function: Finds the last occurrence of character c in string sp
  255.  * and returns a pointer to the character just beyond it. If the
  256.  * character doesn't occur in the string, sp is returned.
  257.  */
  258. {
  259.     register const char c=ch, *r;
  260.         r = sp;
  261.         while (*sp) {
  262.                 if (*sp++ == c) r=sp;
  263.         }
  264.         return r;
  265. }
  266.  
  267.  
  268.  
  269.  
  270.  
  271.     static void
  272. InitAdmin()
  273. /* function: initializes an admin node */
  274. {
  275.     register const char *Suffix;
  276.         register int i;
  277.  
  278.     Head=nil; Dbranch=nil; AccessList=nil; Symbols=nil; Locks=nil;
  279.         StrictLocks=STRICT_LOCKING;
  280.  
  281.         /* guess the comment leader from the suffix*/
  282.         Suffix=bindex(workfilename, '.');
  283.  
  284. #if AKP_BUGFIXES
  285.     /* Improved by AKP: only match '.' AFTER all SLASHes */
  286.     if (Suffix < bindex(workfilename,SLASH)) Suffix = workfilename;
  287. #endif
  288.  
  289.         if (Suffix==workfilename) Suffix= ""; /* empty suffix; will get default*/
  290.     for (i=0; comtable[i].suffix && strcmp(Suffix,comtable[i].suffix); i++)
  291.         ;
  292.     Comment.string = comtable[i].comlead;
  293.     Comment.size = strlen(comtable[i].comlead);
  294.         Lexinit(); /* Note: if finptr==NULL, reads nothing; only initializes*/
  295. }
  296.  
  297.  
  298. #if !RCSSEP
  299.     static int
  300. isRCSfilename(f, p)
  301.     const char *f, *p;
  302. /* Yield true iff F (with pure file name P) is an RCS file name.  */
  303. {
  304.     return
  305.         p-f <= sizeof(rcsdir)-1  &&
  306.         ((p -= sizeof(rcsdir)-1) == f  ||  p[-1] == SLASH)  &&
  307.         strncmp(p, rcsdir, sizeof(rcsdir)-1) == 0;
  308. }
  309. #endif
  310.  
  311. #if RCSSEP
  312. #    define findpair(c,v,f,m) findpairfile(c,v,f)
  313. #else
  314. #    define findpair(c,v,f,m) findpairfile(c,v,f,m)
  315. #endif
  316.  
  317. #if !USE_AKP_PAIRS
  318.     static char *
  319. #if RCSSEP
  320. findpairfile(argc, argv, fname)
  321. #else
  322. findpairfile(argc, argv, fname, rcsmatch)
  323. int rcsmatch; /* *ARGV must be an RCS file name iff this is set.  */
  324. #endif
  325. int argc; char * argv[], *fname;
  326. /* Peek ahead in an ARGC-ARGV argument vector for a pathname ending in FNAME.
  327.  * Yield it if found, and set the corresponding pointer in ARGV to nil.
  328.  * Yield FNAME otherwise.
  329.  */
  330. {
  331.     register char *arg;
  332. #if !RCSSEP
  333.     register char *b;
  334. #endif
  335.     if (
  336.         0 < argc
  337. #if RCSSEP
  338.         && strcmp(bindex(arg = *argv,SLASH), fname) == 0
  339. #else
  340.         && strcmp(b = bindex(arg = *argv,SLASH), fname) == 0
  341.         && isRCSfilename(arg, b) == rcsmatch
  342. #endif
  343.     ) {
  344.         *argv = nil;
  345.         return arg;
  346.         }
  347.         return fname;
  348. }
  349. #endif
  350.  
  351.     static int
  352. handleworkstat(s)
  353.     int s;
  354. {
  355.     if (s==0  &&  !S_ISREG(workstat.st_mode)) {
  356.         error("%s isn't a regular file", workfilename);
  357.         return false;
  358.     }
  359. #if AKP_BUGFIXES
  360.     haveworkstat = (s == 0 ? 0 : errno);
  361. #else
  362.     haveworkstat = errno;
  363. #endif
  364.     return true;
  365. }
  366.  
  367. int getworkstat()
  368. /* Function: get status of workfilename. */
  369. {
  370.     errno = 0;
  371.     return handleworkstat(stat(workfilename, &workstat));
  372. }
  373.  
  374.     int
  375. getfworkstat(f)
  376.     int f;
  377. /* Get status of file descriptor f. */
  378. {
  379.     errno = 0;
  380.     return handleworkstat(fstat(f, &workstat));
  381. }
  382.  
  383.  
  384. #if defined(_POSIX_NO_TRUNC) & _POSIX_NO_TRUNC!=-1
  385. #    define LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED 0
  386. #else
  387. #    define LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED 1
  388. #endif
  389.  
  390. #if LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED
  391. #ifdef NAME_MAX
  392. #    define filenametoolong(path) (NAME_MAX < strlen(bindex(path,SLASH)))
  393. #else
  394.     static int
  395. filenametoolong(path)
  396.     char *path;
  397. /* Yield true if the last file name in PATH is too long. */
  398. {
  399.     static unsigned long dot_namemax;
  400.  
  401.     register size_t namelen;
  402.     register char *lastslash;
  403.     register unsigned long namemax;
  404.  
  405.     lastslash = strrchr(path, SLASH);
  406.     namelen = strlen(lastslash ? lastslash+1 : path);
  407.     if (namelen <= _POSIX_NAME_MAX) /* fast check for shorties */
  408.         return false;
  409.     if (lastslash) {
  410.         *lastslash = 0;
  411.         namemax = pathconf(path, _PC_NAME_MAX);
  412.         *lastslash = SLASH;
  413.     } else {
  414.         /* Cache the results for the working directory, for speed. */
  415.         if (!dot_namemax)
  416.             dot_namemax = pathconf(".", _PC_NAME_MAX);
  417.         namemax = dot_namemax;
  418.     }
  419.     /* If pathconf() yielded -1, namemax is now ULONG_MAX.  */
  420.     return namemax<namelen;
  421. }
  422. #endif
  423. #endif
  424.  
  425.     void
  426. bufalloc(b, size)
  427.     register struct buf *b;
  428.     size_t size;
  429. /* Ensure *B is a name buffer of at least SIZE bytes.
  430.  * *B's old contents can be freed; *B's new contents are undefined.
  431.  */
  432. {
  433.     if (b->size < size) {
  434.         if (b->size)
  435.             tfree(b->string);
  436.         else
  437.             b->size = sizeof(malloc_type);
  438.         while (b->size < size)
  439.             b->size <<= 1;
  440.         b->string = tnalloc(char, b->size);
  441.     }
  442. }
  443.  
  444.     void
  445. bufrealloc(b, size)
  446.     register struct buf *b;
  447.     size_t size;
  448. /* like bufalloc, except *B's old contents, if any, are preserved */
  449. {
  450.     if (b->size < size) {
  451.         if (!b->size)
  452.             bufalloc(b, size);
  453.         else {
  454.             while ((b->size <<= 1)  <  size)
  455.                 ;
  456.             b->string = (char *)testrealloc((malloc_type)b->string, b->size);
  457.         }
  458.     }
  459. }
  460.  
  461.     void
  462. bufautoend(b)
  463.     struct buf *b;
  464. /* Free an auto buffer at block exit. */
  465. {
  466.     if (b->size)
  467.         tfree(b->string);
  468. }
  469.  
  470.     char *
  471. bufenlarge(b, alim)
  472.     register struct buf *b;
  473.     const char **alim;
  474. /* Make *B larger.  Set *ALIM to its new limit, and yield the relocated value
  475.  * of its old limit.
  476.  */
  477. {
  478.     size_t s = b->size;
  479.     bufrealloc(b, s + 1);
  480.     *alim = b->string + b->size;
  481.     return b->string + s;
  482. }
  483.  
  484.     void
  485. bufscat(b, s)
  486.     struct buf *b;
  487.     const char *s;
  488. /* Concatenate S to B's end. */
  489. {
  490.     size_t blen  =  b->string ? strlen(b->string) : 0;
  491.     bufrealloc(b, blen+strlen(s)+1);
  492.     VOID strcpy(b->string+blen, s);
  493. }
  494.  
  495.     void
  496. bufscpy(b, s)
  497.     struct buf *b;
  498.     const char *s;
  499. /* Copy S into B. */
  500. {
  501.     bufalloc(b, strlen(s)+1);
  502.     VOID strcpy(b->string, s);
  503. }
  504.  
  505.  
  506.     FILE *
  507. rcsreadopen(RCSname)
  508.     const char *RCSname;
  509. /* Open RCSNAME for reading and yield its FILE* descriptor.
  510.  * Pass this routine to pairfilenames() for read-only access to the file.  */
  511. {
  512. #if AKP_BUGFIXES
  513.     int errno_hold;        /* AKP: this fixes a bug, I think */
  514. #endif
  515.     FILE *f;
  516.     seteid();
  517. #if AKP_BUGFIXES
  518.     errno = 0;
  519. #endif
  520.     f = fopen(RCSname, "r");
  521.  
  522. #if AKP_BUGFIXES
  523.     errno_hold = errno;
  524.     setrid();
  525.     errno = errno_hold;
  526. #else
  527.     setrid();
  528. #endif
  529.     return f;
  530. }
  531.  
  532. #if !USE_AKP_PAIRS
  533.  
  534.     int
  535. pairfilenames(argc, argv, rcsopen, mustread, tostdout)
  536.     int argc;
  537.     char **argv;
  538.     FILE *(*rcsopen)P((const char*));
  539.     int mustread, tostdout;
  540. /* Function: Pairs the filenames pointed to by argv; argc indicates
  541.  * how many there are.
  542.  * Places a pointer to the RCS filename into RCSfilename,
  543.  * and a pointer to the name of the working file into workfilename.
  544.  * If both the workfilename and the RCS filename are given, and tostdout
  545.  * is true, a warning is printed.
  546.  *
  547.  * If the RCS file exists, places its status into RCSstat.
  548.  *
  549.  * If the RCS file exists, it is RCSOPENed for reading, the file pointer
  550.  * is placed into finptr, and the admin-node is read in; returns 1.
  551.  * If the RCS file does not exist and mustread is set, an error is printed
  552.  * and 0 returned.
  553.  * If the RCS file does not exist and !mustread, the admin node
  554.  * is initialized and -1 returned.
  555.  *
  556.  * 0 is returned on all errors, e.g. files that are not regular files.
  557.  */
  558. {
  559.     static struct buf RCSbuf, tempbuf;
  560.  
  561.     register char *p, *arg, *tempfilename, *RCS1;
  562.     const char *purefname, *pureRCSname;
  563.     FILE *lock1;
  564.  
  565.     if (!(arg = *argv)) return 0; /* already paired filename */
  566.     if (*arg == '-') {
  567.         error("%s option is ignored after file names", arg);
  568.         return 0;
  569.     }
  570.  
  571.     /* Allocate buffer temporary to hold the default paired file name. */
  572.     for (purefname = p = arg; *p; )
  573.         switch (*p++) {
  574.             case SLASH:
  575.             purefname = p;
  576.             break;
  577.             /* Beware characters that cause havoc with ci -k. */
  578.             case KDELIM:
  579.             error("RCS file name `%s' contains %c", arg, KDELIM);
  580.             return 0;
  581.             case ' ': case '\n': case '\t':
  582.             error("RCS file name `%s' contains white space", arg);
  583.             return 0;
  584.         }
  585.     bufalloc(&tempbuf, p - purefname + 3);
  586.     tempfilename = tempbuf.string;
  587.  
  588.         /* first check suffix to see whether it is an RCS file or not */
  589. #if RCSSEP
  590.     if (purefname<(p-=2) && p[0]==RCSSEP && p[1]==RCSSUF)
  591. #else
  592.     if (isRCSfilename(arg, purefname))
  593. #endif
  594.     {
  595.                 /* RCS file name given*/
  596.         RCS1 = arg;
  597.         pureRCSname = purefname;
  598.                 /* derive workfilename*/
  599.         VOID strcpy(tempfilename, purefname);
  600.         tempfilename[p - purefname] = 0;
  601.                 /* try to find workfile name among arguments */
  602.         workfilename = findpair(argc-1,argv+1,tempfilename,false);
  603.         } else {
  604.                 /* working file given; now try to find RCS file */
  605.         workfilename = arg;
  606.                 /* derive RCS file name*/
  607.         VOID sprintf(tempfilename,"%s%c%c", purefname, RCSSEP, RCSSUF);
  608.                 /* Try to find RCS file name among arguments*/
  609.         RCS1 = findpair(argc-1,argv+1,tempfilename,true);
  610.                 pureRCSname=bindex(RCS1, SLASH);
  611.         }
  612.         /* now we have a (tentative) RCS filename in RCS1 and workfilename  */
  613.         /* Second, try to find the right RCS file */
  614.         if (pureRCSname!=RCS1) {
  615.                 /* a path for RCSfile is given; single RCS file to look for */
  616.         errno = 0;
  617.         RCSfilename = p = RCS1;
  618.         finptr = (*rcsopen)(RCSfilename = p = RCS1);
  619.         } else {
  620.         /* no path for RCS file name. Prefix it with path of work */
  621.         /* file if RCS file omitted.  Try RCSDIR subdirectory 1st.*/
  622.         bufalloc(&RCSbuf, strlen(workfilename)+sizeof(rcsdir)+2);
  623.         RCSfilename = p = RCSbuf.string;
  624.         if (RCS1==tempfilename) {
  625.             /* RCS file name not given; prepend work path */
  626.             VOID strncpy(p, arg, purefname-arg);
  627.             p += purefname-arg;
  628.         }
  629.         VOID strcpy(p, rcsdir);
  630.         VOID strcpy(p+sizeof(rcsdir)-1, RCS1);
  631.  
  632.         /* Try D/RCS/file,v. */
  633.         errno = 0;
  634.         if (!(finptr = (*rcsopen)(RCSfilename))
  635.             &&  (errno==ENOTDIR || errno==ENOENT)
  636.             /*
  637.              * Many (broken) systems yield ENOENT, not ENOTDIR,
  638.              * when the problem is a missing RCS subdirectory.
  639.              */
  640.         ) {
  641.             lock1 = frewrite;
  642.  
  643.             /* Try D/file,v. */
  644.             VOID strcpy(p, RCS1);
  645.             errno = 0;
  646.             if (!(finptr=(*rcsopen)(RCSfilename)) && errno==ENOENT) {
  647.                 /*
  648.                  * Neither file exists; determine the default.
  649.                  * Prefer D/RCS/file,v to D/file,v.
  650.                  */
  651.                 if (mustread || lock1) {
  652.                 /* Switch back to D/RCS/file,v. */
  653.                 VOID strcpy(p, rcsdir);
  654.                 VOID strcpy(p+sizeof(rcsdir)-1, RCS1);
  655.                 }
  656.             }
  657.         }
  658.         p = RCSbuf.string;
  659.         }
  660.     if (finptr) {
  661.         if (fstat(fileno(finptr), &RCSstat) < 0)
  662.             efaterror(p);
  663.         if (!S_ISREG(RCSstat.st_mode)) {
  664.             error("%s isn't a regular file -- ignored", p);
  665.                         return 0;
  666.                 }
  667.                 Lexinit(); getadmin();
  668.     } else {
  669.         if (errno!=ENOENT || mustread || !frewrite) {
  670.             if (errno == EEXIST)
  671.                 error("RCS file %s is in use", p);
  672.             else
  673.                 eerror(p);
  674.             return 0;
  675.         }
  676.                 InitAdmin();
  677.         };
  678. #    if LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED
  679.         if (filenametoolong(p)) {
  680.         error("RCS file name %s is too long", p);
  681.         return 0;
  682.         }
  683. #        ifndef NAME_MAX
  684.         /*
  685.          * Check workfilename, even though it is shorter,
  686.          * because it may reside on a different filesystem.
  687.          */
  688.         if (filenametoolong(workfilename)) {
  689.             error("working file name %s is too long", workfilename);
  690.             return 0;
  691.         }
  692. #        endif
  693. #    endif
  694.  
  695.         if (tostdout&&
  696.             !(RCS1==tempfilename||workfilename==tempfilename))
  697.                 /*The last term determines whether a pair of        */
  698.                 /* file names was given in the argument list        */
  699.                 warn("Option -p is set; ignoring output file %s",workfilename);
  700.  
  701.     return finptr ? 1 : -1;
  702. }
  703.  
  704. #else
  705.  
  706. /* USE_AKP_PAIRS */
  707.  
  708. #define HEADSTR "head"
  709. #define HSLEN 4
  710.  
  711. /*
  712.  * pairfilenames: does what the normal code does, but handles 8.3
  713.  * filenames.
  714.  */
  715.  
  716.     int
  717. pairfilenames(argc, argv, rcsopen, mustread, tostdout)
  718.     int argc;
  719.     char **argv;
  720.     FILE *(*rcsopen)P((const char*));
  721.     int mustread, tostdout;
  722. {
  723.     static struct buf RCSbuf, tempbuf;
  724.     char *temp;
  725.     char *temp1;
  726.     int checkedRCS = false;
  727.     int checkedwork = false;
  728.     int retcode = 0;
  729.     FILE *fp;
  730.     struct stat st;
  731.     char tmpbuf[HSLEN+1];
  732.  
  733.     tmpbuf[HSLEN] = '\0';    /* null-terminate the buffer */
  734.  
  735.     if (**argv == '-') {
  736.         error("%s: option is ignored after file names", *argv);
  737.         return 0;
  738.     }
  739.     if (bindex(*argv,KDELIM) != *argv) {
  740.         error("RCS file name `%s' contains %c",*argv,KDELIM);
  741.         return 0;
  742.     }
  743.     if (index(*argv,' ') || index(*argv,'\t') || index(*argv,'\n')) {
  744.         error("RCS file name `%s' contains white space",*argv);
  745.         return 0;
  746.     }
  747.  
  748.     /* try to open the file as given */
  749.     if ((fp = fopen(*argv,"r")) != NULL) {
  750.         /* exists; read the header */
  751.         if (fread(tmpbuf,1,HSLEN,fp) == HSLEN && 
  752.             !strcmp(tmpbuf,HEADSTR)) {
  753.             /* you gave the RCS file name and it exists */
  754.             fclose(fp);
  755.             checkedRCS = true;
  756.             stat(*argv,&RCSstat);
  757.             retcode = 1;
  758.             RCSfilename = *argv;
  759.             temp = bindex(*argv,SLASH);
  760.             bufalloc(&tempbuf,strlen(temp)+1);
  761.             workfilename = tempbuf.string;
  762.             strcpy(workfilename,temp);
  763.  
  764.             /* remove ,v at the end of workfilename */
  765.             if (((temp = bindex(workfilename,RCSSEP)) 
  766.                             != workfilename) &&
  767.                 ((!*temp) || 
  768.                 ((*temp == RCSSUF) && (*(temp+1) == '\0'))))
  769.                 *(--temp) = '\0';
  770.             }
  771.         else {
  772.             /* exists, but isn't an RCS file: must be work file */
  773.             /* RCSfilename is RCS/name.,v or name.,v if no RCS/ */
  774.             fclose(fp);
  775.             checkedwork = true;
  776. justwf:
  777.             workfilename = *argv;
  778.             temp = bindex(*argv,SLASH);
  779.  
  780.             /* magic number 9 below is > max added chars: RCS/.,v */
  781.             if ((stat(RCSDIR,&st) == 0) && (st.st_mode & S_IFDIR)) {
  782.             bufalloc(&RCSbuf,strlen(temp)+9);
  783.             RCSfilename = RCSbuf.string;
  784.             sprintf(RCSfilename,"%s%s%s",RCSDIR,temp,
  785.                 bindex(temp,'.') == temp ? ".,v" : ",v");
  786.             }
  787.             else {
  788.             /* subdir RCS doesn't exist */
  789.             bufalloc(&RCSbuf,strlen(temp)+4);
  790.             RCSfilename = RCSbuf.string;
  791.             sprintf(RCSfilename,"%s%s",temp,
  792.                 bindex(temp,'.') == temp ? ".,v" : ",v");
  793.             }
  794.         }
  795.     }
  796.     else {
  797.         /* file didn't exist */
  798.  
  799.         /* remove ,v after file name */
  800.         if (((temp = bindex(*argv,RCSSEP)) != *argv) &&
  801.         ((!*temp) || 
  802.         ((*temp == RCSSUF) && (*(temp+1) == '\0'))))
  803.             *--temp = '\0';
  804.  
  805.         /* if RCS/name.,v exists and is magic, or name.,v exists and */
  806.         /* is magic, that file is RCSfilename and you gave work.     */
  807.         temp = bindex(temp,SLASH);
  808.         bufalloc(&tempbuf,strlen(temp)+9);
  809.         temp1 = tempbuf.string;
  810.         sprintf(temp1,"%s%s%s",RCSDIR,temp,
  811.         bindex(temp,'.') == temp ? ".,v" : ",v");
  812.         if (stat(temp1,&st) == 0) {
  813.         /* RCS/file.,v exists -- fine. */
  814.         RCSfilename = temp1;
  815.         RCSstat = st;
  816.         workfilename = *argv;
  817.         }
  818.         else {
  819.         /* doesn't exist in RCS subdir */
  820.         sprintf(temp1,"%s%s",temp,
  821.             bindex(temp,'.') == temp ? ".,v" : ",v");
  822.         if (stat(temp1,&st) == 0) {
  823.             /* ./file.,v exists -- fine. */
  824.             RCSfilename = temp1;
  825.             workfilename = *argv;
  826.         }
  827.         else {
  828.             /* file doesn't exist anywhere! */
  829.             /* same case as above */
  830.             goto justwf;
  831.         }
  832.         }
  833.     }
  834.  
  835.     /* at this point, RCSfilename and workfilename are set.        */
  836.     /* if !checkedwork, then verify workfile, ditto checkedRCS. */
  837.  
  838.     if (!checkedwork) {
  839.         if (((fp = fopen(workfilename,"r")) != NULL) &&
  840.         (fread(tmpbuf,1,HSLEN,fp) == HSLEN) &&
  841.         (!strcmp(tmpbuf,HEADSTR))) {
  842.             fclose(fp);
  843.             error("Computed work file name %s is actually an RCS file",
  844.             workfilename);
  845.             return 0;
  846.         }
  847.         else {
  848.         fclose(fp);
  849.         }
  850.     }
  851.     if (!checkedRCS) {
  852.         if ((fp = fopen(RCSfilename,"r")) != NULL) {
  853.         if ((fread(tmpbuf,1,HSLEN,fp) == HSLEN) &&
  854.             (!strcmp(tmpbuf,HEADSTR))) {
  855.             stat(RCSfilename,&RCSstat);
  856.             fclose(fp);
  857.             finptr = (*rcsopen)(RCSfilename);
  858.             Lexinit();
  859.             getadmin();
  860.             return 1;
  861.         }
  862.         else {
  863.             fclose(fp);
  864.             error("Computed RCS file name %s is not an RCS file",
  865.             RCSfilename);
  866.             return 0;
  867.         }
  868.         }
  869.         else {
  870.         /* RCS file doesn't exist */
  871.         if (mustread) {
  872.             error("Can't find computed RCS file %s",RCSfilename);
  873.             return 0;
  874.         }
  875.         else {
  876.             finptr = (*rcsopen)(RCSfilename);
  877.             InitAdmin();
  878.             return -1;
  879.         }
  880.         }
  881.     }
  882.     else {
  883.         /* RCS file was checked, so we know it exists */
  884.         finptr = (*rcsopen)(RCSfilename);
  885.         Lexinit();
  886.         getadmin();
  887.         return 1;
  888.     }
  889. }
  890.  
  891. /* end if USE_AKP_PAIRS */
  892. #endif
  893.  
  894.  
  895.     const char *
  896. getfullRCSname()
  897. /* Function: returns a pointer to the full path name of the RCS file.
  898.  * Gets the working directory's name at most once.
  899.  * Removes leading "../" and "./".
  900.  */
  901. {
  902.     static const char *wd;
  903.     static struct buf rcsbuf, wdbuf;
  904.     static size_t pathlength;
  905.  
  906.     register const char *realname;
  907.     register size_t parentdirlength;
  908.     register unsigned dotdotcounter;
  909.     register char *d;
  910.  
  911.     if (ROOTPATH(RCSfilename)) {
  912.                 return(RCSfilename);
  913.         } else {
  914.         if (!wd) { /* Get working directory for the first time. */
  915.             if (!(d = cgetenv("PWD"))) {
  916.             bufalloc(&wdbuf, 1 +
  917. #                ifdef PATH_MAX
  918.                 PATH_MAX
  919. #                else
  920.                 _POSIX_PATH_MAX
  921. #                endif
  922.             );
  923.             errno = 0;
  924. #            if !has_getcwd
  925.                 d = getwd(wdbuf.string);
  926. #            else
  927.                 while (
  928.                     !(d = getcwd(wdbuf.string,(int)wdbuf.size))
  929.                 &&  errno==ERANGE
  930.                 )
  931.                 bufalloc(&wdbuf, wdbuf.size<<1);
  932. #            endif
  933.             if (!d)
  934.                 efaterror("working directory");
  935.             }
  936.             pathlength = strlen(d);
  937.             while (pathlength && d[pathlength-1]==SLASH) {
  938.             d[--pathlength] = 0;
  939.                         /* Check needed because some getwd implementations */
  940.                         /* generate "/" for the root.                      */
  941.                     }
  942.             wd = d;
  943.                 }
  944.                 /*the following must be redone since RCSfilename may change*/
  945.         /* Find how many `../'s to remove from RCSfilename.  */
  946.                 dotdotcounter =0;
  947.                 realname = RCSfilename;
  948.                 while( realname[0]=='.' &&
  949.                       (realname[1]==SLASH||(realname[1]=='.'&&realname[2]==SLASH))){
  950.                         if (realname[1]==SLASH) {
  951.                             /* drop leading ./ */
  952.                             realname += 2;
  953.                         } else {
  954.                             /* drop leading ../ and remember */
  955.                             dotdotcounter++;
  956.                             realname += 3;
  957.                         }
  958.                 }
  959.         /* Now remove dotdotcounter trailing directories from wd. */
  960.         parentdirlength = pathlength;
  961.         while (dotdotcounter && parentdirlength) {
  962.                     /* move pointer backwards over trailing directory */
  963.             if (wd[--parentdirlength] == SLASH) {
  964.                         dotdotcounter--;
  965.                     }
  966.                 }
  967.         if (dotdotcounter) {
  968.                     error("can't generate full path name for RCS file");
  969.                     return RCSfilename;
  970.                 } else {
  971.                     /* build full path name */
  972.             bufalloc(&rcsbuf, parentdirlength+strlen(realname)+2);
  973.             VOID strncpy(rcsbuf.string, wd, parentdirlength);
  974.             rcsbuf.string[parentdirlength++] = SLASH;
  975.             VOID strcpy(rcsbuf.string+parentdirlength, realname);
  976.             return rcsbuf.string;
  977.         }
  978.         }
  979. }
  980.  
  981.     const char *
  982. tmp()
  983. /* Yield the name of the tmp directory, with a trailing SLASH.  */
  984. {
  985.     static const char *s;
  986.     if (!s)
  987.         if (!(s = getenv("TMP")))
  988.             s = TMPDIR;
  989.         else {
  990.             size_t l = strlen(s);
  991.             int extra = l && s[l-1]!=SLASH;
  992.             char *p = ftnalloc(char, l + extra + 1);
  993.             VOID strcpy(p, s);
  994.             if (extra) {
  995.                 p[l] = SLASH;
  996.                 p[l+1] = 0;
  997.             }
  998.             s = p;
  999.         }
  1000.     return s;
  1001. }
  1002.  
  1003.  
  1004. #if bad_unlink
  1005. /*
  1006.  * bad_unlink means unlink(A) fails if A is read-only.
  1007.  * unlink has been #defined to un_link.
  1008.  */
  1009.  
  1010. #undef unlink
  1011.     int
  1012. un_link(file)
  1013. const char *file;
  1014. {
  1015.     int e;
  1016.     /* first, try unlink outright */
  1017.     if (!unlink(file)) return 0;
  1018.  
  1019.     /* try chmod a+rwx - fail if can't even do that */
  1020.     if (e = chmod(file,0777)) return e;
  1021.     return unlink(file);
  1022. }
  1023. /* re-define unlink to un_link for remainder of this file */
  1024. #define unlink un_link
  1025. #endif
  1026.  
  1027. #if !has_rename | bad_rename
  1028.  
  1029.     int
  1030. re_name(from, to)
  1031.     const char *from, *to;
  1032. /* Function: renames a file with the name given by from to the name given by to.
  1033.  * unlinks the to-file if it already exists. returns -1 on error, 0 otherwise.
  1034.  */
  1035. {       VOID unlink(to);      /* no need to check return code; will be caught by link*/
  1036.                          /* no harm done if file "to" does not exist            */
  1037. #if terrible_rename
  1038.     /* terrible_rename is worse than bad_rename: you have */
  1039.     /* to make "from" writable before you can rename it.  */
  1040.     {
  1041.         struct stat st;
  1042.         int e;
  1043.         if (stat(from,&st)) return -1;    /* error: can't stat 'from' */
  1044.         if (chmod(from,0777)) return -1; /* can't chmod 'from' */
  1045. #if has_rename
  1046.         e = rename(from,to);
  1047. #else
  1048.         e = (link(from,to) || unlink(from));
  1049. #endif
  1050.         /* if this fails, it'll look like the rename failed! */
  1051.         if (chmod(to,st.st_mode)) return -1;
  1052.         return e;
  1053.     }
  1054. #else
  1055. /* !terrible_rename */
  1056. #if has_rename
  1057.     return rename(from,to);
  1058. #else
  1059.         if (link(from,to)<0) return -1;
  1060.         return(unlink(from));
  1061. #endif
  1062. #endif
  1063. }
  1064.  
  1065. #endif
  1066.  
  1067.  
  1068. #if !has_getcwd & !has_getwd
  1069.  
  1070. #if !MAKEDEPEND
  1071. #include <sys/dir.h>
  1072. #endif
  1073.  
  1074.  
  1075. #define dot     "."
  1076. #define dotdot  ".."
  1077.  
  1078.  
  1079.  
  1080. char * getwd(name)
  1081. char * name;
  1082. /* Function: places full pathname of current working directory into name and
  1083.  * returns name on success, NULL on failure.
  1084.  * getwd is an adaptation of pwd. May not return to the current directory on
  1085.  * failure.
  1086.  */
  1087. {
  1088.         FILE    *file;
  1089.         struct  stat    d, dd;
  1090.         char buf[2];    /* to NUL-terminate dir.d_name */
  1091.         struct  direct  dir;
  1092.  
  1093.         int rdev, rino;
  1094.         int off;
  1095.         register i,j;
  1096.  
  1097.     off = 0;
  1098.     name[0] = SLASH;
  1099.         name[1] = '\0';
  1100.         buf[0] = '\0';
  1101.     if (stat(name, &d)<0) return NULL;
  1102.         rdev = d.st_dev;
  1103.         rino = d.st_ino;
  1104.         for (;;) {
  1105.                 if (stat(dot, &d)<0) return NULL;
  1106.                 if (d.st_ino==rino && d.st_dev==rdev) {
  1107.             if (name[off] == SLASH)
  1108.                 name[off] = '\0';
  1109.             VOID chdir(name); /*change back to current directory*/
  1110.                         return name;
  1111.                 }
  1112.                 if ((file = fopen(dotdot,"r")) == NULL) return NULL;
  1113.                 if (fstat(fileno(file), &dd)<0) goto fail;
  1114.         VOID chdir(dotdot);
  1115.                 if(d.st_dev == dd.st_dev) {
  1116.                         if(d.st_ino == dd.st_ino) {
  1117.                 if (name[off] == SLASH)
  1118.                 name[off] = 0;
  1119.                 VOID chdir(name); /*change back to current directory*/
  1120.                 ffclose(file);
  1121.                             return name;
  1122.                         }
  1123.                         do {
  1124.                             if (fread((char *)&dir, sizeof(dir), 1, file) !=1)
  1125.                                 goto fail;
  1126.                         } while (dir.d_ino != d.st_ino);
  1127.                 }
  1128.                 else do {
  1129.                         if(fread((char *)&dir, sizeof(dir), 1, file) != 1) {
  1130.                             goto fail;
  1131.                         }
  1132.                         if (dir.d_ino == 0)
  1133.                 dd.st_ino = d.st_ino + 1;
  1134.                         else if (stat(dir.d_name, &dd) < 0)
  1135.                 goto fail;
  1136.                 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  1137.         ffclose(file);
  1138.  
  1139.                 /* concatenate file name */
  1140.                 i = -1;
  1141.                 while (dir.d_name[++i] != 0);
  1142.                 for(j=off+1; j>0; --j)
  1143.                         name[j+i+1] = name[j];
  1144.                 off=i+off+1;
  1145.         name[i+1] = SLASH;
  1146.                 for(--i; i>=0; --i)
  1147.                         name[i+1] = dir.d_name[i];
  1148.         } /* end for */
  1149.  
  1150. fail:   ffclose(file);
  1151.         return NULL;
  1152. }
  1153.  
  1154.  
  1155. #endif
  1156.  
  1157.  
  1158. #ifdef PAIRTEST
  1159. /* test program for pairfilenames() and getfullRCSname() */
  1160.  
  1161. #if AKP_BUGFIXES
  1162. /* AKP: this function was missing when PAIRTEST is 1 */
  1163. exiting void 
  1164. exiterr()
  1165. {
  1166.     dirtempunlink();
  1167.     tempunlink();
  1168.     _exit(EXIT_FAILURE);
  1169. }
  1170. #endif
  1171.  
  1172. const char cmdid[] = "pair";
  1173.  
  1174. main(argc, argv)
  1175. int argc; char *argv[];
  1176. {
  1177.         int result;
  1178.         int initflag,tostdout;
  1179.         quietflag=tostdout=initflag=false;
  1180.  
  1181. #if AKP_BUGFIXES
  1182.     /* AKP: this was missing so errors didn't get printed */
  1183.     frewrite = stdout;
  1184. #endif
  1185.  
  1186.         while(--argc, ++argv, argc>=1 && ((*argv)[0] == '-')) {
  1187.                 switch ((*argv)[1]) {
  1188.  
  1189.                 case 'p':       tostdout=true;
  1190.                                 break;
  1191.                 case 'i':       initflag=true;
  1192.                                 break;
  1193.                 case 'q':       quietflag=true;
  1194.                                 break;
  1195.                 default:        error("unknown option: %s", *argv);
  1196.                                 break;
  1197.                 }
  1198.         }
  1199.  
  1200.         do {
  1201.                 RCSfilename=workfilename=nil;
  1202.         result = pairfilenames(argc,argv,rcsreadopen,!initflag,tostdout);
  1203.                 if (result!=0) {
  1204.             diagnose("RCS file: %s; working file: %s\nFull RCS file name: %s\n",
  1205.                  RCSfilename,workfilename,getfullRCSname()
  1206.             );
  1207.                 }
  1208.                 switch (result) {
  1209.                         case 0: continue; /* already paired file */
  1210.  
  1211.                         case 1: if (initflag) {
  1212.                                     error("RCS file %s exists already",RCSfilename);
  1213.                                 } else {
  1214.                     diagnose("RCS file %s exists\n",RCSfilename);
  1215.                                 }
  1216.                 ffclose(finptr);
  1217.                                 break;
  1218.  
  1219.             case -1:diagnose("RCS file doesn't exist\n");
  1220.                                 break;
  1221.                 }
  1222.  
  1223.         } while (++argv, --argc>=1);
  1224.  
  1225. }
  1226. #endif
  1227.