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

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