xref: /original-bsd/usr.bin/uucp/libuu/expfile.c (revision e78e7ec3)
1 #ifndef lint
2 static char sccsid[] = "@(#)expfile.c	5.4 (Berkeley) 04/10/85";
3 #endif
4 
5 #include "uucp.h"
6 #include <sys/stat.h>
7 
8 /*
9  *	expand file name
10  *
11  *	return codes: 0 - Ordinary spool area file
12  *		      1 - Other normal file
13  *		      FAIL - no Wrkdir name available
14  */
15 
16 expfile(file)
17 char *file;
18 {
19 	register char *fpart, *p;
20 	char user[WKDSIZE], *up;
21 	char full[MAXFULLNAME];
22 	int uid;
23 
24 	switch(file[0]) {
25 	case '/':
26 		return 1;
27 	case '~':
28 		for (fpart = file + 1, up = user; *fpart != '\0'
29 			&& *fpart != '/' && up < user+sizeof(user)-1; fpart++)
30 				*up++ = *fpart;
31 		*up = '\0';
32 		if (!*user || gninfo(user, &uid, full) != 0) {
33 			strcpy(full, PUBDIR);
34 		}
35 
36 		strcat(full, fpart);
37 		strcpy(file, full);
38 		return 1;
39 	default:
40 		p = index(file, '/');
41 		strcpy(full, Wrkdir);
42 		strcat(full, "/");
43 		strcat(full, file);
44 		strcpy(file, full);
45 		if (Wrkdir[0] == '\0')
46 			return FAIL;
47 		else if (p != NULL)
48 			return 1;
49 		return 0;
50 	}
51 }
52 
53 
54 /*
55  *	check if directory name
56  *
57  *	return codes:  0 - not directory  |  1 - is directory
58  */
59 
60 isdir(name)
61 char *name;
62 {
63 	register int ret;
64 	struct stat s;
65 
66 	ret = stat(subfile(name), &s);
67 	if (ret < 0)
68 		return 0;
69 	if ((s.st_mode & S_IFMT) == S_IFDIR)
70 		return 1;
71 	return 0;
72 }
73 
74 
75 /*
76  *	make all necessary directories
77  *
78  *	return SUCCESS  |  FAIL
79  */
80 
81 mkdirs(name)
82 char *name;
83 {
84 	int ret, mask;
85 	char dir[MAXFULLNAME];
86 	register char *p;
87 
88 	for (p = dir + 1;; p++) {
89 		strcpy(dir, name);
90 		if ((p = index(p, '/')) == NULL)
91 			return SUCCESS;
92 		*p = '\0';
93 		if (isdir(dir))
94 			continue;
95 
96 		DEBUG(4, "mkdir - %s\n", dir);
97 		mask = umask(0);
98 		ret = mkdir(dir, 0777);
99 		umask(mask);
100 		if (ret != 0)
101 			return FAIL;
102 	}
103 	/* NOTREACHED */
104 }
105 
106 /*
107  *	expfile and check return
108  *		print error if it failed.
109  *
110  *	return code - SUCCESS - ok; FAIL if expfile failed
111  */
112 
113 ckexpf(file)
114 register char *file;
115 {
116 
117 	if (expfile(file) != FAIL)
118 		return SUCCESS;
119 
120 	/*  could not expand file name */
121 	/* the gwd routine failed */
122 
123 	logent("CAN'T EXPAND FILENAME - PWD FAILED", file+1);
124 	return FAIL;
125 }
126