1 | /* |
---|
2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/delete/util.c,v $ |
---|
3 | * $Author: jik $ |
---|
4 | * |
---|
5 | * This program is a replacement for rm. Instead of actually deleting |
---|
6 | * files, it marks them for deletion by prefixing them with a ".#" |
---|
7 | * prefix. |
---|
8 | * |
---|
9 | * Copyright (c) 1989 by the Massachusetts Institute of Technology. |
---|
10 | * For copying and distribution information, see the file "mit-copyright.h." |
---|
11 | */ |
---|
12 | |
---|
13 | #if (!defined(lint) && !defined(SABER)) |
---|
14 | static char rcsid_util_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/util.c,v 1.4 1989-01-27 02:59:03 jik Exp $"; |
---|
15 | #endif |
---|
16 | |
---|
17 | #include <stdio.h> |
---|
18 | #include <sys/param.h> |
---|
19 | #include <sys/types.h> |
---|
20 | #include <sys/dir.h> |
---|
21 | #include <strings.h> |
---|
22 | #include <pwd.h> |
---|
23 | #include "util.h" |
---|
24 | |
---|
25 | char *getenv(); |
---|
26 | |
---|
27 | |
---|
28 | char *convert_to_user_name(real_name, user_name) |
---|
29 | char real_name[]; |
---|
30 | char user_name[]; /* RETURN */ |
---|
31 | { |
---|
32 | char *ptr, *q; |
---|
33 | |
---|
34 | strcpy(user_name, real_name); |
---|
35 | while (ptr = strrindex(user_name, ".#")) { |
---|
36 | for (q = ptr; *(q + 2); q++) |
---|
37 | *q = *(q + 2); |
---|
38 | *q = '\0'; |
---|
39 | } |
---|
40 | return (user_name); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | char *strindex(str, sub_str) |
---|
48 | char *str, *sub_str; |
---|
49 | { |
---|
50 | char *ptr = str; |
---|
51 | while (ptr = index(ptr, *sub_str)) { |
---|
52 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
53 | return(ptr); |
---|
54 | ptr++; |
---|
55 | } |
---|
56 | return ((char *) NULL); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | char *strrindex(str, sub_str) |
---|
62 | char *str, *sub_str; |
---|
63 | { |
---|
64 | char *ptr; |
---|
65 | |
---|
66 | if (strlen(str)) |
---|
67 | ptr = &str[strlen(str) - 1]; |
---|
68 | else |
---|
69 | return((char *) NULL); |
---|
70 | while ((*ptr != *sub_str) && (ptr != str)) ptr--; |
---|
71 | while (ptr != str) { |
---|
72 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
73 | return(ptr); |
---|
74 | ptr--; |
---|
75 | while ((*ptr != *sub_str) && (ptr != str)) ptr--; |
---|
76 | } |
---|
77 | if (! strncmp(ptr, sub_str, strlen(sub_str))) |
---|
78 | return(str); |
---|
79 | else |
---|
80 | return ((char *) NULL); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | is_dotfile(filename) |
---|
85 | char *filename; |
---|
86 | { |
---|
87 | return (! (strcmp(filename, ".") && strcmp(filename, ".."))); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | int is_deleted(filename) |
---|
93 | char *filename; |
---|
94 | { |
---|
95 | return(! strncmp(filename, ".#", 2)); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | /* |
---|
102 | * NOTE: Append uses a static array, so its return value must be |
---|
103 | * copied immediately. |
---|
104 | */ |
---|
105 | char *append(filepath, filename) |
---|
106 | char *filepath, *filename; |
---|
107 | { |
---|
108 | static char buf[MAXPATHLEN]; |
---|
109 | |
---|
110 | strcpy(buf, filepath); |
---|
111 | if ((! *filename) || (! *filepath)) { |
---|
112 | strcpy(buf, filename); |
---|
113 | return(buf); |
---|
114 | } |
---|
115 | if (buf[strlen(buf) - 1] == '/') |
---|
116 | buf[strlen(buf) - 1] = '\0'; |
---|
117 | if (strlen(buf) + strlen(filename) + 2 > MAXPATHLEN) { |
---|
118 | *buf = '\0'; |
---|
119 | return(buf); |
---|
120 | } |
---|
121 | strcat(buf, "/"); |
---|
122 | strcat(buf, filename); |
---|
123 | return(buf); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | yes() { |
---|
130 | char buf[BUFSIZ]; |
---|
131 | char *val; |
---|
132 | |
---|
133 | val = fgets(buf, BUFSIZ, stdin); |
---|
134 | if (! val) |
---|
135 | exit(1); |
---|
136 | if (! index(buf, '\n')) do |
---|
137 | fgets(buf + 1, BUFSIZ - 1, stdin); |
---|
138 | while (! index(buf + 1, '\n')); |
---|
139 | return(*buf == 'y'); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | char *lastpart(filename) |
---|
146 | char *filename; |
---|
147 | { |
---|
148 | char *part; |
---|
149 | |
---|
150 | part = rindex(filename, '/'); |
---|
151 | |
---|
152 | if (! part) |
---|
153 | part = filename; |
---|
154 | else if (part == filename) |
---|
155 | part++; |
---|
156 | else if (part - filename + 1 == strlen(filename)) { |
---|
157 | part = rindex(--part, '/'); |
---|
158 | if (! part) |
---|
159 | part = filename; |
---|
160 | else |
---|
161 | part++; |
---|
162 | } |
---|
163 | else |
---|
164 | part++; |
---|
165 | |
---|
166 | return(part); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | char *firstpart(filename, rest) |
---|
173 | char *filename; |
---|
174 | char *rest; /* RETURN */ |
---|
175 | { |
---|
176 | char *part; |
---|
177 | static char buf[MAXPATHLEN]; |
---|
178 | |
---|
179 | strcpy(buf, filename); |
---|
180 | part = index(buf, '/'); |
---|
181 | if (! part) { |
---|
182 | *rest = '\0'; |
---|
183 | return(buf); |
---|
184 | } |
---|
185 | strcpy(rest, part + 1); |
---|
186 | *part = '\0'; |
---|
187 | return(buf); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | char *reg_firstpart(filename, rest) |
---|
194 | char *filename; |
---|
195 | char *rest; /* RETURN */ |
---|
196 | { |
---|
197 | static char first[MAXNAMLEN]; |
---|
198 | |
---|
199 | sprintf(first, "^%s$", firstpart(filename, rest)); |
---|
200 | return(first); |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | get_home(buf) |
---|
209 | char *buf; |
---|
210 | { |
---|
211 | char *user; |
---|
212 | struct passwd *psw; |
---|
213 | |
---|
214 | strcpy(buf, getenv("HOME")); |
---|
215 | |
---|
216 | if (*buf) |
---|
217 | return(0); |
---|
218 | |
---|
219 | user = getenv("USER"); |
---|
220 | psw = getpwnam(user); |
---|
221 | |
---|
222 | if (psw) { |
---|
223 | strcpy(buf, psw->pw_dir); |
---|
224 | return(0); |
---|
225 | } |
---|
226 | |
---|
227 | psw = getpwuid(getuid()); |
---|
228 | |
---|
229 | if (psw) { |
---|
230 | strcpy(buf, psw->pw_dir); |
---|
231 | return(0); |
---|
232 | } |
---|
233 | return(1); |
---|
234 | } |
---|