1 /* Check in revisions of RCS files from working files. */
3 /* Copyright 1982, 1988, 1989 Walter Tichy
4 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Paul Eggert
5 Distributed under license by the Free Software Foundation, Inc.
7 This file is part of RCS.
9 RCS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 RCS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with RCS; see the file COPYING.
21 If not, write to the Free Software Foundation,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 Report problems and direct all questions to:
26 rcs-bugs@cs.purdue.edu
31 * Revision 5.30 1995/06/16 06:19:24 eggert
34 * Revision 5.29 1995/06/01 16:23:43 eggert
36 * Use `cmpdate', not `cmpnum', to compare dates.
37 * This is for MKS RCS's incompatible 20th-century date format.
38 * Don't worry about errno after ftruncate fails.
39 * Fix input file rewinding bug when large_memory && !maps_memory
40 * and checking in a branch tip.
42 * (fixwork): Fall back on chmod if fchmod fails, since it might be ENOSYS.
44 * Revision 5.28 1994/03/20 04:52:58 eggert
45 * Do not generate a corrupted RCS file if the user modifies the working file
46 * while `ci' is running.
47 * Do not remove the lock when `ci -l' reverts.
48 * Move buffer-flushes out of critical sections, since they aren't critical.
49 * Use ORCSerror to clean up after a fatal error.
50 * Specify subprocess input via file descriptor, not file name.
52 * Revision 5.27 1993/11/09 17:40:15 eggert
53 * -V now prints version on stdout and exits. Don't print usage twice.
55 * Revision 5.26 1993/11/03 17:42:27 eggert
56 * Add -z. Don't subtract from RCS file timestamp even if -T.
57 * Scan for and use Name keyword if -k.
58 * Don't discard ignored phrases. Improve quality of diagnostics.
60 * Revision 5.25 1992/07/28 16:12:44 eggert
61 * Add -i, -j, -V. Check that working and RCS files are distinct.
63 * Revision 5.24 1992/02/17 23:02:06 eggert
64 * `-rREV' now just specifies a revision REV; only bare `-r' reverts to default.
67 * Revision 5.23 1992/01/27 16:42:51 eggert
68 * Always unlock branchpoint if caller has a lock.
69 * Add support for bad_chmod_close, bad_creat0. lint -> RCS_lint
71 * Revision 5.22 1992/01/06 02:42:34 eggert
72 * Invoke utime() before chmod() to keep some buggy systems happy.
74 * Revision 5.21 1991/11/20 17:58:07 eggert
75 * Don't read the delta tree from a nonexistent RCS file.
77 * Revision 5.20 1991/10/07 17:32:46 eggert
78 * Fix log bugs. Remove lint.
80 * Revision 5.19 1991/09/26 23:10:30 eggert
81 * Plug file descriptor leak.
83 * Revision 5.18 1991/09/18 07:29:10 eggert
84 * Work around a common ftruncate() bug.
86 * Revision 5.17 1991/09/10 22:15:46 eggert
87 * Fix test for redirected stdin.
89 * Revision 5.16 1991/08/19 23:17:54 eggert
90 * When there are no changes, revert to previous revision instead of aborting.
91 * Add piece tables, -M, -r$. Tune.
93 * Revision 5.15 1991/04/21 11:58:14 eggert
94 * Ensure that working file is newer than RCS file after ci -[lu].
95 * Add -x, RCSINIT, MS-DOS support.
97 * Revision 5.14 1991/02/28 19:18:47 eggert
98 * Don't let a setuid ci create a new RCS file; rcs -i -a must be run first.
99 * Fix ci -ko -l mode bug. Open work file at most once.
101 * Revision 5.13 1991/02/25 07:12:33 eggert
102 * getdate -> getcurdate (SVR4 name clash)
104 * Revision 5.12 1990/12/31 01:00:12 eggert
105 * Don't use uninitialized storage when handling -{N,n}.
107 * Revision 5.11 1990/12/04 05:18:36 eggert
108 * Use -I for prompts and -q for diagnostics.
110 * Revision 5.10 1990/11/05 20:30:10 eggert
111 * Don't remove working file when aborting due to no changes.
113 * Revision 5.9 1990/11/01 05:03:23 eggert
114 * Add -I and new -t behavior. Permit arbitrary data in logs.
116 * Revision 5.8 1990/10/04 06:30:09 eggert
117 * Accumulate exit status across files.
119 * Revision 5.7 1990/09/25 20:11:46 hammer
120 * fixed another small typo
122 * Revision 5.6 1990/09/24 21:48:50 hammer
123 * added cleanups from Paul Eggert.
125 * Revision 5.5 1990/09/21 06:16:38 hammer
126 * made it handle multiple -{N,n}'s. Also, made it treat re-directed stdin
127 * the same as the terminal
129 * Revision 5.4 1990/09/20 02:38:51 eggert
130 * ci -k now checks dates more thoroughly.
132 * Revision 5.3 1990/09/11 02:41:07 eggert
133 * Fix revision bug with `ci -k file1 file2'.
135 * Revision 5.2 1990/09/04 08:02:10 eggert
136 * Permit adjacent revisions with identical time stamps (possible on fast hosts).
137 * Improve incomplete line handling. Standardize yes-or-no procedure.
139 * Revision 5.1 1990/08/29 07:13:44 eggert
140 * Expand locker value like co. Clean old log messages too.
142 * Revision 5.0 1990/08/22 08:10:00 eggert
143 * Don't require a final newline.
144 * Make lock and temp files faster and safer.
145 * Remove compile-time limits; use malloc instead.
146 * Permit dates past 1999/12/31. Switch to GMT.
147 * Add setuid support. Don't pass +args to diff. Check diff's output.
148 * Ansify and Posixate. Add -k, -V. Remove snooping. Tune.
149 * Check diff's output.
151 * Revision 4.9 89/05/01 15:10:54 narten
152 * changed copyright header to reflect current distribution rules
154 * Revision 4.8 88/11/08 13:38:23 narten
155 * changes from root@seismo.CSS.GOV (Super User)
156 * -d with no arguments uses the mod time of the file it is checking in
158 * Revision 4.7 88/08/09 19:12:07 eggert
159 * Make sure workfile is a regular file; use its mode if RCSfile doesn't have one.
160 * Use execv(), not system(); allow cc -R; remove lint.
161 * isatty(fileno(stdin)) -> ttystdin()
163 * Revision 4.6 87/12/18 11:34:41 narten
164 * lint cleanups (from Guy Harris)
166 * Revision 4.5 87/10/18 10:18:48 narten
167 * Updating version numbers. Changes relative to revision 1.1 are actually
170 * Revision 1.3 87/09/24 13:57:19 narten
171 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
174 * Revision 1.2 87/03/27 14:21:33 jenkins
177 * Revision 4.3 83/12/15 12:28:54 wft
178 * ci -u and ci -l now set mode of working file properly.
180 * Revision 4.2 83/12/05 13:40:54 wft
181 * Merged with 3.9.1.1: added calls to clearerr(stdin).
182 * made rewriteflag external.
184 * Revision 4.1 83/05/10 17:03:06 wft
185 * Added option -d and -w, and updated assingment of date, etc. to new delta.
186 * Added handling of default branches.
187 * Option -k generates std. log message; fixed undef. pointer in reading of log.
188 * Replaced getlock() with findlock(), link--unlink with rename(),
189 * getpwuid() with getcaller().
190 * Moved all revision number generation to new routine addelta().
191 * Removed calls to stat(); now done by pairfilenames().
192 * Changed most calls to catchints() with restoreints().
193 * Directed all interactive messages to stderr.
195 * Revision 3.9.1.1 83/10/19 04:21:03 lepreau
196 * Added clearerr(stdin) to getlogmsg() for re-reading stdin.
198 * Revision 3.9 83/02/15 15:25:44 wft
201 * Revision 3.9 83/02/15 15:25:44 wft
202 * Added call to fastcopy() to copy remainder of RCS file.
204 * Revision 3.8 83/01/14 15:34:05 wft
205 * Added ignoring of interrupts while new RCS file is renamed;
206 * Avoids deletion of RCS files by interrupts.
208 * Revision 3.7 82/12/10 16:09:20 wft
209 * Corrected checking of return code from diff.
211 * Revision 3.6 82/12/08 21:34:49 wft
212 * Using DATEFORM to prepare date of checked-in revision;
213 * Fixed return from addbranch().
215 * Revision 3.5 82/12/04 18:32:42 wft
216 * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE. Updated
217 * field lockedby in removelock(), moved getlogmsg() before calling diff.
219 * Revision 3.4 82/12/02 13:27:13 wft
222 * Revision 3.3 82/11/28 20:53:31 wft
223 * Added mustcheckin() to check for redundant checkins.
224 * Added xpandfile() to do keyword expansion for -u and -l;
225 * -m appends linefeed to log message if necessary.
226 * getlogmsg() suppresses prompt if stdin is not a terminal.
227 * Replaced keeplock with lockflag, fclose() with ffclose(),
228 * %02d with %.2d, getlogin() with getpwuid().
230 * Revision 3.2 82/10/18 20:57:23 wft
231 * An RCS file inherits its mode during the first ci from the working file,
232 * otherwise it stays the same, except that write permission is removed.
233 * Fixed ci -l, added ci -u (both do an implicit co after the ci).
234 * Fixed call to getlogin(), added call to getfullRCSname(), added check
236 * Changed conflicting identifiers.
238 * Revision 3.1 82/10/13 16:04:59 wft
239 * fixed type of variables receiving from getc() (char -> int).
240 * added include file dbm.h for getting BYTESIZ. This is used
241 * to check the return code from diff portably.
249 struct Symrev * nextsym;
252 static char const *getcurdate P((void));
253 static int addbranch P((struct hshentry*,struct buf*,int));
254 static int addelta P((void));
255 static int addsyms P((char const*));
256 static int fixwork P((mode_t,time_t));
257 static int removelock P((struct hshentry*));
258 static int xpandfile P((RILE*,struct hshentry const*,char const**,int));
259 static struct cbuf getlogmsg P((void));
260 static void cleanup P((void));
261 static void incnum P((char const*,struct buf*));
262 static void addassoclst P((int,char const*));
265 static RILE *workptr; /* working file pointer */
266 static struct buf newdelnum; /* new revision number */
267 static struct cbuf msg;
268 static int exitstatus;
269 static int forceciflag; /* forces check in */
270 static int keepflag, keepworkingfile, rcsinitflag;
271 static struct hshentries *gendeltas; /* deltas to be generated */
272 static struct hshentry *targetdelta; /* old delta to be generated */
273 static struct hshentry newdelta; /* new delta to be inserted */
274 static struct stat workstat;
275 static struct Symrev *assoclst, **nextassoc;
277 mainProg(ciId, "ci", "$FreeBSD$")
279 static char const cmdusage[] =
280 "\nci usage: ci -{fIklMqru}[rev] -d[date] -mmsg -{nN}name -sstate -ttext -T -Vn -wwho -xsuff -zzone file ...";
281 static char const default_state[] = DEFAULTSTATE;
283 char altdate[datesize];
284 char olddate[datesize];
285 char newdatebuf[datesize + zonelenmax];
286 char targetdatebuf[datesize + zonelenmax];
287 char *a, **newargv, *textfile;
288 char const *author, *krev, *rev, *state;
289 char const *diffname, *expname;
290 char const *newworkname;
291 int initflag, mustread;
292 int lockflag, lockthis, mtimeflag, removedlock, Ttimeflag;
294 int changedRCS, changework, dolog, newhead;
295 int usestatdate; /* Use mod time of file for -d. */
296 mode_t newworkmode; /* mode for working file */
298 struct hshentry *workdelta;
302 author = rev = state = textfile = 0;
303 initflag = lockflag = mustread = false;
306 altdate[0]= '\0'; /* empty alternate date for -d */
308 suffixes = X_DEFAULT;
309 nextassoc = &assoclst;
311 argc = getRCSINIT(argc, argv, &newargv);
313 while (a = *++argv, 0<--argc && *a++=='-') {
319 keepworkingfile = lockflag = false;
323 keepworkingfile = lockflag = true;
326 if (rev) warn("redefinition of revision number");
332 keepworkingfile=true; lockflag=false;
344 interactiveflag = true;
360 if (msg.size) redefined('m');
361 msg = cleanlogmsg(a, strlen(a));
363 error("missing message for -m option");
368 error("missing symbolic name after -n");
372 addassoclst(false, a);
377 error("missing symbolic name after -N");
381 addassoclst(true, a);
386 if (state) redefined('s');
390 error("missing state for -s option");
395 if (textfile) redefined('t');
401 if (altdate[0] || usestatdate)
404 if (!(usestatdate = !*a))
405 str2date(a, altdate);
414 if (author) redefined('w');
418 error("missing author for -w option");
426 setRCSversion(*argv);
440 error("unknown option: %s%s", *argv, cmdusage);
442 } /* end processing of options */
444 /* Handle all pathnames. */
445 if (nerror) cleanup();
446 else if (argc < 1) faterror("no input file%s", cmdusage);
447 else for (; 0 < argc; cleanup(), ++argv, --argc) {
451 switch (pairnames(argc, argv, rcswriteopen, mustread, false)) {
453 case -1: /* New RCS file */
454 # if has_setuid && has_getuid
455 if (euid() != ruid()) {
456 workerror("setuid initial checkin prohibited; use `rcs -i -a' first");
466 case 1: /* Normal checkin with prev . RCS file */
468 rcserror("already exists");
475 * RCSname contains the name of the RCS file, and
476 * workname contains the name of the working file.
477 * If the RCS file exists, finptr contains the file descriptor for the
478 * RCS file, and RCSstat is set. The admin node is initialized.
481 diagnose("%s <-- %s\n", RCSname, workname);
483 if (!(workptr = Iopen(workname, FOPEN_R_WORK, &workstat))) {
489 if (same_file(RCSstat, workstat, 0)) {
490 rcserror("RCS file is the same as working file %s.",
495 if (!checkaccesslist())
501 /* get keyword values from working file */
502 if (!getoldkeys(workptr)) continue;
503 if (!rev && !*(krev = prevrev.string)) {
504 workerror("can't find a revision number");
507 if (!*prevdate.string && *altdate=='\0' && usestatdate==false)
508 workwarn("can't find a date");
509 if (!*prevauthor.string && !author)
510 workwarn("can't find an author");
511 if (!*prevstate.string && !state)
512 workwarn("can't find a state");
513 } /* end processing keepflag */
515 /* Read the delta tree. */
519 /* expand symbolic revision number */
520 if (!fexpandsym(krev, &newdelnum, workptr))
523 /* splice new delta into tree */
524 if ((removedlock = addelta()) < 0)
527 newdelta.num = newdelnum.string;
528 newdelta.branches = 0;
529 newdelta.lockedby = 0; /* This might be changed by addlock(). */
530 newdelta.selector = true;
532 clear_buf(&newdelta.ig);
533 clear_buf(&newdelta.igtext);
536 newdelta.author=author; /* set author given by -w */
537 else if (keepflag && *prevauthor.string)
538 newdelta.author=prevauthor.string; /* preserve old author if possible*/
539 else newdelta.author=getcaller();/* otherwise use caller's id */
540 newdelta.state = default_state;
542 newdelta.state=state; /* set state given by -s */
543 else if (keepflag && *prevstate.string)
544 newdelta.state=prevstate.string; /* preserve old state if possible */
546 time2date(workstat.st_mtime, altdate);
549 newdelta.date=altdate; /* set date given by -d */
550 else if (keepflag && *prevdate.string) {
551 /* Preserve old date if possible. */
552 str2date(prevdate.string, olddate);
553 newdelta.date = olddate;
555 newdelta.date = getcurdate(); /* use current date */
556 /* now check validity of date -- needed because of -d and -k */
558 cmpdate(newdelta.date,targetdelta->date) < 0) {
559 rcserror("Date %s precedes %s in revision %s.",
560 date2str(newdelta.date, newdatebuf),
561 date2str(targetdelta->date, targetdatebuf),
568 if (lockflag && addlock(&newdelta, true) < 0) continue;
570 if (keepflag && *prevname.string)
571 if (addsymbol(newdelta.num, prevname.string, false) < 0)
573 if (!addsyms(newdelta.num))
578 puttree(Head,frewrite);
579 putdesc(false,textfile);
581 changework = Expand < MIN_UNCHANGED_EXPAND;
584 workdelta = &newdelta;
586 /* build rest of file */
588 diagnose("initial revision: %s\n", newdelta.num);
590 newdelta.log=getlogmsg();
591 putdftext(&newdelta, workptr, frewrite, false);
592 RCSstat.st_mode = workstat.st_mode;
593 RCSstat.st_nlink = 0;
596 diffname = maketemp(0);
597 newhead = Head == &newdelta;
600 expname = buildrevision(
601 gendeltas, targetdelta, (FILE*)0, false
605 strcmp(newdelta.state, targetdelta->state) == 0 &&
606 (changework = rcsfcmp(
607 workptr, &workstat, expname, targetdelta
610 diagnose("file is unchanged; reverting to previous revision %s\n",
613 if (removedlock < lockflag) {
614 diagnose("previous revision was not locked; ignoring -l option\n");
618 if (! (changedRCS = lockflag<removedlock || assoclst))
619 workdelta = targetdelta;
622 * We have started to build the wrong new RCS file.
623 * Start over from the beginning.
625 long hwm = ftell(frewrite);
630 * Work around a common ftruncate() bug:
631 * NFS won't let you truncate a file that you
632 * currently lack permissions for, even if you
633 * had permissions when you opened it.
634 * Also, Posix 1003.1b-1993 sec 5.6.7.2 p 128 l 1022
635 * says ftruncate might fail because it's not supported.
639 # define ftruncate(fd,length) (-1)
641 bad_truncate = ftruncate(fileno(frewrite), (off_t)0);
647 if (!(workdelta = genrevs(
648 targetdelta->num, (char*)0, (char*)0, (char*)0,
652 workdelta->log = targetdelta->log;
653 if (newdelta.state != default_state)
654 workdelta->state = newdelta.state;
655 if (lockthis<removedlock && removelock(workdelta)<0)
657 if (!addsyms(workdelta->num))
659 if (dorewrite(true, true) != 0)
661 fastcopy(finptr, frewrite);
663 while (ftell(frewrite) < hwm)
664 /* White out any earlier mistake with '\n's. */
665 /* This is unlikely. */
666 afputc('\n', frewrite);
669 int wfd = Ifileno(workptr);
670 struct stat checkworkstat;
671 char const *diffv[6 + !!OPEN_O_BINARY], **diffp;
672 # if large_memory && !maps_memory
673 FILE *wfile = workptr->stream;
676 # if !has_fflush_input && !(large_memory && maps_memory)
680 diagnose("new revision: %s; previous revision: %s\n",
681 newdelta.num, targetdelta->num
683 newdelta.log = getlogmsg();
686 # if has_fflush_input
687 if (fflush(workptr) != 0)
693 (wfile_off = ftell(wfile)) == -1
694 || fseek(wfile, 0L, SEEK_SET) != 0
695 # if has_fflush_input
696 || fflush(wfile) != 0
702 # if !has_fflush_input && !(large_memory && maps_memory)
703 wfd_off = lseek(wfd, (off_t)0, SEEK_CUR);
706 && lseek(wfd, (off_t)0, SEEK_SET) != 0))
711 *++diffp = DIFFFLAGS;
713 if (Expand == BINARY_EXPAND)
714 *++diffp = "--binary";
716 *++diffp = newhead ? "-" : expname;
717 *++diffp = newhead ? expname : "-";
719 switch (runv(wfd, diffname, diffv)) {
720 case DIFF_FAILURE: case DIFF_SUCCESS: break;
721 default: rcsfaterror("diff failed");
723 # if !has_fflush_input && !(large_memory && maps_memory)
724 if (lseek(wfd, wfd_off, SEEK_CUR) == -1)
727 # if large_memory && !maps_memory
728 if (fseek(wfile, wfile_off, SEEK_SET) != 0)
733 putdftext(&newdelta, workptr, frewrite, false);
734 if (!putdtext(targetdelta,diffname,frewrite,true)) continue;
736 if (!putdtext(&newdelta,diffname,frewrite,true)) continue;
739 * Check whether the working file changed during checkin,
740 * to avoid producing an inconsistent RCS file.
743 fstat(wfd, &checkworkstat) != 0
744 || workstat.st_mtime != checkworkstat.st_mtime
745 || workstat.st_size != checkworkstat.st_size
747 workerror("file changed during checkin");
755 /* Deduce time_t of new revision if it is needed later. */
757 if (mtimeflag | Ttimeflag)
758 wtime = date2time(workdelta->date);
760 if (donerewrite(changedRCS,
761 !Ttimeflag ? (time_t)-1
762 : finptr && wtime < RCSstat.st_mtime ? RCSstat.st_mtime
767 if (!keepworkingfile) {
769 r = un_link(workname); /* Get rid of old file */
771 newworkmode = WORKMODE(RCSstat.st_mode,
772 ! (Expand==VAL_EXPAND || lockthis < StrictLocks)
774 mtime = mtimeflag ? wtime : (time_t)-1;
776 /* Expand if it might change or if we can't fix mode, time. */
777 if (changework || (r=fixwork(newworkmode,mtime)) != 0) {
779 /* Expand keywords in file. */
780 locker_expansion = lockthis;
783 assoclst ? assoclst->ssymbol
784 : keepflag && *prevname.string ? prevname.string
789 workptr, workdelta, &newworkname, dolog
796 * No expansion occurred; try to reuse working file
797 * unless we already tried and failed.
800 if ((r=fixwork(newworkmode,mtime)) == 0)
807 r = chnamemod(&exfile, newworkname,
808 workname, 1, newworkmode, mtime
810 keepdirtemp(newworkname);
824 exitmain(exitstatus);
825 } /* end of main (ci) */
830 if (nerror) exitstatus = EXIT_FAILURE;
840 # define exiterr ciExit
851 /*****************************************************************/
852 /* the rest are auxiliary routines */
857 /* Function: Appends a delta to the delta tree, whose number is
858 * given by newdelnum. Updates Head, newdelnum, newdelnumlength,
859 * and the links in newdelta.
860 * Return -1 on error, 1 if a lock is removed, 0 otherwise.
866 int newdnumlength; /* actual length of new rev. num. */
868 newdnumlength = countnumflds(newdelnum.string);
871 /* this covers non-existing RCS file and a file initialized with rcs -i */
872 if (newdnumlength==0 && Dbranch) {
873 bufscpy(&newdelnum, Dbranch);
874 newdnumlength = countnumflds(Dbranch);
876 if (newdnumlength==0) bufscpy(&newdelnum, "1.1");
877 else if (newdnumlength==1) bufscat(&newdelnum, ".1");
878 else if (newdnumlength>2) {
879 rcserror("Branch point doesn't exist for revision %s.",
883 } /* newdnumlength == 2 is OK; */
888 if (newdnumlength==0) {
889 /* derive new revision number from locks */
890 switch (findlock(true, &targetdelta)) {
893 /* found two or more old locks */
897 /* found an old lock */
898 /* check whether locked revision exists */
899 if (!genrevs(targetdelta->num,(char*)0,(char*)0,(char*)0,&gendeltas))
901 if (targetdelta==Head) {
905 } else if (!targetdelta->next && countnumflds(targetdelta->num)>2) {
906 /* new tip revision on side branch */
907 targetdelta->next= &newdelta;
910 /* middle revision; start a new branch */
911 bufscpy(&newdelnum, "");
912 return addbranch(targetdelta, &newdelnum, 1);
914 incnum(targetdelta->num, &newdelnum);
915 return 1; /* successful use of existing lock */
918 /* no existing lock; try Dbranch */
919 /* update newdelnum */
920 if (StrictLocks || !myself(RCSstat.st_uid)) {
921 rcserror("no lock set by %s", getcaller());
925 bufscpy(&newdelnum, Dbranch);
927 incnum(Head->num, &newdelnum);
929 newdnumlength = countnumflds(newdelnum.string);
930 /* now fall into next statement */
933 if (newdnumlength<=2) {
934 /* add new head per given number */
935 if(newdnumlength==1) {
936 /* make a two-field number out of it*/
937 if (cmpnumfld(newdelnum.string,Head->num,1)==0)
938 incnum(Head->num, &newdelnum);
940 bufscat(&newdelnum, ".1");
942 if (cmpnum(newdelnum.string,Head->num) <= 0) {
943 rcserror("revision %s too low; must be higher than %s",
944 newdelnum.string, Head->num
949 if (0 <= (removedlock = removelock(Head))) {
950 if (!genrevs(Head->num,(char*)0,(char*)0,(char*)0,&gendeltas))
952 newdelta.next = Head;
957 /* put new revision on side branch */
958 /*first, get branch point */
959 tp = newdelnum.string;
960 for (i = newdnumlength - ((newdnumlength&1) ^ 1); --i; )
963 *--tp = 0; /* Kill final dot to get old delta temporarily. */
964 if (!(targetdelta=genrevs(newdelnum.string,(char*)0,(char*)0,(char*)0,&gendeltas)))
966 if (cmpnum(targetdelta->num, newdelnum.string) != 0) {
967 rcserror("can't find branch point %s", newdelnum.string);
970 *tp = '.'; /* Restore final dot. */
971 return addbranch(targetdelta, &newdelnum, 0);
978 addbranch(branchpoint, num, removedlock)
979 struct hshentry *branchpoint;
982 /* adds a new branch and branch delta at branchpoint.
983 * If num is the null string, appends the new branch, incrementing
984 * the highest branch number (initially 1), and setting the level number to 1.
985 * the new delta and branchhead are in globals newdelta and newbranch, resp.
986 * the new number is placed into num.
987 * Return -1 on error, 1 if a lock is removed, 0 otherwise.
988 * If REMOVEDLOCK is 1, a lock was already removed.
991 struct branchhead *bhead, **btrail;
992 struct buf branchnum;
994 int field, numlength;
995 static struct branchhead newbranch; /* new branch to be inserted */
997 numlength = countnumflds(num->string);
999 if (!branchpoint->branches) {
1000 /* start first branch */
1001 branchpoint->branches = &newbranch;
1003 bufscpy(num, branchpoint->num);
1004 bufscat(num, ".1.1");
1005 } else if (numlength&1)
1007 newbranch.nextbranch = 0;
1009 } else if (numlength==0) {
1010 /* append new branch to the end */
1011 bhead=branchpoint->branches;
1012 while (bhead->nextbranch) bhead=bhead->nextbranch;
1013 bhead->nextbranch = &newbranch;
1014 bufautobegin(&branchnum);
1015 getbranchno(bhead->hsh->num, &branchnum);
1016 incnum(branchnum.string, num);
1017 bufautoend(&branchnum);
1019 newbranch.nextbranch = 0;
1021 /* place the branch properly */
1022 field = numlength - ((numlength&1) ^ 1);
1023 /* field of branch number */
1024 btrail = &branchpoint->branches;
1025 while (0 < (result=cmpnumfld(num->string,(*btrail)->hsh->num,field))) {
1026 btrail = &(*btrail)->nextbranch;
1033 /* insert/append new branchhead */
1034 newbranch.nextbranch = *btrail;
1035 *btrail = &newbranch;
1036 if (numlength&1) bufscat(num, ".1");
1038 /* branch exists; append to end */
1039 bufautobegin(&branchnum);
1040 getbranchno(num->string, &branchnum);
1041 targetdelta = genrevs(
1042 branchnum.string, (char*)0, (char*)0, (char*)0,
1045 bufautoend(&branchnum);
1048 if (cmpnum(num->string,targetdelta->num) <= 0) {
1049 rcserror("revision %s too low; must be higher than %s",
1050 num->string, targetdelta->num
1055 && 0 <= (removedlock = removelock(targetdelta))
1058 incnum(targetdelta->num,num);
1059 targetdelta->next = &newdelta;
1063 /* Don't do anything to newbranch. */
1066 newbranch.hsh = &newdelta;
1068 if (branchpoint->lockedby)
1069 if (strcmp(branchpoint->lockedby, getcaller()) == 0)
1070 return removelock(branchpoint); /* This returns 1. */
1078 register struct Symrev *p;
1080 for (p = assoclst; p; p = p->nextsym)
1081 if (addsymbol(num, p->ssymbol, p->override) < 0)
1091 /* Increment the last field of revision number onum by one and
1092 * place the result into nnum.
1095 register char *tp, *np;
1099 bufalloc(nnum, l+2);
1100 np = tp = nnum->string;
1101 VOID strcpy(np, onum);
1102 for (tp = np + l; np != tp; )
1103 if (isdigit(*--tp)) {
1113 /* We changed 999 to 000; now change it to 1000. */
1124 struct hshentry * delta;
1125 /* function: Finds the lock held by caller on delta,
1126 * removes it, and returns nonzero if successful.
1127 * Print an error message and return -1 if there is no such lock.
1128 * An exception is if !StrictLocks, and caller is the owner of
1129 * the RCS file. If caller does not have a lock in this case,
1130 * return 0; return 1 if a lock is actually removed.
1133 register struct rcslock *next, **trail;
1137 for (trail = &Locks; (next = *trail); trail = &next->nextlock)
1138 if (next->delta == delta)
1139 if (strcmp(getcaller(), next->login) == 0) {
1140 /* We found a lock on delta by caller; delete it. */
1141 *trail = next->nextlock;
1142 delta->lockedby = 0;
1145 rcserror("revision %s locked by %s", num, next->login);
1148 if (!StrictLocks && myself(RCSstat.st_uid))
1150 rcserror("no lock set by %s for revision %s", getcaller(), num);
1158 /* Return a pointer to the current date. */
1160 static char buffer[datesize]; /* date buffer */
1163 time2date(now(), buffer);
1169 fixwork(mode_t newworkmode, time_t mtime)
1170 /* The `#if has_prototypes' is needed because mode_t might promote to int. */
1172 fixwork(newworkmode, mtime)
1178 1 < workstat.st_nlink
1179 || (newworkmode&S_IWUSR && !myself(workstat.st_uid))
1180 || setmtime(workname, mtime) != 0
1182 : workstat.st_mode == newworkmode ? 0
1184 : fchmod(Ifileno(workptr), newworkmode) == 0 ? 0
1189 : chmod(workname, newworkmode)
1195 xpandfile(unexfile, delta, exname, dolog)
1197 struct hshentry const *delta;
1198 char const **exname;
1201 * Read unexfile and copy it to a
1202 * file, performing keyword substitution with data from delta.
1203 * Return -1 if unsuccessful, 1 if expansion occurred, 0 otherwise.
1204 * If successful, stores the stream descriptor into *EXFILEP
1205 * and its name into *EXNAME.
1208 char const *targetname;
1211 targetname = makedirtemp(1);
1212 if (!(exfile = fopenSafer(targetname, FOPEN_W_WORK))) {
1214 workerror("can't build working file");
1218 if (MIN_UNEXPAND <= Expand)
1219 fastcopy(unexfile,exfile);
1223 unexfile, exfile, delta, false, (FILE*)0, dolog
1232 *exname = targetname;
1239 /* --------------------- G E T L O G M S G --------------------------------*/
1244 /* Obtain and yield a log message.
1245 * If a log message is given with -m, yield that message.
1246 * If this is the initial revision, yield a standard log message.
1247 * Otherwise, reads a character string from the terminal.
1248 * Stops after reading EOF or a single '.' on a
1249 * line. getlogmsg prompts the first time it is called for the
1250 * log message; during all later calls it asks whether the previous
1251 * log message can be reused.
1255 emptych[] = EMPTYLOG,
1256 initialch[] = "Initial revision";
1257 static struct cbuf const
1258 emptylog = { emptych, sizeof(emptych)-sizeof(char) },
1259 initiallog = { initialch, sizeof(initialch)-sizeof(char) };
1260 static struct buf logbuf;
1261 static struct cbuf logmsg;
1267 if (msg.size) return msg;
1270 /* generate std. log message */
1271 caller = getcaller();
1272 i = sizeof(ciklog)+strlen(caller)+3;
1273 bufalloc(&logbuf, i + datesize + zonelenmax);
1275 VOID sprintf(tp, "%s%s at ", ciklog, caller);
1276 VOID date2str(getcurdate(), tp+i);
1278 logmsg.size = strlen(tp);
1282 if (!targetdelta && (
1283 cmpnum(newdelnum.string,"1.1")==0 ||
1284 cmpnum(newdelnum.string,"1.0")==0
1289 /*previous log available*/
1290 if (yesorno(true, "reuse log message of previous file? [yn](y): "))
1294 /* now read string from stdin */
1295 logmsg = getsstdin("m", "log message", "", &logbuf);
1297 /* now check whether the log message is not empty */
1303 /* Make a linked list of Symbolic names */
1306 addassoclst(flag, sp)
1312 pt = talloc(struct Symrev);
1314 pt->override = flag;
1317 nextassoc = &pt->nextsym;