source: trunk/athena/bin/delete/util.c @ 1853

Revision 1853, 4.4 KB checked in by jik, 35 years ago (diff)
is_dotfile and is_deleted procedures are removed because they are turned into macros in util.h
Line 
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.8 1989-05-04 14:28:11 jik Exp $";
15#endif
16
17#include <stdio.h>
18#include <sys/param.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <sys/dir.h>
22#include <strings.h>
23#include <pwd.h>
24#include "directories.h"
25#include "util.h"
26#include "mit-copyright.h"
27
28char *getenv();
29
30
31char *convert_to_user_name(real_name, user_name)
32char real_name[];
33char user_name[];  /* RETURN */
34{
35     char *ptr, *q;
36     
37     strcpy(user_name, real_name);
38     while (ptr = strrindex(user_name, ".#")) {
39          for (q = ptr; *(q + 2); q++)
40               *q = *(q + 2);
41          *q = '\0';
42     }
43     return (user_name);
44}
45
46     
47
48
49
50char *strindex(str, sub_str)
51char *str, *sub_str;
52{
53     char *ptr = str;
54     while (ptr = index(ptr, *sub_str)) {
55          if (! strncmp(ptr, sub_str, strlen(sub_str)))
56               return(ptr);
57          ptr++;
58     }
59     return ((char *) NULL);
60}
61
62
63
64char *strrindex(str, sub_str)
65char *str, *sub_str;
66{
67     char *ptr;
68
69     if (strlen(str))
70          ptr = &str[strlen(str) - 1];
71     else
72          return((char *) NULL);
73     while ((*ptr != *sub_str) && (ptr != str)) ptr--;
74     while (ptr != str) {
75          if (! strncmp(ptr, sub_str, strlen(sub_str)))
76               return(ptr);
77          ptr--;
78          while ((*ptr != *sub_str) && (ptr != str)) ptr--;
79     }
80     if (! strncmp(ptr, sub_str, strlen(sub_str)))
81          return(str);
82     else
83          return ((char *) NULL);
84}
85     
86     
87/*
88 * NOTE: Append uses a static array, so its return value must be
89 * copied immediately.
90 */
91char *append(filepath, filename)
92char *filepath, *filename;
93{
94     static char buf[MAXPATHLEN];
95
96     strcpy(buf, filepath);
97     if ((! *filename) || (! *filepath)) {
98          strcpy(buf, filename);
99          return(buf);
100     }
101     if (buf[strlen(buf) - 1] == '/')
102          buf[strlen(buf) - 1] = '\0';
103     if (strlen(buf) + strlen(filename) + 2 > MAXPATHLEN) {
104          *buf = '\0';
105          return(buf);
106     }
107     strcat(buf, "/");
108     strcat(buf, filename);
109     return(buf);
110}
111
112
113
114
115yes() {
116     char buf[BUFSIZ];
117     char *val;
118     
119     val = fgets(buf, BUFSIZ, stdin);
120     if (! val) {
121          printf("\n");
122          exit(1);
123     }
124     if (! index(buf, '\n')) do
125          fgets(buf + 1, BUFSIZ - 1, stdin);
126     while (! index(buf + 1, '\n'));
127     return(*buf == 'y');
128}
129
130
131
132
133char *lastpart(filename)
134char *filename;
135{
136     char *part;
137
138     part = rindex(filename, '/');
139
140     if (! part)
141          part = filename;
142     else if (part == filename)
143          part++;
144     else if (part - filename + 1 == strlen(filename)) {
145          part = rindex(--part, '/');
146          if (! part)
147               part = filename;
148          else
149               part++;
150     }
151     else
152          part++;
153
154     return(part);
155}
156
157
158
159
160char *firstpart(filename, rest)
161char *filename;
162char *rest; /* RETURN */
163{
164     char *part;
165     static char buf[MAXPATHLEN];
166
167     strcpy(buf, filename);
168     part = index(buf, '/');
169     if (! part) {
170          *rest = '\0';
171          return(buf);
172     }
173     strcpy(rest, part + 1);
174     *part = '\0';
175     return(buf);
176}
177
178
179
180
181char *reg_firstpart(filename, rest)
182char *filename;
183char *rest; /* RETURN */
184{
185     static char first[MAXNAMLEN];
186     
187     sprintf(first, "^%s$", firstpart(filename, rest));
188     return(first);
189}
190
191
192
193
194
195
196get_home(buf)
197char *buf;
198{
199     char *user;
200     struct passwd *psw;
201     
202     strcpy(buf, getenv("HOME"));
203     
204     if (*buf)
205          return(0);
206
207     user = getenv("USER");
208     psw = getpwnam(user);
209
210     if (psw) {
211          strcpy(buf, psw->pw_dir);
212          return(0);
213     }
214     
215     psw = getpwuid(getuid());
216
217     if (psw) {
218          strcpy(buf, psw->pw_dir);
219          return(0);
220     } 
221     return(1);
222}
223
224
225
226
227timed_out(file_ent, current_time, min_days)
228filerec *file_ent;
229int current_time, min_days;
230{
231     if ((current_time - file_ent->specs.st_mtime) / 86400 >= min_days)
232          return(1);
233     else
234          return(0);
235}
236
237
238
239int directory_exists(dirname)
240char *dirname;
241{
242     struct stat stat_buf;
243
244     if (stat(dirname, &stat_buf))
245          return(0);
246     else if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
247          return(1);
248     else
249          return(0);
250}
251
252               
Note: See TracBrowser for help on using the repository browser.