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