home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / ntcode / gr564s / src / rcsfnms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  27.5 KB  |  1,113 lines

  1. /*
  2.  *            RCS pathname handling
  3.  */
  4. /****************************************************************************
  5.  *                     creation and deletion of /tmp temporaries
  6.  *               pairing of RCS pathnames and working pathnames.
  7.  *                     Testprogram: define PAIRTEST
  8.  ****************************************************************************
  9.  */
  10.  
  11. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  12.    Copyright 1990, 1991, 1992 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 2, 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.12  1992/07/28  16:12:44  eggert
  42.  * Add .sty.  .pl now implies Perl, not Prolog.  Fix fdlock initialization bug.
  43.  * Check that $PWD is really ".".  Be consistent about pathnames vs filenames.
  44.  *
  45.  * Revision 5.11  1992/02/17  23:02:25  eggert
  46.  * `a/RCS/b/c' is now an RCS file with an empty extension, not just `a/b/RCS/c'.
  47.  *
  48.  * Revision 5.10  1992/01/24  18:44:19  eggert
  49.  * Fix bug: Expand and Ignored weren't reinitialized.
  50.  * Avoid `char const c=ch;' compiler bug.
  51.  * Add support for bad_creat0.
  52.  *
  53.  * Revision 5.9  1992/01/06  02:42:34  eggert
  54.  * Shorten long (>31 chars) name.
  55.  * while (E) ; -> while (E) continue;
  56.  *
  57.  * Revision 5.8  1991/09/24  00:28:40  eggert
  58.  * Don't export bindex().
  59.  *
  60.  * Revision 5.7  1991/08/19  03:13:55  eggert
  61.  * Fix messages when rcswriteopen fails.
  62.  * Look in $TMP and $TEMP if $TMPDIR isn't set.  Tune.
  63.  *
  64.  * Revision 5.6  1991/04/21  11:58:23  eggert
  65.  * Fix errno bugs.  Add -x, RCSINIT, MS-DOS support.
  66.  *
  67.  * Revision 5.5  1991/02/26  17:48:38  eggert
  68.  * Fix setuid bug.  Support new link behavior.
  69.  * Define more portable getcwd().
  70.  *
  71.  * Revision 5.4  1990/11/01  05:03:43  eggert
  72.  * Permit arbitrary data in comment leaders.
  73.  *
  74.  * Revision 5.3  1990/09/14  22:56:16  hammer
  75.  * added more filename extensions and their comment leaders
  76.  *
  77.  * Revision 5.2  1990/09/04  08:02:23  eggert
  78.  * Fix typo when !RCSSEP.
  79.  *
  80.  * Revision 5.1  1990/08/29  07:13:59  eggert
  81.  * Work around buggy compilers with defective argument promotion.
  82.  *
  83.  * Revision 5.0  1990/08/22  08:12:50  eggert
  84.  * Ignore signals when manipulating the semaphore file.
  85.  * Modernize list of filename extensions.
  86.  * Permit paths of arbitrary length.  Beware filenames beginning with "-".
  87.  * Remove compile-time limits; use malloc instead.
  88.  * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  89.  * Ansify and Posixate.
  90.  * Don't use access().  Fix test for non-regular files.  Tune.
  91.  *
  92.  * Revision 4.8  89/05/01  15:09:41  narten
  93.  * changed getwd to not stat empty directories.
  94.  * 
  95.  * Revision 4.7  88/08/09  19:12:53  eggert
  96.  * Fix troff macro comment leader bug; add Prolog; allow cc -R; remove lint.
  97.  * 
  98.  * Revision 4.6  87/12/18  11:40:23  narten
  99.  * additional file types added from 4.3 BSD version, and SPARC assembler
  100.  * comment character added. Also, more lint cleanups. (Guy Harris)
  101.  * 
  102.  * Revision 4.5  87/10/18  10:34:16  narten
  103.  * Updating version numbers. Changes relative to 1.1 actually relative
  104.  * to verion 4.3
  105.  * 
  106.  * Revision 1.3  87/03/27  14:22:21  jenkins
  107.  * Port to suns
  108.  * 
  109.  * Revision 1.2  85/06/26  07:34:28  svb
  110.  * Comment leader '% ' for '*.tex' files added.
  111.  * 
  112.  * Revision 4.3  83/12/15  12:26:48  wft
  113.  * Added check for KDELIM in filenames to pairfilenames().
  114.  * 
  115.  * Revision 4.2  83/12/02  22:47:45  wft
  116.  * Added csh, red, and sl filename suffixes.
  117.  * 
  118.  * Revision 4.1  83/05/11  16:23:39  wft
  119.  * Added initialization of Dbranch to InitAdmin(). Canged pairfilenames():
  120.  * 1. added copying of path from workfile to RCS file, if RCS file is omitted;
  121.  * 2. added getting the file status of RCS and working files;
  122.  * 3. added ignoring of directories.
  123.  * 
  124.  * Revision 3.7  83/05/11  15:01:58  wft
  125.  * Added comtable[] which pairs filename suffixes with comment leaders;
  126.  * updated InitAdmin() accordingly.
  127.  * 
  128.  * Revision 3.6  83/04/05  14:47:36  wft
  129.  * fixed Suffix in InitAdmin().
  130.  * 
  131.  * Revision 3.5  83/01/17  18:01:04  wft
  132.  * Added getwd() and rename(); these can be removed by defining
  133.  * V4_2BSD, since they are not needed in 4.2 bsd.
  134.  * Changed sys/param.h to sys/types.h.
  135.  *
  136.  * Revision 3.4  82/12/08  21:55:20  wft
  137.  * removed unused variable.
  138.  *
  139.  * Revision 3.3  82/11/28  20:31:37  wft
  140.  * Changed mktempfile() to store the generated filenames.
  141.  * Changed getfullRCSname() to store the file and pathname, and to
  142.  * delete leading "../" and "./".
  143.  *
  144.  * Revision 3.2  82/11/12  14:29:40  wft
  145.  * changed pairfilenames() to handle file.sfx,v; also deleted checkpathnosfx(),
  146.  * checksuffix(), checkfullpath(). Semaphore name generation updated.
  147.  * mktempfile() now checks for nil path; freefilename initialized properly.
  148.  * Added Suffix .h to InitAdmin. Added testprogram PAIRTEST.
  149.  * Moved rmsema, trysema, trydiraccess, getfullRCSname from rcsutil.c to here.
  150.  *
  151.  * Revision 3.1  82/10/18  14:51:28  wft
  152.  * InitAdmin() now initializes StrictLocks=STRICT_LOCKING (def. in rcsbase.h).
  153.  * renamed checkpath() to checkfullpath().
  154.  */
  155.  
  156.  
  157. #include "rcsbase.h"
  158.  
  159. libId(fnmsId, "$Id: rcsfnms.c,v 5.12 1992/07/28 16:12:44 eggert Exp $")
  160.  
  161. char const *RCSname;
  162. char *workname;
  163. int fdlock;
  164. FILE *workstdout;
  165. struct stat RCSstat;
  166. char const *suffixes;
  167.  
  168. static char const rcsdir[] = "RCS";
  169. #define rcslen (sizeof(rcsdir)-1)
  170.  
  171. static struct buf RCSbuf, RCSb;
  172. static int RCSerrno;
  173.  
  174.  
  175. /* Temp names to be unlinked when done, if they are not nil.  */
  176. #define TEMPNAMES 5 /* must be at least DIRTEMPNAMES (see rcsedit.c) */
  177. static char *volatile tpnames[TEMPNAMES];
  178.  
  179.  
  180. struct compair {
  181.     char const *suffix, *comlead;
  182. };
  183.  
  184. static struct compair const comtable[] = {
  185. /* comtable pairs each pathname suffix with a comment leader. The comment   */
  186. /* leader is placed before each line generated by the $Log keyword. This    */
  187. /* table is used to guess the proper comment leader from the working file's */
  188. /* suffix during initial ci (see InitAdmin()). Comment leaders are needed   */
  189. /* for languages without multiline comments; for others they are optional.  */
  190.     "a",   "-- ",   /* Ada         */
  191.     "ada", "-- ",
  192.     "asm", ";; ",    /* assembler (MS-DOS) */
  193.     "bat", ":: ",    /* batch (MS-DOS) */
  194.         "c",   " * ",   /* C           */
  195.     "c++", "// ",    /* C++ in all its infinite guises */
  196.     "cc",  "// ",
  197.     "cpp", "// ",
  198.     "cxx", "// ",
  199.     "cl",  ";;; ",  /* Common Lisp */
  200.     "cmd", ":: ",    /* command (OS/2) */
  201.     "cmf", "c ",    /* CM Fortran  */
  202.     "cs",  " * ",    /* C*          */
  203.     "el",  "; ",    /* Emacs Lisp  */
  204.     "f",   "c ",    /* Fortran     */
  205.     "for", "c ",
  206.         "h",   " * ",   /* C-header    */
  207.     "hpp", "// ",    /* C++ header  */
  208.     "hxx", "// ",
  209.         "l",   " * ",   /* lex      NOTE: conflict between lex and franzlisp */
  210.     "lisp",";;; ",    /* Lucid Lisp  */
  211.     "lsp", ";; ",    /* Microsoft Lisp */
  212.     "mac", ";; ",    /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
  213.     "me",  ".\\\" ",/* me-macros   t/nroff*/
  214.     "ml",  "; ",    /* mocklisp    */
  215.     "mm",  ".\\\" ",/* mm-macros   t/nroff*/
  216.     "ms",  ".\\\" ",/* ms-macros   t/nroff*/
  217.     "p",   " * ",   /* Pascal      */
  218.     "pas", " * ",
  219.     "ps",  "% ",    /* PostScript  */
  220.     "sty", "% ",    /* LaTeX style */
  221.     "tex", "% ",    /* TeX           */
  222.         "y",   " * ",   /* yacc        */
  223.     nil,   "# "     /* default for unknown suffix; must always be last */
  224. };
  225.  
  226. #if has_mktemp
  227.     static char const *
  228. tmp()
  229. /* Yield the name of the tmp directory.  */
  230. {
  231.     static char const *s;
  232.     if (!s
  233.         &&  !(s = cgetenv("TMPDIR"))    /* Unix tradition */
  234.         &&  !(s = cgetenv("TMP"))    /* DOS tradition */
  235.         &&  !(s = cgetenv("TEMP"))    /* another DOS tradition */
  236.     )
  237.         s = TMPDIR;
  238.     return s;
  239. }
  240. #endif
  241.  
  242.     char const *
  243. maketemp(n)
  244.     int n;
  245. /* Create a unique pathname using n and the process id and store it
  246.  * into the nth slot in tpnames.
  247.  * Because of storage in tpnames, tempunlink() can unlink the file later.
  248.  * Return a pointer to the pathname created.
  249.  */
  250. {
  251.     char *p;
  252.     char const *t = tpnames[n];
  253.  
  254.     if (t)
  255.         return t;
  256.  
  257.     catchints();
  258.     {
  259. #    if has_mktemp
  260.         char const *tp = tmp();
  261.         p = testalloc(strlen(tp) + 10);
  262.         VOID sprintf(p, "%s%cT%cXXXXXX", tp, SLASH, '0'+n);
  263.         if (!mktemp(p) || !*p)
  264.         faterror("can't make temporary pathname `%s%cT%cXXXXXX'",
  265.             tp, SLASH, '0'+n
  266.         );
  267. #    else
  268.         static char tpnamebuf[TEMPNAMES][L_tmpnam];
  269.         p = tpnamebuf[n];
  270.         if (!tmpnam(p) || !*p)
  271. #        ifdef P_tmpdir
  272.             faterror("can't make temporary pathname `%s...'",P_tmpdir);
  273. #        else
  274.             faterror("can't make temporary pathname");
  275. #        endif
  276. #    endif
  277.     }
  278.  
  279.     tpnames[n] = p;
  280.     return p;
  281. }
  282.  
  283.     void
  284. tempunlink()
  285. /* Clean up maketemp() files.  May be invoked by signal handler.
  286.  */
  287. {
  288.     register int i;
  289.     register char *p;
  290.  
  291.     for (i = TEMPNAMES;  0 <= --i;  )
  292.         if ((p = tpnames[i])) {
  293.         VOID unlink(p);
  294.         /*
  295.          * We would tfree(p) here,
  296.          * but this might dump core if we're handing a signal.
  297.          * We're about to exit anyway, so we won't bother.
  298.          */
  299.         tpnames[i] = 0;
  300.         }
  301. }
  302.  
  303.  
  304.     static char const *
  305. bindex(sp, c)
  306.     register char const *sp;
  307.     register int c;
  308. /* Function: Finds the last occurrence of character c in string sp
  309.  * and returns a pointer to the character just beyond it. If the
  310.  * character doesn't occur in the string, sp is returned.
  311.  */
  312. {
  313.     register char const *r;
  314.         r = sp;
  315.         while (*sp) {
  316.                 if (*sp++ == c) r=sp;
  317.         }
  318.         return r;
  319. }
  320.  
  321.  
  322.  
  323.     static int
  324. suffix_matches(suffix, pattern)
  325.     register char const *suffix, *pattern;
  326. {
  327.     register int c;
  328.     if (!pattern)
  329.         return true;
  330.     for (;;)
  331.         switch (*suffix++ - (c = *pattern++)) {
  332.             case 0:
  333.             if (!c)
  334.                 return true;
  335.             break;
  336.  
  337.             case 'A'-'a':
  338.             if (ctab[c] == Letter)
  339.                 break;
  340.             /* fall into */
  341.             default:
  342.             return false;
  343.         }
  344. }
  345.  
  346.  
  347.     static void
  348. InitAdmin()
  349. /* function: initializes an admin node */
  350. {
  351.     register char const *Suffix;
  352.         register int i;
  353.  
  354.     Head=nil; Dbranch=nil; AccessList=nil; Symbols=nil; Locks=nil;
  355.         StrictLocks=STRICT_LOCKING;
  356.  
  357.         /* guess the comment leader from the suffix*/
  358.     Suffix = bindex(workname, '.');
  359.     if (Suffix==workname) Suffix= ""; /* empty suffix; will get default*/
  360.     for (i=0; !suffix_matches(Suffix,comtable[i].suffix); i++)
  361.         continue;
  362.     Comment.string = comtable[i].comlead;
  363.     Comment.size = strlen(comtable[i].comlead);
  364.     Expand = KEYVAL_EXPAND;
  365.     Ignored.string = 0;
  366.     Ignored.size = 0;
  367.     Lexinit(); /* note: if !finptr, reads nothing; only initializes */
  368. }
  369.  
  370.  
  371. #if defined(_POSIX_NO_TRUNC) && _POSIX_NO_TRUNC!=-1
  372. #    define LONG_NAMES_MAY_BE_TRUNCATED 0
  373. #else
  374. #    define LONG_NAMES_MAY_BE_TRUNCATED 1
  375. #endif
  376.  
  377. #if LONG_NAMES_MAY_BE_TRUNCATED
  378. /* Long filenames may be silently truncated, so we first check their length.  */
  379. #ifdef NAME_MAX
  380. #    define filenametoolong(path) (NAME_MAX < strlen(basename(path)))
  381. #else
  382.     static int
  383. filenametoolong(path)
  384.     char *path;
  385. /* Yield true if the last filename in PATH is too long. */
  386. {
  387.     static long dot_namemax;
  388.  
  389.     register size_t namelen;
  390.     register char *base;
  391.     register unsigned long namemax;
  392.  
  393.     base = path + dirlen(path);
  394.     namelen = strlen(base);
  395.     if (namelen <= _POSIX_NAME_MAX) /* fast check for shorties */
  396.         return false;
  397.     if (base != path) {
  398.         char slash = *--base;
  399.         *base = 0;
  400.         namemax = pathconf(path, _PC_NAME_MAX);
  401.         *base = slash;
  402.     } else {
  403.         /* Cache the results for the working directory, for speed. */
  404.         if (!dot_namemax)
  405.             dot_namemax = pathconf(".", _PC_NAME_MAX);
  406.         namemax = dot_namemax;
  407.     }
  408.     /* If pathconf() yielded -1, namemax is now ULONG_MAX.  */
  409.     return namemax<namelen;
  410. }
  411. #endif
  412. #endif
  413.  
  414.     void
  415. bufalloc(b, size)
  416.     register struct buf *b;
  417.     size_t size;
  418. /* Ensure *B is a name buffer of at least SIZE bytes.
  419.  * *B's old contents can be freed; *B's new contents are undefined.
  420.  */
  421. {
  422.     if (b->size < size) {
  423.         if (b->size)
  424.             tfree(b->string);
  425.         else
  426.             b->size = sizeof(malloc_type);
  427.         while (b->size < size)
  428.             b->size <<= 1;
  429.         b->string = tnalloc(char, b->size);
  430.     }
  431. }
  432.  
  433.     void
  434. bufrealloc(b, size)
  435.     register struct buf *b;
  436.     size_t size;
  437. /* like bufalloc, except *B's old contents, if any, are preserved */
  438. {
  439.     if (b->size < size) {
  440.         if (!b->size)
  441.             bufalloc(b, size);
  442.         else {
  443.             while ((b->size <<= 1)  <  size)
  444.                 continue;
  445.             b->string = trealloc(char, b->string, b->size);
  446.         }
  447.     }
  448. }
  449.  
  450.     void
  451. bufautoend(b)
  452.     struct buf *b;
  453. /* Free an auto buffer at block exit. */
  454. {
  455.     if (b->size)
  456.         tfree(b->string);
  457. }
  458.  
  459.     struct cbuf
  460. bufremember(b, s)
  461.     struct buf *b;
  462.     size_t s;
  463. /*
  464.  * Free the buffer B with used size S.
  465.  * Yield a cbuf with identical contents.
  466.  * The cbuf will be reclaimed when this input file is finished.
  467.  */
  468. {
  469.     struct cbuf cb;
  470.  
  471.     if ((cb.size = s))
  472.         cb.string = fremember(trealloc(char, b->string, s));
  473.     else {
  474.         bufautoend(b); /* not really auto */
  475.         cb.string = "";
  476.     }
  477.     return cb;
  478. }
  479.  
  480.     char *
  481. bufenlarge(b, alim)
  482.     register struct buf *b;
  483.     char const **alim;
  484. /* Make *B larger.  Set *ALIM to its new limit, and yield the relocated value
  485.  * of its old limit.
  486.  */
  487. {
  488.     size_t s = b->size;
  489.     bufrealloc(b, s + 1);
  490.     *alim = b->string + b->size;
  491.     return b->string + s;
  492. }
  493.  
  494.     void
  495. bufscat(b, s)
  496.     struct buf *b;
  497.     char const *s;
  498. /* Concatenate S to B's end. */
  499. {
  500.     size_t blen  =  b->string ? strlen(b->string) : 0;
  501.     bufrealloc(b, blen+strlen(s)+1);
  502.     VOID strcpy(b->string+blen, s);
  503. }
  504.  
  505.     void
  506. bufscpy(b, s)
  507.     struct buf *b;
  508.     char const *s;
  509. /* Copy S into B. */
  510. {
  511.     bufalloc(b, strlen(s)+1);
  512.     VOID strcpy(b->string, s);
  513. }
  514.  
  515.  
  516.     char const *
  517. basename(p)
  518.     char const *p;
  519. /* Yield the address of the base filename of the pathname P.  */
  520. {
  521.     register char const *b = p, *q = p;
  522.     for (;;)
  523.         switch (*q++) {
  524.         case SLASHes: b = q; break;
  525.         case 0: return b;
  526.         }
  527. }
  528.  
  529.     size_t
  530. dirlen(p)
  531.     char const *p;
  532. /* Yield the length of P's directory, including its trailing slash.  */
  533. {
  534.     return basename(p) - p;
  535. }
  536.  
  537.  
  538.     static size_t
  539. suffixlen(x)
  540.     char const *x;
  541. /* Yield the length of X, an RCS pathname suffix.  */
  542. {
  543.     register char const *p;
  544.  
  545.     p = x;
  546.     for (;;)
  547.         switch (*p) {
  548.         case 0: case SLASHes:
  549.             return p - x;
  550.  
  551.         default:
  552.             ++p;
  553.             continue;
  554.         }
  555. }
  556.  
  557.     char const *
  558. rcssuffix(name)
  559.     char const *name;
  560. /* Yield the suffix of NAME if it is an RCS pathname, 0 otherwise.  */
  561. {
  562.     char const *x, *p, *nz;
  563.     int d;
  564.     size_t nl, xl;
  565.  
  566.     nl = strlen(name);
  567.     nz = name + nl;
  568.     x = suffixes;
  569.     do {
  570.         if ((xl = suffixlen(x))) {
  571.         if (xl <= nl  &&  memcmp(p = nz-xl, x, xl) == 0)
  572.             return p;
  573.         } else
  574.         for (p = name, d = true;  p < nz - rcslen;  d = isSLASH(*p++))
  575.             if (d && memcmp(p,rcsdir,rcslen)==0 && isSLASH(p[rcslen]))
  576.             return nz;
  577.         x += xl;
  578.     } while (*x++);
  579.     return 0;
  580. }
  581.  
  582.     /*ARGSUSED*/ RILE *
  583. rcsreadopen(RCSpath, status, mustread)
  584.     struct buf *RCSpath;
  585.     struct stat *status;
  586.     int mustread;
  587. /* Open RCSPATH for reading and yield its FILE* descriptor.
  588.  * If successful, set *STATUS to its status.
  589.  * Pass this routine to pairnames() for read-only access to the file.  */
  590. {
  591.     return Iopen(RCSpath->string, FOPEN_R, status);
  592. }
  593.  
  594.     static int
  595. finopen(rcsopen, mustread)
  596.     RILE *(*rcsopen)P((struct buf*,struct stat*,int));
  597.     int mustread;
  598. /*
  599.  * Use RCSOPEN to open an RCS file; MUSTREAD is set if the file must be read.
  600.  * Set finptr to the result and yield true if successful.
  601.  * RCSb holds the file's name.
  602.  * Set RCSbuf to the best RCS name found so far, and RCSerrno to its errno.
  603.  * Yield true if successful or if an unusual failure.
  604.  */
  605. {
  606.     int interesting, preferold;
  607.  
  608.     /*
  609.      * We prefer an old name to that of a nonexisting new RCS file,
  610.      * unless we tried locking the old name and failed.
  611.      */
  612.     preferold  =  RCSbuf.string[0] && (mustread||0<=fdlock);
  613.  
  614.     finptr = (*rcsopen)(&RCSb, &RCSstat, mustread);
  615.     interesting = finptr || errno!=ENOENT;
  616.     if (interesting || !preferold) {
  617.         /* Use the new name.  */
  618.         RCSerrno = errno;
  619.         bufscpy(&RCSbuf, RCSb.string);
  620.     }
  621.     return interesting;
  622. }
  623.  
  624.     static int
  625. fin2open(d, dlen, base, baselen, x, xlen, rcsopen, mustread)
  626.     char const *d, *base, *x;
  627.     size_t dlen, baselen, xlen;
  628.     RILE *(*rcsopen)P((struct buf*,struct stat*,int));
  629.     int mustread;
  630. /*
  631.  * D is a directory name with length DLEN (including trailing slash).
  632.  * BASE is a filename with length BASELEN.
  633.  * X is an RCS pathname suffix with length XLEN.
  634.  * Use RCSOPEN to open an RCS file; MUSTREAD is set if the file must be read.
  635.  * Yield true if successful.
  636.  * Try dRCS/basex first; if that fails and x is nonempty, try dbasex.
  637.  * Put these potential names in RCSb.
  638.  * Set RCSbuf to the best RCS name found so far, and RCSerrno to its errno.
  639.  * Yield true if successful or if an unusual failure.
  640.  */
  641. {
  642.     register char *p;
  643.  
  644.     bufalloc(&RCSb, dlen + rcslen + 1 + baselen + xlen + 1);
  645.  
  646.     /* Try dRCS/basex.  */
  647.     VOID memcpy(p = RCSb.string, d, dlen);
  648.     VOID memcpy(p += dlen, rcsdir, rcslen);
  649.     p += rcslen;
  650.     *p++ = SLASH;
  651.     VOID memcpy(p, base, baselen);
  652.     VOID memcpy(p += baselen, x, xlen);
  653.     p[xlen] = 0;
  654.     if (xlen) {
  655.         if (finopen(rcsopen, mustread))
  656.         return true;
  657.  
  658.         /* Try dbasex.  */
  659.         /* Start from scratch, because finopen() may have changed RCSb.  */
  660.         VOID memcpy(p = RCSb.string, d, dlen);
  661.         VOID memcpy(p += dlen, base, baselen);
  662.         VOID memcpy(p += baselen, x, xlen);
  663.         p[xlen] = 0;
  664.     }
  665.     return finopen(rcsopen, mustread);
  666. }
  667.  
  668.     int
  669. pairnames(argc, argv, rcsopen, mustread, quiet)
  670.     int argc;
  671.     char **argv;
  672.     RILE *(*rcsopen)P((struct buf*,struct stat*,int));
  673.     int mustread, quiet;
  674. /*
  675.  * Pair the pathnames pointed to by argv; argc indicates
  676.  * how many there are.
  677.  * Place a pointer to the RCS pathname into RCSname,
  678.  * and a pointer to the pathname of the working file into workname.
  679.  * If both are given, and workstdout
  680.  * is set, a warning is printed.
  681.  *
  682.  * If the RCS file exists, places its status into RCSstat.
  683.  *
  684.  * If the RCS file exists, it is RCSOPENed for reading, the file pointer
  685.  * is placed into finptr, and the admin-node is read in; returns 1.
  686.  * If the RCS file does not exist and MUSTREAD,
  687.  * print an error unless QUIET and return 0.
  688.  * Otherwise, initialize the admin node and return -1.
  689.  *
  690.  * 0 is returned on all errors, e.g. files that are not regular files.
  691.  */
  692. {
  693.     static struct buf tempbuf;
  694.  
  695.     register char *p, *arg, *RCS1;
  696.     char const *base, *RCSbase, *x;
  697.     int paired;
  698.     size_t arglen, dlen, baselen, xlen;
  699.  
  700.     fdlock = -1;
  701.  
  702.     if (!(arg = *argv)) return 0; /* already paired pathname */
  703.     if (*arg == '-') {
  704.         error("%s option is ignored after pathnames", arg);
  705.         return 0;
  706.     }
  707.  
  708.     base = basename(arg);
  709.  
  710.     /* Allocate buffer temporary to hold the default paired name. */
  711.     p = arg;
  712.     for (;;) {
  713.         switch (*p++) {
  714.             /* Beware characters that cause havoc with ci -k. */
  715.             case KDELIM:
  716.             error("RCS pathname `%s' contains %c", arg, KDELIM);
  717.             return 0;
  718.             case ' ': case '\n': case '\t':
  719.             error("RCS pathname `%s' contains white space", arg);
  720.             return 0;
  721.             default:
  722.             continue;
  723.             case 0:
  724.             break;
  725.         }
  726.         break;
  727.     }
  728.  
  729.     paired = false;
  730.  
  731.         /* first check suffix to see whether it is an RCS file or not */
  732.     if ((x = rcssuffix(arg)))
  733.     {
  734.         /* RCS pathname given */
  735.         RCS1 = arg;
  736.         RCSbase = base;
  737.         baselen = x - base;
  738.         if (
  739.             1 < argc  &&
  740.             !rcssuffix(workname = p = argv[1])  &&
  741.             baselen <= (arglen = strlen(p))  &&
  742.             ((p+=arglen-baselen) == workname  ||  isSLASH(p[-1])) &&
  743.             memcmp(base, p, baselen) == 0
  744.         ) {
  745.             argv[1] = 0;
  746.             paired = true;
  747.         } else {
  748.             bufscpy(&tempbuf, base);
  749.             workname = p = tempbuf.string;
  750.             p[baselen] = 0;
  751.         }
  752.         } else {
  753.                 /* working file given; now try to find RCS file */
  754.         workname = arg;
  755.         baselen = p - base - 1;
  756.         /* Derive RCS pathname.  */
  757.         if (
  758.             1 < argc  &&
  759.             (x = rcssuffix(RCS1 = argv[1]))  &&
  760.             baselen  <=  x - RCS1  &&
  761.             ((RCSbase=x-baselen)==RCS1 || isSLASH(RCSbase[-1])) &&
  762.             memcmp(base, RCSbase, baselen) == 0
  763.         ) {
  764.             argv[1] = 0;
  765.             paired = true;
  766.         } else
  767.             RCSbase = RCS1 = 0;
  768.         }
  769.     /* Now we have a (tentative) RCS pathname in RCS1 and workname.  */
  770.         /* Second, try to find the right RCS file */
  771.     if (RCSbase!=RCS1) {
  772.                 /* a path for RCSfile is given; single RCS file to look for */
  773.         bufscpy(&RCSbuf, RCS1);
  774.         finptr = (*rcsopen)(&RCSbuf, &RCSstat, mustread);
  775.         RCSerrno = errno;
  776.         } else {
  777.         bufscpy(&RCSbuf, "");
  778.         if (RCS1)
  779.             /* RCS filename was given without path.  */
  780.             VOID fin2open(arg, (size_t)0, RCSbase, baselen,
  781.                 x, strlen(x), rcsopen, mustread
  782.             );
  783.         else {
  784.             /* No RCS pathname was given.  */
  785.             /* Try each suffix in turn.  */
  786.             dlen = base-arg;
  787.             x = suffixes;
  788.             while (! fin2open(arg, dlen, base, baselen,
  789.                     x, xlen=suffixlen(x), rcsopen, mustread
  790.             )) {
  791.                 x += xlen;
  792.                 if (!*x++)
  793.                     break;
  794.             }
  795.         }
  796.         }
  797.     RCSname = p = RCSbuf.string;
  798.     if (finptr) {
  799.         if (!S_ISREG(RCSstat.st_mode)) {
  800.             error("%s isn't a regular file -- ignored", p);
  801.                         return 0;
  802.                 }
  803.                 Lexinit(); getadmin();
  804.     } else {
  805.         if (RCSerrno!=ENOENT || mustread || fdlock<0) {
  806.             if (RCSerrno == EEXIST)
  807.                 error("RCS file %s is in use", p);
  808.             else if (!quiet || RCSerrno!=ENOENT)
  809.                 enerror(RCSerrno, p);
  810.             return 0;
  811.         }
  812.                 InitAdmin();
  813.         };
  814. #    if LONG_NAMES_MAY_BE_TRUNCATED
  815.         if (filenametoolong(p)) {
  816.         error("RCS filename %s is too long", p);
  817.         return 0;
  818.         }
  819. #        ifndef NAME_MAX
  820.         /*
  821.          * Check workname too, even though it cannot be longer,
  822.          * because it may reside on a different filesystem.
  823.          */
  824.         if (filenametoolong(workname)) {
  825.             error("working filename %s is too long", workname);
  826.             return 0;
  827.         }
  828. #        endif
  829. #    endif
  830.  
  831.     if (paired && workstdout)
  832.         warn("Option -p is set; ignoring output file %s",workname);
  833.  
  834.     prevkeys = false;
  835.     return finptr ? 1 : -1;
  836. }
  837.  
  838.  
  839.     char const *
  840. getfullRCSname()
  841. /*
  842.  * Return a pointer to the full pathname of the RCS file.
  843.  * Gets the working directory's name at most once.
  844.  * Removes leading "../" and "./".
  845.  */
  846. {
  847.     static char const *wdptr;
  848.     static struct buf rcsbuf, wdbuf;
  849.     static size_t pathlength;
  850.  
  851.     register char const *realname;
  852.     register size_t parentdirlength;
  853.     register int dotdotcounter;
  854.     register char *d;
  855.     register char const *wd;
  856.  
  857.     if (ROOTPATH(RCSname)) {
  858.         return RCSname;
  859.         } else {
  860.         if (!(wd = wdptr)) {
  861.             /* Get working directory for the first time.  */
  862.             char *PWD = cgetenv("PWD");
  863.             struct stat PWDstat, dotstat;
  864.             if (! (
  865.             (d = PWD) &&
  866.             stat(PWD, &PWDstat) == 0 &&
  867.             stat(".", &dotstat) == 0 &&
  868.             same_file(PWDstat, dotstat, 1)
  869.             )) {
  870.             bufalloc(&wdbuf, SIZEABLE_PATH + 1);
  871. #            if !has_getcwd && has_getwd
  872.                 d = getwd(wdbuf.string);
  873. #            else
  874.                 while (
  875.                     !(d = getcwd(wdbuf.string, wdbuf.size))
  876.                 &&  errno==ERANGE
  877.                 )
  878.                 bufalloc(&wdbuf, wdbuf.size<<1);
  879. #            endif
  880.             if (!d  &&  !(d = PWD))
  881.                 efaterror("working directory");
  882.             }
  883.             parentdirlength = strlen(d);
  884.             while (parentdirlength && isSLASH(d[parentdirlength-1])) {
  885.             d[--parentdirlength] = 0;
  886.                     }
  887.             wdptr = wd = d;
  888.             pathlength = parentdirlength;
  889.                 }
  890.         /* The following must be redone since RCSname may change.  */
  891.         /* Find how many `../'s to remove from RCSname.  */
  892.                 dotdotcounter =0;
  893.         realname = RCSname;
  894.         while (realname[0]=='.') {
  895.             if (isSLASH(realname[1])) {
  896.                             /* drop leading ./ */
  897.                             realname += 2;
  898.             } else if (realname[1]=='.' && isSLASH(realname[2])) {
  899.                             /* drop leading ../ and remember */
  900.                             dotdotcounter++;
  901.                             realname += 3;
  902.             } else
  903.                 break;
  904.                 }
  905.         /* Now remove dotdotcounter trailing directories from wd. */
  906.         parentdirlength = pathlength;
  907.         while (dotdotcounter && parentdirlength) {
  908.                     /* move pointer backwards over trailing directory */
  909.             if (isSLASH(wd[--parentdirlength])) {
  910.                         dotdotcounter--;
  911.                     }
  912.                 }
  913.         /* Build full pathname.  */
  914.         bufalloc(&rcsbuf, parentdirlength+strlen(realname)+2);
  915.         d = rcsbuf.string;
  916.         VOID memcpy(d, wd, parentdirlength);
  917.         d += parentdirlength;
  918.         *d++ = SLASH;
  919.         VOID strcpy(d, realname);
  920.         return rcsbuf.string;
  921.         }
  922. }
  923.  
  924. #ifndef isSLASH
  925.     int
  926. isSLASH(c)
  927.     int c;
  928. {
  929.     switch (c) {
  930.         case SLASHes:
  931.         return true;
  932.         default:
  933.         return false;
  934.     }
  935. }
  936. #endif
  937.  
  938.  
  939. #if !has_getcwd && !has_getwd
  940.  
  941.     char *
  942. getcwd(path, size)
  943.     char *path;
  944.     size_t size;
  945. {
  946.     static char const usrbinpwd[] = "/usr/bin/pwd";
  947. #    define binpwd (usrbinpwd+4)
  948.  
  949.     register FILE *fp;
  950.     register int c;
  951.     register char *p, *lim;
  952.     int closeerrno, closeerror, e, fd[2], readerror, toolong, wstatus;
  953.     pid_t child;
  954. #    if !has_waitpid
  955.         pid_t w;
  956. #    endif
  957.  
  958.     if (!size) {
  959.         errno = EINVAL;
  960.         return 0;
  961.     }
  962.     if (pipe(fd) != 0)
  963.         return 0;
  964.     if (!(child = vfork())) {
  965.         if (
  966.             close(fd[0]) == 0 &&
  967.             (fd[1] == STDOUT_FILENO ||
  968. #                ifdef F_DUPFD
  969.                     (VOID close(STDOUT_FILENO),
  970.                     fcntl(fd[1], F_DUPFD, STDOUT_FILENO))
  971. #                else
  972.                     dup2(fd[1], STDOUT_FILENO)
  973. #                endif
  974.                 == STDOUT_FILENO &&
  975.                 close(fd[1]) == 0
  976.             )
  977.         ) {
  978.             VOID close(STDERR_FILENO);
  979.             VOID execl(binpwd, binpwd, (char *)0);
  980.             VOID execl(usrbinpwd, usrbinpwd, (char *)0);
  981.         }
  982.         _exit(EXIT_FAILURE);
  983.     }
  984.     e = errno;
  985.     closeerror = close(fd[1]);
  986.     closeerrno = errno;
  987.     fp = 0;
  988.     readerror = toolong = wstatus = 0;
  989.     p = path;
  990.     if (0 <= child) {
  991.         fp = fdopen(fd[0], "r");
  992.         e = errno;
  993.         if (fp) {
  994.             lim = p + size;
  995.             for (p = path;  ;  *p++ = c) {
  996.                 if ((c=getc(fp)) < 0) {
  997.                     if (feof(fp))
  998.                         break;
  999.                     if (ferror(fp)) {
  1000.                         readerror = 1;
  1001.                         e = errno;
  1002.                         break;
  1003.                     }
  1004.                 }
  1005.                 if (p == lim) {
  1006.                     toolong = 1;
  1007.                     break;
  1008.                 }
  1009.             }
  1010.         }
  1011. #        if has_waitpid
  1012.             if (waitpid(child, &wstatus, 0) < 0)
  1013.                 wstatus = 1;
  1014. #        else
  1015.             do {
  1016.                 if ((w = wait(&wstatus)) < 0) {
  1017.                     wstatus = 1;
  1018.                     break;
  1019.                 }
  1020.             } while (w != child);
  1021. #        endif
  1022.     }
  1023.     if (!fp) {
  1024.         VOID close(fd[0]);
  1025.         errno = e;
  1026.         return 0;
  1027.     }
  1028.     if (fclose(fp) != 0)
  1029.         return 0;
  1030.     if (readerror) {
  1031.         errno = e;
  1032.         return 0;
  1033.     }
  1034.     if (closeerror) {
  1035.         errno = closeerrno;
  1036.         return 0;
  1037.     }
  1038.     if (toolong) {
  1039.         errno = ERANGE;
  1040.         return 0;
  1041.     }
  1042.     if (wstatus  ||  p == path  ||  *--p != '\n') {
  1043.         errno = EACCES;
  1044.         return 0;
  1045.     }
  1046.     *p = '\0';
  1047.     return path;
  1048. }
  1049. #endif
  1050.  
  1051.  
  1052. #ifdef PAIRTEST
  1053. /* test program for pairnames() and getfullRCSname() */
  1054.  
  1055. char const cmdid[] = "pair";
  1056.  
  1057. main(argc, argv)
  1058. int argc; char *argv[];
  1059. {
  1060.         int result;
  1061.     int initflag;
  1062.     quietflag = initflag = false;
  1063.  
  1064.         while(--argc, ++argv, argc>=1 && ((*argv)[0] == '-')) {
  1065.                 switch ((*argv)[1]) {
  1066.  
  1067.         case 'p':       workstdout = stdout;
  1068.                                 break;
  1069.                 case 'i':       initflag=true;
  1070.                                 break;
  1071.                 case 'q':       quietflag=true;
  1072.                                 break;
  1073.                 default:        error("unknown option: %s", *argv);
  1074.                                 break;
  1075.                 }
  1076.         }
  1077.  
  1078.         do {
  1079.         RCSname = workname = 0;
  1080.         result = pairnames(argc,argv,rcsreadopen,!initflag,quietflag);
  1081.                 if (result!=0) {
  1082.             diagnose("RCS pathname: %s; working pathname: %s\nFull RCS pathname: %s\n",
  1083.                  RCSname, workname, getfullRCSname()
  1084.             );
  1085.                 }
  1086.                 switch (result) {
  1087.                         case 0: continue; /* already paired file */
  1088.  
  1089.                         case 1: if (initflag) {
  1090.                     error("RCS file %s exists already",RCSname);
  1091.                                 } else {
  1092.                     diagnose("RCS file %s exists\n", RCSname);
  1093.                                 }
  1094.                 Ifclose(finptr);
  1095.                                 break;
  1096.  
  1097.             case -1:diagnose("RCS file doesn't exist\n");
  1098.                                 break;
  1099.                 }
  1100.  
  1101.         } while (++argv, --argc>=1);
  1102.  
  1103. }
  1104.  
  1105.     exiting void
  1106. exiterr()
  1107. {
  1108.     dirtempunlink();
  1109.     tempunlink();
  1110.     _exit(EXIT_FAILURE);
  1111. }
  1112. #endif
  1113.