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

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