home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / ci.c < prev    next >
Text File  |  1992-02-17  |  36KB  |  1,181 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990, 1991, 1992 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS checkin operation
  29.  */
  30. /*******************************************************************
  31.  *                       check revisions into RCS files
  32.  *******************************************************************
  33.  */
  34.  
  35.  
  36.  
  37. /* $Log: ci.c,v $
  38.  * Revision 5.24  1992/02/17  23:02:06  eggert
  39.  * `-rREV' now just specifies a revision REV; only bare `-r' reverts to default.
  40.  * Add -T.
  41.  *
  42.  * Revision 5.23  1992/01/27  16:42:51  eggert
  43.  * Always unlock branchpoint if caller has a lock.
  44.  * Add support for bad_chmod_close, bad_creat0.  lint -> RCS_lint
  45.  *
  46.  * Revision 5.22  1992/01/06  02:42:34  eggert
  47.  * Invoke utime() before chmod() to keep some buggy systems happy.
  48.  *
  49.  * Revision 5.21  1991/11/20  17:58:07  eggert
  50.  * Don't read the delta tree from a nonexistent RCS file.
  51.  *
  52.  * Revision 5.20  1991/10/07  17:32:46  eggert
  53.  * Fix log bugs.  Remove lint.
  54.  *
  55.  * Revision 5.19  1991/09/26  23:10:30  eggert
  56.  * Plug file descriptor leak.
  57.  *
  58.  * Revision 5.18  1991/09/18  07:29:10  eggert
  59.  * Work around a common ftruncate() bug.
  60.  *
  61.  * Revision 5.17  1991/09/10  22:15:46  eggert
  62.  * Fix test for redirected stdin.
  63.  *
  64.  * Revision 5.16  1991/08/19  23:17:54  eggert
  65.  * When there are no changes, revert to previous revision instead of aborting.
  66.  * Add piece tables, -M, -r$.  Tune.
  67.  *
  68.  * Revision 5.15  1991/04/21  11:58:14  eggert
  69.  * Ensure that working file is newer than RCS file after ci -[lu].
  70.  * Add -x, RCSINIT, MS-DOS support.
  71.  *
  72.  * Revision 5.14  1991/02/28  19:18:47  eggert
  73.  * Don't let a setuid ci create a new RCS file; rcs -i -a must be run first.
  74.  * Fix ci -ko -l mode bug.  Open work file at most once.
  75.  *
  76.  * Revision 5.13  1991/02/25  07:12:33  eggert
  77.  * getdate -> getcurdate (SVR4 name clash)
  78.  *
  79.  * Revision 5.12  1990/12/31  01:00:12  eggert
  80.  * Don't use uninitialized storage when handling -{N,n}.
  81.  *
  82.  * Revision 5.11  1990/12/04  05:18:36  eggert
  83.  * Use -I for prompts and -q for diagnostics.
  84.  *
  85.  * Revision 5.10  1990/11/05  20:30:10  eggert
  86.  * Don't remove working file when aborting due to no changes.
  87.  *
  88.  * Revision 5.9  1990/11/01  05:03:23  eggert
  89.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  90.  *
  91.  * Revision 5.8  1990/10/04  06:30:09  eggert
  92.  * Accumulate exit status across files.
  93.  *
  94.  * Revision 5.7  1990/09/25  20:11:46  hammer
  95.  * fixed another small typo
  96.  *
  97.  * Revision 5.6  1990/09/24  21:48:50  hammer
  98.  * added cleanups from Paul Eggert.
  99.  *
  100.  * Revision 5.5  1990/09/21  06:16:38  hammer
  101.  * made it handle multiple -{N,n}'s.  Also, made it treat re-directed stdin
  102.  * the same as the terminal
  103.  *
  104.  * Revision 5.4  1990/09/20  02:38:51  eggert
  105.  * ci -k now checks dates more thoroughly.
  106.  *
  107.  * Revision 5.3  1990/09/11  02:41:07  eggert
  108.  * Fix revision bug with `ci -k file1 file2'.
  109.  *
  110.  * Revision 5.2  1990/09/04  08:02:10  eggert
  111.  * Permit adjacent revisions with identical time stamps (possible on fast hosts).
  112.  * Improve incomplete line handling.  Standardize yes-or-no procedure.
  113.  *
  114.  * Revision 5.1  1990/08/29  07:13:44  eggert
  115.  * Expand locker value like co.  Clean old log messages too.
  116.  *
  117.  * Revision 5.0  1990/08/22  08:10:00  eggert
  118.  * Don't require a final newline.
  119.  * Make lock and temp files faster and safer.
  120.  * Remove compile-time limits; use malloc instead.
  121.  * Permit dates past 1999/12/31.  Switch to GMT.
  122.  * Add setuid support.  Don't pass +args to diff.  Check diff's output.
  123.  * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
  124.  * Check diff's output.
  125.  *
  126.  * Revision 4.9  89/05/01  15:10:54  narten
  127.  * changed copyright header to reflect current distribution rules
  128.  *
  129.  * Revision 4.8  88/11/08  13:38:23  narten
  130.  * changes from root@seismo.CSS.GOV (Super User)
  131.  * -d with no arguments uses the mod time of the file it is checking in
  132.  *
  133.  * Revision 4.7  88/08/09  19:12:07  eggert
  134.  * Make sure workfile is a regular file; use its mode if RCSfile doesn't have one.
  135.  * Use execv(), not system(); allow cc -R; remove lint.
  136.  * isatty(fileno(stdin)) -> ttystdin()
  137.  *
  138.  * Revision 4.6  87/12/18  11:34:41  narten
  139.  * lint cleanups (from Guy Harris)
  140.  *
  141.  * Revision 4.5  87/10/18  10:18:48  narten
  142.  * Updating version numbers. Changes relative to revision 1.1 are actually
  143.  * relative to 4.3
  144.  *
  145.  * Revision 1.3  87/09/24  13:57:19  narten
  146.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
  147.  * warnings)
  148.  *
  149.  * Revision 1.2  87/03/27  14:21:33  jenkins
  150.  * Port to suns
  151.  *
  152.  * Revision 4.3  83/12/15  12:28:54  wft
  153.  * ci -u and ci -l now set mode of working file properly.
  154.  *
  155.  * Revision 4.2  83/12/05  13:40:54  wft
  156.  * Merged with 3.9.1.1: added calls to clearerr(stdin).
  157.  * made rewriteflag external.
  158.  *
  159.  * Revision 4.1  83/05/10  17:03:06  wft
  160.  * Added option -d and -w, and updated assingment of date, etc. to new delta.
  161.  * Added handling of default branches.
  162.  * Option -k generates std. log message; fixed undef. pointer in reading of log.
  163.  * Replaced getlock() with findlock(), link--unlink with rename(),
  164.  * getpwuid() with getcaller().
  165.  * Moved all revision number generation to new routine addelta().
  166.  * Removed calls to stat(); now done by pairfilenames().
  167.  * Changed most calls to catchints() with restoreints().
  168.  * Directed all interactive messages to stderr.
  169.  *
  170.  * Revision 3.9.1.1  83/10/19  04:21:03  lepreau
  171.  * Added clearerr(stdin) to getlogmsg() for re-reading stdin.
  172.  *
  173.  * Revision 3.9  83/02/15  15:25:44  wft
  174.  * 4.2 prerelease
  175.  *
  176.  * Revision 3.9  83/02/15  15:25:44  wft
  177.  * Added call to fastcopy() to copy remainder of RCS file.
  178.  *
  179.  * Revision 3.8  83/01/14  15:34:05  wft
  180.  * Added ignoring of interrupts while new RCS file is renamed;
  181.  * Avoids deletion of RCS files by interrupts.
  182.  *
  183.  * Revision 3.7  82/12/10  16:09:20  wft
  184.  * Corrected checking of return code from diff.
  185.  *
  186.  * Revision 3.6  82/12/08  21:34:49  wft
  187.  * Using DATEFORM to prepare date of checked-in revision;
  188.  * Fixed return from addbranch().
  189.  *
  190.  * Revision 3.5  82/12/04  18:32:42  wft
  191.  * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE. Updated
  192.  * field lockedby in removelock(), moved getlogmsg() before calling diff.
  193.  *
  194.  * Revision 3.4  82/12/02  13:27:13  wft
  195.  * added option -k.
  196.  *
  197.  * Revision 3.3  82/11/28  20:53:31  wft
  198.  * Added mustcheckin() to check for redundant checkins.
  199.  * Added xpandfile() to do keyword expansion for -u and -l;
  200.  * -m appends linefeed to log message if necessary.
  201.  * getlogmsg() suppresses prompt if stdin is not a terminal.
  202.  * Replaced keeplock with lockflag, fclose() with ffclose(),
  203.  * %02d with %.2d, getlogin() with getpwuid().
  204.  *
  205.  * Revision 3.2  82/10/18  20:57:23  wft
  206.  * An RCS file inherits its mode during the first ci from the working file,
  207.  * otherwise it stays the same, except that write permission is removed.
  208.  * Fixed ci -l, added ci -u (both do an implicit co after the ci).
  209.  * Fixed call to getlogin(), added call to getfullRCSname(), added check
  210.  * for write error.
  211.  * Changed conflicting identifiers.
  212.  *
  213.  * Revision 3.1  82/10/13  16:04:59  wft
  214.  * fixed type of variables receiving from getc() (char -> int).
  215.  * added include file dbm.h for getting BYTESIZ. This is used
  216.  * to check the return code from diff portably.
  217.  */
  218.  
  219. #include "rcsbase.h"
  220.  
  221. struct Symrev {
  222.        char const *ssymbol;
  223.        int override;
  224.        struct Symrev * nextsym;
  225. };
  226.  
  227. static char const *getcurdate P((void));
  228. static int addbranch P((struct hshentry*,struct buf*));
  229. static int addelta P((void));
  230. static int addsyms P((char const*));
  231. static int fixwork P((mode_t,char const*));
  232. static int removelock P((struct hshentry*));
  233. static int xpandfile P((RILE*,struct hshentry const*,char const**));
  234. static struct cbuf getlogmsg P((void));
  235. static void cleanup P((void));
  236. static void incnum P((char const*,struct buf*));
  237. static void addassoclst P((int, char *));
  238.  
  239. static FILE *exfile;
  240. static RILE *workptr;            /* working file pointer        */
  241. static struct buf newdelnum;        /* new revision number        */
  242. static struct cbuf msg;
  243. static int exitstatus;
  244. static int forceciflag;            /* forces check in        */
  245. static int keepflag, keepworkingfile, rcsinitflag;
  246. static struct hshentries *gendeltas;    /* deltas to be generated    */
  247. static struct hshentry *targetdelta;    /* old delta to be generated    */
  248. static struct hshentry newdelta;    /* new delta to be inserted    */
  249. static struct stat workstat;
  250. static struct Symrev *assoclst, *lastassoc;
  251.  
  252. mainProg(ciId, "ci", "$Id: ci.c,v 5.24 1992/02/17 23:02:06 eggert Exp $")
  253. {
  254.     static char const cmdusage[] =
  255.         "\nci usage: ci -{fIklMqru}[rev] -d[date] -mmsg -{nN}name -sstate -ttext -Vn -wwho -xsuff file ...";
  256.     static char const default_state[] = DEFAULTSTATE;
  257.  
  258.     char altdate[datesize];
  259.     char olddate[datesize];
  260.     char newdatebuf[datesize], targetdatebuf[datesize];
  261.     char *a, **newargv, *textfile;
  262.     char const *author, *krev, *rev, *state;
  263.     char const *diffilename, *expfilename;
  264.     char const *workdiffname, *newworkfilename;
  265.     char const *mtime;
  266.     int keepRCStime, lockflag, lockthis, mtimeflag, removedlock, Ttimeflag;
  267.     int r;
  268.     int changedRCS, changework, newhead;
  269.     int usestatdate; /* Use mod time of file for -d.  */
  270.     mode_t newworkmode; /* mode for working file */
  271.     struct hshentry *workdelta;
  272.  
  273.     setrid();
  274.  
  275.     author = rev = state = textfile = nil;
  276.     lockflag = false;
  277.     mtimeflag = false;
  278.     Ttimeflag = false;
  279.     altdate[0]= '\0'; /* empty alternate date for -d */
  280.     usestatdate=false;
  281.     suffixes = X_DEFAULT;
  282.  
  283.     argc = getRCSINIT(argc, argv, &newargv);
  284.     argv = newargv;
  285.     while (a = *++argv,  0<--argc && *a++=='-') {
  286.         switch (*a++) {
  287.  
  288.                 case 'r':
  289.             if (*a)
  290.                 goto revno;
  291.             keepworkingfile = lockflag = false;
  292.             break;
  293.  
  294.         case 'l':
  295.             keepworkingfile = lockflag = true;
  296.         revno:
  297.             if (*a) {
  298.                 if (rev) warn("redefinition of revision number");
  299.                 rev = a;
  300.                         }
  301.                         break;
  302.  
  303.                 case 'u':
  304.                         keepworkingfile=true; lockflag=false;
  305.                         goto revno;
  306.  
  307.         case 'I':
  308.             interactiveflag = true;
  309.             goto revno;
  310.  
  311.                 case 'q':
  312.                         quietflag=true;
  313.                         goto revno;
  314.  
  315.                 case 'f':
  316.                         forceciflag=true;
  317.                         goto revno;
  318.  
  319.                 case 'k':
  320.                         keepflag=true;
  321.                         goto revno;
  322.  
  323.                 case 'm':
  324.             if (msg.size) redefined('m');
  325.             msg = cleanlogmsg(a, strlen(a));
  326.             if (!msg.size)
  327.                 warn("missing message for -m option");
  328.                         break;
  329.  
  330.                 case 'n':
  331.             if (!*a) {
  332.                                 error("missing symbolic name after -n");
  333.                 break;
  334.                     }
  335.             checksid(a);
  336.             addassoclst(false, a);
  337.                 break;
  338.  
  339.         case 'N':
  340.             if (!*a) {
  341.                                 error("missing symbolic name after -N");
  342.                 break;
  343.                     }
  344.             checksid(a);
  345.             addassoclst(true, a);
  346.                 break;
  347.  
  348.                 case 's':
  349.             if (*a) {
  350.                 if (state) redefined('s');
  351.                 checksid(a);
  352.                 state = a;
  353.             } else
  354.                 warn("missing state for -s option");
  355.                         break;
  356.  
  357.                 case 't':
  358.             if (*a) {
  359.                 if (textfile) redefined('t');
  360.                 textfile = a;
  361.                         }
  362.                         break;
  363.  
  364.         case 'd':
  365.             if (altdate[0] || usestatdate)
  366.                 redefined('d');
  367.             altdate[0] = 0;
  368.             if (!(usestatdate = !*a))
  369.                 str2date(a, altdate);
  370.                         break;
  371.  
  372.         case 'M':
  373.             mtimeflag = true;
  374.             goto revno;
  375.  
  376.         case 'w':
  377.             if (*a) {
  378.                 if (author) redefined('w');
  379.                 checksid(a);
  380.                 author = a;
  381.             } else
  382.                 warn("missing author for -w option");
  383.                         break;
  384.  
  385.         case 'x':
  386.             suffixes = a;
  387.             break;
  388.  
  389.         case 'V':
  390.             setRCSversion(*argv);
  391.             break;
  392.  
  393.         case 'T':
  394.             if (!*a) {
  395.                 Ttimeflag = true;
  396.                 break;
  397.             }
  398.             /* fall into */
  399.                 default:
  400.             faterror("unknown option: %s%s", *argv, cmdusage);
  401.                 };
  402.         }  /* end processing of options */
  403.  
  404.     if (argc<1) faterror("no input file%s", cmdusage);
  405.  
  406.         /* now handle all filenames */
  407.         do {
  408.         targetdelta=nil;
  409.     ffree();
  410.  
  411.     switch (pairfilenames(argc, argv, rcswriteopen, false, false)) {
  412.  
  413.         case -1:                /* New RCS file */
  414. #        if has_setuid && has_getuid
  415.             if (euid() != ruid()) {
  416.             error("setuid initial checkin prohibited; use `rcs -i -a' first");
  417.             continue;
  418.             }
  419. #        endif
  420.         rcsinitflag = true;
  421.                 break;
  422.  
  423.         case 0:                 /* Error */
  424.                 continue;
  425.  
  426.         case 1:                 /* Normal checkin with prev . RCS file */
  427.         rcsinitflag = !Head;
  428.         }
  429.  
  430.         /* now RCSfilename contains the name of the RCS file, and
  431.          * workfilename contains the name of the working file.
  432.      * If the RCS file exists, finptr contains the file descriptor for the
  433.      * RCS file, and RCSstat is set. The admin node is initialized.
  434.          */
  435.  
  436.     diagnose("%s  <--  %s\n", RCSfilename,workfilename);
  437.  
  438.     if (!(workptr = Iopen(workfilename, FOPEN_R_WORK, &workstat))) {
  439.         eerror(workfilename);
  440.         continue;
  441.     }
  442.     if (finptr && !checkaccesslist()) continue; /* give up */
  443.  
  444.     krev = rev;
  445.         if (keepflag) {
  446.                 /* get keyword values from working file */
  447.         if (!getoldkeys(workptr)) continue;
  448.         if (!rev  &&  !*(krev = prevrev.string)) {
  449.             error("can't find a revision number in %s",workfilename);
  450.                         continue;
  451.                 }
  452.         if (!*prevdate.string && *altdate=='\0' && usestatdate==false)
  453.             warn("can't find a date in %s", workfilename);
  454.         if (!*prevauthor.string && !author)
  455.             warn("can't find an author in %s", workfilename);
  456.         if (!*prevstate.string && !state)
  457.             warn("can't find a state in %s", workfilename);
  458.         } /* end processing keepflag */
  459.  
  460.     /* Read the delta tree.  */
  461.     if (finptr)
  462.         gettree();
  463.  
  464.         /* expand symbolic revision number */
  465.     if (!fexpandsym(krev, &newdelnum, workptr))
  466.         continue;
  467.  
  468.         /* splice new delta into tree */
  469.     if ((removedlock = addelta()) < 0)
  470.         continue;
  471.  
  472.     newdelta.num = newdelnum.string;
  473.         newdelta.branches=nil;
  474.         newdelta.lockedby=nil; /*might be changed by addlock() */
  475.     newdelta.selector = true;
  476.     /* set author */
  477.     if (author!=nil)
  478.         newdelta.author=author;     /* set author given by -w         */
  479.     else if (keepflag && *prevauthor.string)
  480.         newdelta.author=prevauthor.string; /* preserve old author if possible*/
  481.     else    newdelta.author=getcaller();/* otherwise use caller's id      */
  482.     newdelta.state = default_state;
  483.     if (state!=nil)
  484.         newdelta.state=state;       /* set state given by -s          */
  485.     else if (keepflag && *prevstate.string)
  486.         newdelta.state=prevstate.string;   /* preserve old state if possible */
  487.     if (usestatdate) {
  488.         time2date(workstat.st_mtime, altdate);
  489.     }
  490.     if (*altdate!='\0')
  491.         newdelta.date=altdate;      /* set date given by -d           */
  492.     else if (keepflag && *prevdate.string) {
  493.         /* Preserve old date if possible.  */
  494.         str2date(prevdate.string, olddate);
  495.         newdelta.date = olddate;
  496.     } else
  497.         newdelta.date = getcurdate();  /* use current date */
  498.     /* now check validity of date -- needed because of -d and -k          */
  499.     if (targetdelta!=nil &&
  500.         cmpnum(newdelta.date,targetdelta->date) < 0) {
  501.         error("Date %s precedes %s in existing revision %s.",
  502.             date2str(newdelta.date, newdatebuf),
  503.             date2str(targetdelta->date, targetdatebuf),
  504.             targetdelta->num
  505.         );
  506.         continue;
  507.     }
  508.  
  509.  
  510.     if (lockflag  &&  addlock(&newdelta) < 0) continue;
  511.     if (!addsyms(newdelta.num))
  512.         continue;
  513.  
  514.  
  515.     putadmin();
  516.         puttree(Head,frewrite);
  517.     putdesc(false,textfile);
  518.  
  519.     changework = Expand != OLD_EXPAND;
  520.     keepRCStime = false;
  521.     lockthis = lockflag;
  522.     workdelta = &newdelta;
  523.  
  524.         /* build rest of file */
  525.     if (rcsinitflag) {
  526.         diagnose("initial revision: %s\n", newdelnum.string);
  527.                 /* get logmessage */
  528.                 newdelta.log=getlogmsg();
  529.         putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false);
  530.         RCSstat.st_mode = workstat.st_mode;
  531.         RCSstat.st_nlink = 0;
  532.         changedRCS = true;
  533.         } else {
  534.         diffilename = maketemp(0);
  535.         workdiffname = workfilename;
  536.         if (workdiffname[0] == '+') {
  537.             /* Some diffs have options with leading '+'.  */
  538.             char *dp = ftnalloc(char, strlen(workfilename)+3);
  539.             workdiffname = dp;
  540.             *dp++ = '.';
  541.             *dp++ = SLASH;
  542.             VOID strcpy(dp, workfilename);
  543.         }
  544.         newhead  =  Head == &newdelta;
  545.         if (!newhead)
  546.             foutptr = frewrite;
  547.         expfilename = buildrevision(
  548.             gendeltas, targetdelta, (FILE*)0, false
  549.         );
  550.         if (
  551.             !forceciflag  &&
  552.             (changework = rcsfcmp(
  553.             workptr, &workstat, expfilename, targetdelta
  554.             )) <= 0
  555.         ) {
  556.             diagnose("file is unchanged; reverting to previous revision %s\n",
  557.             targetdelta->num
  558.             );
  559.             if (removedlock < lockflag) {
  560.             diagnose("previous revision was not locked; ignoring -l option\n");
  561.             lockthis = 0;
  562.             }
  563.             if (!(changedRCS  =
  564.                 lockflag < removedlock
  565.             ||  assoclst
  566.             ||    newdelta.state != default_state
  567.                 &&    strcmp(newdelta.state, targetdelta->state) != 0
  568.             ))
  569.             workdelta = targetdelta;
  570.             else {
  571.             /*
  572.              * We have started to build the wrong new RCS file.
  573.              * Start over from the beginning.
  574.              */
  575.             long hwm = ftell(frewrite);
  576.             int bad_truncate;
  577.             if (fseek(frewrite, 0L, SEEK_SET) != 0)
  578.                 Oerror();
  579. #            if !has_ftruncate
  580.                 bad_truncate = 1;
  581. #            else
  582.                 /*
  583.                  * Work around a common ftruncate() bug.
  584.                  * We can't rely on has_truncate, because we might
  585.                  * be using a filesystem exported to us via NFS.
  586.                  */
  587.                 bad_truncate = ftruncate(fileno(frewrite),(off_t)0);
  588.                 if (bad_truncate  &&  errno != EACCES)
  589.                 Oerror();
  590. #            endif
  591.             Irewind(finptr);
  592.             Lexinit();
  593.             getadmin();
  594.             gettree();
  595.             if (!(workdelta = genrevs(
  596.                 targetdelta->num, (char*)0, (char*)0, (char*)0,
  597.                 &gendeltas
  598.             )))
  599.                 continue;
  600.             workdelta->log = targetdelta->log;
  601.             if (newdelta.state != default_state)
  602.                 workdelta->state = newdelta.state;
  603.             if (removedlock && removelock(workdelta)<0)
  604.                 continue;
  605.             if (!addsyms(workdelta->num))
  606.                 continue;
  607.             if (dorewrite(true, true) != 0)
  608.                 continue;
  609.             fastcopy(finptr, frewrite);
  610.             if (bad_truncate)
  611.                 while (ftell(frewrite) < hwm)
  612.                 /* White out any earlier mistake with '\n's.  */
  613.                 /* This is unlikely.  */
  614.                 afputc('\n', frewrite);
  615.             keepRCStime = Ttimeflag;
  616.             }
  617.         } else {
  618.             diagnose("new revision: %s; previous revision: %s\n",
  619.             newdelnum.string, targetdelta->num
  620.             );
  621.             newdelta.log = getlogmsg();
  622.             switch (run((char*)0, diffilename,
  623.             DIFF DIFF_FLAGS,
  624.             newhead ? workdiffname : expfilename,
  625.             newhead ? expfilename : workdiffname,
  626.             (char*)0
  627.             )) {
  628.             case DIFF_FAILURE: case DIFF_SUCCESS: break;
  629.             default: faterror("diff failed");
  630.             }
  631.             if (newhead) {
  632.             Irewind(workptr);
  633.             putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false);
  634.             if (!putdtext(targetdelta->num,targetdelta->log,diffilename,frewrite,true)) continue;
  635.             } else
  636.             if (!putdtext(newdelnum.string,newdelta.log,diffilename,frewrite,true)) continue;
  637.             changedRCS = true;
  638.                 }
  639.         }
  640.     if (donerewrite(changedRCS, keepRCStime) != 0)
  641.         continue;
  642.  
  643.         if (!keepworkingfile) {
  644.         Izclose(&workptr);
  645.         r = un_link(workfilename); /* Get rid of old file */
  646.         } else {
  647.         newworkmode = WORKMODE(RCSstat.st_mode,
  648.             !   (Expand==VAL_EXPAND  ||  lockthis < StrictLocks)
  649.         );
  650.         mtime = mtimeflag ? workdelta->date : (char const*)0;
  651.  
  652.         /* Expand if it might change or if we can't fix mode, time.  */
  653.         if (changework  ||  (r=fixwork(newworkmode,mtime)) != 0) {
  654.             Irewind(workptr);
  655.             /* Expand keywords in file.  */
  656.             locker_expansion = lockthis;
  657.             switch (xpandfile(workptr, workdelta, &newworkfilename)) {
  658.             default:
  659.                 continue;
  660.  
  661.             case 0:
  662.                 /*
  663.                  * No expansion occurred; try to reuse working file
  664.                  * unless we already tried and failed.
  665.                  */
  666.                 if (changework)
  667.                 if ((r=fixwork(newworkmode,mtime)) == 0)
  668.                     break;
  669.                 /* fall into */
  670.             case 1:
  671.                 if (!(r = setfiledate(newworkfilename,mtime))) {
  672.                 Izclose(&workptr);
  673.                 ignoreints();
  674.                 r = chnamemod(&exfile, newworkfilename,
  675.                     workfilename, 1, newworkmode
  676.                 );
  677.                 keepdirtemp(newworkfilename);
  678.                 restoreints();
  679.                 }
  680.             }
  681.         }
  682.         }
  683.     if (r != 0) {
  684.         eerror(workfilename);
  685.         continue;
  686.     }
  687.     diagnose("done\n");
  688.  
  689.         } while (cleanup(),
  690.                  ++argv, --argc >=1);
  691.  
  692.     tempunlink();
  693.     exitmain(exitstatus);
  694. }       /* end of main (ci) */
  695.  
  696.     static void
  697. cleanup()
  698. {
  699.     if (nerror) exitstatus = EXIT_FAILURE;
  700.     Izclose(&finptr);
  701.     Izclose(&workptr);
  702.     Ozclose(&exfile);
  703.     Ozclose(&fcopy);
  704.     ORCSclose();
  705.     dirtempunlink();
  706. }
  707.  
  708. #if RCS_lint
  709. #    define exiterr ciExit
  710. #endif
  711.     exiting void
  712. exiterr()
  713. {
  714.     dirtempunlink();
  715.     tempunlink();
  716.     _exit(EXIT_FAILURE);
  717. }
  718.  
  719. /*****************************************************************/
  720. /* the rest are auxiliary routines                               */
  721.  
  722.  
  723.     static int
  724. addelta()
  725. /* Function: Appends a delta to the delta tree, whose number is
  726.  * given by newdelnum.  Updates Head, newdelnum, newdelnumlength,
  727.  * and the links in newdelta.
  728.  * Return -1 on error, 1 if a lock is removed, 0 otherwise.
  729.  */
  730. {
  731.     register char *tp;
  732.     register unsigned i;
  733.     int removedlock;
  734.     unsigned newdnumlength;  /* actual length of new rev. num. */
  735.  
  736.     newdnumlength = countnumflds(newdelnum.string);
  737.  
  738.     if (rcsinitflag) {
  739.                 /* this covers non-existing RCS file and a file initialized with rcs -i */
  740.         if ((newdnumlength==0)&&(Dbranch!=nil)) {
  741.             bufscpy(&newdelnum, Dbranch);
  742.             newdnumlength = countnumflds(Dbranch);
  743.         }
  744.         if (newdnumlength==0) bufscpy(&newdelnum, "1.1");
  745.         else if (newdnumlength==1) bufscat(&newdelnum, ".1");
  746.         else if (newdnumlength>2) {
  747.             error("Branch point doesn't exist for %s.",newdelnum.string);
  748.             return -1;
  749.                 } /* newdnumlength == 2 is OK;  */
  750.                 Head = &newdelta;
  751.                 newdelta.next=nil;
  752.         return 0;
  753.         }
  754.         if (newdnumlength==0) {
  755.                 /* derive new revision number from locks */
  756.         switch (findlock(true, &targetdelta)) {
  757.  
  758.           default:
  759.             /* found two or more old locks */
  760.             return -1;
  761.  
  762.           case 1:
  763.                     /* found an old lock */
  764.                     /* check whether locked revision exists */
  765.             if (!genrevs(targetdelta->num,(char*)0,(char*)0,(char*)0,&gendeltas))
  766.             return -1;
  767.                     if (targetdelta==Head) {
  768.                         /* make new head */
  769.                         newdelta.next=Head;
  770.                         Head= &newdelta;
  771.             } else if (!targetdelta->next && countnumflds(targetdelta->num)>2) {
  772.                         /* new tip revision on side branch */
  773.                         targetdelta->next= &newdelta;
  774.                         newdelta.next = nil;
  775.                     } else {
  776.                         /* middle revision; start a new branch */
  777.             bufscpy(&newdelnum, "");
  778.             return addbranch(targetdelta,&newdelnum);
  779.                     }
  780.             incnum(targetdelta->num, &newdelnum);
  781.             return 1; /* successful use of existing lock */
  782.  
  783.           case 0:
  784.                     /* no existing lock; try Dbranch */
  785.                     /* update newdelnum */
  786.             if (StrictLocks || !myself(RCSstat.st_uid)) {
  787.             error("no lock set by %s",getcaller());
  788.             return -1;
  789.                     }
  790.                     if (Dbranch) {
  791.             bufscpy(&newdelnum, Dbranch);
  792.                     } else {
  793.             incnum(Head->num, &newdelnum);
  794.                     }
  795.             newdnumlength = countnumflds(newdelnum.string);
  796.                     /* now fall into next statement */
  797.                 }
  798.         }
  799.         if (newdnumlength<=2) {
  800.                 /* add new head per given number */
  801.                 if(newdnumlength==1) {
  802.                     /* make a two-field number out of it*/
  803.             if (cmpnumfld(newdelnum.string,Head->num,1)==0)
  804.             incnum(Head->num, &newdelnum);
  805.             else
  806.             bufscat(&newdelnum, ".1");
  807.                 }
  808.         if (cmpnum(newdelnum.string,Head->num) <= 0) {
  809.                     error("deltanumber %s too low; must be higher than %s",
  810.               newdelnum.string, Head->num);
  811.             return -1;
  812.                 }
  813.         targetdelta = Head;
  814.         if (0 <= (removedlock = removelock(Head))) {
  815.             if (!genrevs(Head->num,(char*)0,(char*)0,(char*)0,&gendeltas))
  816.             return -1;
  817.             newdelta.next = Head;
  818.             Head = &newdelta;
  819.         }
  820.         return removedlock;
  821.         } else {
  822.                 /* put new revision on side branch */
  823.                 /*first, get branch point */
  824.         tp = newdelnum.string;
  825.         for (i = newdnumlength - (newdnumlength&1 ^ 1);  (--i);  )
  826.             while (*tp++ != '.')
  827.                 continue;
  828.         *--tp = 0; /* Kill final dot to get old delta temporarily. */
  829.         if (!(targetdelta=genrevs(newdelnum.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas)))
  830.             return -1;
  831.         if (cmpnum(targetdelta->num, newdelnum.string) != 0) {
  832.             error("can't find branchpoint %s", newdelnum.string);
  833.             return -1;
  834.                 }
  835.         *tp = '.'; /* Restore final dot. */
  836.         return addbranch(targetdelta,&newdelnum);
  837.         }
  838. }
  839.  
  840.  
  841.  
  842.     static int
  843. addbranch(branchpoint,num)
  844.     struct hshentry *branchpoint;
  845.     struct buf *num;
  846. /* adds a new branch and branch delta at branchpoint.
  847.  * If num is the null string, appends the new branch, incrementing
  848.  * the highest branch number (initially 1), and setting the level number to 1.
  849.  * the new delta and branchhead are in globals newdelta and newbranch, resp.
  850.  * the new number is placed into num.
  851.  * Return -1 on error, 1 if a lock is removed, 0 otherwise.
  852.  */
  853. {
  854.     struct branchhead *bhead, **btrail;
  855.     struct buf branchnum;
  856.     int removedlock, result;
  857.     unsigned field, numlength;
  858.     static struct branchhead newbranch;  /* new branch to be inserted */
  859.  
  860.     numlength = countnumflds(num->string);
  861.  
  862.         if (branchpoint->branches==nil) {
  863.                 /* start first branch */
  864.                 branchpoint->branches = &newbranch;
  865.                 if (numlength==0) {
  866.             bufscpy(num, branchpoint->num);
  867.             bufscat(num, ".1.1");
  868.         } else if (numlength&1)
  869.             bufscat(num, ".1");
  870.                 newbranch.nextbranch=nil;
  871.  
  872.     } else if (numlength==0) {
  873.                 /* append new branch to the end */
  874.                 bhead=branchpoint->branches;
  875.                 while (bhead->nextbranch) bhead=bhead->nextbranch;
  876.                 bhead->nextbranch = &newbranch;
  877.         bufautobegin(&branchnum);
  878.         getbranchno(bhead->hsh->num, &branchnum);
  879.         incnum(branchnum.string, num);
  880.         bufautoend(&branchnum);
  881.         bufscat(num, ".1");
  882.                 newbranch.nextbranch=nil;
  883.         } else {
  884.                 /* place the branch properly */
  885.         field = numlength - (numlength&1 ^ 1);
  886.                 /* field of branch number */
  887.         btrail = &branchpoint->branches;
  888.         while (0 < (result=cmpnumfld(num->string,(*btrail)->hsh->num,field))) {
  889.             btrail = &(*btrail)->nextbranch;
  890.             if (!*btrail) {
  891.                 result = -1;
  892.                 break;
  893.             }
  894.                 }
  895.         if (result < 0) {
  896.                         /* insert/append new branchhead */
  897.             newbranch.nextbranch = *btrail;
  898.             *btrail = &newbranch;
  899.             if (numlength&1) bufscat(num, ".1");
  900.                 } else {
  901.                         /* branch exists; append to end */
  902.             bufautobegin(&branchnum);
  903.             getbranchno(num->string, &branchnum);
  904.             targetdelta=genrevs(branchnum.string,(char*)nil,
  905.                         (char*)nil,(char*)nil,&gendeltas);
  906.             bufautoend(&branchnum);
  907.             if (!targetdelta)
  908.                 return -1;
  909.             if (cmpnum(num->string,targetdelta->num) <= 0) {
  910.                                 error("deltanumber %s too low; must be higher than %s",
  911.                       num->string,targetdelta->num);
  912.                 return -1;
  913.                         }
  914.             if (0 <= (removedlock = removelock(targetdelta))) {
  915.                 if (numlength&1)
  916.                 incnum(targetdelta->num,num);
  917.                 targetdelta->next = &newdelta;
  918.                 newdelta.next = 0;
  919.             }
  920.             return removedlock;
  921.             /* Don't do anything to newbranch.  */
  922.                 }
  923.         }
  924.         newbranch.hsh = &newdelta;
  925.         newdelta.next=nil;
  926.     if (branchpoint->lockedby)
  927.         if (strcmp(branchpoint->lockedby, getcaller()) == 0)
  928.         return removelock(branchpoint); /* This returns 1.  */
  929.     return 0;
  930. }
  931.  
  932.     static int
  933. addsyms(num)
  934.     char const *num;
  935. {
  936.     register struct Symrev *p;
  937.  
  938.     for (p = assoclst;  p;  p = p->nextsym)
  939.         if (addsymbol(num, p->ssymbol, p->override)  <  0)
  940.             return false;
  941.     return true;
  942. }
  943.  
  944.  
  945.     static void
  946. incnum(onum,nnum)
  947.     char const *onum;
  948.     struct buf *nnum;
  949. /* Increment the last field of revision number onum by one and
  950.  * place the result into nnum.
  951.  */
  952. {
  953.     register char *tp, *np;
  954.     register size_t l;
  955.  
  956.     l = strlen(onum);
  957.     bufalloc(nnum, l+2);
  958.     np = tp = nnum->string;
  959.     VOID strcpy(np, onum);
  960.     for (tp = np + l;  np != tp;  )
  961.         if (isdigit(*--tp)) {
  962.             if (*tp != '9') {
  963.                 ++*tp;
  964.                 return;
  965.             }
  966.             *tp = '0';
  967.         } else {
  968.             tp++;
  969.             break;
  970.         }
  971.     /* We changed 999 to 000; now change it to 1000.  */
  972.     *tp = '1';
  973.     tp = np + l;
  974.     *tp++ = '0';
  975.     *tp = 0;
  976. }
  977.  
  978.  
  979.  
  980.     static int
  981. removelock(delta)
  982. struct hshentry * delta;
  983. /* function: Finds the lock held by caller on delta,
  984.  * removes it, and returns nonzero if successful.
  985.  * Print an error message and return -1 if there is no such lock.
  986.  * An exception is if !StrictLocks, and caller is the owner of
  987.  * the RCS file. If caller does not have a lock in this case,
  988.  * return 0; return 1 if a lock is actually removed.
  989.  */
  990. {
  991.     register struct lock *next, **trail;
  992.     char const *num;
  993.  
  994.         num=delta->num;
  995.     for (trail = &Locks;  (next = *trail);  trail = &next->nextlock)
  996.         if (next->delta == delta)
  997.         if (strcmp(getcaller(), next->login) == 0) {
  998.             /* We found a lock on delta by caller; delete it.  */
  999.             *trail = next->nextlock;
  1000.             delta->lockedby = 0;
  1001.             return 1;
  1002.         } else {
  1003.                     error("revision %s locked by %s",num,next->login);
  1004.             return -1;
  1005.                 }
  1006.     if (!StrictLocks && myself(RCSstat.st_uid))
  1007.         return 0;
  1008.     error("no lock set by %s for revision %s", getcaller(), num);
  1009.     return -1;
  1010. }
  1011.  
  1012.  
  1013.  
  1014.     static char const *
  1015. getcurdate()
  1016. /* Return a pointer to the current date.  */
  1017. {
  1018.     static char buffer[datesize]; /* date buffer */
  1019.  
  1020.     if (!buffer[0])
  1021.         time2date(now(), buffer);
  1022.         return buffer;
  1023. }
  1024.  
  1025.     static int
  1026. #if has_prototypes
  1027. fixwork(mode_t newworkmode, char const *mtime)
  1028.   /* The `#if has_prototypes' is needed because mode_t might promote to int.  */
  1029. #else
  1030.   fixwork(newworkmode, mtime)
  1031.     mode_t newworkmode;
  1032.     char const *mtime;
  1033. #endif
  1034. {
  1035.     return
  1036.             1 < workstat.st_nlink
  1037.             ||    newworkmode&S_IWUSR && !myself(workstat.st_uid)
  1038.             ||    setfiledate(workfilename, mtime) != 0
  1039.         ?   -1
  1040.         :    workstat.st_mode == newworkmode  ?  0
  1041.         :
  1042. #        if has_fchmod
  1043.             fchmod(Ifileno(workptr), newworkmode)
  1044. #        else
  1045. #        if bad_chmod_close
  1046.             -1
  1047. #        else
  1048.             chmod(workfilename, newworkmode)
  1049. #        endif
  1050. #        endif
  1051.     ;
  1052. }
  1053.  
  1054.     static int
  1055. xpandfile(unexfile, delta, exfilename)
  1056.     RILE *unexfile;
  1057.     struct hshentry const *delta;
  1058.     char const **exfilename;
  1059. /*
  1060.  * Read unexfile and copy it to a
  1061.  * file, performing keyword substitution with data from delta.
  1062.  * Return -1 if unsuccessful, 1 if expansion occurred, 0 otherwise.
  1063.  * If successful, stores the stream descriptor into *EXFILEP
  1064.  * and its name into *EXFILENAME.
  1065.  */
  1066. {
  1067.     char const *targetfname;
  1068.     int e, r;
  1069.  
  1070.     targetfname = makedirtemp(1);
  1071.     if (!(exfile = fopen(targetfname, FOPEN_W_WORK))) {
  1072.         eerror(targetfname);
  1073.         error("can't expand working file");
  1074.         return -1;
  1075.         }
  1076.     r = 0;
  1077.     if (Expand == OLD_EXPAND)
  1078.         fastcopy(unexfile,exfile);
  1079.     else {
  1080.         for (;;) {
  1081.             e = expandline(unexfile,exfile,delta,false,(FILE*)nil);
  1082.             if (e < 0)
  1083.                 break;
  1084.             r |= e;
  1085.             if (e <= 1)
  1086.                 break;
  1087.         }
  1088.     }
  1089.     *exfilename = targetfname;
  1090.     aflush(exfile);
  1091.     return r & 1;
  1092. }
  1093.  
  1094.  
  1095.  
  1096.  
  1097. /* --------------------- G E T L O G M S G --------------------------------*/
  1098.  
  1099.  
  1100.     static struct cbuf
  1101. getlogmsg()
  1102. /* Obtain and yield a log message.
  1103.  * If a log message is given with -m, yield that message.
  1104.  * If this is the initial revision, yield a standard log message.
  1105.  * Otherwise, reads a character string from the terminal.
  1106.  * Stops after reading EOF or a single '.' on a
  1107.  * line. getlogmsg prompts the first time it is called for the
  1108.  * log message; during all later calls it asks whether the previous
  1109.  * log message can be reused.
  1110.  */
  1111. {
  1112.     static char const
  1113.         emptych[] = EMPTYLOG,
  1114.         initialch[] = "Initial revision";
  1115.     static struct cbuf const
  1116.         emptylog = { emptych, sizeof(emptych)-sizeof(char) },
  1117.         initiallog = { initialch, sizeof(initialch)-sizeof(char) };
  1118.     static struct buf logbuf;
  1119.     static struct cbuf logmsg;
  1120.  
  1121.     register char *tp;
  1122.     register size_t i;
  1123.     char const *caller;
  1124.  
  1125.     if (msg.size) return msg;
  1126.  
  1127.     if (keepflag) {
  1128.         /* generate std. log message */
  1129.         caller = getcaller();
  1130.         i = sizeof(ciklog)+strlen(caller)+3;
  1131.         bufalloc(&logbuf, i+datesize);
  1132.         tp = logbuf.string;
  1133.         VOID sprintf(tp, "%s%s at ", ciklog, caller);
  1134.         VOID date2str(getcurdate(), tp+i);
  1135.         logmsg.string = tp;
  1136.         logmsg.size = strlen(tp);
  1137.         return logmsg;
  1138.     }
  1139.  
  1140.     if (!targetdelta && (
  1141.         cmpnum(newdelnum.string,"1.1")==0 ||
  1142.         cmpnum(newdelnum.string,"1.0")==0
  1143.     ))
  1144.         return initiallog;
  1145.  
  1146.     if (logmsg.size) {
  1147.                 /*previous log available*/
  1148.         if (yesorno(true, "reuse log message of previous file? [yn](y): "))
  1149.         return logmsg;
  1150.         }
  1151.  
  1152.         /* now read string from stdin */
  1153.     logmsg = getsstdin("m", "log message", "", &logbuf);
  1154.  
  1155.         /* now check whether the log message is not empty */
  1156.     if (logmsg.size)
  1157.         return logmsg;
  1158.     return emptylog;
  1159. }
  1160.  
  1161. /*  Make a linked list of Symbolic names  */
  1162.  
  1163.         static void
  1164. addassoclst(flag, sp)
  1165. int  flag;
  1166. char * sp;
  1167. {
  1168.         struct Symrev *pt;
  1169.  
  1170.     pt = talloc(struct Symrev);
  1171.     pt->ssymbol = sp;
  1172.     pt->override = flag;
  1173.     pt->nextsym = nil;
  1174.     if (lastassoc)
  1175.             lastassoc->nextsym = pt;
  1176.     else
  1177.             assoclst = pt;
  1178.     lastassoc = pt;
  1179.     return;
  1180. }
  1181.