home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / rcs-5.6 / part08 < prev    next >
Encoding:
Text File  |  1991-12-19  |  86.5 KB  |  3,270 lines

  1. Newsgroups: comp.sources.unix
  2. From: hammer@cs.purdue.edu (Adam Hammer)
  3. Subject: v25i084: rcs-5.6 - Revision Control System, V5.6, Part08/11
  4. Sender: sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: hammer@cs.purdue.edu (Adam Hammer)
  8. Posting-Number: Volume 25, Issue 84
  9. Archive-Name: rcs-5.6/part08
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 8 (of 11)."
  18. # Contents:  src/rcs.c src/rcsedit.c
  19. # Wrapped by vixie@cognition.pa.dec.com on Fri Dec 20 16:23:41 1991
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'src/rcs.c' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'src/rcs.c'\"
  23. else
  24. echo shar: Extracting \"'src/rcs.c'\" \(43795 characters\)
  25. sed "s/^X//" >'src/rcs.c' <<'END_OF_FILE'
  26. X/*
  27. X *                      RCS create/change operation
  28. X */
  29. X/* Copyright (C) 1982, 1988, 1989 Walter Tichy
  30. X   Copyright 1990, 1991 by Paul Eggert
  31. X   Distributed under license by the Free Software Foundation, Inc.
  32. X
  33. This file is part of RCS.
  34. X
  35. RCS is free software; you can redistribute it and/or modify
  36. it under the terms of the GNU General Public License as published by
  37. the Free Software Foundation; either version 2, or (at your option)
  38. any later version.
  39. X
  40. RCS is distributed in the hope that it will be useful,
  41. but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  43. GNU General Public License for more details.
  44. X
  45. You should have received a copy of the GNU General Public License
  46. along with RCS; see the file COPYING.  If not, write to
  47. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  48. X
  49. Report problems and direct all questions to:
  50. X
  51. X    rcs-bugs@cs.purdue.edu
  52. X
  53. X*/
  54. X
  55. X
  56. X
  57. X
  58. X/* $Log: rcs.c,v $
  59. X * Revision 5.12  1991/11/20  17:58:08  eggert
  60. X * Don't read the delta tree from a nonexistent RCS file.
  61. X *
  62. X * Revision 5.11  1991/10/07  17:32:46  eggert
  63. X * Remove lint.
  64. X *
  65. X * Revision 5.10  1991/08/19  23:17:54  eggert
  66. X * Add -m, -r$, piece tables.  Revision separator is `:', not `-'.  Tune.
  67. X *
  68. X * Revision 5.9  1991/04/21  11:58:18  eggert
  69. X * Add -x, RCSINIT, MS-DOS support.
  70. X *
  71. X * Revision 5.8  1991/02/25  07:12:38  eggert
  72. X * strsave -> str_save (DG/UX name clash)
  73. X * 0444 -> S_IRUSR|S_IRGRP|S_IROTH for portability
  74. X *
  75. X * Revision 5.7  1990/12/18  17:19:21  eggert
  76. X * Fix bug with multiple -n and -N options.
  77. X *
  78. X * Revision 5.6  1990/12/04  05:18:40  eggert
  79. X * Use -I for prompts and -q for diagnostics.
  80. X *
  81. X * Revision 5.5  1990/11/11  00:06:35  eggert
  82. X * Fix `rcs -e' core dump.
  83. X *
  84. X * Revision 5.4  1990/11/01  05:03:33  eggert
  85. X * Add -I and new -t behavior.  Permit arbitrary data in logs.
  86. X *
  87. X * Revision 5.3  1990/10/04  06:30:16  eggert
  88. X * Accumulate exit status across files.
  89. X *
  90. X * Revision 5.2  1990/09/04  08:02:17  eggert
  91. X * Standardize yes-or-no procedure.
  92. X *
  93. X * Revision 5.1  1990/08/29  07:13:51  eggert
  94. X * Remove unused setuid support.  Clean old log messages too.
  95. X *
  96. X * Revision 5.0  1990/08/22  08:12:42  eggert
  97. X * Don't lose names when applying -a option to multiple files.
  98. X * Remove compile-time limits; use malloc instead.  Add setuid support.
  99. X * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  100. X * Ansify and Posixate.  Add -V.  Fix umask bug.  Make linting easier.  Tune.
  101. X * Yield proper exit status.  Check diff's output.
  102. X *
  103. X * Revision 4.11  89/05/01  15:12:06  narten
  104. X * changed copyright header to reflect current distribution rules
  105. X * 
  106. X * Revision 4.10  88/11/08  16:01:54  narten
  107. X * didn't install previous patch correctly
  108. X * 
  109. X * Revision 4.9  88/11/08  13:56:01  narten
  110. X * removed include <sysexits.h> (not needed)
  111. X * minor fix for -A option
  112. X * 
  113. X * Revision 4.8  88/08/09  19:12:27  eggert
  114. X * Don't access freed storage.
  115. X * Use execv(), not system(); yield proper exit status; remove lint.
  116. X * 
  117. X * Revision 4.7  87/12/18  11:37:17  narten
  118. X * lint cleanups (Guy Harris)
  119. X * 
  120. X * Revision 4.6  87/10/18  10:28:48  narten
  121. X * Updating verison numbers. Changes relative to 1.1 are actually 
  122. X * relative to 4.3
  123. X * 
  124. X * Revision 1.4  87/09/24  13:58:52  narten
  125. X * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  126. X * warnings)
  127. X * 
  128. X * Revision 1.3  87/03/27  14:21:55  jenkins
  129. X * Port to suns
  130. X * 
  131. X * Revision 1.2  85/12/17  13:59:09  albitz
  132. X * Changed setstate to rcs_setstate because of conflict with random.o.
  133. X * 
  134. X * Revision 4.3  83/12/15  12:27:33  wft
  135. X * rcs -u now breaks most recent lock if it can't find a lock by the caller.
  136. X * 
  137. X * Revision 4.2  83/12/05  10:18:20  wft
  138. X * Added conditional compilation for sending mail.
  139. X * Alternatives: V4_2BSD, V6, USG, and other.
  140. X * 
  141. X * Revision 4.1  83/05/10  16:43:02  wft
  142. X * Simplified breaklock(); added calls to findlock() and getcaller().
  143. X * Added option -b (default branch). Updated -s and -w for -b.
  144. X * Removed calls to stat(); now done by pairfilenames().
  145. X * Replaced most catchints() calls with restoreints().
  146. X * Removed check for exit status of delivermail().
  147. X * Directed all interactive output to stderr.
  148. X * 
  149. X * Revision 3.9.1.1  83/12/02  22:08:51  wft
  150. X * Added conditional compilation for 4.2 sendmail and 4.1 delivermail.
  151. X * 
  152. X * Revision 3.9  83/02/15  15:38:39  wft
  153. X * Added call to fastcopy() to copy remainder of RCS file.
  154. X *
  155. X * Revision 3.8  83/01/18  17:37:51  wft
  156. X * Changed sendmail(): now uses delivermail, and asks whether to break the lock.
  157. X *
  158. X * Revision 3.7  83/01/15  18:04:25  wft
  159. X * Removed putree(); replaced with puttree() in rcssyn.c.
  160. X * Combined putdellog() and scanlogtext(); deleted putdellog().
  161. X * Cleaned up diagnostics and error messages. Fixed problem with
  162. X * mutilated files in case of deletions in 2 files in a single command.
  163. X * Changed marking of selector from 'D' to DELETE.
  164. X *
  165. X * Revision 3.6  83/01/14  15:37:31  wft
  166. X * Added ignoring of interrupts while new RCS file is renamed;
  167. X * Avoids deletion of RCS files by interrupts.
  168. X *
  169. X * Revision 3.5  82/12/10  21:11:39  wft
  170. X * Removed unused variables, fixed checking of return code from diff,
  171. X * introduced variant COMPAT2 for skipping Suffix on -A files.
  172. X *
  173. X * Revision 3.4  82/12/04  13:18:20  wft
  174. X * Replaced getdelta() with gettree(), changed breaklock to update
  175. X * field lockedby, added some diagnostics.
  176. X *
  177. X * Revision 3.3  82/12/03  17:08:04  wft
  178. X * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
  179. X * /usr/ucb/Mail with macro MAIL. Removed handling of Suffix (-x).
  180. X * fixed -u for missing revno. Disambiguated structure members.
  181. X *
  182. X * Revision 3.2  82/10/18  21:05:07  wft
  183. X * rcs -i now generates a file mode given by the umask minus write permission;
  184. X * otherwise, rcs keeps the mode, but removes write permission.
  185. X * I added a check for write error, fixed call to getlogin(), replaced
  186. X * curdir() with getfullRCSname(), cleaned up handling -U/L, and changed
  187. X * conflicting, long identifiers.
  188. X *
  189. X * Revision 3.1  82/10/13  16:11:07  wft
  190. X * fixed type of variables receiving from getc() (char -> int).
  191. X */
  192. X
  193. X
  194. X#include "rcsbase.h"
  195. X
  196. struct  Lockrev {
  197. X    char const *revno;
  198. X        struct  Lockrev   * nextrev;
  199. X};
  200. X
  201. struct  Symrev {
  202. X    char const *revno;
  203. X    char const *ssymbol;
  204. X        int     override;
  205. X        struct  Symrev  * nextsym;
  206. X};
  207. X
  208. struct Message {
  209. X    char const *revno;
  210. X    struct cbuf message;
  211. X    struct Message *nextmessage;
  212. X};
  213. X
  214. struct  Status {
  215. X    char const *revno;
  216. X    char const *status;
  217. X        struct  Status  * nextstatus;
  218. X};
  219. X
  220. enum changeaccess {append, erase};
  221. struct chaccess {
  222. X    char const *login;
  223. X    enum changeaccess command;
  224. X    struct chaccess *nextchaccess;
  225. X};
  226. X
  227. struct delrevpair {
  228. X    char const *strt;
  229. X    char const *end;
  230. X        int     code;
  231. X};
  232. X
  233. static int buildeltatext P((struct hshentries const*));
  234. static int removerevs P((void));
  235. static int sendmail P((char const*,char const*));
  236. static struct Lockrev *rmnewlocklst P((struct Lockrev const*));
  237. static void breaklock P((struct hshentry const*));
  238. static void buildtree P((void));
  239. static void cleanup P((void));
  240. static void doaccess P((void));
  241. static void doassoc P((void));
  242. static void dolocks P((void));
  243. static void domessages P((void));
  244. static void getaccessor P((char*,enum changeaccess));
  245. static void getassoclst P((int,char*));
  246. static void getchaccess P((char const*,enum changeaccess));
  247. static void getdelrev P((char*));
  248. static void getmessage P((char*));
  249. static void getstates P((char*));
  250. static void rcs_setstate P((char const*,char const*));
  251. static void scanlogtext P((struct hshentry*,int));
  252. static void setlock P((char const*));
  253. X
  254. static struct buf numrev;
  255. static char const *headstate;
  256. static int chgheadstate, exitstatus, lockhead, unlockcaller;
  257. static struct Lockrev *newlocklst, *rmvlocklst;
  258. static struct Message *messagelst, *lastmessage;
  259. static struct Status *statelst, *laststate;
  260. static struct Symrev *assoclst, *lastassoc;
  261. static struct chaccess *chaccess, **nextchaccess;
  262. static struct delrevpair delrev;
  263. static struct hshentry *cuthead, *cuttail, *delstrt;
  264. static struct hshentries *gendeltas;
  265. X
  266. mainProg(rcsId, "rcs", "$Id: rcs.c,v 5.12 1991/11/20 17:58:08 eggert Exp $")
  267. X{
  268. X    static char const cmdusage[] =
  269. X        "\nrcs usage: rcs -{ae}logins -Afile -{blu}[rev] -cstring -{iLU} -{nNs}name[:rev] -orange -t[file] -Vn file ...";
  270. X
  271. X    char *a, **newargv, *textfile;
  272. X    char const *branchsym, *commsyml;
  273. X    int branchflag, expmode, initflag;
  274. X    int e, r, strictlock, strict_selected, textflag;
  275. X    mode_t defaultRCSmode;    /* default mode for new RCS files */
  276. X    mode_t RCSmode;
  277. X    struct buf branchnum;
  278. X    struct stat workstat;
  279. X        struct  Lockrev *curlock,  * rmvlock, *lockpt;
  280. X        struct  Status  * curstate;
  281. X
  282. X    nosetid();
  283. X
  284. X    nextchaccess = &chaccess;
  285. X    branchsym = commsyml = textfile = nil;
  286. X    branchflag = strictlock = false;
  287. X    bufautobegin(&branchnum);
  288. X    curlock = rmvlock = nil;
  289. X    defaultRCSmode = 0;
  290. X    expmode = -1;
  291. X    suffixes = X_DEFAULT;
  292. X        initflag= textflag = false;
  293. X        strict_selected = 0;
  294. X
  295. X        /*  preprocessing command options    */
  296. X    argc = getRCSINIT(argc, argv, &newargv);
  297. X    argv = newargv;
  298. X    while (a = *++argv,  0<--argc && *a++=='-') {
  299. X        switch (*a++) {
  300. X
  301. X        case 'i':   /*  initial version  */
  302. X                        initflag = true;
  303. X                        break;
  304. X
  305. X                case 'b':  /* change default branch */
  306. X            if (branchflag) redefined('b');
  307. X                        branchflag= true;
  308. X            branchsym = a;
  309. X                        break;
  310. X
  311. X                case 'c':   /*  change comment symbol   */
  312. X            if (commsyml) redefined('c');
  313. X            commsyml = a;
  314. X                        break;
  315. X
  316. X                case 'a':  /*  add new accessor   */
  317. X            getaccessor(*argv+1, append);
  318. X                        break;
  319. X
  320. X                case 'A':  /*  append access list according to accessfile  */
  321. X            if (!*a) {
  322. X                error("missing file name after -A");
  323. X                            break;
  324. X                        }
  325. X            *argv = a;
  326. X            if (0 < pairfilenames(1,argv,rcsreadopen,true,false)) {
  327. X                while (AccessList) {
  328. X                getchaccess(str_save(AccessList->login),append);
  329. X                AccessList = AccessList->nextaccess;
  330. X                }
  331. X                Izclose(&finptr);
  332. X                        }
  333. X                        break;
  334. X
  335. X                case 'e':    /*  remove accessors   */
  336. X            getaccessor(*argv+1, erase);
  337. X                        break;
  338. X
  339. X                case 'l':    /*   lock a revision if it is unlocked   */
  340. X            if (!*a) {
  341. X                /* Lock head or default branch.  */
  342. X                            lockhead = true;
  343. X                            break;
  344. X                        }
  345. X            lockpt = talloc(struct Lockrev);
  346. X            lockpt->revno = a;
  347. X                        lockpt->nextrev = nil;
  348. X                        if ( curlock )
  349. X                            curlock->nextrev = lockpt;
  350. X                        else
  351. X                            newlocklst = lockpt;
  352. X                        curlock = lockpt;
  353. X                        break;
  354. X
  355. X                case 'u':   /*  release lock of a locked revision   */
  356. X            if (!*a) {
  357. X                            unlockcaller=true;
  358. X                            break;
  359. X                        }
  360. X            lockpt = talloc(struct Lockrev);
  361. X            lockpt->revno = a;
  362. X                        lockpt->nextrev = nil;
  363. X                        if (rmvlock)
  364. X                            rmvlock->nextrev = lockpt;
  365. X                        else
  366. X                            rmvlocklst = lockpt;
  367. X                        rmvlock = lockpt;
  368. X
  369. X                        curlock = rmnewlocklst(lockpt);
  370. X                        break;
  371. X
  372. X                case 'L':   /*  set strict locking */
  373. X                        if (strict_selected++) {  /* Already selected L or U? */
  374. X               if (!strictlock)      /* Already selected -U? */
  375. X                   warn("-L overrides -U.");
  376. X                        }
  377. X                        strictlock = true;
  378. X                        break;
  379. X
  380. X                case 'U':   /*  release strict locking */
  381. X                        if (strict_selected++) {  /* Already selected L or U? */
  382. X               if (strictlock)      /* Already selected -L? */
  383. X                   warn("-L overrides -U.");
  384. X                        }
  385. X            else
  386. X                strictlock = false;
  387. X                        break;
  388. X
  389. X                case 'n':    /*  add new association: error, if name exists */
  390. X            if (!*a) {
  391. X                error("missing symbolic name after -n");
  392. X                            break;
  393. X                        }
  394. X                        getassoclst(false, (*argv)+1);
  395. X                        break;
  396. X
  397. X                case 'N':   /*  add or change association   */
  398. X            if (!*a) {
  399. X                error("missing symbolic name after -N");
  400. X                            break;
  401. X                        }
  402. X                        getassoclst(true, (*argv)+1);
  403. X                        break;
  404. X
  405. X        case 'm':   /*  change log message  */
  406. X            getmessage(a);
  407. X            break;
  408. X
  409. X        case 'o':   /*  delete revisions  */
  410. X            if (delrev.strt) redefined('o');
  411. X            if (!*a) {
  412. X                error("missing revision range after -o");
  413. X                            break;
  414. X                        }
  415. X                        getdelrev( (*argv)+1 );
  416. X                        break;
  417. X
  418. X                case 's':   /*  change state attribute of a revision  */
  419. X            if (!*a) {
  420. X                error("state missing after -s");
  421. X                            break;
  422. X                        }
  423. X                        getstates( (*argv)+1);
  424. X                        break;
  425. X
  426. X                case 't':   /*  change descriptive text   */
  427. X                        textflag=true;
  428. X            if (*a) {
  429. X                if (textfile) redefined('t');
  430. X                textfile = a;
  431. X                        }
  432. X                        break;
  433. X
  434. X        case 'I':
  435. X            interactiveflag = true;
  436. X            break;
  437. X
  438. X                case 'q':
  439. X                        quietflag = true;
  440. X                        break;
  441. X
  442. X        case 'x':
  443. X            suffixes = a;
  444. X            break;
  445. X
  446. X        case 'V':
  447. X            setRCSversion(*argv);
  448. X            break;
  449. X
  450. X        case 'k':    /*  set keyword expand mode  */
  451. X            if (0 <= expmode) redefined('k');
  452. X            if (0 <= (expmode = str2expmode(a)))
  453. X                break;
  454. X            /* fall into */
  455. X                default:
  456. X            faterror("unknown option: %s%s", *argv, cmdusage);
  457. X                };
  458. X        }  /* end processing of options */
  459. X
  460. X    if (argc<1) faterror("no input file%s", cmdusage);
  461. X        if (nerror) {
  462. X        diagnose("%s aborted\n",cmdid);
  463. X        exitmain(EXIT_FAILURE);
  464. X        }
  465. X    if (initflag) {
  466. X        defaultRCSmode = umask((mode_t)0);
  467. X        VOID umask(defaultRCSmode);
  468. X        defaultRCSmode = (S_IRUSR|S_IRGRP|S_IROTH) & ~defaultRCSmode;
  469. X    }
  470. X
  471. X        /* now handle all filenames */
  472. X        do {
  473. X    ffree();
  474. X
  475. X        if ( initflag ) {
  476. X        switch (pairfilenames(argc, argv, rcswriteopen, false, false)) {
  477. X                case -1: break;        /*  not exist; ok */
  478. X                case  0: continue;     /*  error         */
  479. X                case  1: error("file %s exists already", RCSfilename);
  480. X                         continue;
  481. X            }
  482. X    }
  483. X        else  {
  484. X        switch (pairfilenames(argc, argv, rcswriteopen, true, false)) {
  485. X                case -1: continue;    /*  not exist      */
  486. X                case  0: continue;    /*  errors         */
  487. X                case  1: break;       /*  file exists; ok*/
  488. X            }
  489. X    }
  490. X
  491. X
  492. X        /* now RCSfilename contains the name of the RCS file, and
  493. X         * workfilename contains the name of the working file.
  494. X         * if !initflag, finptr contains the file descriptor for the
  495. X         * RCS file. The admin node is initialized.
  496. X         */
  497. X
  498. X    diagnose("RCS file: %s\n", RCSfilename);
  499. X
  500. X    RCSmode = defaultRCSmode;
  501. X    if (initflag) {
  502. X        if (stat(workfilename, &workstat) == 0)
  503. X            RCSmode = workstat.st_mode;
  504. X    } else {
  505. X        if (!checkaccesslist()) continue;
  506. X        gettree(); /* Read the delta tree.  */
  507. X        RCSmode = RCSstat.st_mode;
  508. X    }
  509. X    RCSmode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
  510. X
  511. X        /*  update admin. node    */
  512. X        if (strict_selected) StrictLocks = strictlock;
  513. X    if (commsyml) {
  514. X        Comment.string = commsyml;
  515. X        Comment.size = strlen(commsyml);
  516. X    }
  517. X    if (0 <= expmode) Expand = expmode;
  518. X
  519. X        /* update default branch */
  520. X    if (branchflag && expandsym(branchsym, &branchnum)) {
  521. X        if (countnumflds(branchnum.string)) {
  522. X        Dbranch = branchnum.string;
  523. X            } else
  524. X                Dbranch = nil;
  525. X        }
  526. X
  527. X    doaccess();    /* Update access list.  */
  528. X
  529. X    doassoc();    /* Update association list.  */
  530. X
  531. X    dolocks();    /* Update locks.  */
  532. X
  533. X    domessages();    /* Update log messages.  */
  534. X
  535. X        /*  update state attribution  */
  536. X        if (chgheadstate) {
  537. X            /* change state of default branch or head */
  538. X            if (Dbranch==nil) {
  539. X                if (Head==nil)
  540. X             warn("can't change states in an empty tree");
  541. X                else Head->state = headstate;
  542. X            } else {
  543. X        rcs_setstate(Dbranch,headstate); /* Can't set directly */
  544. X            }
  545. X        }
  546. X        curstate = statelst;
  547. X        while( curstate ) {
  548. X            rcs_setstate(curstate->revno,curstate->status);
  549. X            curstate = curstate->nextstatus;
  550. X        }
  551. X
  552. X        cuthead = cuttail = nil;
  553. X    if (delrev.strt && removerevs()) {
  554. X            /*  rebuild delta tree if some deltas are deleted   */
  555. X            if ( cuttail )
  556. X        VOID genrevs(cuttail->num, (char *)nil,(char *)nil,
  557. X                 (char *)nil, &gendeltas);
  558. X            buildtree();
  559. X        }
  560. X
  561. X    if (nerror)
  562. X        continue;
  563. X
  564. X        putadmin(frewrite);
  565. X        if ( Head )
  566. X           puttree(Head, frewrite);
  567. X    putdesc(textflag,textfile);
  568. X
  569. X        if ( Head) {
  570. X        if (!delrev.strt && !messagelst) {
  571. X        /* No revision was deleted and no message was changed.  */
  572. X        fastcopy(finptr, frewrite);
  573. X            } else {
  574. X        if (!cuttail || buildeltatext(gendeltas)) {
  575. X            advise_access(finptr, MADV_SEQUENTIAL);
  576. X            scanlogtext((struct hshentry *)nil, false);
  577. X                    /* copy rest of delta text nodes that are not deleted      */
  578. X        }
  579. X            }
  580. X        }
  581. X    Izclose(&finptr);
  582. X        if ( ! nerror ) {  /*  move temporary file to RCS file if no error */
  583. X        /* update mode */
  584. X        ignoreints();
  585. X        r = chnamemod(&frewrite, newRCSfilename, RCSfilename, RCSmode);
  586. X        e = errno;
  587. X        keepdirtemp(newRCSfilename);
  588. X        restoreints();
  589. X        if (r != 0) {
  590. X        enerror(e, RCSfilename);
  591. X        error("saved in %s", newRCSfilename);
  592. X        dirtempunlink();
  593. X                break;
  594. X            }
  595. X        diagnose("done\n");
  596. X        } else {
  597. X        diagnose("%s aborted; %s unchanged.\n",cmdid,RCSfilename);
  598. X        }
  599. X    } while (cleanup(),
  600. X                 ++argv, --argc >=1);
  601. X
  602. X    tempunlink();
  603. X    exitmain(exitstatus);
  604. X}       /* end of main (rcs) */
  605. X
  606. X    static void
  607. cleanup()
  608. X{
  609. X    if (nerror) exitstatus = EXIT_FAILURE;
  610. X    Izclose(&finptr);
  611. X    Ozclose(&fcopy);
  612. X    Ozclose(&frewrite);
  613. X    dirtempunlink();
  614. X}
  615. X
  616. X    exiting void
  617. exiterr()
  618. X{
  619. X    dirtempunlink();
  620. X    tempunlink();
  621. X    _exit(EXIT_FAILURE);
  622. X}
  623. X
  624. X
  625. X    static void
  626. getassoclst(flag, sp)
  627. int     flag;
  628. char    * sp;
  629. X/*  Function:   associate a symbolic name to a revision or branch,      */
  630. X/*              and store in assoclst                                   */
  631. X
  632. X{
  633. X        struct   Symrev  * pt;
  634. X    char const *temp;
  635. X        int                c;
  636. X
  637. X        while( (c=(*++sp)) == ' ' || c == '\t' || c =='\n')  ;
  638. X        temp = sp;
  639. X    sp = checkid(sp, ':');  /*  check for invalid symbolic name  */
  640. X    c = *sp;   *sp = '\0';
  641. X        while( c == ' ' || c == '\t' || c == '\n')  c = *++sp;
  642. X
  643. X        if ( c != ':' && c != '\0') {
  644. X        error("invalid string %s after option -n or -N",sp);
  645. X            return;
  646. X        }
  647. X
  648. X    pt = talloc(struct Symrev);
  649. X        pt->ssymbol = temp;
  650. X        pt->override = flag;
  651. X    if (c == '\0')  /*  delete symbol  */
  652. X            pt->revno = nil;
  653. X        else {
  654. X            while( (c = *++sp) == ' ' || c == '\n' || c == '\t')  ;
  655. X        pt->revno = sp;
  656. X        }
  657. X        pt->nextsym = nil;
  658. X        if (lastassoc)
  659. X            lastassoc->nextsym = pt;
  660. X        else
  661. X            assoclst = pt;
  662. X        lastassoc = pt;
  663. X        return;
  664. X}
  665. X
  666. X
  667. X    static void
  668. getchaccess(login, command)
  669. X    char const *login;
  670. X    enum changeaccess command;
  671. X{
  672. X    register struct chaccess *pt;
  673. X
  674. X    *nextchaccess = pt = talloc(struct chaccess);
  675. X    pt->login = login;
  676. X    pt->command = command;
  677. X    pt->nextchaccess = nil;
  678. X    nextchaccess = &pt->nextchaccess;
  679. X}
  680. X
  681. X
  682. X
  683. X    static void
  684. getaccessor(opt, command)
  685. X    char *opt;
  686. X    enum changeaccess command;
  687. X/*   Function:  get the accessor list of options -e and -a,     */
  688. X/*        and store in chaccess                */
  689. X
  690. X
  691. X{
  692. X        register c;
  693. X    register char *sp;
  694. X
  695. X    sp = opt;
  696. X        while( ( c = *++sp) == ' ' || c == '\n' || c == '\t' || c == ',') ;
  697. X        if ( c == '\0') {
  698. X        if (command == erase  &&  sp-opt == 1) {
  699. X        getchaccess((char const*)nil, command);
  700. X        return;
  701. X        }
  702. X        error("missing login name after option -a or -e");
  703. X        return;
  704. X        }
  705. X
  706. X        while( c != '\0') {
  707. X        getchaccess(sp, command);
  708. X        sp = checkid(sp,',');
  709. X        c = *sp;   *sp = '\0';
  710. X                while( c == ' ' || c == '\n' || c == '\t'|| c == ',')c =(*++sp);
  711. X        }
  712. X}
  713. X
  714. X
  715. X    static void
  716. getmessage(option)
  717. X    char *option;
  718. X{
  719. X    struct Message *pt;
  720. X    struct cbuf cb;
  721. X    char *m;
  722. X
  723. X    if (!(m = strchr(option, ':'))) {
  724. X        error("-m option lacks revision number");
  725. X        return;
  726. X    }
  727. X    *m++ = 0;
  728. X    cb = cleanlogmsg(m, strlen(m));
  729. X    if (!cb.size) {
  730. X        error("-m option lacks log message");
  731. X        return;
  732. X    }
  733. X    pt = talloc(struct Message);
  734. X    pt->revno = option;
  735. X    pt->message = cb;
  736. X    pt->nextmessage = 0;
  737. X    if (lastmessage)
  738. X        lastmessage->nextmessage = pt;
  739. X    else
  740. X        messagelst = pt;
  741. X    lastmessage = pt;
  742. X}
  743. X
  744. X
  745. X    static void
  746. getstates(sp)
  747. char    *sp;
  748. X/*   Function:  get one state attribute and the corresponding   */
  749. X/*              revision and store in statelst                  */
  750. X
  751. X{
  752. X    char const *temp;
  753. X        struct  Status  *pt;
  754. X        register        c;
  755. X
  756. X        while( (c=(*++sp)) ==' ' || c == '\t' || c == '\n')  ;
  757. X        temp = sp;
  758. X    sp = checkid(sp,':');  /* check for invalid state attribute */
  759. X    c = *sp;   *sp = '\0';
  760. X        while( c == ' ' || c == '\t' || c == '\n' )  c = *++sp;
  761. X
  762. X        if ( c == '\0' ) {  /*  change state of def. branch or Head  */
  763. X            chgheadstate = true;
  764. X            headstate  = temp;
  765. X            return;
  766. X        }
  767. X        else if ( c != ':' ) {
  768. X        error("missing ':' after state in option -s");
  769. X            return;
  770. X        }
  771. X
  772. X        while( (c = *++sp) == ' ' || c == '\t' || c == '\n')  ;
  773. X    pt = talloc(struct Status);
  774. X        pt->status     = temp;
  775. X        pt->revno      = sp;
  776. X        pt->nextstatus = nil;
  777. X        if (laststate)
  778. X            laststate->nextstatus = pt;
  779. X        else
  780. X            statelst = pt;
  781. X        laststate = pt;
  782. X}
  783. X
  784. X
  785. X
  786. X    static void
  787. getdelrev(sp)
  788. char    *sp;
  789. X/*   Function:  get revision range or branch to be deleted,     */
  790. X/*              and place in delrev                             */
  791. X{
  792. X        int    c;
  793. X        struct  delrevpair      *pt;
  794. X    int separator;
  795. X
  796. X    pt = &delrev;
  797. X        while((c = (*++sp)) == ' ' || c == '\n' || c == '\t') ;
  798. X
  799. X    /* Support old ambiguous '-' syntax; this will go away.  */
  800. X    if (strchr(sp,':'))
  801. X        separator = ':';
  802. X    else {
  803. X        if (strchr(sp,'-')  &&  VERSION(5) <= RCSversion)
  804. X            warn("`-' is obsolete in `-o%s'; use `:' instead", sp);
  805. X        separator = '-';
  806. X    }
  807. X
  808. X    if (c == separator) { /* -o:rev */
  809. X            while( (c = (*++sp)) == ' ' || c == '\n' || c == '\t')  ;
  810. X            pt->strt = sp;    pt->code = 1;
  811. X            while( c != ' ' && c != '\n' && c != '\t' && c != '\0') c =(*++sp);
  812. X            *sp = '\0';
  813. X        pt->end = nil;
  814. X            return;
  815. X        }
  816. X        else {
  817. X            pt->strt = sp;
  818. X            while( c != ' ' && c != '\n' && c != '\t' && c != '\0'
  819. X           && c != separator )  c = *++sp;
  820. X            *sp = '\0';
  821. X            while( c == ' ' || c == '\n' || c == '\t' )  c = *++sp;
  822. X            if ( c == '\0' )  {  /*   -o rev or branch   */
  823. X                pt->end = nil;   pt->code = 0;
  824. X                return;
  825. X            }
  826. X        if (c != separator) {
  827. X        faterror("invalid range %s %s after -o", pt->strt, sp);
  828. X            }
  829. X            while( (c = *++sp) == ' ' || c == '\n' || c == '\t')  ;
  830. X        if (!c) {  /* -orev: */
  831. X                pt->end = nil;   pt->code = 2;
  832. X                return;
  833. X            }
  834. X        }
  835. X    /* -orev1:rev2 */
  836. X    pt->end = sp;  pt->code = 3;
  837. X        while( c!= ' ' && c != '\n' && c != '\t' && c != '\0') c = *++sp;
  838. X        *sp = '\0';
  839. X}
  840. X
  841. X
  842. X
  843. X
  844. X    static void
  845. scanlogtext(delta,edit)
  846. X    struct hshentry *delta;
  847. X    int edit;
  848. X/* Function: Scans delta text nodes up to and including the one given
  849. X * by delta, or up to last one present, if delta==nil.
  850. X * For the one given by delta (if delta!=nil), the log message is saved into
  851. X * delta->log if delta==cuttail; the text is edited if EDIT is set, else copied.
  852. X * Assumes the initial lexeme must be read in first.
  853. X * Does not advance nexttok after it is finished, except if delta==nil.
  854. X */
  855. X{
  856. X    struct hshentry const *nextdelta;
  857. X    struct cbuf cb;
  858. X
  859. X    for (;;) {
  860. X        foutptr = 0;
  861. X        if (eoflex()) {
  862. X                    if(delta)
  863. X            faterror("can't find delta for revision %s", delta->num);
  864. X            return; /* no more delta text nodes */
  865. X                }
  866. X        nextlex();
  867. X        if (!(nextdelta=getnum()))
  868. X            faterror("delta number corrupted");
  869. X        if (nextdelta->selector) {
  870. X            foutptr = frewrite;
  871. X            aprintf(frewrite,DELNUMFORM,nextdelta->num,Klog);
  872. X                }
  873. X        getkeystring(Klog);
  874. X        if (nextdelta == cuttail) {
  875. X            cb = savestring(&curlogbuf);
  876. X            if (!delta->log.string)
  877. X                delta->log = cleanlogmsg(curlogbuf.string, cb.size);
  878. X        } else if (nextdelta->log.string && nextdelta->selector) {
  879. X            foutptr = 0;
  880. X            readstring();
  881. X            foutptr = frewrite;
  882. X            putstring(foutptr, false, nextdelta->log, true);
  883. X            afputc(nextc, foutptr);
  884. X                } else {readstring();
  885. X                }
  886. X                nextlex();
  887. X        while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  888. X            ignorephrase();
  889. X        getkeystring(Ktext);
  890. X
  891. X        if (delta==nextdelta)
  892. X            break;
  893. X        readstring(); /* skip over it */
  894. X
  895. X    }
  896. X    /* got the one we're looking for */
  897. X    if (edit)
  898. X        editstring((struct hshentry *)nil);
  899. X    else
  900. X        enterstring();
  901. X}
  902. X
  903. X
  904. X
  905. X    static struct Lockrev *
  906. rmnewlocklst(which)
  907. X    struct Lockrev const *which;
  908. X/*   Function:  remove lock to revision which->revno from newlocklst   */
  909. X
  910. X{
  911. X        struct  Lockrev   * pt, *pre;
  912. X
  913. X        while( newlocklst && (! strcmp(newlocklst->revno, which->revno))){
  914. X        struct Lockrev *pn = newlocklst->nextrev;
  915. X        tfree(newlocklst);
  916. X        newlocklst = pn;
  917. X        }
  918. X
  919. X        pt = pre = newlocklst;
  920. X        while( pt ) {
  921. X            if ( ! strcmp(pt->revno, which->revno) ) {
  922. X                pre->nextrev = pt->nextrev;
  923. X        tfree(pt);
  924. X        pt = pre->nextrev;
  925. X            }
  926. X            else {
  927. X                pre = pt;
  928. X                pt = pt->nextrev;
  929. X            }
  930. X        }
  931. X        return pre;
  932. X}
  933. X
  934. X
  935. X
  936. X    static void
  937. doaccess()
  938. X{
  939. X    register struct chaccess *ch;
  940. X    register struct access **p, *t;
  941. X
  942. X    for (ch = chaccess;  ch;  ch = ch->nextchaccess) {
  943. X        switch (ch->command) {
  944. X        case erase:
  945. X            if (!ch->login)
  946. X                AccessList = nil;
  947. X            else
  948. X                for (p = &AccessList;  (t = *p);  )
  949. X                if (strcmp(ch->login, t->login) == 0)
  950. X                    *p = t->nextaccess;
  951. X                else
  952. X                    p = &t->nextaccess;
  953. X            break;
  954. X        case append:
  955. X            for (p = &AccessList;  ;  p = &t->nextaccess)
  956. X                if (!(t = *p)) {
  957. X                    *p = t = ftalloc(struct access);
  958. X                    t->login = ch->login;
  959. X                    t->nextaccess = nil;
  960. X                    break;
  961. X                } else if (strcmp(ch->login, t->login) == 0)
  962. X                    break;
  963. X            break;
  964. X        }
  965. X    }
  966. X}
  967. X
  968. X
  969. X    static int
  970. sendmail(Delta, who)
  971. X    char const *Delta, *who;
  972. X/*   Function:  mail to who, informing him that his lock on delta was
  973. X *   broken by caller. Ask first whether to go ahead. Return false on
  974. X *   error or if user decides not to break the lock.
  975. X */
  976. X{
  977. X#ifdef SENDMAIL
  978. X    char const *messagefile;
  979. X    int old1, old2, c;
  980. X        FILE    * mailmess;
  981. X#endif
  982. X
  983. X
  984. X    aprintf(stderr, "Revision %s is already locked by %s.\n", Delta, who);
  985. X    if (!yesorno(false, "Do you want to break the lock? [ny](n): "))
  986. X        return false;
  987. X
  988. X        /* go ahead with breaking  */
  989. X#ifdef SENDMAIL
  990. X    messagefile = maketemp(0);
  991. X    if (!(mailmess = fopen(messagefile, "w"))) {
  992. X        efaterror(messagefile);
  993. X        }
  994. X
  995. X    aprintf(mailmess, "Subject: Broken lock on %s\n\nYour lock on revision %s of file %s\nhas been broken by %s for the following reason:\n",
  996. X        basename(RCSfilename), Delta, getfullRCSname(), getcaller()
  997. X    );
  998. X    aputs("State the reason for breaking the lock:\n(terminate with single '.' or end of file)\n>> ", stderr);
  999. X
  1000. X        old1 = '\n';    old2 = ' ';
  1001. X        for (; ;) {
  1002. X        c = getcstdin();
  1003. X        if (feof(stdin)) {
  1004. X        aprintf(mailmess, "%c\n", old1);
  1005. X                break;
  1006. X            }
  1007. X            else if ( c == '\n' && old1 == '.' && old2 == '\n')
  1008. X                break;
  1009. X            else {
  1010. X        afputc(old1, mailmess);
  1011. X                old2 = old1;   old1 = c;
  1012. X        if (c=='\n') aputs(">> ", stderr);
  1013. X            }
  1014. X        }
  1015. X    Ozclose(&mailmess);
  1016. X
  1017. X    if (run(messagefile, (char*)nil, SENDMAIL, who, (char*)nil))
  1018. X        warn("Mail may have failed."),
  1019. X#else
  1020. X        warn("Mail notification of broken locks is not available."),
  1021. X#endif
  1022. X        warn("Please tell `%s' why you broke the lock.", who);
  1023. X    return(true);
  1024. X}
  1025. X
  1026. X
  1027. X
  1028. X    static void
  1029. breaklock(delta)
  1030. X    struct hshentry const *delta;
  1031. X/* function: Finds the lock held by caller on delta,
  1032. X * and removes it.
  1033. X * Sends mail if a lock different from the caller's is broken.
  1034. X * Prints an error message if there is no such lock or error.
  1035. X */
  1036. X{
  1037. X        register struct lock * next, * trail;
  1038. X    char const *num;
  1039. X        struct lock dummy;
  1040. X
  1041. X    num=delta->num;
  1042. X        dummy.nextlock=next=Locks;
  1043. X        trail = &dummy;
  1044. X        while (next!=nil) {
  1045. X        if (strcmp(num, next->delta->num) == 0) {
  1046. X            if (
  1047. X                strcmp(getcaller(),next->login) != 0
  1048. X                &&    !sendmail(num, next->login)
  1049. X            ) {
  1050. X                error("%s still locked by %s", num, next->login);
  1051. X                return;
  1052. X            }
  1053. X            break; /* exact match */
  1054. X                }
  1055. X                trail=next;
  1056. X                next=next->nextlock;
  1057. X        }
  1058. X        if (next!=nil) {
  1059. X                /*found one */
  1060. X        diagnose("%s unlocked\n",next->delta->num);
  1061. X                trail->nextlock=next->nextlock;
  1062. X                next->delta->lockedby=nil;
  1063. X                Locks=dummy.nextlock;
  1064. X        } else  {
  1065. X        error("no lock set on revision %s", num);
  1066. X        }
  1067. X}
  1068. X
  1069. X
  1070. X
  1071. X    static struct hshentry *
  1072. searchcutpt(object, length, store)
  1073. X    char const *object;
  1074. X    unsigned length;
  1075. X    struct hshentries *store;
  1076. X/*   Function:  Search store and return entry with number being object. */
  1077. X/*              cuttail = nil, if the entry is Head; otherwise, cuttail */
  1078. X/*              is the entry point to the one with number being object  */
  1079. X
  1080. X{
  1081. X    cuthead = nil;
  1082. X    while (compartial(store->first->num, object, length)) {
  1083. X        cuthead = store->first;
  1084. X        store = store->rest;
  1085. X    }
  1086. X    return store->first;
  1087. X}
  1088. X
  1089. X
  1090. X
  1091. X    static int
  1092. branchpoint(strt, tail)
  1093. struct  hshentry        *strt,  *tail;
  1094. X/*   Function: check whether the deltas between strt and tail    */
  1095. X/*        are locked or branch point, return 1 if any is  */
  1096. X/*        locked or branch point; otherwise, return 0 and */
  1097. X/*        mark deleted                    */
  1098. X
  1099. X{
  1100. X        struct  hshentry    *pt;
  1101. X    struct lock const *lockpt;
  1102. X        int     flag;
  1103. X
  1104. X
  1105. X        pt = strt;
  1106. X        flag = false;
  1107. X        while( pt != tail) {
  1108. X            if ( pt->branches ){ /*  a branch point  */
  1109. X                flag = true;
  1110. X        error("can't remove branch point %s", pt->num);
  1111. X            }
  1112. X        lockpt = Locks;
  1113. X        while(lockpt && lockpt->delta != pt)
  1114. X        lockpt = lockpt->nextlock;
  1115. X        if ( lockpt ) {
  1116. X        flag = true;
  1117. X        error("can't remove locked revision %s",pt->num);
  1118. X        }
  1119. X            pt = pt->next;
  1120. X        }
  1121. X
  1122. X        if ( ! flag ) {
  1123. X            pt = strt;
  1124. X            while( pt != tail ) {
  1125. X        pt->selector = false;
  1126. X        diagnose("deleting revision %s\n",pt->num);
  1127. X                pt = pt->next;
  1128. X            }
  1129. X        }
  1130. X        return flag;
  1131. X}
  1132. X
  1133. X
  1134. X
  1135. X    static int
  1136. removerevs()
  1137. X/*   Function:  get the revision range to be removed, and place the     */
  1138. X/*              first revision removed in delstrt, the revision before  */
  1139. X/*              delstrt in cuthead( nil, if delstrt is head), and the   */
  1140. X/*              revision after the last removed revision in cuttail(nil */
  1141. X/*              if the last is a leaf                                   */
  1142. X
  1143. X{
  1144. X    struct  hshentry *target, *target2, *temp;
  1145. X    unsigned length;
  1146. X    int flag;
  1147. X
  1148. X        flag = false;
  1149. X    if (!expandsym(delrev.strt, &numrev)) return 0;
  1150. X    target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1151. X        if ( ! target ) return 0;
  1152. X    if (cmpnum(target->num, numrev.string)) flag = true;
  1153. X    length = countnumflds(numrev.string);
  1154. X
  1155. X    if (delrev.code == 0) {  /*  -o  rev    or    -o  branch   */
  1156. X        if (length & 1)
  1157. X        temp=searchcutpt(target->num,length+1,gendeltas);
  1158. X        else if (flag) {
  1159. X        error("Revision %s doesn't exist.", numrev.string);
  1160. X        return 0;
  1161. X        }
  1162. X        else
  1163. X        temp = searchcutpt(numrev.string, length, gendeltas);
  1164. X        cuttail = target->next;
  1165. X            if ( branchpoint(temp, cuttail) ) {
  1166. X                cuttail = nil;
  1167. X                return 0;
  1168. X            }
  1169. X            delstrt = temp;     /* first revision to be removed   */
  1170. X            return 1;
  1171. X        }
  1172. X
  1173. X    if (length & 1) {   /*  invalid branch after -o   */
  1174. X        error("invalid branch range %s after -o", numrev.string);
  1175. X            return 0;
  1176. X        }
  1177. X
  1178. X    if (delrev.code == 1) {  /*  -o  -rev   */
  1179. X            if ( length > 2 ) {
  1180. X                temp = searchcutpt( target->num, length-1, gendeltas);
  1181. X                cuttail = target->next;
  1182. X            }
  1183. X            else {
  1184. X                temp = searchcutpt(target->num, length, gendeltas);
  1185. X                cuttail = target;
  1186. X                while( cuttail && ! cmpnumfld(target->num,cuttail->num,1) )
  1187. X                    cuttail = cuttail->next;
  1188. X            }
  1189. X            if ( branchpoint(temp, cuttail) ){
  1190. X                cuttail = nil;
  1191. X                return 0;
  1192. X            }
  1193. X            delstrt = temp;
  1194. X            return 1;
  1195. X        }
  1196. X
  1197. X    if (delrev.code == 2) {   /*  -o  rev-   */
  1198. X            if ( length == 2 ) {
  1199. X                temp = searchcutpt(target->num, 1,gendeltas);
  1200. X                if ( flag)
  1201. X                    cuttail = target;
  1202. X                else
  1203. X                    cuttail = target->next;
  1204. X            }
  1205. X            else  {
  1206. X                if ( flag){
  1207. X                    cuthead = target;
  1208. X                    if ( !(temp = target->next) ) return 0;
  1209. X                }
  1210. X                else
  1211. X                    temp = searchcutpt(target->num, length, gendeltas);
  1212. X        getbranchno(temp->num, &numrev);  /* get branch number */
  1213. X        target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1214. X            }
  1215. X            if ( branchpoint( temp, cuttail ) ) {
  1216. X                cuttail = nil;
  1217. X                return 0;
  1218. X            }
  1219. X            delstrt = temp;
  1220. X            return 1;
  1221. X        }
  1222. X
  1223. X        /*   -o   rev1-rev2   */
  1224. X    if (!expandsym(delrev.end, &numrev)) return 0;
  1225. X    if (
  1226. X        length != countnumflds(numrev.string)
  1227. X        ||    length>2 && compartial(numrev.string, target->num, length-1)
  1228. X    ) {
  1229. X        error("invalid revision range %s-%s", target->num, numrev.string);
  1230. X            return 0;
  1231. X        }
  1232. X
  1233. X    target2 = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas);
  1234. X        if ( ! target2 ) return 0;
  1235. X
  1236. X        if ( length > 2) {  /* delete revisions on branches  */
  1237. X            if ( cmpnum(target->num, target2->num) > 0) {
  1238. X        if (cmpnum(target2->num, numrev.string))
  1239. X                    flag = true;
  1240. X                else
  1241. X                    flag = false;
  1242. X                temp = target;
  1243. X                target = target2;
  1244. X                target2 = temp;
  1245. X            }
  1246. X            if ( flag ) {
  1247. X                if ( ! cmpnum(target->num, target2->num) ) {
  1248. X            error("Revisions %s-%s don't exist.", delrev.strt,delrev.end);
  1249. X                    return 0;
  1250. X                }
  1251. X                cuthead = target;
  1252. X                temp = target->next;
  1253. X            }
  1254. X            else
  1255. X                temp = searchcutpt(target->num, length, gendeltas);
  1256. X            cuttail = target2->next;
  1257. X        }
  1258. X        else { /*  delete revisions on trunk  */
  1259. X            if ( cmpnum( target->num, target2->num) < 0 ) {
  1260. X                temp = target;
  1261. X                target = target2;
  1262. X                target2 = temp;
  1263. X            }
  1264. X            else
  1265. X        if (cmpnum(target2->num, numrev.string))
  1266. X                    flag = true;
  1267. X                else
  1268. X                    flag = false;
  1269. X            if ( flag ) {
  1270. X                if ( ! cmpnum(target->num, target2->num) ) {
  1271. X            error("Revisions %s-%s don't exist.", delrev.strt, delrev.end);
  1272. X                    return 0;
  1273. X                }
  1274. X                cuttail = target2;
  1275. X            }
  1276. X            else
  1277. X                cuttail = target2->next;
  1278. X            temp = searchcutpt(target->num, length, gendeltas);
  1279. X        }
  1280. X        if ( branchpoint(temp, cuttail) )  {
  1281. X            cuttail = nil;
  1282. X            return 0;
  1283. X        }
  1284. X        delstrt = temp;
  1285. X        return 1;
  1286. X}
  1287. X
  1288. X
  1289. X
  1290. X    static void
  1291. doassoc()
  1292. X/*   Function: add or delete(if revno is nil) association    */
  1293. X/*        which is stored in assoclst            */
  1294. X
  1295. X{
  1296. X    char const *p;
  1297. X    struct Symrev const *curassoc;
  1298. X    struct  assoc   * pre,  * pt;
  1299. X
  1300. X        /*  add new associations   */
  1301. X        curassoc = assoclst;
  1302. X        while( curassoc ) {
  1303. X            if ( curassoc->revno == nil ) {  /* delete symbol  */
  1304. X        pre = pt = Symbols;
  1305. X                while( pt && strcmp(pt->symbol,curassoc->ssymbol) ) {
  1306. X            pre = pt;
  1307. X            pt = pt->nextassoc;
  1308. X        }
  1309. X        if ( pt )
  1310. X            if ( pre == pt )
  1311. X            Symbols = pt->nextassoc;
  1312. X            else
  1313. X            pre->nextassoc = pt->nextassoc;
  1314. X        else
  1315. X            warn("can't delete nonexisting symbol %s",curassoc->ssymbol);
  1316. X        }
  1317. X        else {
  1318. X        if (curassoc->revno[0]) {
  1319. X            p = 0;
  1320. X            if (expandsym(curassoc->revno, &numrev))
  1321. X            p = fstr_save(numrev.string);
  1322. X        } else if (!(p = tiprev()))
  1323. X            error("no latest revision to associate with symbol %s",
  1324. X                curassoc->ssymbol
  1325. X            );
  1326. X        if (p)
  1327. X            VOID addsymbol(p, curassoc->ssymbol, curassoc->override);
  1328. X        }
  1329. X            curassoc = curassoc->nextsym;
  1330. X        }
  1331. X
  1332. X}
  1333. X
  1334. X
  1335. X
  1336. X    static void
  1337. dolocks()
  1338. X/* Function: remove lock for caller or first lock if unlockcaller is set;
  1339. X *           remove locks which are stored in rmvlocklst,
  1340. X *           add new locks which are stored in newlocklst,
  1341. X *           add lock for Dbranch or Head if lockhead is set.
  1342. X */
  1343. X{
  1344. X    struct Lockrev const *lockpt;
  1345. X    struct hshentry *target;
  1346. X
  1347. X    if (unlockcaller) { /*  find lock for caller  */
  1348. X            if ( Head ) {
  1349. X        if (Locks) {
  1350. X            switch (findlock(true, &target)) {
  1351. X              case 0:
  1352. X            breaklock(Locks->delta); /* remove most recent lock */
  1353. X            break;
  1354. X              case 1:
  1355. X            diagnose("%s unlocked\n",target->num);
  1356. X            break;
  1357. X            }
  1358. X        } else {
  1359. X            warn("No locks are set.");
  1360. X        }
  1361. X            } else {
  1362. X        warn("can't unlock an empty tree");
  1363. X            }
  1364. X        }
  1365. X
  1366. X        /*  remove locks which are stored in rmvlocklst   */
  1367. X        lockpt = rmvlocklst;
  1368. X        while( lockpt ) {
  1369. X        if (expandsym(lockpt->revno, &numrev)) {
  1370. X        target = genrevs(numrev.string, (char *)nil, (char *)nil, (char *)nil, &gendeltas);
  1371. X                if ( target )
  1372. X           if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1373. X            error("can't unlock nonexisting revision %s",lockpt->revno);
  1374. X                   else
  1375. X            breaklock(target);
  1376. X                        /* breaklock does its own diagnose */
  1377. X            }
  1378. X            lockpt = lockpt->nextrev;
  1379. X        }
  1380. X
  1381. X        /*  add new locks which stored in newlocklst  */
  1382. X        lockpt = newlocklst;
  1383. X        while( lockpt ) {
  1384. X        setlock(lockpt->revno);
  1385. X            lockpt = lockpt->nextrev;
  1386. X        }
  1387. X
  1388. X    if (lockhead) {  /*  lock default branch or head  */
  1389. X            if (Dbranch) {
  1390. X        setlock(Dbranch);
  1391. X        } else if (Head) {
  1392. X        if (0 <= addlock(Head))
  1393. X            diagnose("%s locked\n",Head->num);
  1394. X            } else {
  1395. X        warn("can't lock an empty tree");
  1396. X            }
  1397. X        }
  1398. X
  1399. X}
  1400. X
  1401. X
  1402. X
  1403. X    static void
  1404. setlock(rev)
  1405. X    char const *rev;
  1406. X/* Function: Given a revision or branch number, finds the corresponding
  1407. X * delta and locks it for caller.
  1408. X */
  1409. X{
  1410. X        struct  hshentry *target;
  1411. X
  1412. X    if (expandsym(rev, &numrev)) {
  1413. X        target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1414. X                 (char*)nil, &gendeltas);
  1415. X            if ( target )
  1416. X           if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1417. X            error("can't lock nonexisting revision %s", numrev.string);
  1418. X               else
  1419. X            if (0 <= addlock(target))
  1420. X            diagnose("%s locked\n", target->num);
  1421. X        }
  1422. X}
  1423. X
  1424. X
  1425. X    static void
  1426. domessages()
  1427. X{
  1428. X    struct hshentry *target;
  1429. X    struct Message *p;
  1430. X
  1431. X    for (p = messagelst;  p;  p = p->nextmessage)
  1432. X        if (
  1433. X        expandsym(p->revno, &numrev)  &&
  1434. X        (target = genrevs(
  1435. X            numrev.string, (char*)0, (char*)0, (char*)0, &gendeltas
  1436. X        ))
  1437. X        )
  1438. X        target->log = p->message;
  1439. X}
  1440. X
  1441. X
  1442. X    static void
  1443. rcs_setstate(rev,status)
  1444. X    char const *rev, *status;
  1445. X/* Function: Given a revision or branch number, finds the corresponding delta
  1446. X * and sets its state to status.
  1447. X */
  1448. X{
  1449. X        struct  hshentry *target;
  1450. X
  1451. X    if (expandsym(rev, &numrev)) {
  1452. X        target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1453. X                 (char*)nil, &gendeltas);
  1454. X            if ( target )
  1455. X           if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1456. X            error("can't set state of nonexisting revision %s to %s",
  1457. X              numrev.string, status);
  1458. X               else
  1459. X                    target->state = status;
  1460. X        }
  1461. X}
  1462. X
  1463. X
  1464. X
  1465. X
  1466. X
  1467. X    static int
  1468. buildeltatext(deltas)
  1469. X    struct hshentries const *deltas;
  1470. X/*   Function:  put the delta text on frewrite and make necessary   */
  1471. X/*              change to delta text                                */
  1472. X{
  1473. X    register FILE *fcut;    /* temporary file to rebuild delta tree */
  1474. X    char const *cutfilename, *diffilename;
  1475. X
  1476. X    cutfilename = nil;
  1477. X    cuttail->selector = false;
  1478. X    scanlogtext(deltas->first, false);
  1479. X        if ( cuthead )  {
  1480. X        cutfilename = maketemp(3);
  1481. X        if (!(fcut = fopen(cutfilename, FOPEN_W_WORK))) {
  1482. X        efaterror(cutfilename);
  1483. X            }
  1484. X
  1485. X        while (deltas->first != cuthead) {
  1486. X        deltas = deltas->rest;
  1487. X        scanlogtext(deltas->first, true);
  1488. X            }
  1489. X
  1490. X        snapshotedit(fcut);
  1491. X        Ofclose(fcut);
  1492. X        }
  1493. X
  1494. X    while (deltas->first != cuttail)
  1495. X        scanlogtext((deltas = deltas->rest)->first, true);
  1496. X    finishedit((struct hshentry *)nil, (FILE*)0, true);
  1497. X    Ozclose(&fcopy);
  1498. X
  1499. X        if ( cuthead ) {
  1500. X        diffilename = maketemp(0);
  1501. X        switch (run((char*)nil,diffilename,
  1502. X            DIFF DIFF_FLAGS, cutfilename, resultfile, (char*)nil
  1503. X        )) {
  1504. X        case DIFF_FAILURE: case DIFF_SUCCESS: break;
  1505. X        default: faterror ("diff failed");
  1506. X        }
  1507. X        return putdtext(cuttail->num,cuttail->log,diffilename,frewrite,true);
  1508. X    } else
  1509. X        return putdtext(cuttail->num,cuttail->log,resultfile,frewrite,false);
  1510. X}
  1511. X
  1512. X
  1513. X
  1514. X    static void
  1515. buildtree()
  1516. X/*   Function:  actually removes revisions whose selector field  */
  1517. X/*        is false, and rebuilds the linkage of deltas.     */
  1518. X/*              asks for reconfirmation if deleting last revision*/
  1519. X{
  1520. X    struct    hshentry   * Delta;
  1521. X        struct  branchhead      *pt, *pre;
  1522. X
  1523. X        if ( cuthead )
  1524. X           if ( cuthead->next == delstrt )
  1525. X                cuthead->next = cuttail;
  1526. X           else {
  1527. X                pre = pt = cuthead->branches;
  1528. X                while( pt && pt->hsh != delstrt )  {
  1529. X                    pre = pt;
  1530. X                    pt = pt->nextbranch;
  1531. X                }
  1532. X                if ( cuttail )
  1533. X                    pt->hsh = cuttail;
  1534. X                else if ( pt == pre )
  1535. X                    cuthead->branches = pt->nextbranch;
  1536. X                else
  1537. X                    pre->nextbranch = pt->nextbranch;
  1538. X            }
  1539. X    else {
  1540. X            if ( cuttail == nil && !quietflag) {
  1541. X        if (!yesorno(false, "Do you really want to delete all revisions? [ny](n): ")) {
  1542. X            error("No revision deleted");
  1543. X            Delta = delstrt;
  1544. X            while( Delta) {
  1545. X            Delta->selector = true;
  1546. X            Delta = Delta->next;
  1547. X            }
  1548. X            return;
  1549. X        }
  1550. X        }
  1551. X            Head = cuttail;
  1552. X    }
  1553. X        return;
  1554. X}
  1555. X
  1556. X#if lint
  1557. X/* This lets us lint everything all at once. */
  1558. X
  1559. char const cmdid[] = "";
  1560. X
  1561. X#define go(p,e) {int p P((int,char**)); void e P((void)); if(*argv)return p(argc,argv);if(*argv[1])e();}
  1562. X
  1563. X    int
  1564. main(argc, argv)
  1565. X    int argc;
  1566. X    char **argv;
  1567. X{
  1568. X    go(ciId,    ciExit);
  1569. X    go(coId,    coExit);
  1570. X    go(identId,    identExit);
  1571. X    go(mergeId,    mergeExit);
  1572. X    go(rcsId,    exiterr);
  1573. X    go(rcscleanId,    rcscleanExit);
  1574. X    go(rcsdiffId,    rdiffExit);
  1575. X    go(rcsmergeId,    rmergeExit);
  1576. X    go(rlogId,    rlogExit);
  1577. X    return 0;
  1578. X}
  1579. X#endif
  1580. END_OF_FILE
  1581. if test 43795 -ne `wc -c <'src/rcs.c'`; then
  1582.     echo shar: \"'src/rcs.c'\" unpacked with wrong size!
  1583. fi
  1584. # end of 'src/rcs.c'
  1585. fi
  1586. if test -f 'src/rcsedit.c' -a "${1}" != "-c" ; then 
  1587.   echo shar: Will not clobber existing file \"'src/rcsedit.c'\"
  1588. else
  1589. echo shar: Extracting \"'src/rcsedit.c'\" \(39683 characters\)
  1590. sed "s/^X//" >'src/rcsedit.c' <<'END_OF_FILE'
  1591. X/*
  1592. X *                     RCS stream editor
  1593. X */
  1594. X/**********************************************************************************
  1595. X *                       edits the input file according to a
  1596. X *                       script from stdin, generated by diff -n
  1597. X *                       performs keyword expansion
  1598. X **********************************************************************************
  1599. X */
  1600. X
  1601. X/* Copyright (C) 1982, 1988, 1989 Walter Tichy
  1602. X   Copyright 1990, 1991 by Paul Eggert
  1603. X   Distributed under license by the Free Software Foundation, Inc.
  1604. X
  1605. This file is part of RCS.
  1606. X
  1607. RCS is free software; you can redistribute it and/or modify
  1608. it under the terms of the GNU General Public License as published by
  1609. the Free Software Foundation; either version 2, or (at your option)
  1610. any later version.
  1611. X
  1612. RCS is distributed in the hope that it will be useful,
  1613. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1614. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1615. GNU General Public License for more details.
  1616. X
  1617. You should have received a copy of the GNU General Public License
  1618. along with RCS; see the file COPYING.  If not, write to
  1619. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  1620. X
  1621. Report problems and direct all questions to:
  1622. X
  1623. X    rcs-bugs@cs.purdue.edu
  1624. X
  1625. X*/
  1626. X
  1627. X
  1628. X/* $Log: rcsedit.c,v $
  1629. X * Revision 5.11  1991/11/03  01:11:44  eggert
  1630. X * Move the warning about link breaking to where they're actually being broken.
  1631. X *
  1632. X * Revision 5.10  1991/10/07  17:32:46  eggert
  1633. X * Support piece tables even if !has_mmap.  Fix rare NFS bugs.
  1634. X *
  1635. X * Revision 5.9  1991/09/17  19:07:40  eggert
  1636. X * SGI readlink() yields ENXIO, not EINVAL, for nonlinks.
  1637. X *
  1638. X * Revision 5.8  1991/08/19  03:13:55  eggert
  1639. X * Add piece tables, NFS bug workarounds.  Catch odd filenames.  Tune.
  1640. X *
  1641. X * Revision 5.7  1991/04/21  11:58:21  eggert
  1642. X * Fix errno bugs.  Add -x, RCSINIT, MS-DOS support.
  1643. X *
  1644. X * Revision 5.6  1991/02/25  07:12:40  eggert
  1645. X * Fix setuid bug.  Support new link behavior.  Work around broken "w+" fopen.
  1646. X *
  1647. X * Revision 5.5  1990/12/30  05:07:35  eggert
  1648. X * Fix report of busy RCS files when !defined(O_CREAT) | !defined(O_EXCL).
  1649. X *
  1650. X * Revision 5.4  1990/11/01  05:03:40  eggert
  1651. X * Permit arbitrary data in comment leaders.
  1652. X *
  1653. X * Revision 5.3  1990/09/11  02:41:13  eggert
  1654. X * Tune expandline().
  1655. X *
  1656. X * Revision 5.2  1990/09/04  08:02:21  eggert
  1657. X * Count RCS lines better.  Improve incomplete line handling.
  1658. X *
  1659. X * Revision 5.1  1990/08/29  07:13:56  eggert
  1660. X * Add -kkvl.
  1661. X * Fix bug when getting revisions to files ending in incomplete lines.
  1662. X * Fix bug in comment leader expansion.
  1663. X *
  1664. X * Revision 5.0  1990/08/22  08:12:47  eggert
  1665. X * Don't require final newline.
  1666. X * Don't append "checked in with -k by " to logs,
  1667. X * so that checking in a program with -k doesn't change it.
  1668. X * Don't generate trailing white space for empty comment leader.
  1669. X * Remove compile-time limits; use malloc instead.  Add -k, -V.
  1670. X * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  1671. X * Ansify and Posixate.  Check diff's output.
  1672. X *
  1673. X * Revision 4.8  89/05/01  15:12:35  narten
  1674. X * changed copyright header to reflect current distribution rules
  1675. X * 
  1676. X * Revision 4.7  88/11/08  13:54:14  narten
  1677. X * misplaced semicolon caused infinite loop
  1678. X * 
  1679. X * Revision 4.6  88/08/09  19:12:45  eggert
  1680. X * Shrink stdio code size; allow cc -R.
  1681. X * 
  1682. X * Revision 4.5  87/12/18  11:38:46  narten
  1683. X * Changes from the 43. version. Don't know the significance of the
  1684. X * first change involving "rewind". Also, additional "lint" cleanup.
  1685. X * (Guy Harris)
  1686. X * 
  1687. X * Revision 4.4  87/10/18  10:32:21  narten
  1688. X * Updating version numbers. Changes relative to version 1.1 actually
  1689. X * relative to 4.1
  1690. X * 
  1691. X * Revision 1.4  87/09/24  13:59:29  narten
  1692. X * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  1693. X * warnings)
  1694. X * 
  1695. X * Revision 1.3  87/09/15  16:39:39  shepler
  1696. X * added an initializatin of the variables editline and linecorr
  1697. X * this will be done each time a file is processed.
  1698. X * (there was an obscure bug where if co was used to retrieve multiple files
  1699. X *  it would dump)
  1700. X * fix attributed to  Roy Morris @FileNet Corp ...!felix!roy
  1701. X * 
  1702. X * Revision 1.2  87/03/27  14:22:17  jenkins
  1703. X * Port to suns
  1704. X * 
  1705. X * Revision 4.1  83/05/12  13:10:30  wft
  1706. X * Added new markers Id and RCSfile; added locker to Header and Id.
  1707. X * Overhauled expandline completely() (problem with $01234567890123456789@).
  1708. X * Moved trymatch() and marker table to rcskeys.c.
  1709. X * 
  1710. X * Revision 3.7  83/05/12  13:04:39  wft
  1711. X * Added retry to expandline to resume after failed match which ended in $.
  1712. X * Fixed truncation problem for $19chars followed by@@.
  1713. X * Log no longer expands full path of RCS file.
  1714. X * 
  1715. X * Revision 3.6  83/05/11  16:06:30  wft
  1716. X * added retry to expandline to resume after failed match which ended in $.
  1717. X * Fixed truncation problem for $19chars followed by@@.
  1718. X * 
  1719. X * Revision 3.5  82/12/04  13:20:56  wft
  1720. X * Added expansion of keyword Locker.
  1721. X *
  1722. X * Revision 3.4  82/12/03  12:26:54  wft
  1723. X * Added line number correction in case editing does not start at the
  1724. X * beginning of the file.
  1725. X * Changed keyword expansion to always print a space before closing KDELIM;
  1726. X * Expansion for Header shortened.
  1727. X *
  1728. X * Revision 3.3  82/11/14  14:49:30  wft
  1729. X * removed Suffix from keyword expansion. Replaced fclose with ffclose.
  1730. X * keyreplace() gets log message from delta, not from curlogmsg.
  1731. X * fixed expression overflow in while(c=putc(GETC....
  1732. X * checked nil printing.
  1733. X *
  1734. X * Revision 3.2  82/10/18  21:13:39  wft
  1735. X * I added checks for write errors during the co process, and renamed
  1736. X * expandstring() to xpandstring().
  1737. X *
  1738. X * Revision 3.1  82/10/13  15:52:55  wft
  1739. X * changed type of result of getc() from char to int.
  1740. X * made keyword expansion loop in expandline() portable to machines
  1741. X * without sign-extension.
  1742. X */
  1743. X
  1744. X
  1745. X#include "rcsbase.h"
  1746. X
  1747. libId(editId, "$Id: rcsedit.c,v 5.11 1991/11/03 01:11:44 eggert Exp $")
  1748. X
  1749. static void keyreplace P((enum markers,struct hshentry const*,FILE*));
  1750. X
  1751. X
  1752. XFILE *fcopy;         /* result file descriptor                */
  1753. char const *resultfile;  /* result file name                    */
  1754. int locker_expansion;     /* should the locker name be appended to Id val?   */
  1755. X#if !large_memory
  1756. X    static RILE *fedit; /* edit file descriptor */
  1757. X    static char const *editfile; /* edit pathname */
  1758. X#endif
  1759. static unsigned long editline; /* edit line counter; #lines before cursor   */
  1760. static long linecorr; /* #adds - #deletes in each edit run.            */
  1761. X               /*used to correct editline in case file is not rewound after */
  1762. X               /* applying one delta                                        */
  1763. X
  1764. X#define DIRTEMPNAMES 2
  1765. enum maker {notmade, real, effective};
  1766. struct buf dirtfname[DIRTEMPNAMES];        /* unlink these when done */
  1767. static enum maker volatile dirtfmaker[DIRTEMPNAMES];    /* if these are set */
  1768. X
  1769. X
  1770. X#if has_NFS || bad_unlink
  1771. X    int
  1772. un_link(s)
  1773. X    char const *s;
  1774. X/*
  1775. X * Remove S, even if it is unwritable.
  1776. X * Ignore unlink() ENOENT failures; NFS generates bogus ones.
  1777. X */
  1778. X{
  1779. X#    if bad_unlink
  1780. X        int e;
  1781. X        if (unlink(s) == 0)
  1782. X            return 0;
  1783. X        e = errno;
  1784. X#        if has_NFS
  1785. X            if (e == ENOENT)
  1786. X                return 0;
  1787. X#        endif
  1788. X        if (chmod(s, S_IWUSR) != 0) {
  1789. X            errno = e;
  1790. X            return -1;
  1791. X        }
  1792. X#    endif
  1793. X#    if has_NFS
  1794. X        return unlink(s)==0 || errno==ENOENT  ?  0  :  -1;
  1795. X#    else
  1796. X        return unlink(s);
  1797. X#    endif
  1798. X}
  1799. X#endif
  1800. X
  1801. X#if !has_rename
  1802. X#  if !has_NFS
  1803. X#    define do_link(s,t) link(s,t)
  1804. X#  else
  1805. X    static int
  1806. do_link(s, t)
  1807. X    char const *s, *t;
  1808. X/* Link S to T, ignoring bogus EEXIST problems due to NFS failures.  */
  1809. X{
  1810. X    struct stat sb, tb;
  1811. X
  1812. X    if (link(s,t) == 0)
  1813. X        return 0;
  1814. X    if (errno != EEXIST)
  1815. X        return -1;
  1816. X    if (
  1817. X        stat(s, &sb) == 0  &&
  1818. X        stat(t, &tb) == 0  &&
  1819. X        sb.st_ino == tb.st_ino  &&
  1820. X        sb.st_dev == tb.st_dev
  1821. X    )
  1822. X        return 0;
  1823. X    errno = EEXIST;
  1824. X    return -1;
  1825. X}
  1826. X#  endif
  1827. X#endif
  1828. X
  1829. X
  1830. X    static exiting void
  1831. editEndsPrematurely()
  1832. X{
  1833. X    fatserror("edit script ends prematurely");
  1834. X}
  1835. X
  1836. X    static exiting void
  1837. editLineNumberOverflow()
  1838. X{
  1839. X    fatserror("edit script refers to line past end of file");
  1840. X}
  1841. X
  1842. X
  1843. X#if large_memory
  1844. X
  1845. X#if has_memmove
  1846. X#    define movelines(s1, s2, n) VOID memmove(s1, s2, (n)*sizeof(Iptr_type))
  1847. X#else
  1848. X    static void
  1849. movelines(s1, s2, n)
  1850. X    register Iptr_type *s1;
  1851. X    register Iptr_type const *s2;
  1852. X    register unsigned long n;
  1853. X{
  1854. X    if (s1 < s2)
  1855. X        do {
  1856. X            *s1++ = *s2++;
  1857. X        } while (--n);
  1858. X    else {
  1859. X        s1 += n;
  1860. X        s2 += n;
  1861. X        do {
  1862. X            *--s1 = *--s2;
  1863. X        } while (--n);
  1864. X    }
  1865. X}
  1866. X#endif
  1867. X
  1868. X/*
  1869. X * `line' contains pointers to the lines in the currently `edited' file.
  1870. X * It is a 0-origin array that represents linelim-gapsize lines.
  1871. X * line[0..gap-1] and line[gap+gapsize..linelim-1] contain pointers to lines.
  1872. X * line[gap..gap+gapsize-1] contains garbage.
  1873. X *
  1874. X * Any @s in lines are duplicated.
  1875. X * Lines are terminated by \n, or (for a last partial line only) by single @.
  1876. X */
  1877. static Iptr_type *line;
  1878. static unsigned long gap, gapsize, linelim;
  1879. X
  1880. X
  1881. X    static void
  1882. insertline(n, l)
  1883. X    unsigned long n;
  1884. X    Iptr_type l;
  1885. X/* Before line N, insert line L.  N is 0-origin.  */
  1886. X{
  1887. X    if (linelim-gapsize < n)
  1888. X        editLineNumberOverflow();
  1889. X    if (!gapsize)
  1890. X        line =
  1891. X        !linelim ?
  1892. X            tnalloc(Iptr_type, linelim = gapsize = 1024)
  1893. X        : (
  1894. X            gap = gapsize = linelim,
  1895. X            trealloc(Iptr_type, line, linelim <<= 1)
  1896. X        );
  1897. X    if (n < gap)
  1898. X        movelines(line+n+gapsize, line+n, gap-n);
  1899. X    else if (gap < n)
  1900. X        movelines(line+gap, line+gap+gapsize, n-gap);
  1901. X
  1902. X    line[n] = l;
  1903. X    gap = n + 1;
  1904. X    gapsize--;
  1905. X}
  1906. X
  1907. X    static void
  1908. deletelines(n, nlines)
  1909. X    unsigned long n, nlines;
  1910. X/* Delete lines N through N+NLINES-1.  N is 0-origin.  */
  1911. X{
  1912. X    unsigned long l = n + nlines;
  1913. X    if (linelim-gapsize < l  ||  l < n)
  1914. X        editLineNumberOverflow();
  1915. X    if (l < gap)
  1916. X        movelines(line+l+gapsize, line+l, gap-l);
  1917. X    else if (gap < n)
  1918. X        movelines(line+gap, line+gap+gapsize, n-gap);
  1919. X
  1920. X    gap = n;
  1921. X    gapsize += nlines;
  1922. X}
  1923. X
  1924. X    static void
  1925. snapshotline(f, l)
  1926. X    register FILE *f;
  1927. X    register Iptr_type l;
  1928. X{
  1929. X    register int c;
  1930. X    do {
  1931. X        if ((c = *l++) == SDELIM  &&  *l++ != SDELIM)
  1932. X            return;
  1933. X        aputc(c, f);
  1934. X    } while (c != '\n');
  1935. X}
  1936. X
  1937. X    void
  1938. snapshotedit(f)
  1939. X    FILE *f;
  1940. X/* Copy the current state of the edits to F.  */
  1941. X{
  1942. X    register Iptr_type *p, *lim, *l=line;
  1943. X    for (p=l, lim=l+gap;  p<lim;  )
  1944. X        snapshotline(f, *p++);
  1945. X    for (p+=gapsize, lim=l+linelim;  p<lim;  )
  1946. X        snapshotline(f, *p++);
  1947. X}
  1948. X
  1949. X    static void
  1950. finisheditline(fin, fout, l, delta)
  1951. X    RILE *fin;
  1952. X    FILE *fout;
  1953. X    Iptr_type l;
  1954. X    struct hshentry const *delta;
  1955. X{
  1956. X    Iseek(fin, l);
  1957. X    if (expandline(fin, fout, delta, true, (FILE*)0)  <  0)
  1958. X        faterror("finisheditline internal error");
  1959. X}
  1960. X
  1961. X    void
  1962. finishedit(delta, outfile, done)
  1963. X    struct hshentry const *delta;
  1964. X    FILE *outfile;
  1965. X    int done;
  1966. X/*
  1967. X * Doing expansion if DELTA is set, output the state of the edits to OUTFILE.
  1968. X * But do nothing unless DONE is set (which means we are on the last pass).
  1969. X */
  1970. X{
  1971. X    if (done) {
  1972. X        openfcopy(outfile);
  1973. X        outfile = fcopy;
  1974. X        if (!delta)
  1975. X            snapshotedit(outfile);
  1976. X        else {
  1977. X            register Iptr_type *p, *lim, *l = line;
  1978. X            register RILE *fin = finptr;
  1979. X            Iptr_type here = Itell(fin);
  1980. X            for (p=l, lim=l+gap;  p<lim;  )
  1981. X                finisheditline(fin, outfile, *p++, delta);
  1982. X            for (p+=gapsize, lim=l+linelim;  p<lim;  )
  1983. X                finisheditline(fin, outfile, *p++, delta);
  1984. X            Iseek(fin, here);
  1985. X        }
  1986. X    }
  1987. X}
  1988. X
  1989. X/* Open a temporary FILENAME for output, truncating any previous contents.  */
  1990. X#   define fopen_update_truncate(filename) fopen(filename, FOPEN_W_WORK)
  1991. X#else /* !large_memory */
  1992. X    static FILE *
  1993. fopen_update_truncate(filename)
  1994. X    char const *filename;
  1995. X{
  1996. X#    if bad_fopen_wplus
  1997. X        if (un_link(filename) != 0)
  1998. X            efaterror(filename);
  1999. X#    endif
  2000. X    return fopen(filename, FOPEN_WPLUS_WORK);
  2001. X}
  2002. X#endif
  2003. X
  2004. X
  2005. X    void
  2006. openfcopy(f)
  2007. X    FILE *f;
  2008. X{
  2009. X    if (!(fcopy = f)) {
  2010. X        if (!resultfile)
  2011. X            resultfile = maketemp(2);
  2012. X        if (!(fcopy = fopen_update_truncate(resultfile)))
  2013. X            efaterror(resultfile);
  2014. X    }
  2015. X}
  2016. X
  2017. X
  2018. X#if !large_memory
  2019. X
  2020. X    static void
  2021. swapeditfiles(outfile)
  2022. X    FILE *outfile;
  2023. X/* Function: swaps resultfile and editfile, assigns fedit=fcopy,
  2024. X * and rewinds fedit for reading.  Set fcopy to outfile if nonnull;
  2025. X * otherwise, set fcopy to be resultfile opened for reading and writing.
  2026. X */
  2027. X{
  2028. X    char const *tmpptr;
  2029. X
  2030. X    editline = 0;  linecorr = 0;
  2031. X    if (fseek(fcopy, 0L, SEEK_SET) != 0)
  2032. X        Oerror();
  2033. X    fedit = fcopy;
  2034. X        tmpptr=editfile; editfile=resultfile; resultfile=tmpptr;
  2035. X    openfcopy(outfile);
  2036. X}
  2037. X
  2038. X    void
  2039. snapshotedit(f)
  2040. X    FILE *f;
  2041. X/* Copy the current state of the edits to F.  */
  2042. X{
  2043. X    finishedit((struct hshentry *)nil, (FILE*)0, false);
  2044. X    fastcopy(fedit, f);
  2045. X    Irewind(fedit);
  2046. X}
  2047. X
  2048. X    void
  2049. finishedit(delta, outfile, done)
  2050. X    struct hshentry const *delta;
  2051. X    FILE *outfile;
  2052. X    int done;
  2053. X/* copy the rest of the edit file and close it (if it exists).
  2054. X * if delta!=nil, perform keyword substitution at the same time.
  2055. X * If DONE is set, we are finishing the last pass.
  2056. X */
  2057. X{
  2058. X    register RILE *fe;
  2059. X    register FILE *fc;
  2060. X
  2061. X    fe = fedit;
  2062. X    if (fe) {
  2063. X        fc = fcopy;
  2064. X                if (delta!=nil) {
  2065. X            while (1 < expandline(fe,fc,delta,false,(FILE*)0))
  2066. X                ;
  2067. X                } else {
  2068. X            fastcopy(fe,fc);
  2069. X                }
  2070. X        Ifclose(fe);
  2071. X        }
  2072. X    if (!done)
  2073. X        swapeditfiles(outfile);
  2074. X}
  2075. X#endif
  2076. X
  2077. X
  2078. X
  2079. X#if large_memory
  2080. X#    define copylines(upto,delta) (editline = (upto))
  2081. X#else
  2082. X    static void
  2083. copylines(upto,delta)
  2084. X    register unsigned long upto;
  2085. X    struct hshentry const *delta;
  2086. X/*
  2087. X * Copy input lines editline+1..upto from fedit to fcopy.
  2088. X * If delta != nil, keyword expansion is done simultaneously.
  2089. X * editline is updated. Rewinds a file only if necessary.
  2090. X */
  2091. X{
  2092. X    register int c;
  2093. X    declarecache;
  2094. X    register FILE *fc;
  2095. X    register RILE *fe;
  2096. X
  2097. X    if (upto < editline) {
  2098. X                /* swap files */
  2099. X        finishedit((struct hshentry *)nil, (FILE*)0, false);
  2100. X                /* assumes edit only during last pass, from the beginning*/
  2101. X        }
  2102. X    fe = fedit;
  2103. X    fc = fcopy;
  2104. X    if (editline < upto)
  2105. X        if (delta)
  2106. X        do {
  2107. X            if (expandline(fe,fc,delta,false,(FILE*)0) <= 1)
  2108. X                editLineNumberOverflow();
  2109. X        } while (++editline < upto);
  2110. X        else {
  2111. X        setupcache(fe); cache(fe);
  2112. X        do {
  2113. X            do {
  2114. X                cachegeteof(c, editLineNumberOverflow(););
  2115. X                aputc(c, fc);
  2116. X            } while (c != '\n');
  2117. X        } while (++editline < upto);
  2118. X        uncache(fe);
  2119. X        }
  2120. X}
  2121. X#endif
  2122. X
  2123. X
  2124. X
  2125. X    void
  2126. xpandstring(delta)
  2127. X    struct hshentry const *delta;
  2128. X/* Function: Reads a string terminated by SDELIM from finptr and writes it
  2129. X * to fcopy. Double SDELIM is replaced with single SDELIM.
  2130. X * Keyword expansion is performed with data from delta.
  2131. X * If foutptr is nonnull, the string is also copied unchanged to foutptr.
  2132. X */
  2133. X{
  2134. X    while (1 < expandline(finptr,fcopy,delta,true,foutptr))
  2135. X        ;
  2136. X}
  2137. X
  2138. X
  2139. X    void
  2140. copystring()
  2141. X/* Function: copies a string terminated with a single SDELIM from finptr to
  2142. X * fcopy, replacing all double SDELIM with a single SDELIM.
  2143. X * If foutptr is nonnull, the string also copied unchanged to foutptr.
  2144. X * editline is incremented by the number of lines copied.
  2145. X * Assumption: next character read is first string character.
  2146. X */
  2147. X{    register c;
  2148. X    declarecache;
  2149. X    register FILE *frew, *fcop;
  2150. X    register int amidline;
  2151. X    register RILE *fin;
  2152. X
  2153. X    fin = finptr;
  2154. X    setupcache(fin); cache(fin);
  2155. X    frew = foutptr;
  2156. X    fcop = fcopy;
  2157. X    amidline = false;
  2158. X    for (;;) {
  2159. X        GETC(frew,c);
  2160. X        switch (c) {
  2161. X            case '\n':
  2162. X            ++editline;
  2163. X            ++rcsline;
  2164. X            amidline = false;
  2165. X            break;
  2166. X            case SDELIM:
  2167. X            GETC(frew,c);
  2168. X            if (c != SDELIM) {
  2169. X                /* end of string */
  2170. X                nextc = c;
  2171. X                editline += amidline;
  2172. X                uncache(fin);
  2173. X                return;
  2174. X            }
  2175. X            /* fall into */
  2176. X            default:
  2177. X            amidline = true;
  2178. X            break;
  2179. X                }
  2180. X        aputc(c,fcop);
  2181. X        }
  2182. X}
  2183. X
  2184. X
  2185. X    void
  2186. enterstring()
  2187. X/* Like copystring, except the string is put into the edit data structure.  */
  2188. X{
  2189. X#if !large_memory
  2190. X    editfile = 0;
  2191. X    fedit = 0;
  2192. X    editline = linecorr = 0;
  2193. X    resultfile = maketemp(1);
  2194. X    if (!(fcopy = fopen_update_truncate(resultfile)))
  2195. X        efaterror(resultfile);
  2196. X    copystring();
  2197. X#else
  2198. X    register int c;
  2199. X    declarecache;
  2200. X    register FILE *frew;
  2201. X    register unsigned long e, oe;
  2202. X    register int amidline, oamidline;
  2203. X    register Iptr_type optr;
  2204. X    register RILE *fin;
  2205. X
  2206. X    e = 0;
  2207. X    gap = 0;
  2208. X    gapsize = linelim;
  2209. X    fin = finptr;
  2210. X    setupcache(fin); cache(fin);
  2211. X    advise_access(fin, MADV_NORMAL);
  2212. X    frew = foutptr;
  2213. X    amidline = false;
  2214. X    for (;;) {
  2215. X        optr = cachetell();
  2216. X        GETC(frew,c);
  2217. X        oamidline = amidline;
  2218. X        oe = e;
  2219. X        switch (c) {
  2220. X            case '\n':
  2221. X            ++e;
  2222. X            ++rcsline;
  2223. X            amidline = false;
  2224. X            break;
  2225. X            case SDELIM:
  2226. X            GETC(frew,c);
  2227. X            if (c != SDELIM) {
  2228. X                /* end of string */
  2229. X                nextc = c;
  2230. X                editline = e + amidline;
  2231. X                linecorr = 0;
  2232. X                uncache(fin);
  2233. X                return;
  2234. X            }
  2235. X            /* fall into */
  2236. X            default:
  2237. X            amidline = true;
  2238. X            break;
  2239. X        }
  2240. X        if (!oamidline)
  2241. X            insertline(oe, optr);
  2242. X    }
  2243. X#endif
  2244. X}
  2245. X
  2246. X
  2247. X
  2248. X
  2249. X    void
  2250. X#if large_memory
  2251. edit_string()
  2252. X#else
  2253. X  editstring(delta)
  2254. X    struct hshentry const *delta;
  2255. X#endif
  2256. X/*
  2257. X * Read an edit script from finptr and applies it to the edit file.
  2258. X#if !large_memory
  2259. X * The result is written to fcopy.
  2260. X * If delta!=nil, keyword expansion is performed simultaneously.
  2261. X * If running out of lines in fedit, fedit and fcopy are swapped.
  2262. X * editfile is the name of the file that goes with fedit.
  2263. X#endif
  2264. X * If foutptr is set, the edit script is also copied verbatim to foutptr.
  2265. X * Assumes that all these files are open.
  2266. X * resultfile is the name of the file that goes with fcopy.
  2267. X * Assumes the next input character from finptr is the first character of
  2268. X * the edit script. Resets nextc on exit.
  2269. X */
  2270. X{
  2271. X        int ed; /* editor command */
  2272. X        register int c;
  2273. X    declarecache;
  2274. X    register FILE *frew;
  2275. X#    if !large_memory
  2276. X        register FILE *f;
  2277. X        unsigned long line_lim = ULONG_MAX;
  2278. X        register RILE *fe;
  2279. X#    endif
  2280. X    register unsigned long i;
  2281. X    register RILE *fin;
  2282. X#    if large_memory
  2283. X        register unsigned long j;
  2284. X#    endif
  2285. X    struct diffcmd dc;
  2286. X
  2287. X        editline += linecorr; linecorr=0; /*correct line number*/
  2288. X    frew = foutptr;
  2289. X    fin = finptr;
  2290. X    setupcache(fin);
  2291. X    initdiffcmd(&dc);
  2292. X    while (0  <=  (ed = getdiffcmd(fin,true,frew,&dc)))
  2293. X#if !large_memory
  2294. X        if (line_lim <= dc.line1)
  2295. X            editLineNumberOverflow();
  2296. X        else
  2297. X#endif
  2298. X        if (!ed) {
  2299. X            copylines(dc.line1-1, delta);
  2300. X                        /* skip over unwanted lines */
  2301. X            i = dc.nlines;
  2302. X            linecorr -= i;
  2303. X            editline += i;
  2304. X#            if large_memory
  2305. X                deletelines(editline+linecorr, i);
  2306. X#            else
  2307. X                fe = fedit;
  2308. X                do {
  2309. X                                /*skip next line*/
  2310. X                do {
  2311. X                    Igeteof(fe, c, { if (i!=1) editLineNumberOverflow(); line_lim = dc.dafter; break; } );
  2312. X                } while (c != '\n');
  2313. X                } while (--i);
  2314. X#            endif
  2315. X        } else {
  2316. X            copylines(dc.line1, delta); /*copy only; no delete*/
  2317. X            i = dc.nlines;
  2318. X#            if large_memory
  2319. X                j = editline+linecorr;
  2320. X#            endif
  2321. X            linecorr += i;
  2322. X#if !large_memory
  2323. X            f = fcopy;
  2324. X            if (delta)
  2325. X                do {
  2326. X                switch (expandline(fin,f,delta,true,frew)) {
  2327. X                    case 0: case 1:
  2328. X                    if (i==1)
  2329. X                        return;
  2330. X                    /* fall into */
  2331. X                    case -1:
  2332. X                    editEndsPrematurely();
  2333. X                }
  2334. X                } while (--i);
  2335. X            else
  2336. X#endif
  2337. X            {
  2338. X                cache(fin);
  2339. X                do {
  2340. X#                if large_memory
  2341. X                    insertline(j++, cachetell());
  2342. X#                endif
  2343. X                for (;;) {
  2344. X                    GETC(frew, c);
  2345. X#                    if !large_memory
  2346. X                    aputc(c, f);
  2347. X#                    endif
  2348. X                    if (c == '\n')
  2349. X                    break;
  2350. X                    if (c==SDELIM) {
  2351. X                    GETC(frew, c);
  2352. X                    if (c!=SDELIM) {
  2353. X                        if (--i)
  2354. X                        editEndsPrematurely();
  2355. X                        nextc = c;
  2356. X                        uncache(fin);
  2357. X                        return;
  2358. X                    }
  2359. X                    }
  2360. X                }
  2361. X                ++rcsline;
  2362. X                } while (--i);
  2363. X                uncache(fin);
  2364. X            }
  2365. X                }
  2366. X}
  2367. X
  2368. X
  2369. X
  2370. X/* The rest is for keyword expansion */
  2371. X
  2372. X
  2373. X
  2374. X    int
  2375. expandline(infile, outfile, delta, delimstuffed, frewfile)
  2376. X    RILE *infile;
  2377. X    FILE *outfile, *frewfile;
  2378. X    struct hshentry const *delta;
  2379. X    int delimstuffed;
  2380. X/*
  2381. X * Read a line from INFILE and write it to OUTFILE.
  2382. X * If DELIMSTUFFED is true, double SDELIM is replaced with single SDELIM.
  2383. X * Keyword expansion is performed with data from delta.
  2384. X * If FREWFILE is set, copy the line unchanged to FREWFILE.
  2385. X * DELIMSTUFFED must be true if FREWFILE is set.
  2386. X * Yields -1 if no data is copied, 0 if an incomplete line is copied,
  2387. X * 2 if a complete line is copied; adds 1 to yield if expansion occurred.
  2388. X */
  2389. X{
  2390. X    register c;
  2391. X    declarecache;
  2392. X    register FILE *out, *frew;
  2393. X    register char * tp;
  2394. X    register int e, ds, r;
  2395. X    char const *tlim;
  2396. X    static struct buf keyval;
  2397. X        enum markers matchresult;
  2398. X
  2399. X    setupcache(infile); cache(infile);
  2400. X    out = outfile;
  2401. X    frew = frewfile;
  2402. X    ds = delimstuffed;
  2403. X    bufalloc(&keyval, keylength+3);
  2404. X    e = 0;
  2405. X    r = -1;
  2406. X
  2407. X        for (;;) {
  2408. X        if (ds) {
  2409. X        GETC(frew, c);
  2410. X        } else
  2411. X        cachegeteof(c, goto uncache_exit;);
  2412. X        for (;;) {
  2413. X        switch (c) {
  2414. X            case SDELIM:
  2415. X            if (ds) {
  2416. X                GETC(frew, c);
  2417. X                if (c != SDELIM) {
  2418. X                                /* end of string */
  2419. X                                nextc=c;
  2420. X                goto uncache_exit;
  2421. X                }
  2422. X            }
  2423. X            /* fall into */
  2424. X            default:
  2425. X            aputc(c,out);
  2426. X            r = 0;
  2427. X            break;
  2428. X
  2429. X            case '\n':
  2430. X            rcsline += ds;
  2431. X            aputc(c,out);
  2432. X            r = 2;
  2433. X            goto uncache_exit;
  2434. X
  2435. X            case KDELIM:
  2436. X            r = 0;
  2437. X                        /* check for keyword */
  2438. X                        /* first, copy a long enough string into keystring */
  2439. X            tp = keyval.string;
  2440. X            *tp++ = KDELIM;
  2441. X            for (;;) {
  2442. X                if (ds) {
  2443. X                GETC(frew, c);
  2444. X                } else
  2445. X                cachegeteof(c, goto keystring_eof;);
  2446. X                if (tp < keyval.string+keylength+1)
  2447. X                switch (ctab[c]) {
  2448. X                    case LETTER: case Letter:
  2449. X                    *tp++ = c;
  2450. X                    continue;
  2451. X                    default:
  2452. X                    break;
  2453. X                }
  2454. X                break;
  2455. X                        }
  2456. X            *tp++ = c; *tp = '\0';
  2457. X            matchresult = trymatch(keyval.string+1);
  2458. X            if (matchresult==Nomatch) {
  2459. X                tp[-1] = 0;
  2460. X                aputs(keyval.string, out);
  2461. X                continue;   /* last c handled properly */
  2462. X            }
  2463. X
  2464. X            /* Now we have a keyword terminated with a K/VDELIM */
  2465. X            if (c==VDELIM) {
  2466. X                  /* try to find closing KDELIM, and replace value */
  2467. X                  tlim = keyval.string + keyval.size;
  2468. X                  for (;;) {
  2469. X                      if (ds) {
  2470. X                    GETC(frew, c);
  2471. X                      } else
  2472. X                    cachegeteof(c, goto keystring_eof;);
  2473. X                      if (c=='\n' || c==KDELIM)
  2474. X                    break;
  2475. X                      *tp++ =c;
  2476. X                      if (tlim <= tp)
  2477. X                      tp = bufenlarge(&keyval, &tlim);
  2478. X                      if (c==SDELIM && ds) { /*skip next SDELIM */
  2479. X                        GETC(frew, c);
  2480. X                        if (c != SDELIM) {
  2481. X                            /* end of string before closing KDELIM or newline */
  2482. X                            nextc = c;
  2483. X                            goto keystring_eof;
  2484. X                        }
  2485. X                      }
  2486. X                  }
  2487. X                  if (c!=KDELIM) {
  2488. X                    /* couldn't find closing KDELIM -- give up */
  2489. X                    *tp = 0;
  2490. X                    aputs(keyval.string, out);
  2491. X                    continue;   /* last c handled properly */
  2492. X                  }
  2493. X            }
  2494. X            /* now put out the new keyword value */
  2495. X            keyreplace(matchresult,delta,out);
  2496. X            e = 1;
  2497. X            break;
  2498. X                }
  2499. X        break;
  2500. X        }
  2501. X        }
  2502. X
  2503. X    keystring_eof:
  2504. X    *tp = 0;
  2505. X    aputs(keyval.string, out);
  2506. X    uncache_exit:
  2507. X    uncache(infile);
  2508. X    return r + e;
  2509. X}
  2510. X
  2511. X
  2512. char const ciklog[ciklogsize] = "checked in with -k by ";
  2513. X
  2514. X    static void
  2515. keyreplace(marker,delta,out)
  2516. X    enum markers marker;
  2517. X    register struct hshentry const *delta;
  2518. X    register FILE *out;
  2519. X/* function: outputs the keyword value(s) corresponding to marker.
  2520. X * Attributes are derived from delta.
  2521. X */
  2522. X{
  2523. X    register char const *sp, *cp, *date;
  2524. X    register char c;
  2525. X    register size_t cs, cw, ls;
  2526. X    char const *sp1;
  2527. X    char datebuf[datesize];
  2528. X    int RCSv;
  2529. X
  2530. X    sp = Keyword[(int)marker];
  2531. X
  2532. X    if (Expand == KEY_EXPAND) {
  2533. X        aprintf(out, "%c%s%c", KDELIM, sp, KDELIM);
  2534. X        return;
  2535. X    }
  2536. X
  2537. X        date= delta->date;
  2538. X    RCSv = RCSversion;
  2539. X
  2540. X    if (Expand == KEYVAL_EXPAND  ||  Expand == KEYVALLOCK_EXPAND)
  2541. X        aprintf(out, "%c%s%c%c", KDELIM, sp, VDELIM,
  2542. X            marker==Log && RCSv<VERSION(5)  ?  '\t'  :  ' '
  2543. X        );
  2544. X
  2545. X        switch (marker) {
  2546. X        case Author:
  2547. X        aputs(delta->author, out);
  2548. X                break;
  2549. X        case Date:
  2550. X        aputs(date2str(date,datebuf), out);
  2551. X                break;
  2552. X        case Id:
  2553. X    case Header:
  2554. X        aprintf(out, "%s %s %s %s %s",
  2555. X              marker==Id || RCSv<VERSION(4)
  2556. X            ? basename(RCSfilename)
  2557. X            : getfullRCSname(),
  2558. X            delta->num,
  2559. X            date2str(date, datebuf),
  2560. X            delta->author,
  2561. X              RCSv==VERSION(3) && delta->lockedby ? "Locked"
  2562. X            : delta->state
  2563. X        );
  2564. X        if (delta->lockedby!=nil)
  2565. X            if (VERSION(5) <= RCSv) {
  2566. X            if (locker_expansion || Expand==KEYVALLOCK_EXPAND)
  2567. X                aprintf(out, " %s", delta->lockedby);
  2568. X            } else if (RCSv == VERSION(4))
  2569. X            aprintf(out, " Locker: %s", delta->lockedby);
  2570. X                break;
  2571. X        case Locker:
  2572. X        if (delta->lockedby)
  2573. X            if (
  2574. X                locker_expansion
  2575. X            ||    Expand == KEYVALLOCK_EXPAND
  2576. X            ||    RCSv <= VERSION(4)
  2577. X            )
  2578. X            aputs(delta->lockedby, out);
  2579. X                break;
  2580. X        case Log:
  2581. X        case RCSfile:
  2582. X        aputs(basename(RCSfilename), out);
  2583. X                break;
  2584. X        case Revision:
  2585. X        aputs(delta->num, out);
  2586. X                break;
  2587. X        case Source:
  2588. X        aputs(getfullRCSname(), out);
  2589. X                break;
  2590. X        case State:
  2591. X        aputs(delta->state, out);
  2592. X                break;
  2593. X    default:
  2594. X        break;
  2595. X        }
  2596. X    if (Expand == KEYVAL_EXPAND  ||  Expand == KEYVALLOCK_EXPAND) {
  2597. X        afputc(' ', out);
  2598. X        afputc(KDELIM, out);
  2599. X    }
  2600. X    if (marker == Log) {
  2601. X        sp = delta->log.string;
  2602. X        ls = delta->log.size;
  2603. X        if (sizeof(ciklog)-1<=ls && !memcmp(sp,ciklog,sizeof(ciklog)-1))
  2604. X            return;
  2605. X        afputc('\n', out);
  2606. X        cp = Comment.string;
  2607. X        cw = cs = Comment.size;
  2608. X        awrite(cp, cs, out);
  2609. X        /* oddity: 2 spaces between date and time, not 1 as usual */
  2610. X        sp1 = strchr(date2str(date,datebuf), ' ');
  2611. X        aprintf(out, "Revision %s  %.*s %s  %s",
  2612. X            delta->num, (int)(sp1-datebuf), datebuf, sp1, delta->author
  2613. X        );
  2614. X        /* Do not include state: it may change and is not updated.  */
  2615. X        /* Comment is the comment leader.  */
  2616. X        if (VERSION(5) <= RCSv)
  2617. X            for (;  cw && (cp[cw-1]==' ' || cp[cw-1]=='\t');  --cw)
  2618. X            ;
  2619. X        for (;;) {
  2620. X            afputc('\n', out);
  2621. X            awrite(cp, cw, out);
  2622. X            if (!ls)
  2623. X            break;
  2624. X            --ls;
  2625. X            c = *sp++;
  2626. X            if (c != '\n') {
  2627. X            awrite(cp+cw, cs-cw, out);
  2628. X            do {
  2629. X                afputc(c,out);
  2630. X                if (!ls)
  2631. X                break;
  2632. X                --ls;
  2633. X                c = *sp++;
  2634. X            } while (c != '\n');
  2635. X            }
  2636. X        }
  2637. X    }
  2638. X}
  2639. X
  2640. X#if has_readlink
  2641. X    static int
  2642. resolve_symlink(L)
  2643. X    struct buf *L;
  2644. X/*
  2645. X * If L is a symbolic link, resolve it to the name that it points to.
  2646. X * If unsuccessful, set errno and yield -1.
  2647. X * If it points to an existing file, yield 1.
  2648. X * Otherwise, set errno=ENOENT and yield 0.
  2649. X */
  2650. X{
  2651. X    char *b, a[SIZEABLE_PATH];
  2652. X    int e;
  2653. X    size_t s;
  2654. X    ssize_t r;
  2655. X    struct buf bigbuf;
  2656. X    unsigned linkcount = MAXSYMLINKS + 1;
  2657. X
  2658. X    b = a;
  2659. X    s = sizeof(a);
  2660. X    bufautobegin(&bigbuf);
  2661. X    while ((r = readlink(L->string,b,s))  !=  -1)
  2662. X        if (r == s) {
  2663. X        bufalloc(&bigbuf, s<<1);
  2664. X        b = bigbuf.string;
  2665. X        s = bigbuf.size;
  2666. X        } else if (!--linkcount) {
  2667. X        errno = ELOOP;
  2668. X        return -1;
  2669. X        } else {
  2670. X        /* Splice symbolic link into L.  */
  2671. X        b[r] = '\0';
  2672. X        L->string[ROOTPATH(b) ? (size_t)0 : dirlen(L->string)]  =  '\0';
  2673. X        bufscat(L, b);
  2674. X        }
  2675. X    e = errno;
  2676. X    bufautoend(&bigbuf);
  2677. X    errno = e;
  2678. X    switch (e) {
  2679. X        case ENXIO:
  2680. X        case EINVAL: return 1;
  2681. X        case ENOENT: return 0;
  2682. X        default: return -1;
  2683. X    }
  2684. X}
  2685. X#endif
  2686. X
  2687. X    RILE *
  2688. rcswriteopen(RCSbuf, status, mustread)
  2689. X    struct buf *RCSbuf;
  2690. X    struct stat *status;
  2691. X    int mustread;
  2692. X/*
  2693. X * Create the lock file corresponding to RCSNAME.
  2694. X * Then try to open RCSNAME for reading and yield its FILE* descriptor.
  2695. X * Put its status into *STATUS too.
  2696. X * MUSTREAD is true if the file must already exist, too.
  2697. X * If all goes well, discard any previously acquired locks,
  2698. X * and set frewrite to the FILE* descriptor of the lock file,
  2699. X * which will eventually turn into the new RCS file.
  2700. X */
  2701. X{
  2702. X    register char *tp;
  2703. X    register char const *sp, *RCSname, *x;
  2704. X    RILE *f;
  2705. X    size_t l;
  2706. X    int e, exists, fdesc, previouslock, r;
  2707. X    struct buf *dirt;
  2708. X    struct stat statbuf;
  2709. X
  2710. X    previouslock  =  frewrite != 0;
  2711. X    exists =
  2712. X#        if has_readlink
  2713. X            resolve_symlink(RCSbuf);
  2714. X#        else
  2715. X                stat(RCSbuf->string, &statbuf) == 0  ?  1
  2716. X            :   errno==ENOENT ? 0 : -1;
  2717. X#        endif
  2718. X    if (exists < (mustread|previouslock))
  2719. X        /*
  2720. X         * There's an unusual problem with the RCS file;
  2721. X         * or the RCS file doesn't exist,
  2722. X         * and we must read or we already have a lock elsewhere.
  2723. X         */
  2724. X        return 0;
  2725. X
  2726. X    RCSname = RCSbuf->string;
  2727. X    sp = basename(RCSname);
  2728. X    l = sp - RCSname;
  2729. X    dirt = &dirtfname[previouslock];
  2730. X    bufscpy(dirt, RCSname);
  2731. X    tp = dirt->string + l;
  2732. X    x = rcssuffix(RCSname);
  2733. X#    if has_readlink
  2734. X        if (!x) {
  2735. X        error("symbolic link to non RCS filename `%s'", RCSname);
  2736. X        errno = EINVAL;
  2737. X        return 0;
  2738. X        }
  2739. X#    endif
  2740. X    if (*sp == *x) {
  2741. X        error("RCS filename `%s' incompatible with suffix `%s'", sp, x);
  2742. X        errno = EINVAL;
  2743. X        return 0;
  2744. X    }
  2745. X    /* Create a lock file whose name is a function of the RCS filename.  */
  2746. X    if (*x) {
  2747. X        /*
  2748. X         * The suffix is nonempty.
  2749. X         * The lock filename is the first char of of the suffix,
  2750. X         * followed by the RCS filename with last char removed.  E.g.:
  2751. X         *    foo,v    RCS filename with suffix ,v
  2752. X         *    ,foo,    lock filename
  2753. X         */
  2754. X        *tp++ = *x;
  2755. X        while (*sp)
  2756. X            *tp++ = *sp++;
  2757. X        *--tp = 0;
  2758. X    } else {
  2759. X        /*
  2760. X         * The suffix is empty.
  2761. X         * The lock filename is the RCS filename
  2762. X         * with last char replaced by '_'.
  2763. X         */
  2764. X        while ((*tp++ = *sp++))
  2765. X            ;
  2766. X        tp -= 2;
  2767. X        if (*tp == '_') {
  2768. X            error("RCS filename `%s' ends with `%c'", RCSname, *tp);
  2769. X            errno = EINVAL;
  2770. X            return 0;
  2771. X        }
  2772. X        *tp = '_';
  2773. X    }
  2774. X
  2775. X    sp = tp = dirt->string;
  2776. X
  2777. X    f = 0;
  2778. X
  2779. X    /*
  2780. X    * good news:
  2781. X    *    open(f, O_CREAT|O_EXCL|O_TRUNC|O_WRONLY, READONLY) is atomic
  2782. X    *    according to Posix 1003.1-1990.
  2783. X    * bad news:
  2784. X    *    NFS ignores O_EXCL and doesn't comply with Posix 1003.1-1990.
  2785. X    * good news:
  2786. X    *    (O_TRUNC,READONLY) normally guarantees atomicity even with NFS.
  2787. X    * bad news:
  2788. X    *    If you're root, (O_TRUNC,READONLY) doesn't guarantee atomicity.
  2789. X    * good news:
  2790. X    *    Root-over-the-wire NFS access is rare for security reasons.
  2791. X    *    This bug has never been reported in practice with RCS.
  2792. X    * So we don't worry about this bug.
  2793. X    *
  2794. X    * An even rarer NFS bug can occur when clients retry requests.
  2795. X    * Suppose client A renames the lock file ",f," to "f,v"
  2796. X    * at about the same time that client B creates ",f,",
  2797. X    * and suppose A's first rename request is delayed, so A reissues it.
  2798. X    * The sequence of events might be:
  2799. X    *    A sends rename(",f,", "f,v")
  2800. X    *    B sends create(",f,")
  2801. X    *    A sends retry of rename(",f,", "f,v")
  2802. X    *    server receives, does, and acknowledges A's first rename()
  2803. X    *    A receives acknowledgment, and its RCS program exits
  2804. X    *    server receives, does, and acknowledges B's create()
  2805. X    *    server receives, does, and acknowledges A's retry of rename()
  2806. X    * This not only wrongly deletes B's lock, it removes the RCS file!
  2807. X    * Most NFS implementations have idempotency caches that usually prevent
  2808. X    * this scenario, but such caches are finite and can be overrun.
  2809. X    * This problem afflicts programs that use the traditional
  2810. X    * Unix method of using link() and unlink() to get and release locks,
  2811. X    * as well as RCS's method of using open() and rename().
  2812. X    * There is no easy workaround for either link-unlink or open-rename.
  2813. X    * Any new method based on lockf() seemingly would be incompatible with
  2814. X    * the old methods; besides, lockf() is notoriously buggy under NFS.
  2815. X    * Since this problem afflicts scads of Unix programs, but is so rare
  2816. X    * that nobody seems to be worried about it, we won't worry either.
  2817. X    */
  2818. X#    define READONLY (S_IRUSR|S_IRGRP|S_IROTH)
  2819. X#    if !open_can_creat
  2820. X#        define create(f) creat(f, READONLY)
  2821. X#    else
  2822. X#        define create(f) open(f, O_BINARY|O_CREAT|O_EXCL|O_TRUNC|O_WRONLY, READONLY)
  2823. X#    endif
  2824. X
  2825. X    catchints();
  2826. X    ignoreints();
  2827. X
  2828. X    /*
  2829. X     * Create a lock file for an RCS file.  This should be atomic, i.e.
  2830. X     * if two processes try it simultaneously, at most one should succeed.
  2831. X     */
  2832. X    seteid();
  2833. X    fdesc = create(sp);
  2834. X    e = errno;
  2835. X    setrid();
  2836. X
  2837. X    if (fdesc < 0) {
  2838. X        if (e == EACCES  &&  stat(tp,&statbuf) == 0)
  2839. X            /* The RCS file is busy.  */
  2840. X            e = EEXIST;
  2841. X    } else {
  2842. X        dirtfmaker[0] = effective;
  2843. X        e = ENOENT;
  2844. X        if (exists) {
  2845. X            f = Iopen(RCSname, FOPEN_R, status);
  2846. X            e = errno;
  2847. X            if (f && previouslock) {
  2848. X            /* Discard the previous lock in favor of this one.  */
  2849. X            Ozclose(&frewrite);
  2850. X            seteid();
  2851. X            if ((r = un_link(newRCSfilename)) != 0)
  2852. X                e = errno;
  2853. X            setrid();
  2854. X            if (r != 0)
  2855. X                enfaterror(e, newRCSfilename);
  2856. X            bufscpy(&dirtfname[0], tp);
  2857. X            }
  2858. X        }
  2859. X        if (!(frewrite = fdopen(fdesc, FOPEN_W))) {
  2860. X            efaterror(newRCSfilename);
  2861. X        }
  2862. X    }
  2863. X
  2864. X    restoreints();
  2865. X
  2866. X    errno = e;
  2867. X    return f;
  2868. X}
  2869. X
  2870. X    void
  2871. keepdirtemp(name)
  2872. X    char const *name;
  2873. X/* Do not unlink name, either because it's not there any more,
  2874. X * or because it has already been unlinked.
  2875. X */
  2876. X{
  2877. X    register int i;
  2878. X    for (i=DIRTEMPNAMES; 0<=--i; )
  2879. X        if (dirtfname[i].string == name) {
  2880. X            dirtfmaker[i] = notmade;
  2881. X            return;
  2882. X        }
  2883. X    faterror("keepdirtemp");
  2884. X}
  2885. X
  2886. X    char const *
  2887. makedirtemp(name, n)
  2888. X    register char const *name;
  2889. X    int n;
  2890. X/*
  2891. X * Have maketemp() do all the work if name is null.
  2892. X * Otherwise, create a unique filename in name's dir using n and name
  2893. X * and store it into the dirtfname[n].
  2894. X * Because of storage in tfnames, dirtempunlink() can unlink the file later.
  2895. X * Return a pointer to the filename created.
  2896. X */
  2897. X{
  2898. X    register char *tp, *np;
  2899. X    register size_t dl;
  2900. X    register struct buf *bn;
  2901. X
  2902. X    if (!name)
  2903. X        return maketemp(n);
  2904. X    dl = dirlen(name);
  2905. X    bn = &dirtfname[n];
  2906. X    bufalloc(bn,
  2907. X#        if has_mktemp
  2908. X            dl + 9
  2909. X#        else
  2910. X            strlen(name) + 3
  2911. X#        endif
  2912. X    );
  2913. X    bufscpy(bn, name);
  2914. X    np = tp = bn->string;
  2915. X    tp += dl;
  2916. X    *tp++ = '_';
  2917. X    *tp++ = '0'+n;
  2918. X    catchints();
  2919. X#    if has_mktemp
  2920. X        VOID strcpy(tp, "XXXXXX");
  2921. X        if (!mktemp(np) || !*np)
  2922. X            faterror("can't make temporary file name `%.*s%c_%cXXXXXX'",
  2923. X            (int)dl, name, SLASH, '0'+n
  2924. X            );
  2925. X#    else
  2926. X        /*
  2927. X         * Posix 1003.1-1990 has no reliable way
  2928. X         * to create a unique file in a named directory.
  2929. X         * We fudge here.  If the working file name is abcde,
  2930. X         * the temp filename is _Ncde where N is a digit.
  2931. X         */
  2932. X        name += dl;
  2933. X        if (*name) name++;
  2934. X        if (*name) name++;
  2935. X        VOID strcpy(tp, name);
  2936. X#    endif
  2937. X    dirtfmaker[n] = real;
  2938. X    return np;
  2939. X}
  2940. X
  2941. X    void
  2942. dirtempunlink()
  2943. X/* Clean up makedirtemp() files.  May be invoked by signal handler. */
  2944. X{
  2945. X    register int i;
  2946. X    enum maker m;
  2947. X
  2948. X    for (i = DIRTEMPNAMES;  0 <= --i;  )
  2949. X        if ((m = dirtfmaker[i]) != notmade) {
  2950. X        if (m == effective)
  2951. X            seteid();
  2952. X        VOID un_link(dirtfname[i].string);
  2953. X        if (m == effective)
  2954. X            setrid();
  2955. X        dirtfmaker[i] = notmade;
  2956. X        }
  2957. X}
  2958. X
  2959. X
  2960. X    int
  2961. X#if has_prototypes
  2962. chnamemod(FILE **fromp, char const *from, char const *to, mode_t mode)
  2963. X  /* The `#if has_prototypes' is needed because mode_t might promote to int.  */
  2964. X#else
  2965. X  chnamemod(fromp,from,to,mode) FILE **fromp; char const *from,*to; mode_t mode;
  2966. X#endif
  2967. X/*
  2968. X * Rename a file (with optional stream pointer *FROMP) from FROM to TO.
  2969. X * FROM already exists.
  2970. X * Change its mode to MODE, before renaming if possible.
  2971. X * If FROMP, close and clear *FROMP before renaming it.
  2972. X * Unlink TO if it already exists.
  2973. X * Return -1 on error (setting errno), 0 otherwise.
  2974. X */
  2975. X{
  2976. X#    if bad_a_rename
  2977. X        /*
  2978. X         * This host is brain damaged.  A race condition is possible
  2979. X         * while the lock file is temporarily writable.
  2980. X         * There doesn't seem to be a workaround.
  2981. X         */
  2982. X        mode_t mode_while_renaming = mode|S_IWUSR;
  2983. X#    else
  2984. X#        define mode_while_renaming mode
  2985. X#    endif
  2986. X    if (fromp) {
  2987. X#        if has_fchmod
  2988. X            if (fchmod(fileno(*fromp), mode_while_renaming) != 0)
  2989. X                return -1;
  2990. X#        endif
  2991. X        Ozclose(fromp);
  2992. X    }
  2993. X#    if has_fchmod
  2994. X        else
  2995. X#    endif
  2996. X        if (chmod(from, mode_while_renaming) != 0)
  2997. X        return -1;
  2998. X
  2999. X#    if !has_rename || bad_b_rename
  3000. X        VOID un_link(to);
  3001. X        /*
  3002. X         * We need not check the result;
  3003. X         * link() or rename() will catch it.
  3004. X         * No harm is done if TO does not exist.
  3005. X         * However, there's a short window of inconsistency
  3006. X         * during which TO does not exist.
  3007. X         */
  3008. X#    endif
  3009. X
  3010. X    return
  3011. X#        if !has_rename
  3012. X        do_link(from,to) != 0  ?  -1  :  un_link(from)
  3013. X#        else
  3014. X            rename(from, to) != 0
  3015. X#            if has_NFS
  3016. X            && errno != ENOENT
  3017. X#            endif
  3018. X        ?  -1
  3019. X#        if bad_a_rename
  3020. X        :  mode != mode_while_renaming  ?  chmod(to, mode)
  3021. X#        endif
  3022. X        :  0
  3023. X#        endif
  3024. X    ;
  3025. X
  3026. X#    undef mode_while_renaming
  3027. X}
  3028. X
  3029. X
  3030. X
  3031. X    int
  3032. findlock(delete, target)
  3033. X    int delete;
  3034. X    struct hshentry **target;
  3035. X/*
  3036. X * Find the first lock held by caller and return a pointer
  3037. X * to the locked delta; also removes the lock if DELETE.
  3038. X * If one lock, put it into *TARGET.
  3039. X * Return 0 for no locks, 1 for one, 2 for two or more.
  3040. X */
  3041. X{
  3042. X    register struct lock *next, **trail, **found;
  3043. X
  3044. X    found = 0;
  3045. X    for (trail = &Locks;  (next = *trail);  trail = &next->nextlock)
  3046. X        if (strcmp(getcaller(), next->login)  ==  0) {
  3047. X            if (found) {
  3048. X                error("multiple revisions locked by %s; please specify one", getcaller());
  3049. X                return 2;
  3050. X            }
  3051. X            found = trail;
  3052. X        }
  3053. X    if (!found)
  3054. X        return 0;
  3055. X    next = *found;
  3056. X    *target = next->delta;
  3057. X    if (delete) {
  3058. X        next->delta->lockedby = nil;
  3059. X        *found = next->nextlock;
  3060. X    }
  3061. X    return 1;
  3062. X}
  3063. X
  3064. X    int
  3065. addlock(delta)
  3066. X    struct hshentry * delta;
  3067. X/*
  3068. X * Add a lock held by caller to DELTA and yield 1 if successful.
  3069. X * Print an error message and yield -1 if no lock is added because
  3070. X * DELTA is locked by somebody other than caller.
  3071. X * Return 0 if the caller already holds the lock.
  3072. X */
  3073. X{
  3074. X    register struct lock *next;
  3075. X
  3076. X    next=Locks;
  3077. X    for (next = Locks;  next;  next = next->nextlock)
  3078. X        if (cmpnum(delta->num, next->delta->num) == 0)
  3079. X            if (strcmp(getcaller(), next->login) == 0)
  3080. X                return 0;
  3081. X            else {
  3082. X                error("revision %s already locked by %s",
  3083. X                      delta->num, next->login
  3084. X                );
  3085. X                return -1;
  3086. X            }
  3087. X    next = ftalloc(struct lock);
  3088. X    delta->lockedby = next->login = getcaller();
  3089. X    next->delta = delta;
  3090. X    next->nextlock = Locks;
  3091. X    Locks = next;
  3092. X    return 1;
  3093. X}
  3094. X
  3095. X
  3096. X    int
  3097. addsymbol(num, name, rebind)
  3098. X    char const *num, *name;
  3099. X    int rebind;
  3100. X/*
  3101. X * Associate with revision NUM the new symbolic NAME.
  3102. X * If NAME already exists and REBIND is set, associate NAME with NUM;
  3103. X * otherwise, print an error message and return false;
  3104. X * Return true if successful.
  3105. X */
  3106. X{
  3107. X    register struct assoc *next;
  3108. X
  3109. X    for (next = Symbols;  next;  next = next->nextassoc)
  3110. X        if (strcmp(name, next->symbol)  ==  0)
  3111. X            if (rebind  ||  strcmp(next->num,num) == 0) {
  3112. X                next->num = num;
  3113. X                return true;
  3114. X            } else {
  3115. X                error("symbolic name %s already bound to %s",
  3116. X                    name, next->num
  3117. X                );
  3118. X                return false;
  3119. X            }
  3120. X    next = ftalloc(struct assoc);
  3121. X    next->symbol = name;
  3122. X    next->num = num;
  3123. X    next->nextassoc = Symbols;
  3124. X    Symbols = next;
  3125. X    return true;
  3126. X}
  3127. X
  3128. X
  3129. X
  3130. X    char const *
  3131. getcaller()
  3132. X/* Get the caller's login name.  */
  3133. X{
  3134. X#    if has_setuid
  3135. X        return getusername(euid()!=ruid());
  3136. X#    else
  3137. X        return getusername(false);
  3138. X#    endif
  3139. X}
  3140. X
  3141. X
  3142. X    int
  3143. checkaccesslist()
  3144. X/*
  3145. X * Return true if caller is the superuser, the owner of the
  3146. X * file, the access list is empty, or caller is on the access list.
  3147. X * Otherwise, print an error message and return false.
  3148. X */
  3149. X{
  3150. X    register struct access const *next;
  3151. X
  3152. X    if (!AccessList || myself(RCSstat.st_uid) || strcmp(getcaller(),"root")==0)
  3153. X        return true;
  3154. X
  3155. X    next = AccessList;
  3156. X    do {
  3157. X        if (strcmp(getcaller(), next->login)  ==  0)
  3158. X            return true;
  3159. X    } while ((next = next->nextaccess));
  3160. X
  3161. X    error("user %s not on the access list", getcaller());
  3162. X    return false;
  3163. X}
  3164. X
  3165. X
  3166. X    int
  3167. dorewrite(lockflag, changed)
  3168. X    int lockflag, changed;
  3169. X/*
  3170. X * Do nothing if LOCKFLAG is zero.
  3171. X * Prepare to rewrite an RCS file if CHANGED is positive.
  3172. X * Stop rewriting if CHANGED is zero, because there won't be any changes.
  3173. X * Fail if CHANGED is negative.
  3174. X * Return true on success.
  3175. X */
  3176. X{
  3177. X    int r, e;
  3178. X
  3179. X    if (lockflag)
  3180. X        if (changed) {
  3181. X            if (changed < 0)
  3182. X                return false;
  3183. X            putadmin(frewrite);
  3184. X            puttree(Head, frewrite);
  3185. X            aprintf(frewrite, "\n\n%s%c", Kdesc, nextc);
  3186. X            foutptr = frewrite;
  3187. X        } else {
  3188. X            Ozclose(&frewrite);
  3189. X            seteid();
  3190. X            ignoreints();
  3191. X            r = un_link(newRCSfilename);
  3192. X            e = errno;
  3193. X            keepdirtemp(newRCSfilename);
  3194. X            restoreints();
  3195. X            setrid();
  3196. X            if (r != 0) {
  3197. X                enerror(e, RCSfilename);
  3198. X                return false;
  3199. X            }
  3200. X        }
  3201. X    return true;
  3202. X}
  3203. X
  3204. X    int
  3205. donerewrite(changed)
  3206. X    int changed;
  3207. X/*
  3208. X * Finish rewriting an RCS file if CHANGED is nonzero.
  3209. X * Return true on success.
  3210. X */
  3211. X{
  3212. X    int r, e;
  3213. X
  3214. X    if (changed && !nerror) {
  3215. X        if (finptr) {
  3216. X            fastcopy(finptr, frewrite);
  3217. X            Izclose(&finptr);
  3218. X        }
  3219. X        if (1 < RCSstat.st_nlink)
  3220. X            warn("breaking hard link to %s", RCSfilename);
  3221. X        seteid();
  3222. X        ignoreints();
  3223. X        r = chnamemod(&frewrite, newRCSfilename, RCSfilename,
  3224. X            RCSstat.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH)
  3225. X        );
  3226. X        e = errno;
  3227. X        keepdirtemp(newRCSfilename);
  3228. X        restoreints();
  3229. X        setrid();
  3230. X        if (r != 0) {
  3231. X            enerror(e, RCSfilename);
  3232. X            error("saved in %s", newRCSfilename);
  3233. X            dirtempunlink();
  3234. X            return false;
  3235. X        }
  3236. X    }
  3237. X    return true;
  3238. X}
  3239. X
  3240. X    void
  3241. aflush(f)
  3242. X    FILE *f;
  3243. X{
  3244. X    if (fflush(f) != 0)
  3245. X        Oerror();
  3246. X}
  3247. END_OF_FILE
  3248. if test 39683 -ne `wc -c <'src/rcsedit.c'`; then
  3249.     echo shar: \"'src/rcsedit.c'\" unpacked with wrong size!
  3250. fi
  3251. # end of 'src/rcsedit.c'
  3252. fi
  3253. echo shar: End of archive 8 \(of 11\).
  3254. cp /dev/null ark8isdone
  3255. MISSING=""
  3256. for I in 1 2 3 4 5 6 7 8 9 10 11 ; do
  3257.     if test ! -f ark${I}isdone ; then
  3258.     MISSING="${MISSING} ${I}"
  3259.     fi
  3260. done
  3261. if test "${MISSING}" = "" ; then
  3262.     echo You have unpacked all 11 archives.
  3263.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  3264. else
  3265.     echo You still need to unpack the following archives:
  3266.     echo "        " ${MISSING}
  3267. fi
  3268. ##  End of shell archive.
  3269. exit 0
  3270.