1 /* Copyright (C) 1982, 1988, 1989 Walter Tichy
4 * Redistribution and use in source and binary forms are permitted
5 * provided that the above copyright notice and this paragraph are
6 * duplicated in all such forms and that any documentation,
7 * advertising materials, and other materials related to such
8 * distribution and use acknowledge that the software was developed
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 * Report all problems and direct all questions to:
15 * rcs-bugs@cs.purdue.edu
27 * RCS checkout operation
31 "$Header: /pub/NetBSD/misc/repositories/cvsroot/src/usr.bin/rcs/src/Attic/co.c,v 1.1 1993/03/21 09:58:06 cgd Exp $ Purdue CS";
33 /*****************************************************************************
34 * check out revisions from RCS files
35 *****************************************************************************
40 * Revision 4.7 89/05/01 15:11:41 narten
41 * changed copyright header to reflect current distribution rules
43 * Revision 4.6 88/11/08 12:02:31 narten
44 * changes from eggert@sm.unisys.com (Paul Eggert)
46 * Revision 4.6 88/08/09 19:12:15 eggert
47 * Fix "co -d" core dump; rawdate wasn't always initialized.
48 * Use execv(), not system(); fix putchar('\0') and diagnose() botches; remove lint
50 * Revision 4.5 87/12/18 11:35:40 narten
51 * lint cleanups (from Guy Harris)
53 * Revision 4.4 87/10/18 10:20:53 narten
54 * Updating version numbers changes relative to 1.1, are actually
57 * Revision 1.3 87/09/24 13:58:30 narten
58 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
61 * Revision 1.2 87/03/27 14:21:38 jenkins
64 * Revision 1.1 84/01/23 14:49:58 kcs
67 * Revision 4.2 83/12/05 13:39:48 wft
68 * made rewriteflag external.
70 * Revision 4.1 83/05/10 16:52:55 wft
71 * Added option -u and -f.
72 * Added handling of default branch.
73 * Replaced getpwuid() with getcaller().
74 * Removed calls to stat(); now done by pairfilenames().
75 * Changed and renamed rmoldfile() to rmworkfile().
76 * Replaced catchints() calls with restoreints(), unlink()--link() with rename();
78 * Revision 3.7 83/02/15 15:27:07 wft
79 * Added call to fastcopy() to copy remainder of RCS file.
81 * Revision 3.6 83/01/15 14:37:50 wft
82 * Added ignoring of interrupts while RCS file is renamed; this avoids
83 * deletion of RCS files during the unlink/link window.
85 * Revision 3.5 82/12/08 21:40:11 wft
86 * changed processing of -d to use DATEFORM; removed actual from
87 * call to preparejoin; re-fixed printing of done at the end.
89 * Revision 3.4 82/12/04 18:40:00 wft
90 * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE.
91 * Fixed printing of "done".
93 * Revision 3.3 82/11/28 22:23:11 wft
94 * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
95 * %02d with %.2d, mode generation for working file with WORKMODE.
96 * Fixed nil printing. Fixed -j combined with -l and -p, and exit
97 * for non-existing revisions in preparejoin().
99 * Revision 3.2 82/10/18 20:47:21 wft
100 * Mode of working file is now maintained even for co -l, but write permission
102 * The working file inherits its mode from the RCS file, plus write permission
103 * for the owner. The write permission is not given if locking is strict and
105 * An existing working file without write permission is deleted automatically.
106 * Otherwise, co asks (empty answer: abort co).
107 * Call to getfullRCSname() added, check for write error added, call
108 * for getlogin() fixed.
110 * Revision 3.1 82/10/13 16:01:30 wft
111 * fixed type of variables receiving from getc() (char -> int).
112 * removed unused variables.
120 #include <sys/types.h>
121 #include <sys/stat.h>
124 static char rcsbaseid
[] = RCSBASE
;
126 static char co
[] = CO
;
127 static char merge
[] = MERGE
;
129 extern FILE * fopen();
131 extern char * getcaller(); /*get login of caller */
132 extern struct hshentry
* genrevs(); /*generate delta numbers */
133 extern char * getancestor();
134 extern int nextc
; /*next input character */
135 extern int nerror
; /*counter for errors */
136 extern char Kdesc
[]; /*keyword for description */
137 extern char * buildrevision(); /*constructs desired revision */
138 extern int buildjoin(); /*join several revisions */
139 extern char * mktempfile(); /*temporary file name generator */
140 extern struct hshentry
* findlock();/*find (and delete) a lock */
141 extern struct lock
* addlock(); /*add a new lock */
142 extern long maketime(); /*convert parsed time to unix time. */
143 extern struct tm
* localtime(); /*convert unixtime into a tm-structure */
144 extern FILE * finptr
; /* RCS input file */
145 extern FILE * frewrite
; /* new RCS file */
146 extern int rewriteflag
; /* indicates whether input should be */
147 /* echoed to frewrite */
149 char * newRCSfilename
, * neworkfilename
;
150 char * RCSfilename
, * workfilename
;
151 extern struct stat RCSstat
, workstat
; /* file status of RCS and work file */
152 extern int haveRCSstat
, haveworkstat
;/* status indicators */
154 char * date
, * rev
, * state
, * author
, * join
;
155 char finaldate
[datelength
];
157 int forceflag
, lockflag
, unlockflag
, tostdout
;
158 char * caller
; /* caller's login; */
161 char numericrev
[revlength
]; /* holds expanded revision number */
162 struct hshentry
* gendeltas
[hshsize
]; /* stores deltas to be generated */
163 struct hshentry
* targetdelta
; /* final delta to be generated */
165 char * joinlist
[joinlength
]; /* pointers to revisions to be joined */
166 int lastjoin
; /* index of last element in joinlist */
172 int killock
; /* indicates whether a lock is removed*/
174 struct tm parseddate
, *ftm
;
180 cmdusage
= "command format:\nco -f[rev] -l[rev] -p[rev] -q[rev] -r[rev] -ddate -sstate -w[login] -jjoinlist file ...";
181 date
= rev
= state
= author
= join
= nil
;
182 forceflag
= lockflag
= unlockflag
= tostdout
= quietflag
= false;
186 while (--argc
,++argv
, argc
>=1 && ((*argv
)[0] == '-')) {
187 switch ((*argv
)[1]) {
190 revno
: if ((*argv
)[2]!='\0') {
191 if (rev
!=nil
) warn("Redefinition of revision number");
203 warn("-l has precedence over -u");
211 warn("-l has precedence over -u");
225 if ((*argv
)[2]!='\0') {
226 if (date
!=nil
) warn("Redefinition of -d option");
229 /* process date/time */
230 if (partime(rawdate
,&parseddate
)==0)
231 faterror("Can't parse date/time: %s",rawdate
);
232 if ((unixtime
=maketime(&parseddate
))== 0L)
233 faterror("Inconsistent date/time: %s",rawdate
);
234 ftm
=localtime(&unixtime
);
235 VOID
sprintf(finaldate
,DATEFORM
,
236 ftm
->tm_year
,ftm
->tm_mon
+1,ftm
->tm_mday
,ftm
->tm_hour
,ftm
->tm_min
,ftm
->tm_sec
);
241 if ((*argv
)[2]!='\0'){
242 if (join
!=nil
)warn("Redefinition of -j option");
248 if ((*argv
)[2]!='\0'){
249 if (state
!=nil
)warn("Redefinition of -s option");
255 if (author
!=nil
)warn("Redefinition of -w option");
256 if ((*argv
)[2]!='\0')
258 else author
= caller
;
262 faterror("unknown option: %s\n%s", *argv
,cmdusage
);
265 } /* end of option processing */
267 if (argc
<1) faterror("No input file\n%s",cmdusage
);
269 /* now handle all filenames */
272 finptr
=frewrite
=NULL
;
275 if (!pairfilenames(argc
,argv
,true,tostdout
)) continue;
277 /* now RCSfilename contains the name of the RCS file, and finptr
278 * the file descriptor. If tostdout is false, workfilename contains
279 * the name of the working file, otherwise undefined (not nil!).
280 * Also, RCSstat, workstat, and haveworkstat have been set.
282 diagnose("%s --> %s", RCSfilename
,tostdout
?"stdout":workfilename
);
285 if (!tostdout
&& !trydiraccess(workfilename
)) continue; /* give up */
286 if ((lockflag
||unlockflag
) && !checkaccesslist(caller
)) continue; /* give up */
287 if (!trysema(RCSfilename
,lockflag
||unlockflag
)) continue; /* give up */
290 gettree(); /* reads in the delta tree */
293 /* no revisions; create empty file */
294 diagnose("no revisions present; generating empty revision 0.0");
296 if (!creatempty()) continue;
297 /* Can't reserve a delta, so don't call addlock */
300 /* expand symbolic revision number */
301 if (!expandsym(rev
,numericrev
))
303 } elsif (unlockflag
&& (targetdelta
=findlock(caller
,false))!=nil
) {
304 VOID
strcpy(numericrev
,targetdelta
->num
);
305 } elsif (Dbranch
!=nil
) {
306 VOID
strcpy(numericrev
,Dbranch
->num
);
307 } else numericrev
[0]='\0'; /* empty */
308 /* get numbers of deltas to be generated */
309 if (!(targetdelta
=genrevs(numericrev
,date
,author
,state
,gendeltas
)))
311 /* check reservations */
312 if (lockflag
&& !addlock(targetdelta
,caller
))
316 if((killock
=rmlock(caller
,targetdelta
))== -1)
322 if (join
&& !preparejoin()) continue;
324 diagnose("revision %s%s",targetdelta
->num
,
325 lockflag
?" (locked)":
326 unlockflag
?" (unlocked)":"");
328 /* remove old working file if necessary */
330 if (!rmworkfile()) continue;
332 /* prepare for rewriting the RCS file */
333 if (lockflag
||(killock
==1)) {
334 newRCSfilename
=mktempfile(RCSfilename
,NEWRCSFILE
);
335 if ((frewrite
=fopen(newRCSfilename
, "w"))==NULL
) {
336 error("Can't open file %s",newRCSfilename
);
340 puttree(Head
,frewrite
);
341 VOID
fprintf(frewrite
, "\n\n%s%c",Kdesc
,nextc
);
345 /* skip description */
346 getdesc(false); /* don't echo*/
348 if (!(neworkfilename
=buildrevision(gendeltas
,targetdelta
,
349 tostdout
?(join
!=nil
?"/tmp/":(char *)nil
):workfilename
,true)))
352 if ((lockflag
||killock
==1)&&nerror
==0) {
353 /* rewrite the rest of the RCSfile */
354 fastcopy(finptr
,frewrite
);
355 ffclose(frewrite
); frewrite
=NULL
;
357 if (rename(newRCSfilename
,RCSfilename
)<0) {
358 error("Can't rewrite %s; saved in: %s",
359 RCSfilename
, newRCSfilename
);
360 newRCSfilename
[0]='\0'; /* avoid deletion*/
364 newRCSfilename
[0]='\0'; /* avoid re-deletion by cleanup()*/
365 if (chmod(RCSfilename
,RCSstat
.st_mode
& ~0222)<0)
366 warn("Can't preserve mode of %s",RCSfilename
);
371 logcommand("co",targetdelta
,gendeltas
,caller
);
375 rmsema(); /* kill semaphore file so other co's can proceed */
376 if (!buildjoin(neworkfilename
)) continue;
379 if (rename(neworkfilename
,workfilename
) <0) {
380 error("Can't create %s; see %s",workfilename
,neworkfilename
);
381 neworkfilename
[0]= '\0'; /*avoid deletion*/
384 neworkfilename
[0]= '\0'; /*avoid re-deletion by cleanup()*/
388 if (chmod(workfilename
, WORKMODE(RCSstat
.st_mode
))<0)
389 warn("Can't adjust mode of %s",workfilename
);
392 if (!tostdout
) diagnose("done");
398 } /* end of main (co) */
401 /*****************************************************************
402 * The following routines are auxiliary routines
403 *****************************************************************/
406 /* Function: unlinks workfilename, if it exists, under the following conditions:
407 * If it is read-only, workfilename is unlinked.
408 * Otherwise (file writable):
409 * if !quietmode asks the user whether to really delete it (default: fail);
411 * Returns false on failure to unlink, true otherwise.
414 int response
, c
; /* holds user response to queries */
416 if (haveworkstat
< 0) /* File doesn't exist; set by pairfilenames*/
417 return (true); /* No problem */
419 if ((workstat
.st_mode
& 0222)&&!forceflag
) { /* File is writable */
421 VOID
fprintf(stderr
,"writable %s exists; overwrite? [ny](n): ",workfilename
);
422 /* must be stderr in case of IO redirect */
423 c
=response
=getchar();
424 while (!(c
==EOF
|| c
=='\n')) c
=getchar(); /*skip rest*/
425 if (!(response
=='y'||response
=='Y')) {
426 warn("checkout aborted.");
430 error("writable %s exists; checkout aborted.",workfilename
);
434 /* now unlink: either not writable, forceflag, or permission given */
435 if (unlink(workfilename
) != 0) { /* Remove failed */
436 error("Can't unlink %s",workfilename
);
444 /* Function: creates an empty working file.
445 * First, removes an existing working file with rmworkfile().
448 int fdesc
; /* file descriptor */
450 if (!rmworkfile()) return false;
451 fdesc
=creat(workfilename
,0);
453 faterror("Cannot create %s",workfilename
);
456 VOID
close(fdesc
); /* empty file */
462 int rmlock(who
,delta
)
463 char * who
; struct hshentry
* delta
;
464 /* Function: removes the lock held by who on delta.
465 * Returns -1 if someone else holds the lock,
466 * 0 if there is no lock on delta,
467 * and 1 if a lock was found and removed.
469 { register struct lock
* next
, * trail
;
472 int whomatch
, nummatch
;
475 dummy
.nextlock
=next
=Locks
;
478 whomatch
=strcmp(who
,next
->login
);
479 nummatch
=strcmp(num
,next
->delta
->num
);
480 if ((whomatch
==0) && (nummatch
==0)) break;
481 /*found a lock on delta by who*/
482 if ((whomatch
!=0)&&(nummatch
==0)) {
483 error("revision %s locked by %s; use co -r or rcs -u",num
,next
->login
);
490 /*found one; delete it */
491 trail
->nextlock
=next
->nextlock
;
492 Locks
=dummy
.nextlock
;
493 next
->delta
->lockedby
=nil
; /* reset locked-by */
494 return 1; /*success*/
495 } else return 0; /*no lock on delta*/
501 /*****************************************************************
502 * The rest of the routines are for handling joins
503 *****************************************************************/
505 char * getrev(sp
, tp
, buffsize
)
506 register char * sp
, *tp
; int buffsize
;
507 /* Function: copies a symbolic revision number from sp to tp,
508 * appends a '\0', and returns a pointer to the character following
509 * the revision number; returns nil if the revision number is more than
510 * buffsize characters long.
511 * The revision number is terminated by space, tab, comma, colon,
512 * semicolon, newline, or '\0'.
513 * used for parsing the -j option.
520 while (((c
= *sp
)!=' ')&&(c
!='\t')&&(c
!='\n')&&(c
!=':')&&(c
!=',')
521 &&(c
!=';')&&(c
!='\0')) {
522 if (length
>=buffsize
) return false;
533 /* Function: Parses a join list pointed to by join and places pointers to the
534 * revision numbers into joinlist.
537 struct hshentry
* * joindeltas
;
538 struct hshentry
* tmpdelta
;
540 char symbolrev
[revlength
],numrev
[revlength
];
542 joindeltas
= (struct hshentry
* *)talloc(hshsize
*sizeof(struct hshentry
*));
546 while ((*j
==' ')||(*j
=='\t')||(*j
==',')) j
++;
548 if (lastjoin
>=joinlength
-2) {
549 error("too many joins");
552 if(!(j
=getrev(j
,symbolrev
,revlength
))) return false;
553 if (!expandsym(symbolrev
,numrev
)) return false;
554 tmpdelta
=genrevs(numrev
,(char *)nil
,(char *)nil
,(char *)nil
,(struct hshentry
* *)joindeltas
);
557 else joinlist
[++lastjoin
]=tmpdelta
->num
;
558 while ((*j
==' ') || (*j
=='\t')) j
++;
561 while((*j
==' ') || (*j
=='\t')) j
++;
563 if(!(j
=getrev(j
,symbolrev
,revlength
))) return false;
564 if (!expandsym(symbolrev
,numrev
)) return false;
565 tmpdelta
=genrevs(numrev
,(char *)nil
,(char *)nil
,(char *)nil
, (struct hshentry
* *) joindeltas
);
568 else joinlist
[++lastjoin
]=tmpdelta
->num
;
570 error("join pair incomplete");
574 if (lastjoin
==0) { /* first pair */
575 /* common ancestor missing */
576 joinlist
[1]=joinlist
[0];
578 /*derive common ancestor*/
579 joinlist
[0]=talloc(revlength
);
580 if (!getancestor(targetdelta
->num
,joinlist
[1],joinlist
[0]))
583 error("join pair incomplete");
596 buildjoin(initialfile
)
598 /* Function: merge pairs of elements in joinlist into initialfile
599 * If tostdout==true, copy result to stdout.
600 * All unlinking of initialfile, rev2, and rev3 should be done by cleanup().
603 char commarg
[revlength
+3];
604 char subs
[revlength
];
608 rev2
=mktempfile("/tmp/",JOINFIL2
);
609 rev3
=mktempfile("/tmp/",JOINFIL3
);
613 /*prepare marker for merge*/
615 VOID
strcpy(subs
,targetdelta
->num
);
616 else VOID
sprintf(subs
, "merge%d",i
/2);
617 diagnose("revision %s",joinlist
[i
]);
618 VOID
sprintf(commarg
,"-p%s",joinlist
[i
]);
619 if (run((char*)nil
,rev2
, co
,commarg
,"-q",RCSfilename
,(char*)nil
)) {
620 nerror
++;return false;
622 diagnose("revision %s",joinlist
[i
+1]);
623 VOID
sprintf(commarg
,"-p%s",joinlist
[i
+1]);
624 if (run((char *)nil
,rev3
, co
,commarg
,"-q",RCSfilename
,(char*)nil
)) {
625 nerror
++; return false;
627 diagnose("merging...");
629 (i
+2)>=lastjoin
&& tostdout
630 ? run((char*)nil
,(char*)nil
, merge
,"-p",initialfile
,rev2
,rev3
,subs
,joinlist
[i
+1],(char*)nil
)
631 : run((char*)nil
,(char*)nil
, merge
, initialfile
,rev2
,rev3
,subs
,joinlist
[i
+1],(char*)nil
)) {
632 nerror
++; return false;