-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcsfnms.c
executable file
·1226 lines (1101 loc) · 33.5 KB
/
rcsfnms.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* RCS file name handling
*/
/****************************************************************************
* creation and deletion of /tmp temporaries
* pairing of RCS file names and working file names.
* Testprogram: define PAIRTEST
****************************************************************************
*/
/* Copyright (C) 1982, 1988, 1989 Walter Tichy
Copyright 1990 by Paul Eggert
Distributed under license by the Free Software Foundation, Inc.
This file is part of RCS.
RCS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCS; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Report problems and direct all questions to:
rcs-bugs@cs.purdue.edu
*/
/* $Log: rcsfnms.c,v $
* Revision 5.7 1991/01/30 14:13:06 apratt
* Fixed a bug: bindex returns ptr to char PAST match if there's a match.
*
* Revision 5.7 1991/01/30 14:11:14 apratt
* Fixed a bug: bindex returns ptr to char PAST match if there's a match.
*
* Revision 5.6 91/01/29 17:45:54 apratt
* Added AKP_BUGFIXES around my bug fixes, changed headstr
*
* Revision 5.5 91/01/16 15:45:00 apratt
* This version works passably on the ST.
*
* Revision 5.4 90/11/01 05:03:43 eggert
* checked in with -k by apratt at 91.01.10.13.15.14.
*
* Revision 5.4 1990/11/01 05:03:43 eggert
* Permit arbitrary data in comment leaders.
*
* Revision 5.3 1990/09/14 22:56:16 hammer
* added more filename extensions and their comment leaders
*
* Revision 5.2 1990/09/04 08:02:23 eggert
* Fix typo when !RCSSEP.
*
* Revision 5.1 1990/08/29 07:13:59 eggert
* Work around buggy compilers with defective argument promotion.
*
* Revision 5.0 1990/08/22 08:12:50 eggert
* Ignore signals when manipulating the semaphore file.
* Modernize list of file name extensions.
* Permit paths of arbitrary length. Beware file names beginning with "-".
* Remove compile-time limits; use malloc instead.
* Permit dates past 1999/12/31. Make lock and temp files faster and safer.
* Ansify and Posixate.
* Don't use access(). Fix test for non-regular files. Tune.
*
* Revision 4.8 89/05/01 15:09:41 narten
* changed getwd to not stat empty directories.
*
* Revision 4.7 88/08/09 19:12:53 eggert
* Fix troff macro comment leader bug; add Prolog; allow cc -R; remove lint.
*
* Revision 4.6 87/12/18 11:40:23 narten
* additional file types added from 4.3 BSD version, and SPARC assembler
* comment character added. Also, more lint cleanups. (Guy Harris)
*
* Revision 4.5 87/10/18 10:34:16 narten
* Updating version numbers. Changes relative to 1.1 actually relative
* to verion 4.3
*
* Revision 1.3 87/03/27 14:22:21 jenkins
* Port to suns
*
* Revision 1.2 85/06/26 07:34:28 svb
* Comment leader '% ' for '*.tex' files added.
*
* Revision 4.3 83/12/15 12:26:48 wft
* Added check for KDELIM in file names to pairfilenames().
*
* Revision 4.2 83/12/02 22:47:45 wft
* Added csh, red, and sl file name suffixes.
*
* Revision 4.1 83/05/11 16:23:39 wft
* Added initialization of Dbranch to InitAdmin(). Canged pairfilenames():
* 1. added copying of path from workfile to RCS file, if RCS file is omitted;
* 2. added getting the file status of RCS and working files;
* 3. added ignoring of directories.
*
* Revision 3.7 83/05/11 15:01:58 wft
* Added comtable[] which pairs file name suffixes with comment leaders;
* updated InitAdmin() accordingly.
*
* Revision 3.6 83/04/05 14:47:36 wft
* fixed Suffix in InitAdmin().
*
* Revision 3.5 83/01/17 18:01:04 wft
* Added getwd() and rename(); these can be removed by defining
* V4_2BSD, since they are not needed in 4.2 bsd.
* Changed sys/param.h to sys/types.h.
*
* Revision 3.4 82/12/08 21:55:20 wft
* removed unused variable.
*
* Revision 3.3 82/11/28 20:31:37 wft
* Changed mktempfile() to store the generated file names.
* Changed getfullRCSname() to store the file and pathname, and to
* delete leading "../" and "./".
*
* Revision 3.2 82/11/12 14:29:40 wft
* changed pairfilenames() to handle file.sfx,v; also deleted checkpathnosfx(),
* checksuffix(), checkfullpath(). Semaphore name generation updated.
* mktempfile() now checks for nil path; freefilename initialized properly.
* Added Suffix .h to InitAdmin. Added testprogram PAIRTEST.
* Moved rmsema, trysema, trydiraccess, getfullRCSname from rcsutil.c to here.
*
* Revision 3.1 82/10/18 14:51:28 wft
* InitAdmin() now initializes StrictLocks=STRICT_LOCKING (def. in rcsbase.h).
* renamed checkpath() to checkfullpath().
*/
#include "rcsbase.h"
libId(fnmsId, "$Id: rcsfnms.c,v 5.7 1991/01/30 14:13:06 apratt Exp $")
const char *RCSfilename;
char *workfilename;
struct stat RCSstat, workstat; /* file status for RCS file and working file */
int haveworkstat;
#if !USE_AKP_PAIRS
static const char rcsdir[] = RCSDIR;
#endif
#define TEMPNAMES 4 /* must be at least DIRTEMPNAMES (see rcsedit.c) */
static char tfnames[TEMPNAMES][L_tmpnam]; /* unlink these when done */
static volatile int tfmade[TEMPNAMES]; /* if these flags are set */
struct compair {
const char *suffix, *comlead;
};
static const struct compair comtable[] = {
/* comtable pairs each filename suffix with a comment leader. The comment */
/* leader is placed before each line generated by the $Log keyword. This */
/* table is used to guess the proper comment leader from the working file's */
/* suffix during initial ci (see InitAdmin()). Comment leaders are needed */
/* for languages without multiline comments; for others they are optional. */
"a", "-- ", /* Ada */
"c", " * ", /* C */
"C", "// ", /* C++ in all its infinite guises */
"CC", "// ",
"c++", "// ",
"cc", "// ",
"cxx", "// ",
"cl", ";;; ", /* Common Lisp */
"cmf", "C ", /* CM FORTRAN */
"cs", " * ", /* C* */
"el", "; ", /* Emacs Lisp */
"f", "c ", /* Fortran */
"for", "c ",
"h", " * ", /* C-header */
"l", " * ", /* lex NOTE: conflict between lex and franzlisp */
"lisp",";;; ", /* Lucid Lisp */
"mac", "; ", /* macro vms or dec-20 or pdp-11 macro */
"me", ".\\\" ",/* me-macros t/nroff*/
"ml", "; ", /* mocklisp */
"mm", ".\\\" ",/* mm-macros t/nroff*/
"ms", ".\\\" ",/* ms-macros t/nroff*/
"p", " * ", /* Pascal */
"pl", "% ", /* Prolog */
"tex", "% ", /* TeX */
"y", " * ", /* yacc */
nil, "# " /* default for unknown suffix; must always be last */
};
void
ffclose(fptr)
FILE * fptr;
/* Function: checks ferror(fptr) and aborts the program if there were
* errors; otherwise closes fptr.
*/
{ if (ferror(fptr) || fclose(fptr)==EOF)
IOerror();
}
char *
maketemp(n)
int n;
/* Create a unique filename using n and the process id and store it
* into the nth slot in tfnames.
* Because of storage in tfnames, tempunlink() can unlink the file later.
* Returns a pointer to the filename created.
*/
{
char *p = tfnames[n];
if (!tfmade[n]) {
#if has_tmpnam
if (!tmpnam(p))
#else
VOID sprintf(p, "%sRCS%cXXXXXX", tmp(), 'A'+n);
if (!mktemp(p))
#endif
faterror("can't make temporary file name");
}
tfmade[n] = true;
return p;
}
void
tempunlink()
/* Clean up maketemp() files. May be invoked by signal handler.
*/
{
register int i;
for (i = TEMPNAMES; 0 <= --i; )
if (tfmade[i]) {
VOID unlink(tfnames[i]);
tfmade[i] = 0;
}
}
const char *
bindex(sp,ch)
register const char *sp;
int ch;
/* Function: Finds the last occurrence of character c in string sp
* and returns a pointer to the character just beyond it. If the
* character doesn't occur in the string, sp is returned.
*/
{
register const char c=ch, *r;
r = sp;
while (*sp) {
if (*sp++ == c) r=sp;
}
return r;
}
static void
InitAdmin()
/* function: initializes an admin node */
{
register const char *Suffix;
register int i;
Head=nil; Dbranch=nil; AccessList=nil; Symbols=nil; Locks=nil;
StrictLocks=STRICT_LOCKING;
/* guess the comment leader from the suffix*/
Suffix=bindex(workfilename, '.');
#if AKP_BUGFIXES
/* Improved by AKP: only match '.' AFTER all SLASHes */
if (Suffix < bindex(workfilename,SLASH)) Suffix = workfilename;
#endif
if (Suffix==workfilename) Suffix= ""; /* empty suffix; will get default*/
for (i=0; comtable[i].suffix && strcmp(Suffix,comtable[i].suffix); i++)
;
Comment.string = comtable[i].comlead;
Comment.size = strlen(comtable[i].comlead);
Lexinit(); /* Note: if finptr==NULL, reads nothing; only initializes*/
}
#if !RCSSEP
static int
isRCSfilename(f, p)
const char *f, *p;
/* Yield true iff F (with pure file name P) is an RCS file name. */
{
return
p-f <= sizeof(rcsdir)-1 &&
((p -= sizeof(rcsdir)-1) == f || p[-1] == SLASH) &&
strncmp(p, rcsdir, sizeof(rcsdir)-1) == 0;
}
#endif
#if RCSSEP
# define findpair(c,v,f,m) findpairfile(c,v,f)
#else
# define findpair(c,v,f,m) findpairfile(c,v,f,m)
#endif
#if !USE_AKP_PAIRS
static char *
#if RCSSEP
findpairfile(argc, argv, fname)
#else
findpairfile(argc, argv, fname, rcsmatch)
int rcsmatch; /* *ARGV must be an RCS file name iff this is set. */
#endif
int argc; char * argv[], *fname;
/* Peek ahead in an ARGC-ARGV argument vector for a pathname ending in FNAME.
* Yield it if found, and set the corresponding pointer in ARGV to nil.
* Yield FNAME otherwise.
*/
{
register char *arg;
#if !RCSSEP
register char *b;
#endif
if (
0 < argc
#if RCSSEP
&& strcmp(bindex(arg = *argv,SLASH), fname) == 0
#else
&& strcmp(b = bindex(arg = *argv,SLASH), fname) == 0
&& isRCSfilename(arg, b) == rcsmatch
#endif
) {
*argv = nil;
return arg;
}
return fname;
}
#endif
static int
handleworkstat(s)
int s;
{
if (s==0 && !S_ISREG(workstat.st_mode)) {
error("%s isn't a regular file", workfilename);
return false;
}
#if AKP_BUGFIXES
haveworkstat = (s == 0 ? 0 : errno);
#else
haveworkstat = errno;
#endif
return true;
}
int getworkstat()
/* Function: get status of workfilename. */
{
errno = 0;
return handleworkstat(stat(workfilename, &workstat));
}
int
getfworkstat(f)
int f;
/* Get status of file descriptor f. */
{
errno = 0;
return handleworkstat(fstat(f, &workstat));
}
#if defined(_POSIX_NO_TRUNC) & _POSIX_NO_TRUNC!=-1
# define LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED 0
#else
# define LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED 1
#endif
#if LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED
#ifdef NAME_MAX
# define filenametoolong(path) (NAME_MAX < strlen(bindex(path,SLASH)))
#else
static int
filenametoolong(path)
char *path;
/* Yield true if the last file name in PATH is too long. */
{
static unsigned long dot_namemax;
register size_t namelen;
register char *lastslash;
register unsigned long namemax;
lastslash = strrchr(path, SLASH);
namelen = strlen(lastslash ? lastslash+1 : path);
if (namelen <= _POSIX_NAME_MAX) /* fast check for shorties */
return false;
if (lastslash) {
*lastslash = 0;
namemax = pathconf(path, _PC_NAME_MAX);
*lastslash = SLASH;
} else {
/* Cache the results for the working directory, for speed. */
if (!dot_namemax)
dot_namemax = pathconf(".", _PC_NAME_MAX);
namemax = dot_namemax;
}
/* If pathconf() yielded -1, namemax is now ULONG_MAX. */
return namemax<namelen;
}
#endif
#endif
void
bufalloc(b, size)
register struct buf *b;
size_t size;
/* Ensure *B is a name buffer of at least SIZE bytes.
* *B's old contents can be freed; *B's new contents are undefined.
*/
{
if (b->size < size) {
if (b->size)
tfree(b->string);
else
b->size = sizeof(malloc_type);
while (b->size < size)
b->size <<= 1;
b->string = tnalloc(char, b->size);
}
}
void
bufrealloc(b, size)
register struct buf *b;
size_t size;
/* like bufalloc, except *B's old contents, if any, are preserved */
{
if (b->size < size) {
if (!b->size)
bufalloc(b, size);
else {
while ((b->size <<= 1) < size)
;
b->string = (char *)testrealloc((malloc_type)b->string, b->size);
}
}
}
void
bufautoend(b)
struct buf *b;
/* Free an auto buffer at block exit. */
{
if (b->size)
tfree(b->string);
}
char *
bufenlarge(b, alim)
register struct buf *b;
const char **alim;
/* Make *B larger. Set *ALIM to its new limit, and yield the relocated value
* of its old limit.
*/
{
size_t s = b->size;
bufrealloc(b, s + 1);
*alim = b->string + b->size;
return b->string + s;
}
void
bufscat(b, s)
struct buf *b;
const char *s;
/* Concatenate S to B's end. */
{
size_t blen = b->string ? strlen(b->string) : 0;
bufrealloc(b, blen+strlen(s)+1);
VOID strcpy(b->string+blen, s);
}
void
bufscpy(b, s)
struct buf *b;
const char *s;
/* Copy S into B. */
{
bufalloc(b, strlen(s)+1);
VOID strcpy(b->string, s);
}
FILE *
rcsreadopen(RCSname)
const char *RCSname;
/* Open RCSNAME for reading and yield its FILE* descriptor.
* Pass this routine to pairfilenames() for read-only access to the file. */
{
#if AKP_BUGFIXES
int errno_hold; /* AKP: this fixes a bug, I think */
#endif
FILE *f;
seteid();
#if AKP_BUGFIXES
errno = 0;
#endif
f = fopen(RCSname, "r");
#if AKP_BUGFIXES
errno_hold = errno;
setrid();
errno = errno_hold;
#else
setrid();
#endif
return f;
}
#if !USE_AKP_PAIRS
int
pairfilenames(argc, argv, rcsopen, mustread, tostdout)
int argc;
char **argv;
FILE *(*rcsopen)P((const char*));
int mustread, tostdout;
/* Function: Pairs the filenames pointed to by argv; argc indicates
* how many there are.
* Places a pointer to the RCS filename into RCSfilename,
* and a pointer to the name of the working file into workfilename.
* If both the workfilename and the RCS filename are given, and tostdout
* is true, a warning is printed.
*
* If the RCS file exists, places its status into RCSstat.
*
* If the RCS file exists, it is RCSOPENed for reading, the file pointer
* is placed into finptr, and the admin-node is read in; returns 1.
* If the RCS file does not exist and mustread is set, an error is printed
* and 0 returned.
* If the RCS file does not exist and !mustread, the admin node
* is initialized and -1 returned.
*
* 0 is returned on all errors, e.g. files that are not regular files.
*/
{
static struct buf RCSbuf, tempbuf;
register char *p, *arg, *tempfilename, *RCS1;
const char *purefname, *pureRCSname;
FILE *lock1;
if (!(arg = *argv)) return 0; /* already paired filename */
if (*arg == '-') {
error("%s option is ignored after file names", arg);
return 0;
}
/* Allocate buffer temporary to hold the default paired file name. */
for (purefname = p = arg; *p; )
switch (*p++) {
case SLASH:
purefname = p;
break;
/* Beware characters that cause havoc with ci -k. */
case KDELIM:
error("RCS file name `%s' contains %c", arg, KDELIM);
return 0;
case ' ': case '\n': case '\t':
error("RCS file name `%s' contains white space", arg);
return 0;
}
bufalloc(&tempbuf, p - purefname + 3);
tempfilename = tempbuf.string;
/* first check suffix to see whether it is an RCS file or not */
#if RCSSEP
if (purefname<(p-=2) && p[0]==RCSSEP && p[1]==RCSSUF)
#else
if (isRCSfilename(arg, purefname))
#endif
{
/* RCS file name given*/
RCS1 = arg;
pureRCSname = purefname;
/* derive workfilename*/
VOID strcpy(tempfilename, purefname);
tempfilename[p - purefname] = 0;
/* try to find workfile name among arguments */
workfilename = findpair(argc-1,argv+1,tempfilename,false);
} else {
/* working file given; now try to find RCS file */
workfilename = arg;
/* derive RCS file name*/
VOID sprintf(tempfilename,"%s%c%c", purefname, RCSSEP, RCSSUF);
/* Try to find RCS file name among arguments*/
RCS1 = findpair(argc-1,argv+1,tempfilename,true);
pureRCSname=bindex(RCS1, SLASH);
}
/* now we have a (tentative) RCS filename in RCS1 and workfilename */
/* Second, try to find the right RCS file */
if (pureRCSname!=RCS1) {
/* a path for RCSfile is given; single RCS file to look for */
errno = 0;
RCSfilename = p = RCS1;
finptr = (*rcsopen)(RCSfilename = p = RCS1);
} else {
/* no path for RCS file name. Prefix it with path of work */
/* file if RCS file omitted. Try RCSDIR subdirectory 1st.*/
bufalloc(&RCSbuf, strlen(workfilename)+sizeof(rcsdir)+2);
RCSfilename = p = RCSbuf.string;
if (RCS1==tempfilename) {
/* RCS file name not given; prepend work path */
VOID strncpy(p, arg, purefname-arg);
p += purefname-arg;
}
VOID strcpy(p, rcsdir);
VOID strcpy(p+sizeof(rcsdir)-1, RCS1);
/* Try D/RCS/file,v. */
errno = 0;
if (!(finptr = (*rcsopen)(RCSfilename))
&& (errno==ENOTDIR || errno==ENOENT)
/*
* Many (broken) systems yield ENOENT, not ENOTDIR,
* when the problem is a missing RCS subdirectory.
*/
) {
lock1 = frewrite;
/* Try D/file,v. */
VOID strcpy(p, RCS1);
errno = 0;
if (!(finptr=(*rcsopen)(RCSfilename)) && errno==ENOENT) {
/*
* Neither file exists; determine the default.
* Prefer D/RCS/file,v to D/file,v.
*/
if (mustread || lock1) {
/* Switch back to D/RCS/file,v. */
VOID strcpy(p, rcsdir);
VOID strcpy(p+sizeof(rcsdir)-1, RCS1);
}
}
}
p = RCSbuf.string;
}
if (finptr) {
if (fstat(fileno(finptr), &RCSstat) < 0)
efaterror(p);
if (!S_ISREG(RCSstat.st_mode)) {
error("%s isn't a regular file -- ignored", p);
return 0;
}
Lexinit(); getadmin();
} else {
if (errno!=ENOENT || mustread || !frewrite) {
if (errno == EEXIST)
error("RCS file %s is in use", p);
else
eerror(p);
return 0;
}
InitAdmin();
};
# if LONG_NAMES_MAY_BE_SILENTLY_TRUNCATED
if (filenametoolong(p)) {
error("RCS file name %s is too long", p);
return 0;
}
# ifndef NAME_MAX
/*
* Check workfilename, even though it is shorter,
* because it may reside on a different filesystem.
*/
if (filenametoolong(workfilename)) {
error("working file name %s is too long", workfilename);
return 0;
}
# endif
# endif
if (tostdout&&
!(RCS1==tempfilename||workfilename==tempfilename))
/*The last term determines whether a pair of */
/* file names was given in the argument list */
warn("Option -p is set; ignoring output file %s",workfilename);
return finptr ? 1 : -1;
}
#else
/* USE_AKP_PAIRS */
#define HEADSTR "head"
#define HSLEN 4
/*
* pairfilenames: does what the normal code does, but handles 8.3
* filenames.
*/
int
pairfilenames(argc, argv, rcsopen, mustread, tostdout)
int argc;
char **argv;
FILE *(*rcsopen)P((const char*));
int mustread, tostdout;
{
static struct buf RCSbuf, tempbuf;
char *temp;
char *temp1;
int checkedRCS = false;
int checkedwork = false;
int retcode = 0;
FILE *fp;
struct stat st;
char tmpbuf[HSLEN+1];
tmpbuf[HSLEN] = '\0'; /* null-terminate the buffer */
if (**argv == '-') {
error("%s: option is ignored after file names", *argv);
return 0;
}
if (bindex(*argv,KDELIM) != *argv) {
error("RCS file name `%s' contains %c",*argv,KDELIM);
return 0;
}
if (index(*argv,' ') || index(*argv,'\t') || index(*argv,'\n')) {
error("RCS file name `%s' contains white space",*argv);
return 0;
}
/* try to open the file as given */
if ((fp = fopen(*argv,"r")) != NULL) {
/* exists; read the header */
if (fread(tmpbuf,1,HSLEN,fp) == HSLEN &&
!strcmp(tmpbuf,HEADSTR)) {
/* you gave the RCS file name and it exists */
fclose(fp);
checkedRCS = true;
stat(*argv,&RCSstat);
retcode = 1;
RCSfilename = *argv;
temp = bindex(*argv,SLASH);
bufalloc(&tempbuf,strlen(temp)+1);
workfilename = tempbuf.string;
strcpy(workfilename,temp);
/* remove ,v at the end of workfilename */
if (((temp = bindex(workfilename,RCSSEP))
!= workfilename) &&
((!*temp) ||
((*temp == RCSSUF) && (*(temp+1) == '\0'))))
*(--temp) = '\0';
}
else {
/* exists, but isn't an RCS file: must be work file */
/* RCSfilename is RCS/name.,v or name.,v if no RCS/ */
fclose(fp);
checkedwork = true;
justwf:
workfilename = *argv;
temp = bindex(*argv,SLASH);
/* magic number 9 below is > max added chars: RCS/.,v */
if ((stat(RCSDIR,&st) == 0) && (st.st_mode & S_IFDIR)) {
bufalloc(&RCSbuf,strlen(temp)+9);
RCSfilename = RCSbuf.string;
sprintf(RCSfilename,"%s%s%s",RCSDIR,temp,
bindex(temp,'.') == temp ? ".,v" : ",v");
}
else {
/* subdir RCS doesn't exist */
bufalloc(&RCSbuf,strlen(temp)+4);
RCSfilename = RCSbuf.string;
sprintf(RCSfilename,"%s%s",temp,
bindex(temp,'.') == temp ? ".,v" : ",v");
}
}
}
else {
/* file didn't exist */
/* remove ,v after file name */
if (((temp = bindex(*argv,RCSSEP)) != *argv) &&
((!*temp) ||
((*temp == RCSSUF) && (*(temp+1) == '\0'))))
*--temp = '\0';
/* if RCS/name.,v exists and is magic, or name.,v exists and */
/* is magic, that file is RCSfilename and you gave work. */
temp = bindex(temp,SLASH);
bufalloc(&tempbuf,strlen(temp)+9);
temp1 = tempbuf.string;
sprintf(temp1,"%s%s%s",RCSDIR,temp,
bindex(temp,'.') == temp ? ".,v" : ",v");
if (stat(temp1,&st) == 0) {
/* RCS/file.,v exists -- fine. */
RCSfilename = temp1;
RCSstat = st;
workfilename = *argv;
}
else {
/* doesn't exist in RCS subdir */
sprintf(temp1,"%s%s",temp,
bindex(temp,'.') == temp ? ".,v" : ",v");
if (stat(temp1,&st) == 0) {
/* ./file.,v exists -- fine. */
RCSfilename = temp1;
workfilename = *argv;
}
else {
/* file doesn't exist anywhere! */
/* same case as above */
goto justwf;
}
}
}
/* at this point, RCSfilename and workfilename are set. */
/* if !checkedwork, then verify workfile, ditto checkedRCS. */
if (!checkedwork) {
if (((fp = fopen(workfilename,"r")) != NULL) &&
(fread(tmpbuf,1,HSLEN,fp) == HSLEN) &&
(!strcmp(tmpbuf,HEADSTR))) {
fclose(fp);
error("Computed work file name %s is actually an RCS file",
workfilename);
return 0;
}
else {
fclose(fp);
}
}
if (!checkedRCS) {
if ((fp = fopen(RCSfilename,"r")) != NULL) {
if ((fread(tmpbuf,1,HSLEN,fp) == HSLEN) &&
(!strcmp(tmpbuf,HEADSTR))) {
stat(RCSfilename,&RCSstat);
fclose(fp);
finptr = (*rcsopen)(RCSfilename);
Lexinit();
getadmin();
return 1;
}
else {
fclose(fp);
error("Computed RCS file name %s is not an RCS file",
RCSfilename);
return 0;
}
}
else {
/* RCS file doesn't exist */
if (mustread) {
error("Can't find computed RCS file %s",RCSfilename);
return 0;
}
else {
finptr = (*rcsopen)(RCSfilename);
InitAdmin();
return -1;
}
}
}
else {
/* RCS file was checked, so we know it exists */
finptr = (*rcsopen)(RCSfilename);
Lexinit();
getadmin();
return 1;
}
}
/* end if USE_AKP_PAIRS */
#endif
const char *
getfullRCSname()
/* Function: returns a pointer to the full path name of the RCS file.
* Gets the working directory's name at most once.
* Removes leading "../" and "./".
*/
{
static const char *wd;
static struct buf rcsbuf, wdbuf;
static size_t pathlength;
register const char *realname;
register size_t parentdirlength;
register unsigned dotdotcounter;
register char *d;
if (ROOTPATH(RCSfilename)) {
return(RCSfilename);
} else {
if (!wd) { /* Get working directory for the first time. */
if (!(d = cgetenv("PWD"))) {
bufalloc(&wdbuf, 1 +
# ifdef PATH_MAX
PATH_MAX
# else
_POSIX_PATH_MAX
# endif
);
errno = 0;
# if !has_getcwd
d = getwd(wdbuf.string);
# else
while (
!(d = getcwd(wdbuf.string,(int)wdbuf.size))
&& errno==ERANGE
)
bufalloc(&wdbuf, wdbuf.size<<1);
# endif
if (!d)
efaterror("working directory");
}
pathlength = strlen(d);
while (pathlength && d[pathlength-1]==SLASH) {
d[--pathlength] = 0;
/* Check needed because some getwd implementations */
/* generate "/" for the root. */
}
wd = d;
}
/*the following must be redone since RCSfilename may change*/
/* Find how many `../'s to remove from RCSfilename. */
dotdotcounter =0;
realname = RCSfilename;
while( realname[0]=='.' &&
(realname[1]==SLASH||(realname[1]=='.'&&realname[2]==SLASH))){
if (realname[1]==SLASH) {
/* drop leading ./ */
realname += 2;
} else {
/* drop leading ../ and remember */
dotdotcounter++;
realname += 3;
}
}
/* Now remove dotdotcounter trailing directories from wd. */
parentdirlength = pathlength;
while (dotdotcounter && parentdirlength) {
/* move pointer backwards over trailing directory */
if (wd[--parentdirlength] == SLASH) {
dotdotcounter--;
}
}
if (dotdotcounter) {
error("can't generate full path name for RCS file");
return RCSfilename;
} else {
/* build full path name */
bufalloc(&rcsbuf, parentdirlength+strlen(realname)+2);
VOID strncpy(rcsbuf.string, wd, parentdirlength);
rcsbuf.string[parentdirlength++] = SLASH;
VOID strcpy(rcsbuf.string+parentdirlength, realname);
return rcsbuf.string;
}
}
}
const char *
tmp()
/* Yield the name of the tmp directory, with a trailing SLASH. */
{
static const char *s;
if (!s)
if (!(s = getenv("TMP")))
s = TMPDIR;
else {
size_t l = strlen(s);
int extra = l && s[l-1]!=SLASH;
char *p = ftnalloc(char, l + extra + 1);
VOID strcpy(p, s);
if (extra) {
p[l] = SLASH;
p[l+1] = 0;
}
s = p;
}
return s;