xref: /original-bsd/usr.bin/uucp/libuu/subdir.c (revision 4c01ad61)
1 #ifndef lint
2 static char sccsid[] = "@(#)subdir.c	5.3 (Berkeley) 04/10/85";
3 #endif
4 
5 #include "uucp.h"
6 /*
7  * By Tom Truscott, March 1983
8  *
9  * Prefix table.
10  * If a prefix is "abc", for example,
11  * then any file Spool/abc... is mapped to Spool/abc/abc... .
12  * The first prefix found is used, so D.foo should preceed D. in table.
13  *
14  * Each prefix must be a subdirectory of Spool, owned by uucp!
15  * Remember: use cron to uuclean these directories daily,
16  * and check them manually every now and then.  Beware complacency!
17  */
18 
19 static char *prefix[] = {
20 	DLocalX,	/* Outbound 'xqt' request files */
21 	DLocal,		/* Outbound data files */
22 	"D.",		/* Other "D." files (remember the "."!) */
23 	"C.",		/* "C." subdirectory */
24 	"X.",		/* "X." subdirectory */
25 	"TM.",		/* Temporaries for inbound files */
26 	0
27 };
28 
29 /*
30  * filename mapping kludges to put uucp work files in other directories.
31  */
32 
33 #define	BUFLEN	50
34 
35 static	char fn1[BUFLEN], fn2[BUFLEN];	/* remapped filename areas */
36 static	int	inspool;		/* true iff working dir is Spool */
37 
38 /*
39  * return (possibly) remapped string s
40  */
41 char *
42 subfile(as)
43 char *as;
44 {
45 	register char *s, **p;
46 	register int n;
47 	static char *tptr = NULL;
48 
49 	/* Alternate buffers so "link(subfile(a), subfile(b))" works */
50 	if (tptr != fn1)
51 		tptr = fn1;
52 	else
53 		tptr = fn2;
54 
55 	s = as;
56 	tptr[0] = '\0';
57 
58 	/* if s begins with Spool/, copy that to tptr and advance s */
59 	if (strncmp(s, Spool, n = strlen(Spool)) == 0 && s[n] == '/') {
60 		if (!inspool) {
61 			strcpy(tptr, Spool);
62 			strcat(tptr, "/");
63 		}
64 		s += n + 1;
65 	}
66 	else
67 		if (!inspool)
68 			return as;
69 
70 	/* look for first prefix which matches, and make subdirectory */
71 	for (p = &prefix[0]; *p; p++) {
72 		if (strncmp(s, *p, n = strlen(*p))==0 && s[n] && s[n] != '/') {
73 			strcat(tptr, *p);
74 			strcat(tptr, "/");
75 			strcat(tptr, s);
76 			return tptr;
77 		}
78 	}
79 	return as;
80 }
81 
82 /*
83  * save away filename
84  */
85 subchdir(s)
86 register char *s;
87 {
88 	inspool = (strcmp(s, Spool) == 0);
89 	return chdir(s);
90 }
91 
92 /*
93  * return possibly corrected directory for searching
94  */
95 char *
96 subdir(d, pre)
97 register char *d, pre;
98 {
99 	if (strcmp(d, Spool) == 0)
100 		if (pre == CMDPRE)
101 			return CMDSDIR;
102 		else if (pre == XQTPRE)
103 			return XEQTDIR;
104 	return d;
105 }
106