head	5.9;
branch	5.9.2;
access;
symbols
	HWGRCSP7F:5.9.2.2
	HWGRCSP6F:5.9.2.2
	HWGRCSP5F:5.9.2.1
	HWGRCSp4:5.9.2.1
	HWGRCSp3:5.9.2
	HWGRCS_Fish:5.9.1
	HWGRCS:5.9.1.1;
locks; strict;
comment	@ * @;


5.9
date	91.10.07.17.32.46;	author eggert;	state Exp;
branches
	5.9.1.1
	5.9.2.1;
next	;

5.9.1.1
date	93.01.18.13.23.57;	author heinz;	state Exp;
branches;
next	5.9.1.2;

5.9.1.2
date	93.02.11.21.06.51;	author heinz;	state Exp;
branches;
next	5.9.1.3;

5.9.1.3
date	93.03.20.21.18.12;	author heinz;	state Exp;
branches;
next	5.9.1.4;

5.9.1.4
date	93.03.22.19.12.00;	author heinz;	state Exp;
branches;
next	;

5.9.2.1
date	93.04.13.20.07.54;	author heinz;	state Exp;
branches;
next	5.9.2.2;

5.9.2.2
date	93.12.18.17.17.10;	author heinz;	state Exp;
branches;
next	5.9.2.3;

5.9.2.3
date	94.02.14.19.29.45;	author heinz;	state Exp;
branches;
next	;


desc
@Checked in with -k 16.jan.93 HWG
@


5.9
log
@checked in with -k by heinz at 1993/01/17 01:53:17
@
text
@/* Copyright (C) 1982, 1988, 1989 Walter Tichy
   Copyright 1990, 1991 by Paul Eggert
   Distributed under license by the Free Software Foundation, Inc.

This file is part of RCS.

RCS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RCS; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

Report problems and direct all questions to:

    rcs-bugs@@cs.purdue.edu

*/

/*
 *                     RCS checkout operation
 */
/*****************************************************************************
 *                       check out revisions from RCS files
 *****************************************************************************
 */


/* $Log: co.c,v $
 * Revision 5.9  1991/10/07  17:32:46  eggert
 * ci -u src/RCS/co.c,v src/co.c <<\.
 * -k affects just working file, not RCS file.
 *
 * Revision 5.8  1991/08/19  03:13:55  eggert
 * Warn before removing somebody else's file.
 * Add -M.  Fix co -j bugs.  Tune.
 *
 * Revision 5.7  1991/04/21  11:58:15  eggert
 * Ensure that working file is newer than RCS file after co -[lu].
 * Add -x, RCSINIT, MS-DOS support.
 *
 * Revision 5.6  1990/12/04  05:18:38  eggert
 * Don't checkaccesslist() unless necessary.
 * Use -I for prompts and -q for diagnostics.
 *
 * Revision 5.5  1990/11/01  05:03:26  eggert
 * Fix -j.  Add -I.
 *
 * Revision 5.4  1990/10/04  06:30:11  eggert
 * Accumulate exit status across files.
 *
 * Revision 5.3  1990/09/11  02:41:09  eggert
 * co -kv yields a readonly working file.
 *
 * Revision 5.2  1990/09/04  08:02:13  eggert
 * Standardize yes-or-no procedure.
 *
 * Revision 5.0  1990/08/22  08:10:02  eggert
 * Permit multiple locks by same user.  Add setuid support.
 * Remove compile-time limits; use malloc instead.
 * Permit dates past 1999/12/31.  Switch to GMT.
 * Make lock and temp files faster and safer.
 * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
 *
 * Revision 4.7  89/05/01  15:11:41  narten
 * changed copyright header to reflect current distribution rules
 * 
 * Revision 4.6  88/08/09  19:12:15  eggert
 * Fix "co -d" core dump; rawdate wasn't always initialized.
 * Use execv(), not system(); fix putchar('\0') and diagnose() botches; remove lint
 * 
 * Revision 4.5  87/12/18  11:35:40  narten
 * lint cleanups (from Guy Harris)
 * 
 * Revision 4.4  87/10/18  10:20:53  narten
 * Updating version numbers changes relative to 1.1, are actually
 * relative to 4.2
 * 
 * Revision 1.3  87/09/24  13:58:30  narten
 * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
 * warnings)
 * 
 * Revision 1.2  87/03/27  14:21:38  jenkins
 * Port to suns
 * 
 * Revision 4.2  83/12/05  13:39:48  wft
 * made rewriteflag external.
 * 
 * Revision 4.1  83/05/10  16:52:55  wft
 * Added option -u and -f.
 * Added handling of default branch.
 * Replaced getpwuid() with getcaller().
 * Removed calls to stat(); now done by pairfilenames().
 * Changed and renamed rmoldfile() to rmworkfile().
 * Replaced catchints() calls with restoreints(), unlink()--link() with rename();
 * 
 * Revision 3.7  83/02/15  15:27:07  wft
 * Added call to fastcopy() to copy remainder of RCS file.
 *
 * Revision 3.6  83/01/15  14:37:50  wft
 * Added ignoring of interrupts while RCS file is renamed; this avoids
 * deletion of RCS files during the unlink/link window.
 *
 * Revision 3.5  82/12/08  21:40:11  wft
 * changed processing of -d to use DATEFORM; removed actual from
 * call to preparejoin; re-fixed printing of done at the end.
 *
 * Revision 3.4  82/12/04  18:40:00  wft
 * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE.
 * Fixed printing of "done".
 *
 * Revision 3.3  82/11/28  22:23:11  wft
 * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
 * %02d with %.2d, mode generation for working file with WORKMODE.
 * Fixed nil printing. Fixed -j combined with -l and -p, and exit
 * for non-existing revisions in preparejoin().
 *
 * Revision 3.2  82/10/18  20:47:21  wft
 * Mode of working file is now maintained even for co -l, but write permission
 * is removed.
 * The working file inherits its mode from the RCS file, plus write permission
 * for the owner. The write permission is not given if locking is strict and
 * co does not lock.
 * An existing working file without write permission is deleted automatically.
 * Otherwise, co asks (empty answer: abort co).
 * Call to getfullRCSname() added, check for write error added, call
 * for getlogin() fixed.
 *
 * Revision 3.1  82/10/13  16:01:30  wft
 * fixed type of variables receiving from getc() (char -> int).
 * removed unused variables.
 */




#include "rcsbase.h"

static char const *getancestor P((char const*,char const*));
static int buildjoin P((char const*));
static int preparejoin P((void));
static int rmlock P((struct hshentry const*));
static int rmworkfile P((void));
static void cleanup P((void));

static char const quietarg[] = "-q";

static char const *expandarg, *join, *suffixarg, *versionarg;
static char const *joinlist[joinlength]; /* revisions to be joined */
static FILE *neworkptr;
static int exitstatus;
static int forceflag;
static int lastjoin;			/* index of last element in joinlist  */
static int lockflag; /* -1 -> unlock, 0 -> do nothing, 1 -> lock */
static int mtimeflag;
static struct hshentries *gendeltas;	/* deltas to be generated	*/
static struct hshentry *targetdelta;	/* final delta to be generated	*/
static struct stat workstat;

mainProg(coId, "co", "$Id: co.c,v 5.9 1991/10/07 17:32:46 eggert Exp $")
{
	static char const cmdusage[] =
		"\nco usage: co -{flpqru}[rev] -ddate -jjoinlist -sstate -w[login] -Vn file ...";

	char *a, **newargv;
	char const *author, *date, *rev, *state;
	char const *joinfilename, *newdate, *neworkfilename;
	int changelock;  /* 1 if a lock has been changed, -1 if error */
	int expmode, r, tostdout, workstatstat;
	struct buf numericrev;	/* expanded revision number	*/
	char finaldate[datesize];

	setrid();
	author = date = rev = state = nil;
	bufautobegin(&numericrev);
	expmode = -1;
	suffixes = X_DEFAULT;
	tostdout = false;

	argc = getRCSINIT(argc, argv, &newargv);
	argv = newargv;
	while (a = *++argv,  0<--argc && *a++=='-') {
		switch (*a++) {

                case 'r':
		revno:
			if (*a) {
				if (rev) warn("redefinition of revision number");
				rev = a;
                        }
                        break;

		case 'f':
			forceflag=true;
			goto revno;

                case 'l':
			if (lockflag < 0) {
                                warn("-l overrides -u.");
                        }
			lockflag = 1;
                        goto revno;

                case 'u':
			if (0 < lockflag) {
                                warn("-l overrides -u.");
                        }
			lockflag = -1;
                        goto revno;

                case 'p':
			tostdout = true;
                        goto revno;

		case 'I':
			interactiveflag = true;
			goto revno;

                case 'q':
                        quietflag=true;
                        goto revno;

                case 'd':
			if (date)
				redefined('d');
			str2date(a, finaldate);
                        date=finaldate;
                        break;

                case 'j':
			if (*a) {
				if (join) redefined('j');
				join = a;
                        }
                        break;

		case 'M':
			mtimeflag = true;
			goto revno;

                case 's':
			if (*a) {
				if (state) redefined('s');
				state = a;
                        }
                        break;

                case 'w':
			if (author) redefined('w');
			if (*a)
				author = a;
			else
				author = getcaller();
                        break;

		case 'x':
			suffixarg = *argv;
			suffixes = a;
			break;

		case 'V':
			versionarg = *argv;
			setRCSversion(versionarg);
			break;

		case 'k':    /*  set keyword expand mode  */
			expandarg = *argv;
			if (0 <= expmode) redefined('k');
			if (0 <= (expmode = str2expmode(a)))
			    break;
			/* fall into */
                default:
			faterror("unknown option: %s%s", *argv, cmdusage);

                };
        } /* end of option processing */

	if (argc<1) faterror("no input file%s", cmdusage);
	if (tostdout)
#	    if text_equals_binary_stdio || text_work_stdio
		workstdout = stdout;
#	    else
		if (!(workstdout = fdopen(STDOUT_FILENO, FOPEN_W_WORK)))
		    efaterror("stdout");
#	    endif

        /* now handle all filenames */
        do {
	ffree();

	if (pairfilenames(argc, argv, lockflag?rcswriteopen:rcsreadopen, true, false)  <=  0)
		continue;

        /* now RCSfilename contains the name of the RCS file, and finptr
	 * points at it.  workfilename contains the name of the working file.
	 * Also, RCSstat has been set.
         */
	diagnose("%s  -->  %s\n", RCSfilename,tostdout?"stdout":workfilename);

	workstatstat = -1;
	if (tostdout) {
		neworkfilename = 0;
		neworkptr = workstdout;
	} else {
		workstatstat = stat(workfilename, &workstat);
		neworkfilename = makedirtemp(workfilename, 1);
		if (!(neworkptr = fopen(neworkfilename, FOPEN_W_WORK))) {
			if (errno == EACCES)
				error("%s: parent directory isn't writable",
					workfilename
				);
			else
				eerror(neworkfilename);
			continue;
		}
	}

        gettree();  /* reads in the delta tree */

        if (Head==nil) {
                /* no revisions; create empty file */
		diagnose("no revisions present; generating empty revision 0.0\n");
		Ozclose(&fcopy);
		if (workstatstat == 0)
			if (!rmworkfile()) continue;
		changelock = 0;
		newdate = 0;
                /* Can't reserve a delta, so don't call addlock */
        } else {
                if (rev!=nil) {
                        /* expand symbolic revision number */
			if (!expandsym(rev, &numericrev))
                                continue;
		} else
			switch (lockflag<0 ? findlock(false,&targetdelta) : 0) {
			    default:
				continue;
			    case 0:
				bufscpy(&numericrev, Dbranch?Dbranch:"");
				break;
			    case 1:
				bufscpy(&numericrev, targetdelta->num);
				break;
			}
                /* get numbers of deltas to be generated */
		if (!(targetdelta=genrevs(numericrev.string,date,author,state,&gendeltas)))
                        continue;
                /* check reservations */
		changelock =
			lockflag < 0 ?
				rmlock(targetdelta)
			: lockflag == 0 ?
				0
			:
				addlock(targetdelta);

		if (
			changelock < 0 ||
			changelock && !checkaccesslist() ||
			!dorewrite(lockflag, changelock)
		)
			continue;

		if (0 <= expmode)
			Expand = expmode;
		if (0 < lockflag  &&  Expand == VAL_EXPAND) {
			error("cannot combine -kv and -l");
			continue;
		}

                if (join && !preparejoin()) continue;

		diagnose("revision %s%s\n",targetdelta->num,
			 0<lockflag ? " (locked)" :
			 lockflag<0 ? " (unlocked)" : "");

		/* Prepare to remove old working file if necessary.  */
		if (workstatstat == 0)
                        if (!rmworkfile()) continue;

                /* skip description */
                getdesc(false); /* don't echo*/

		locker_expansion = 0 < lockflag;
		joinfilename = buildrevision(
			gendeltas, targetdelta,
			join&&tostdout ? (FILE*)0 : neworkptr,
			Expand!=OLD_EXPAND
		);
#		if !large_memory
			if (fcopy == neworkptr)
				fcopy = 0;  /* Don't close it twice.  */
#		endif
		if_advise_access(changelock && gendeltas->first!=targetdelta,
			finptr, MADV_SEQUENTIAL
		);

		if (!donerewrite(changelock))
			continue;

		newdate = targetdelta->date;
		if (join) {
			newdate = 0;
			if (!joinfilename) {
				aflush(neworkptr);
				joinfilename = neworkfilename;
			}
			if (!buildjoin(joinfilename))
				continue;
		}
        }
	if (!tostdout) {
	    r = 0;
	    if (mtimeflag && newdate) {
		if (!join)
		    aflush(neworkptr);
		r = setfiledate(neworkfilename, newdate);
	    }
	    if (r == 0) {
		ignoreints();
		r = chnamemod(&neworkptr, neworkfilename, workfilename,
		  WORKMODE(RCSstat.st_mode,
		    !(Expand==VAL_EXPAND || lockflag<=0&&StrictLocks)
		  )
		);
		keepdirtemp(neworkfilename);
		restoreints();
	    }
	    if (r != 0) {
		eerror(workfilename);
		error("see %s", neworkfilename);
		continue;
	    }
	    diagnose("done\n");
	}
        } while (cleanup(),
                 ++argv, --argc >=1);

	tempunlink();
	Ofclose(workstdout);
	exitmain(exitstatus);

}       /* end of main (co) */

	static void
cleanup()
{
	if (nerror) exitstatus = EXIT_FAILURE;
	Izclose(&finptr);
	Ozclose(&frewrite);
#	if !large_memory
		if (fcopy!=workstdout) Ozclose(&fcopy);
#	endif
	if (neworkptr!=workstdout) Ozclose(&neworkptr);
	dirtempunlink();
}

#if lint
#	define exiterr coExit
#endif
	exiting void
exiterr()
{
	dirtempunlink();
	tempunlink();
	_exit(EXIT_FAILURE);
}


/*****************************************************************
 * The following routines are auxiliary routines
 *****************************************************************/

	static int
rmworkfile()
/* Function: prepares to remove workfilename, if it exists, and if
 * it is read-only.
 * Otherwise (file writable):
 *   if !quietmode asks the user whether to really delete it (default: fail);
 *   otherwise failure.
 * Returns true if permission is gotten.
 */
{
	if (workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH) && !forceflag) {
	    /* File is writable */
	    if (!yesorno(false, "writable %s exists%s; remove it? [ny](n): ",
			workfilename,
			myself(workstat.st_uid) ? "" : ", and you do not own it"
	    )) {
		error(!quietflag && ttystdin()
			? "checkout aborted"
			: "writable %s exists; checkout aborted", workfilename);
		return false;
            }
        }
	/* Actual unlink is done later by caller. */
	return true;
}


	static int
rmlock(delta)
	struct hshentry const *delta;
/* Function: removes the lock held by caller on delta.
 * Returns -1 if someone else holds the lock,
 * 0 if there is no lock on delta,
 * and 1 if a lock was found and removed.
 */
{       register struct lock * next, * trail;
	char const *num;
        struct lock dummy;
        int whomatch, nummatch;

        num=delta->num;
        dummy.nextlock=next=Locks;
        trail = &dummy;
        while (next!=nil) {
		whomatch = strcmp(getcaller(), next->login);
                nummatch=strcmp(num,next->delta->num);
                if ((whomatch==0) && (nummatch==0)) break;
			/*found a lock on delta by caller*/
                if ((whomatch!=0)&&(nummatch==0)) {
                    error("revision %s locked by %s; use co -r or rcs -u",num,next->login);
                    return -1;
                }
                trail=next;
                next=next->nextlock;
        }
        if (next!=nil) {
                /*found one; delete it */
                trail->nextlock=next->nextlock;
                Locks=dummy.nextlock;
                next->delta->lockedby=nil; /* reset locked-by */
                return 1; /*success*/
        } else  return 0; /*no lock on delta*/
}




/*****************************************************************
 * The rest of the routines are for handling joins
 *****************************************************************/


	static char const *
addjoin(joinrev)
	char *joinrev;
/* Add joinrev's number to joinlist, yielding address of char past joinrev,
 * or nil if no such revision exists.
 */
{
	register char *j;
	register struct hshentry const *d;
	char terminator;
	struct buf numrev;
	struct hshentries *joindeltas;

	j = joinrev;
	for (;;) {
	    switch (*j++) {
		default:
		    continue;
		case 0:
		case ' ': case '\t': case '\n':
		case ':': case ',': case ';':
		    break;
	    }
	    break;
	}
	terminator = *--j;
	*j = 0;
	bufautobegin(&numrev);
	d = 0;
	if (expandsym(joinrev, &numrev))
	    d = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&joindeltas);
	bufautoend(&numrev);
	*j = terminator;
	if (d) {
		joinlist[++lastjoin] = d->num;
		return j;
	}
	return nil;
}

	static int
preparejoin()
/* Function: Parses a join list pointed to by join and places pointers to the
 * revision numbers into joinlist.
 */
{
	register char const *j;

        j=join;
        lastjoin= -1;
        for (;;) {
                while ((*j==' ')||(*j=='\t')||(*j==',')) j++;
                if (*j=='\0') break;
                if (lastjoin>=joinlength-2) {
                        error("too many joins");
                        return(false);
                }
		if (!(j = addjoin(j))) return false;
                while ((*j==' ') || (*j=='\t')) j++;
                if (*j == ':') {
                        j++;
                        while((*j==' ') || (*j=='\t')) j++;
                        if (*j!='\0') {
				if (!(j = addjoin(j))) return false;
                        } else {
                                error("join pair incomplete");
                                return false;
                        }
                } else {
                        if (lastjoin==0) { /* first pair */
                                /* common ancestor missing */
                                joinlist[1]=joinlist[0];
                                lastjoin=1;
                                /*derive common ancestor*/
				if (!(joinlist[0] = getancestor(targetdelta->num,joinlist[1])))
                                       return false;
                        } else {
                                error("join pair incomplete");
                                return false;
                        }
                }
        }
        if (lastjoin<1) {
                error("empty join");
                return false;
        } else  return true;
}



	static char const *
getancestor(r1, r2)
	char const *r1, *r2;
/* Yield the common ancestor of r1 and r2 if successful, nil otherwise.
 * Work reliably only if r1 and r2 are not branch numbers.
 */
{
	static struct buf t1, t2;

	unsigned l1, l2, l3;
	char const *r;

	l1 = countnumflds(r1);
	l2 = countnumflds(r2);
	if ((2<l1 || 2<l2)  &&  cmpnum(r1,r2)!=0) {
	    /* not on main trunk or identical */
	    l3 = 0;
	    while (cmpnumfld(r1, r2, l3+1)==0 && cmpnumfld(r1, r2, l3+2)==0)
		l3 += 2;
	    /* This will terminate since r1 and r2 are not the same; see above. */
	    if (l3==0) {
		/* no common prefix; common ancestor on main trunk */
		VOID partialno(&t1, r1, l1>2 ? (unsigned)2 : l1);
		VOID partialno(&t2, r2, l2>2 ? (unsigned)2 : l2);
		r = cmpnum(t1.string,t2.string)<0 ? t1.string : t2.string;
		if (cmpnum(r,r1)!=0 && cmpnum(r,r2)!=0)
			return r;
	    } else if (cmpnumfld(r1, r2, l3+1)!=0)
			return partialno(&t1,r1,l3);
	}
	error("common ancestor of %s and %s undefined", r1, r2);
	return nil;
}



	static int
buildjoin(initialfile)
	char const *initialfile;
/* Function: merge pairs of elements in joinlist into initialfile
 * If workstdout is set, copy result to stdout.
 * All unlinking of initialfile, rev2, and rev3 should be done by tempunlink().
 */
{
	struct buf commarg;
	struct buf subs;
	char const *rev2, *rev3;
        int i;
	char const *cov[10], *mergev[12];
	char const **p;

	bufautobegin(&commarg);
	bufautobegin(&subs);
	rev2 = maketemp(0);
	rev3 = maketemp(3); /* buildrevision() may use 1 and 2 */

	cov[0] = nil;
	/* cov[1] setup below */
	cov[2] = CO;
	/* cov[3] setup below */
	p = &cov[4];
	if (expandarg) *p++ = expandarg;
	if (suffixarg) *p++ = suffixarg;
	if (versionarg) *p++ = versionarg;
	*p++ = quietarg;
	*p++ = RCSfilename;
	*p = nil;

	mergev[0] = nil;
	mergev[1] = nil;
	mergev[2] = MERGE;
	mergev[3] = mergev[5] = "-L";
	/* rest of mergev setup below */

        i=0;
        while (i<lastjoin) {
                /*prepare marker for merge*/
                if (i==0)
			bufscpy(&subs, targetdelta->num);
		else {
			bufscat(&subs, ",");
			bufscat(&subs, joinlist[i-2]);
			bufscat(&subs, ":");
			bufscat(&subs, joinlist[i-1]);
		}
		diagnose("revision %s\n",joinlist[i]);
		bufscpy(&commarg, "-p");
		bufscat(&commarg, joinlist[i]);
		cov[1] = rev2;
		cov[3] = commarg.string;
		if (runv(cov))
			goto badmerge;
		diagnose("revision %s\n",joinlist[i+1]);
		bufscpy(&commarg, "-p");
		bufscat(&commarg, joinlist[i+1]);
		cov[1] = rev3;
		cov[3] = commarg.string;
		if (runv(cov))
			goto badmerge;
		diagnose("merging...\n");
		mergev[4] = subs.string;
		mergev[6] = joinlist[i+1];
		p = &mergev[7];
		if (quietflag) *p++ = quietarg;
		if (lastjoin<=i+2 && workstdout) *p++ = "-p";
		*p++ = initialfile;
		*p++ = rev2;
		*p++ = rev3;
		*p = nil;
		switch (runv(mergev)) {
		    case DIFF_FAILURE: case DIFF_SUCCESS:
			break;
		    default:
			goto badmerge;
		}
                i=i+2;
        }
	bufautoend(&commarg);
	bufautoend(&subs);
        return true;

    badmerge:
	nerror++;
	bufautoend(&commarg);
	bufautoend(&subs);
	return false;
}
@


5.9.2.1
log
@Fix for -j.
@
text
@a36 14
 * Revision 5.9.1.4  1993/03/22  19:12:00  heinz
 * The ARCHIVE fix really wasn't a good way to fix it. I took it out.
 * It cured the symptoms, not the problem. For networking support I need to
 * 'fix' stat(), fstat() and chmod() inside amiga.c.
 *
 * Revision 5.9.1.3  1993/03/20  21:18:12  heinz
 * Amiga ARCHIVE bit will now always be cleared
 *
 * Revision 5.9.1.2  1993/02/11  21:06:51  heinz
 * -M should work on the Amiga, now.
 *
 * Revision 5.9.1.1  1993/01/18  13:23:57  heinz
 * Start of the AMIGA port of RCS 5.6. I call it HWGRCS now ;^)
 *
d74 1
a74 1
 *
d78 1
a78 1
 *
d81 1
a81 1
 *
d85 1
a85 1
 *
d87 1
a87 1
 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
d89 1
a89 1
 *
d92 1
a92 1
 *
d95 1
a95 1
 *
d103 1
a103 1
 *
d160 1
a160 1
static int lastjoin;                    /* index of last element in joinlist  */
d163 2
a164 2
static struct hshentries *gendeltas;    /* deltas to be generated       */
static struct hshentry *targetdelta;    /* final delta to be generated  */
d167 1
a167 1
mainProg(coId, "co", "$Id: co.c,v 5.9.1.4 1993/03/22 19:12:00 heinz Exp heinz $")
d169 2
a170 2
        static char const cmdusage[] =
                "\nco usage: co -{flpqru}[rev] -ddate -jjoinlist -sstate -w[login] -Vn file ...";
d172 19
a190 19
        char *a, **newargv;
        char const *author, *date, *rev, *state;
        char const *joinfilename, *newdate, *neworkfilename;
        int changelock;  /* 1 if a lock has been changed, -1 if error */
        int expmode, r, tostdout, workstatstat;
        struct buf numericrev;  /* expanded revision number     */
        char finaldate[datesize];

        setrid();
        author = date = rev = state = nil;
        bufautobegin(&numericrev);
        expmode = -1;
        suffixes = X_DEFAULT;
        tostdout = false;

        argc = getRCSINIT(argc, argv, &newargv);
        argv = newargv;
        while (a = *++argv,  0<--argc && *a++=='-') {
                switch (*a++) {
d193 4
a196 4
                revno:
                        if (*a) {
                                if (rev) warn("redefinition of revision number");
                                rev = a;
d200 3
a202 3
                case 'f':
                        forceflag=true;
                        goto revno;
d205 1
a205 1
                        if (lockflag < 0) {
d208 1
a208 1
                        lockflag = 1;
d212 1
a212 1
                        if (0 < lockflag) {
d215 1
a215 1
                        lockflag = -1;
d219 1
a219 1
                        tostdout = true;
d222 3
a224 3
                case 'I':
                        interactiveflag = true;
                        goto revno;
d231 3
a233 3
                        if (date)
                                redefined('d');
                        str2date(a, finaldate);
d238 3
a240 3
                        if (*a) {
                                if (join) redefined('j');
                                join = a;
d244 3
a246 3
                case 'M':
                        mtimeflag = true;
                        goto revno;
d249 3
a251 3
                        if (*a) {
                                if (state) redefined('s');
                                state = a;
d256 5
a260 15
                        if (author) redefined('w');
                        if (*a)
                                author = a;
                        else
                                author = getcaller();
                        break;

                case 'x':
                        suffixarg = *argv;
                        suffixes = a;
                        break;

                case 'V':
                        versionarg = *argv;
                        setRCSversion(versionarg);
d263 16
a278 6
                case 'k':    /*  set keyword expand mode  */
                        expandarg = *argv;
                        if (0 <= expmode) redefined('k');
                        if (0 <= (expmode = str2expmode(a)))
                            break;
                        /* fall into */
d280 1
a280 1
                        faterror("unknown option: %s%s", *argv, cmdusage);
d285 8
a292 8
        if (argc<1) faterror("no input file%s", cmdusage);
        if (tostdout)
#           if text_equals_binary_stdio || text_work_stdio
                workstdout = stdout;
#           else
                if (!(workstdout = fdopen(STDOUT_FILENO, FOPEN_W_WORK)))
                    efaterror("stdout");
#           endif
d296 1
a296 1
        ffree();
d298 2
a299 2
        if (pairfilenames(argc, argv, lockflag?rcswriteopen:rcsreadopen, true, false)  <=  0)
                continue;
d302 2
a303 2
         * points at it.  workfilename contains the name of the working file.
         * Also, RCSstat has been set.
d305 1
a305 1
        diagnose("%s  -->  %s\n", RCSfilename,tostdout?"stdout":workfilename);
d307 17
a323 17
        workstatstat = -1;
        if (tostdout) {
                neworkfilename = 0;
                neworkptr = workstdout;
        } else {
                workstatstat = stat(workfilename, &workstat);
                neworkfilename = makedirtemp(workfilename, 1);
                if (!(neworkptr = fopen(neworkfilename, FOPEN_W_WORK))) {
                        if (errno == EACCES)
                                error("%s: parent directory isn't writable",
                                        workfilename
                                );
                        else
                                eerror(neworkfilename);
                        continue;
                }
        }
d329 6
a334 6
                diagnose("no revisions present; generating empty revision 0.0\n");
                Ozclose(&fcopy);
                if (workstatstat == 0)
                        if (!rmworkfile()) continue;
                changelock = 0;
                newdate = 0;
d339 1
a339 1
                        if (!expandsym(rev, &numericrev))
d341 11
a351 11
                } else
                        switch (lockflag<0 ? findlock(false,&targetdelta) : 0) {
                            default:
                                continue;
                            case 0:
                                bufscpy(&numericrev, Dbranch?Dbranch:"");
                                break;
                            case 1:
                                bufscpy(&numericrev, targetdelta->num);
                                break;
                        }
d353 1
a353 1
                if (!(targetdelta=genrevs(numericrev.string,date,author,state,&gendeltas)))
d356 21
a376 14
                changelock =
                        lockflag < 0 ?
                                rmlock(targetdelta)
                        : lockflag == 0 ?
                                0
                        :
                                addlock(targetdelta);

                if (
                        changelock < 0 ||
                        changelock && !checkaccesslist() ||
                        !dorewrite(lockflag, changelock)
                )
                        continue;
a377 7
                if (0 <= expmode)
                        Expand = expmode;
                if (0 < lockflag  &&  Expand == VAL_EXPAND) {
                        error("cannot combine -kv and -l");
                        continue;
                }

d380 3
a382 3
                diagnose("revision %s%s\n",targetdelta->num,
                         0<lockflag ? " (locked)" :
                         lockflag<0 ? " (unlocked)" : "");
d384 2
a385 2
                /* Prepare to remove old working file if necessary.  */
                if (workstatstat == 0)
d391 27
a417 70
                locker_expansion = 0 < lockflag;
                joinfilename = buildrevision(
                        gendeltas, targetdelta,
                        join&&tostdout ? (FILE*)0 : neworkptr,
                        Expand!=OLD_EXPAND
                );
#               if !large_memory
                        if (fcopy == neworkptr)
                                fcopy = 0;  /* Don't close it twice.  */
#               endif
                if_advise_access(changelock && gendeltas->first!=targetdelta,
                        finptr, MADV_SEQUENTIAL
                );

                if (!donerewrite(changelock))
                        continue;

                newdate = targetdelta->date;
                if (join) {
                        newdate = 0;
                        if (!joinfilename) {
                                aflush(neworkptr);
                                joinfilename = neworkfilename;
                        }
#ifdef AMIGA
                /* The Amiga filesystem doesn't let you read open write only files.
                   That is a problem for buildjoin().
                   We know it's the neworkptr file and if everything is
                   ok, it will be closed a few lines below in chnamemod().
                   So we might as well close it here to make buildjoin()
                   possible. A little nasty to us this knowledge about chnamemod()
                   but I don't think chnamemod() will ever change in this respect. */
                        Ozclose(&neworkptr);
#endif
                        if (!buildjoin(joinfilename))
                                continue;
                }
        }
        if (!tostdout) {
            r = 0;
            if (mtimeflag && newdate) {
                if (!join)
                    aflush(neworkptr);
#ifdef AMIGA
                /* The Amiga filesystem can't set dates on open files!
                   We know it's the neworkptr file and if everything is
                   ok, it will be closed a few lines below in chnamemod().
                   So we might as well close it here to make setfiledate()
                   possible. A little nasty to us this knowledge about chnamemod()
                   but I don't think chnamemod() will ever change in this respect. */
                Ozclose(&neworkptr);
#endif
                r = setfiledate(neworkfilename, newdate);
            }
            if (r == 0) {
                ignoreints();
                r = chnamemod(&neworkptr, neworkfilename, workfilename,
                  WORKMODE(RCSstat.st_mode,
                    !(Expand==VAL_EXPAND || lockflag<=0&&StrictLocks)
                  )
                );
                keepdirtemp(neworkfilename);
                restoreints();
            }
            if (r != 0) {
                eerror(workfilename);
                error("see %s", neworkfilename);
                continue;
            }
            diagnose("done\n");
d419 24
d446 3
a448 3
        tempunlink();
        Ofclose(workstdout);
        exitmain(exitstatus);
d452 1
a452 1
        static void
d455 8
a462 8
        if (nerror) exitstatus = EXIT_FAILURE;
        Izclose(&finptr);
        Ozclose(&frewrite);
#       if !large_memory
                if (fcopy!=workstdout) Ozclose(&fcopy);
#       endif
        if (neworkptr!=workstdout) Ozclose(&neworkptr);
        dirtempunlink();
d466 1
a466 1
#       define exiterr coExit
d468 1
a468 1
        exiting void
d471 3
a473 3
        dirtempunlink();
        tempunlink();
        _exit(EXIT_FAILURE);
d481 1
a481 1
        static int
d491 10
a500 10
        if (workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH) && !forceflag) {
            /* File is writable */
            if (!yesorno(false, "writable %s exists%s; remove it? [ny](n): ",
                        workfilename,
                        myself(workstat.st_uid) ? "" : ", and you do not own it"
            )) {
                error(!quietflag && ttystdin()
                        ? "checkout aborted"
                        : "writable %s exists; checkout aborted", workfilename);
                return false;
d503 2
a504 2
        /* Actual unlink is done later by caller. */
        return true;
d508 1
a508 1
        static int
d510 1
a510 1
        struct hshentry const *delta;
d517 1
a517 1
        char const *num;
d525 1
a525 1
                whomatch = strcmp(getcaller(), next->login);
d528 1
a528 1
                        /*found a lock on delta by caller*/
d553 1
a553 1
        static char const *
d555 1
a555 1
        char *joinrev;
d560 31
a590 31
        register char *j;
        register struct hshentry const *d;
        char terminator;
        struct buf numrev;
        struct hshentries *joindeltas;

        j = joinrev;
        for (;;) {
            switch (*j++) {
                default:
                    continue;
                case 0:
                case ' ': case '\t': case '\n':
                case ':': case ',': case ';':
                    break;
            }
            break;
        }
        terminator = *--j;
        *j = 0;
        bufautobegin(&numrev);
        d = 0;
        if (expandsym(joinrev, &numrev))
            d = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&joindeltas);
        bufautoend(&numrev);
        *j = terminator;
        if (d) {
                joinlist[++lastjoin] = d->num;
                return j;
        }
        return nil;
d593 1
a593 1
        static int
d599 1
a599 1
        register char const *j;
d610 1
a610 1
                if (!(j = addjoin(j))) return false;
d616 1
a616 1
                                if (!(j = addjoin(j))) return false;
d627 1
a627 1
                                if (!(joinlist[0] = getancestor(targetdelta->num,joinlist[1])))
d643 1
a643 1
        static char const *
d645 1
a645 1
        char const *r1, *r2;
d650 1
a650 1
        static struct buf t1, t2;
d652 2
a653 2
        unsigned l1, l2, l3;
        char const *r;
d655 20
a674 20
        l1 = countnumflds(r1);
        l2 = countnumflds(r2);
        if ((2<l1 || 2<l2)  &&  cmpnum(r1,r2)!=0) {
            /* not on main trunk or identical */
            l3 = 0;
            while (cmpnumfld(r1, r2, l3+1)==0 && cmpnumfld(r1, r2, l3+2)==0)
                l3 += 2;
            /* This will terminate since r1 and r2 are not the same; see above. */
            if (l3==0) {
                /* no common prefix; common ancestor on main trunk */
                VOID partialno(&t1, r1, l1>2 ? (unsigned)2 : l1);
                VOID partialno(&t2, r2, l2>2 ? (unsigned)2 : l2);
                r = cmpnum(t1.string,t2.string)<0 ? t1.string : t2.string;
                if (cmpnum(r,r1)!=0 && cmpnum(r,r2)!=0)
                        return r;
            } else if (cmpnumfld(r1, r2, l3+1)!=0)
                        return partialno(&t1,r1,l3);
        }
        error("common ancestor of %s and %s undefined", r1, r2);
        return nil;
d679 1
a679 1
        static int
d681 1
a681 1
        char const *initialfile;
d687 3
a689 3
        struct buf commarg;
        struct buf subs;
        char const *rev2, *rev3;
d691 2
a692 2
        char const *cov[10], *mergev[12];
        char const **p;
d694 22
a715 22
        bufautobegin(&commarg);
        bufautobegin(&subs);
        rev2 = maketemp(0);
        rev3 = maketemp(3); /* buildrevision() may use 1 and 2 */

        cov[0] = nil;
        /* cov[1] setup below */
        cov[2] = CO;
        /* cov[3] setup below */
        p = &cov[4];
        if (expandarg) *p++ = expandarg;
        if (suffixarg) *p++ = suffixarg;
        if (versionarg) *p++ = versionarg;
        *p++ = quietarg;
        *p++ = RCSfilename;
        *p = nil;

        mergev[0] = nil;
        mergev[1] = nil;
        mergev[2] = MERGE;
        mergev[3] = mergev[5] = "-L";
        /* rest of mergev setup below */
d721 37
a757 37
                        bufscpy(&subs, targetdelta->num);
                else {
                        bufscat(&subs, ",");
                        bufscat(&subs, joinlist[i-2]);
                        bufscat(&subs, ":");
                        bufscat(&subs, joinlist[i-1]);
                }
                diagnose("revision %s\n",joinlist[i]);
                bufscpy(&commarg, "-p");
                bufscat(&commarg, joinlist[i]);
                cov[1] = rev2;
                cov[3] = commarg.string;
                if (runv(cov))
                        goto badmerge;
                diagnose("revision %s\n",joinlist[i+1]);
                bufscpy(&commarg, "-p");
                bufscat(&commarg, joinlist[i+1]);
                cov[1] = rev3;
                cov[3] = commarg.string;
                if (runv(cov))
                        goto badmerge;
                diagnose("merging...\n");
                mergev[4] = subs.string;
                mergev[6] = joinlist[i+1];
                p = &mergev[7];
                if (quietflag) *p++ = quietarg;
                if (lastjoin<=i+2 && workstdout) *p++ = "-p";
                *p++ = initialfile;
                *p++ = rev2;
                *p++ = rev3;
                *p = nil;
                switch (runv(mergev)) {
                    case DIFF_FAILURE: case DIFF_SUCCESS:
                        break;
                    default:
                        goto badmerge;
                }
d760 2
a761 2
        bufautoend(&commarg);
        bufautoend(&subs);
d765 4
a768 4
        nerror++;
        bufautoend(&commarg);
        bufautoend(&subs);
        return false;
@


5.9.2.2
log
@Changed all checks for AMIGA to _AMIGA. This is more standard like
and helps future updates. Major patch cleanup on the way.
[Note: Added a VOID_CLOSEDIR define for this global change, too!]
@
text
@a36 3
 * Revision 5.9.2.1  1993/04/13  20:07:54  heinz
 * Fix for -j.
 *
d181 1
a181 1
mainProg(coId, "co", "$Id: co.c,v 5.9.2.1 1993/04/13 20:07:54 heinz Exp heinz $")
d429 1
a429 1
#ifdef _AMIGA
d438 1
a438 1
#endif /* _AMIGA */
d448 1
a448 1
#ifdef _AMIGA
d456 1
a456 1
#endif /* _AMIGA */
@


5.9.2.3
log
@First version of Amiga keywords implemented. Ugly, but acceptable for now.
@
text
@a36 5
 * Revision 5.9.2.2  1993/12/18  17:17:10  heinz
 * Changed all checks for AMIGA to _AMIGA. This is more standard like
 * and helps future updates. Major patch cleanup on the way.
 * [Note: Added a VOID_CLOSEDIR define for this global change, too!]
 *
d184 1
a184 4
#ifdef _AMIGA
char HWGversion[] = "$" "VER: " PRGPREFIX "co " PRGVERSION "." PRGREVISION " (" PRGDATE ")" PRGVERTEXT;
#endif /* _AMIGA */
mainProg(coId, "co", "$Id: co.c,v 5.9.2.2 1993/12/18 17:17:10 heinz Exp heinz $")
@


5.9.1.1
log
@Start of the AMIGA port of RCS 5.6. I call it HWGRCS now ;^)
@
text
@@


5.9.1.2
log
@-M should work on the Amiga, now.
@
text
@d28 1
a28 1
 *		       RCS checkout operation
d31 1
a31 1
 *			 check out revisions from RCS files
a36 3
 * Revision 5.9.1.1  1993/01/18  13:23:57  heinz
 * Start of the AMIGA port of RCS 5.6. I call it HWGRCS now ;^)
 *
d66 1
a66 1
 * Permit multiple locks by same user.	Add setuid support.
d70 1
a70 1
 * Ansify and Posixate.  Add -k, -V.  Remove snooping.	Tune.
d74 1
a74 1
 *
d78 1
a78 1
 *
d81 1
a81 1
 *
d85 1
a85 1
 *
d87 1
a87 1
 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
d89 1
a89 1
 *
d92 1
a92 1
 *
d95 1
a95 1
 *
d103 1
a103 1
 *
d167 1
a167 1
mainProg(coId, "co", "$Id: co.c,v 5.9.1.1 1993/01/18 13:23:57 heinz Exp heinz $")
d192 1
a192 1
		case 'r':
d197 2
a198 2
			}
			break;
d204 1
a204 1
		case 'l':
d206 2
a207 2
				warn("-l overrides -u.");
			}
d209 1
a209 1
			goto revno;
d211 1
a211 1
		case 'u':
d213 2
a214 2
				warn("-l overrides -u.");
			}
d216 1
a216 1
			goto revno;
d218 1
a218 1
		case 'p':
d220 1
a220 1
			goto revno;
d226 3
a228 3
		case 'q':
			quietflag=true;
			goto revno;
d230 1
a230 1
		case 'd':
d234 2
a235 2
			date=finaldate;
			break;
d237 1
a237 1
		case 'j':
d241 2
a242 2
			}
			break;
d248 1
a248 1
		case 's':
d252 2
a253 2
			}
			break;
d255 1
a255 1
		case 'w':
d261 1
a261 1
			break;
d279 1
a279 1
		default:
d282 2
a283 2
		};
	} /* end of option processing */
d294 2
a295 2
	/* now handle all filenames */
	do {
d301 1
a301 1
	/* now RCSfilename contains the name of the RCS file, and finptr
d304 1
a304 1
	 */
d325 1
a325 1
	gettree();  /* reads in the delta tree */
d327 2
a328 2
	if (Head==nil) {
		/* no revisions; create empty file */
d335 4
a338 4
		/* Can't reserve a delta, so don't call addlock */
	} else {
		if (rev!=nil) {
			/* expand symbolic revision number */
d340 1
a340 1
				continue;
d352 1
a352 1
		/* get numbers of deltas to be generated */
d354 2
a355 2
			continue;
		/* check reservations */
d378 1
a378 1
		if (join && !preparejoin()) continue;
d386 1
a386 1
			if (!rmworkfile()) continue;
d388 2
a389 2
		/* skip description */
		getdesc(false); /* don't echo*/
d418 1
a418 1
	}
a423 9
#ifdef AMIGA
		/* The Amiga filesystem can't set dates on open files!
		   We know it's the neworkptr file and if everything is
		   ok, it will be closed a few lines below in chnamemod().
		   So we might as well close it here to make setfiledate()
		   possible. A little nasty to us this knowledge about chnamemod()
		   but I don't think chnamemod() will ever change in this respect. */
		Ozclose(&neworkptr);
#endif
d443 2
a444 2
	} while (cleanup(),
		 ++argv, --argc >=1);
d450 1
a450 1
}	/* end of main (co) */
d501 2
a502 2
	    }
	}
d516 1
a516 1
{	register struct lock * next, * trail;
d518 2
a519 2
	struct lock dummy;
	int whomatch, nummatch;
d521 4
a524 4
	num=delta->num;
	dummy.nextlock=next=Locks;
	trail = &dummy;
	while (next!=nil) {
d526 2
a527 2
		nummatch=strcmp(num,next->delta->num);
		if ((whomatch==0) && (nummatch==0)) break;
d529 14
a542 14
		if ((whomatch!=0)&&(nummatch==0)) {
		    error("revision %s locked by %s; use co -r or rcs -u",num,next->login);
		    return -1;
		}
		trail=next;
		next=next->nextlock;
	}
	if (next!=nil) {
		/*found one; delete it */
		trail->nextlock=next->nextlock;
		Locks=dummy.nextlock;
		next->delta->lockedby=nil; /* reset locked-by */
		return 1; /*success*/
	} else	return 0; /*no lock on delta*/
d601 9
a609 9
	j=join;
	lastjoin= -1;
	for (;;) {
		while ((*j==' ')||(*j=='\t')||(*j==',')) j++;
		if (*j=='\0') break;
		if (lastjoin>=joinlength-2) {
			error("too many joins");
			return(false);
		}
d611 5
a615 5
		while ((*j==' ') || (*j=='\t')) j++;
		if (*j == ':') {
			j++;
			while((*j==' ') || (*j=='\t')) j++;
			if (*j!='\0') {
d617 10
a626 10
			} else {
				error("join pair incomplete");
				return false;
			}
		} else {
			if (lastjoin==0) { /* first pair */
				/* common ancestor missing */
				joinlist[1]=joinlist[0];
				lastjoin=1;
				/*derive common ancestor*/
d628 11
a638 11
				       return false;
			} else {
				error("join pair incomplete");
				return false;
			}
		}
	}
	if (lastjoin<1) {
		error("empty join");
		return false;
	} else	return true;
d690 1
a690 1
	int i;
d717 4
a720 4
	i=0;
	while (i<lastjoin) {
		/*prepare marker for merge*/
		if (i==0)
d758 2
a759 2
		i=i+2;
	}
d762 1
a762 1
	return true;
@


5.9.1.3
log
@Amiga ARCHIVE bit will now always be cleared
@
text
@d28 1
a28 1
 *                     RCS checkout operation
d31 1
a31 1
 *                       check out revisions from RCS files
a36 3
 * Revision 5.9.1.2  1993/02/11  21:06:51  heinz
 * -M should work on the Amiga, now.
 *
d69 1
a69 1
 * Permit multiple locks by same user.  Add setuid support.
d73 1
a73 1
 * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
d163 1
a163 1
static int lastjoin;                    /* index of last element in joinlist  */
d166 2
a167 2
static struct hshentries *gendeltas;    /* deltas to be generated       */
static struct hshentry *targetdelta;    /* final delta to be generated  */
d170 1
a170 1
mainProg(coId, "co", "$Id: co.c,v 5.9.1.2 1993/02/11 21:06:51 heinz Exp heinz $")
d172 2
a173 2
        static char const cmdusage[] =
                "\nco usage: co -{flpqru}[rev] -ddate -jjoinlist -sstate -w[login] -Vn file ...";
d175 252
a426 261
        char *a, **newargv;
        char const *author, *date, *rev, *state;
        char const *joinfilename, *newdate, *neworkfilename;
        int changelock;  /* 1 if a lock has been changed, -1 if error */
        int expmode, r, tostdout, workstatstat;
        struct buf numericrev;  /* expanded revision number     */
        char finaldate[datesize];

        setrid();
        author = date = rev = state = nil;
        bufautobegin(&numericrev);
        expmode = -1;
        suffixes = X_DEFAULT;
        tostdout = false;

        argc = getRCSINIT(argc, argv, &newargv);
        argv = newargv;
        while (a = *++argv,  0<--argc && *a++=='-') {
                switch (*a++) {

                case 'r':
                revno:
                        if (*a) {
                                if (rev) warn("redefinition of revision number");
                                rev = a;
                        }
                        break;

                case 'f':
                        forceflag=true;
                        goto revno;

                case 'l':
                        if (lockflag < 0) {
                                warn("-l overrides -u.");
                        }
                        lockflag = 1;
                        goto revno;

                case 'u':
                        if (0 < lockflag) {
                                warn("-l overrides -u.");
                        }
                        lockflag = -1;
                        goto revno;

                case 'p':
                        tostdout = true;
                        goto revno;

                case 'I':
                        interactiveflag = true;
                        goto revno;

                case 'q':
                        quietflag=true;
                        goto revno;

                case 'd':
                        if (date)
                                redefined('d');
                        str2date(a, finaldate);
                        date=finaldate;
                        break;

                case 'j':
                        if (*a) {
                                if (join) redefined('j');
                                join = a;
                        }
                        break;

                case 'M':
                        mtimeflag = true;
                        goto revno;

                case 's':
                        if (*a) {
                                if (state) redefined('s');
                                state = a;
                        }
                        break;

                case 'w':
                        if (author) redefined('w');
                        if (*a)
                                author = a;
                        else
                                author = getcaller();
                        break;

                case 'x':
                        suffixarg = *argv;
                        suffixes = a;
                        break;

                case 'V':
                        versionarg = *argv;
                        setRCSversion(versionarg);
                        break;

                case 'k':    /*  set keyword expand mode  */
                        expandarg = *argv;
                        if (0 <= expmode) redefined('k');
                        if (0 <= (expmode = str2expmode(a)))
                            break;
                        /* fall into */
                default:
                        faterror("unknown option: %s%s", *argv, cmdusage);

                };
        } /* end of option processing */

        if (argc<1) faterror("no input file%s", cmdusage);
        if (tostdout)
#           if text_equals_binary_stdio || text_work_stdio
                workstdout = stdout;
#           else
                if (!(workstdout = fdopen(STDOUT_FILENO, FOPEN_W_WORK)))
                    efaterror("stdout");
#           endif

        /* now handle all filenames */
        do {
        ffree();

        if (pairfilenames(argc, argv, lockflag?rcswriteopen:rcsreadopen, true, false)  <=  0)
                continue;

        /* now RCSfilename contains the name of the RCS file, and finptr
         * points at it.  workfilename contains the name of the working file.
         * Also, RCSstat has been set.
         */
        diagnose("%s  -->  %s\n", RCSfilename,tostdout?"stdout":workfilename);

        workstatstat = -1;
        if (tostdout) {
                neworkfilename = 0;
                neworkptr = workstdout;
        } else {
                workstatstat = stat(workfilename, &workstat);
#ifdef AMIGA
        /* No matter what we do, we uncoditionally clear the archive bit of the
           Amiga. This doesn't hurt RCS as it isn't used or looked at
           anywhere and it helps backup SW a lot. Of course I could check if
           this mode is written back anywhere before I patch into RCS source.
           But it is a lot safer to just clean it at every stat call no matter
           what. */
                workstat.st_mode &= ~S_IARCHIVE;
#endif
                neworkfilename = makedirtemp(workfilename, 1);
                if (!(neworkptr = fopen(neworkfilename, FOPEN_W_WORK))) {
                        if (errno == EACCES)
                                error("%s: parent directory isn't writable",
                                        workfilename
                                );
                        else
                                eerror(neworkfilename);
                        continue;
                }
        }

        gettree();  /* reads in the delta tree */

        if (Head==nil) {
                /* no revisions; create empty file */
                diagnose("no revisions present; generating empty revision 0.0\n");
                Ozclose(&fcopy);
                if (workstatstat == 0)
                        if (!rmworkfile()) continue;
                changelock = 0;
                newdate = 0;
                /* Can't reserve a delta, so don't call addlock */
        } else {
                if (rev!=nil) {
                        /* expand symbolic revision number */
                        if (!expandsym(rev, &numericrev))
                                continue;
                } else
                        switch (lockflag<0 ? findlock(false,&targetdelta) : 0) {
                            default:
                                continue;
                            case 0:
                                bufscpy(&numericrev, Dbranch?Dbranch:"");
                                break;
                            case 1:
                                bufscpy(&numericrev, targetdelta->num);
                                break;
                        }
                /* get numbers of deltas to be generated */
                if (!(targetdelta=genrevs(numericrev.string,date,author,state,&gendeltas)))
                        continue;
                /* check reservations */
                changelock =
                        lockflag < 0 ?
                                rmlock(targetdelta)
                        : lockflag == 0 ?
                                0
                        :
                                addlock(targetdelta);

                if (
                        changelock < 0 ||
                        changelock && !checkaccesslist() ||
                        !dorewrite(lockflag, changelock)
                )
                        continue;

                if (0 <= expmode)
                        Expand = expmode;
                if (0 < lockflag  &&  Expand == VAL_EXPAND) {
                        error("cannot combine -kv and -l");
                        continue;
                }

                if (join && !preparejoin()) continue;

                diagnose("revision %s%s\n",targetdelta->num,
                         0<lockflag ? " (locked)" :
                         lockflag<0 ? " (unlocked)" : "");

                /* Prepare to remove old working file if necessary.  */
                if (workstatstat == 0)
                        if (!rmworkfile()) continue;

                /* skip description */
                getdesc(false); /* don't echo*/

                locker_expansion = 0 < lockflag;
                joinfilename = buildrevision(
                        gendeltas, targetdelta,
                        join&&tostdout ? (FILE*)0 : neworkptr,
                        Expand!=OLD_EXPAND
                );
#               if !large_memory
                        if (fcopy == neworkptr)
                                fcopy = 0;  /* Don't close it twice.  */
#               endif
                if_advise_access(changelock && gendeltas->first!=targetdelta,
                        finptr, MADV_SEQUENTIAL
                );

                if (!donerewrite(changelock))
                        continue;

                newdate = targetdelta->date;
                if (join) {
                        newdate = 0;
                        if (!joinfilename) {
                                aflush(neworkptr);
                                joinfilename = neworkfilename;
                        }
                        if (!buildjoin(joinfilename))
                                continue;
                }
        }
        if (!tostdout) {
            r = 0;
            if (mtimeflag && newdate) {
                if (!join)
                    aflush(neworkptr);
d428 7
a434 7
                /* The Amiga filesystem can't set dates on open files!
                   We know it's the neworkptr file and if everything is
                   ok, it will be closed a few lines below in chnamemod().
                   So we might as well close it here to make setfiledate()
                   possible. A little nasty to us this knowledge about chnamemod()
                   but I don't think chnamemod() will ever change in this respect. */
                Ozclose(&neworkptr);
d436 25
a460 25
                r = setfiledate(neworkfilename, newdate);
            }
            if (r == 0) {
                ignoreints();
                r = chnamemod(&neworkptr, neworkfilename, workfilename,
                  WORKMODE(RCSstat.st_mode,
                    !(Expand==VAL_EXPAND || lockflag<=0&&StrictLocks)
                  )
                );
                keepdirtemp(neworkfilename);
                restoreints();
            }
            if (r != 0) {
                eerror(workfilename);
                error("see %s", neworkfilename);
                continue;
            }
            diagnose("done\n");
        }
        } while (cleanup(),
                 ++argv, --argc >=1);

        tempunlink();
        Ofclose(workstdout);
        exitmain(exitstatus);
d462 1
a462 1
}       /* end of main (co) */
d464 1
a464 1
        static void
d467 8
a474 8
        if (nerror) exitstatus = EXIT_FAILURE;
        Izclose(&finptr);
        Ozclose(&frewrite);
#       if !large_memory
                if (fcopy!=workstdout) Ozclose(&fcopy);
#       endif
        if (neworkptr!=workstdout) Ozclose(&neworkptr);
        dirtempunlink();
d478 1
a478 1
#       define exiterr coExit
d480 1
a480 1
        exiting void
d483 3
a485 3
        dirtempunlink();
        tempunlink();
        _exit(EXIT_FAILURE);
d493 1
a493 1
        static int
d503 14
a516 14
        if (workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH) && !forceflag) {
            /* File is writable */
            if (!yesorno(false, "writable %s exists%s; remove it? [ny](n): ",
                        workfilename,
                        myself(workstat.st_uid) ? "" : ", and you do not own it"
            )) {
                error(!quietflag && ttystdin()
                        ? "checkout aborted"
                        : "writable %s exists; checkout aborted", workfilename);
                return false;
            }
        }
        /* Actual unlink is done later by caller. */
        return true;
d520 1
a520 1
        static int
d522 1
a522 1
        struct hshentry const *delta;
d528 27
a554 27
{       register struct lock * next, * trail;
        char const *num;
        struct lock dummy;
        int whomatch, nummatch;

        num=delta->num;
        dummy.nextlock=next=Locks;
        trail = &dummy;
        while (next!=nil) {
                whomatch = strcmp(getcaller(), next->login);
                nummatch=strcmp(num,next->delta->num);
                if ((whomatch==0) && (nummatch==0)) break;
                        /*found a lock on delta by caller*/
                if ((whomatch!=0)&&(nummatch==0)) {
                    error("revision %s locked by %s; use co -r or rcs -u",num,next->login);
                    return -1;
                }
                trail=next;
                next=next->nextlock;
        }
        if (next!=nil) {
                /*found one; delete it */
                trail->nextlock=next->nextlock;
                Locks=dummy.nextlock;
                next->delta->lockedby=nil; /* reset locked-by */
                return 1; /*success*/
        } else  return 0; /*no lock on delta*/
d565 1
a565 1
        static char const *
d567 1
a567 1
        char *joinrev;
d572 31
a602 31
        register char *j;
        register struct hshentry const *d;
        char terminator;
        struct buf numrev;
        struct hshentries *joindeltas;

        j = joinrev;
        for (;;) {
            switch (*j++) {
                default:
                    continue;
                case 0:
                case ' ': case '\t': case '\n':
                case ':': case ',': case ';':
                    break;
            }
            break;
        }
        terminator = *--j;
        *j = 0;
        bufautobegin(&numrev);
        d = 0;
        if (expandsym(joinrev, &numrev))
            d = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&joindeltas);
        bufautoend(&numrev);
        *j = terminator;
        if (d) {
                joinlist[++lastjoin] = d->num;
                return j;
        }
        return nil;
d605 1
a605 1
        static int
d611 1
a611 1
        register char const *j;
d613 38
a650 38
        j=join;
        lastjoin= -1;
        for (;;) {
                while ((*j==' ')||(*j=='\t')||(*j==',')) j++;
                if (*j=='\0') break;
                if (lastjoin>=joinlength-2) {
                        error("too many joins");
                        return(false);
                }
                if (!(j = addjoin(j))) return false;
                while ((*j==' ') || (*j=='\t')) j++;
                if (*j == ':') {
                        j++;
                        while((*j==' ') || (*j=='\t')) j++;
                        if (*j!='\0') {
                                if (!(j = addjoin(j))) return false;
                        } else {
                                error("join pair incomplete");
                                return false;
                        }
                } else {
                        if (lastjoin==0) { /* first pair */
                                /* common ancestor missing */
                                joinlist[1]=joinlist[0];
                                lastjoin=1;
                                /*derive common ancestor*/
                                if (!(joinlist[0] = getancestor(targetdelta->num,joinlist[1])))
                                       return false;
                        } else {
                                error("join pair incomplete");
                                return false;
                        }
                }
        }
        if (lastjoin<1) {
                error("empty join");
                return false;
        } else  return true;
d655 1
a655 1
        static char const *
d657 1
a657 1
        char const *r1, *r2;
d662 1
a662 1
        static struct buf t1, t2;
d664 2
a665 2
        unsigned l1, l2, l3;
        char const *r;
d667 20
a686 20
        l1 = countnumflds(r1);
        l2 = countnumflds(r2);
        if ((2<l1 || 2<l2)  &&  cmpnum(r1,r2)!=0) {
            /* not on main trunk or identical */
            l3 = 0;
            while (cmpnumfld(r1, r2, l3+1)==0 && cmpnumfld(r1, r2, l3+2)==0)
                l3 += 2;
            /* This will terminate since r1 and r2 are not the same; see above. */
            if (l3==0) {
                /* no common prefix; common ancestor on main trunk */
                VOID partialno(&t1, r1, l1>2 ? (unsigned)2 : l1);
                VOID partialno(&t2, r2, l2>2 ? (unsigned)2 : l2);
                r = cmpnum(t1.string,t2.string)<0 ? t1.string : t2.string;
                if (cmpnum(r,r1)!=0 && cmpnum(r,r2)!=0)
                        return r;
            } else if (cmpnumfld(r1, r2, l3+1)!=0)
                        return partialno(&t1,r1,l3);
        }
        error("common ancestor of %s and %s undefined", r1, r2);
        return nil;
d691 1
a691 1
        static int
d693 1
a693 1
        char const *initialfile;
d699 76
a774 76
        struct buf commarg;
        struct buf subs;
        char const *rev2, *rev3;
        int i;
        char const *cov[10], *mergev[12];
        char const **p;

        bufautobegin(&commarg);
        bufautobegin(&subs);
        rev2 = maketemp(0);
        rev3 = maketemp(3); /* buildrevision() may use 1 and 2 */

        cov[0] = nil;
        /* cov[1] setup below */
        cov[2] = CO;
        /* cov[3] setup below */
        p = &cov[4];
        if (expandarg) *p++ = expandarg;
        if (suffixarg) *p++ = suffixarg;
        if (versionarg) *p++ = versionarg;
        *p++ = quietarg;
        *p++ = RCSfilename;
        *p = nil;

        mergev[0] = nil;
        mergev[1] = nil;
        mergev[2] = MERGE;
        mergev[3] = mergev[5] = "-L";
        /* rest of mergev setup below */

        i=0;
        while (i<lastjoin) {
                /*prepare marker for merge*/
                if (i==0)
                        bufscpy(&subs, targetdelta->num);
                else {
                        bufscat(&subs, ",");
                        bufscat(&subs, joinlist[i-2]);
                        bufscat(&subs, ":");
                        bufscat(&subs, joinlist[i-1]);
                }
                diagnose("revision %s\n",joinlist[i]);
                bufscpy(&commarg, "-p");
                bufscat(&commarg, joinlist[i]);
                cov[1] = rev2;
                cov[3] = commarg.string;
                if (runv(cov))
                        goto badmerge;
                diagnose("revision %s\n",joinlist[i+1]);
                bufscpy(&commarg, "-p");
                bufscat(&commarg, joinlist[i+1]);
                cov[1] = rev3;
                cov[3] = commarg.string;
                if (runv(cov))
                        goto badmerge;
                diagnose("merging...\n");
                mergev[4] = subs.string;
                mergev[6] = joinlist[i+1];
                p = &mergev[7];
                if (quietflag) *p++ = quietarg;
                if (lastjoin<=i+2 && workstdout) *p++ = "-p";
                *p++ = initialfile;
                *p++ = rev2;
                *p++ = rev3;
                *p = nil;
                switch (runv(mergev)) {
                    case DIFF_FAILURE: case DIFF_SUCCESS:
                        break;
                    default:
                        goto badmerge;
                }
                i=i+2;
        }
        bufautoend(&commarg);
        bufautoend(&subs);
        return true;
d777 4
a780 4
        nerror++;
        bufautoend(&commarg);
        bufautoend(&subs);
        return false;
@


5.9.1.4
log
@The ARCHIVE fix really wasn't a good way to fix it. I took it out.
It cured the symptoms, not the problem. For networking support I need to
'fix' stat(), fstat() and chmod() inside amiga.c.
@
text
@a36 3
 * Revision 5.9.1.3  1993/03/20  21:18:12  heinz
 * Amiga ARCHIVE bit will now always be cleared
 *
d173 1
a173 1
mainProg(coId, "co", "$Id: co.c,v 5.9.1.3 1993/03/20 21:18:12 heinz Exp heinz $")
d319 9
@
