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