File: nsfind.c

package info (click to toggle)
lfc-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,676 kB
  • ctags: 10,779
  • sloc: ansic: 146,136; sh: 13,176; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 861; fortran: 113
file content (344 lines) | stat: -rw-r--r-- 8,260 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright (C) 2000-2006 by CERN/IT/PDP/DM
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: nsfind.c,v $ $Revision: 1.3 $ $Date: 2006/09/12 07:11:51 $ CERN IT-PDP/DM Jean-Philippe Baud";
#endif /* not lint */

/*	nsfind - search for files in name server */
#include <errno.h>
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#if defined(_WIN32)
#include <winsock2.h>
#include "statbits.h"
#endif
#include "Cns.h"
#include "Cns_api.h"
#include "Cregexp.h"
#include "serrno.h"
#include "u64subr.h"
#define ONEDAY (24*60*60)
#define SIXMONTHS (6*30*24*60*60)
extern	char	*getenv();
#if sgi
extern char *strdup _PROTO((CONST char *));
#endif
char atimeflg;
int atimeval;
Cregexp_t *expstruct;
char ctimeflg;
int ctimeval;
int errflg;
u_signed64 fileid;
int lsflag;
char mtimeflg;
int mtimeval;
int typeval;
time_t current_time;
main(argc, argv)
int argc;
char **argv;
{
	int c;
	char fullpath[CA_MAXPATHLEN+1];
	int i;
	int lastpath = 0;
	char *p;
	char *path;
#if defined(_WIN32)
	WSADATA wsadata;
#endif

	for (i = 1; i < argc; i++) {
		if (*argv[i] != '-') {	/* path */
			lastpath = i;
		} else {
			if (strcmp (argv[i], "-atime") == 0) {
				if (i >= argc - 1) {
					errflg++;
					continue;
				}
				i++;
				atimeflg = *argv[i];
				if (atimeflg == '-' || atimeflg == '+')
					atimeval = atoi (argv[i]+1);
				else
					atimeval = atoi (argv[i]);
			} else if (strcmp (argv[i], "-ctime") == 0) {
				if (i >= argc - 1) {
					errflg++;
					continue;
				}
				i++;
				ctimeflg = *argv[i];
				if (ctimeflg == '-' || ctimeflg == '+')
					ctimeval = atoi (argv[i]+1);
				else
					ctimeval = atoi (argv[i]);
			} else if (strcmp (argv[i], "-inum") == 0) {
				if (i >= argc - 1) {
					errflg++;
					continue;
				}
				i++;
				fileid = strtou64 (argv[i]);
			} else if (strcmp (argv[i], "-ls") == 0) {
				lsflag = 1;
			} else if (strcmp (argv[i], "-mtime") == 0) {
				if (i >= argc - 1) {
					errflg++;
					continue;
				}
				i++;
				mtimeflg = *argv[i];
				if (mtimeflg == '-' || mtimeflg == '+')
					mtimeval = atoi (argv[i]+1);
				else
					mtimeval = atoi (argv[i]);
			} else if (strcmp (argv[i], "-name") == 0) {
				if (i >= argc - 1) {
					errflg++;
					continue;
				}
				i++;
				if ((expstruct = Cregexp_comp (argv[i])) == NULL)
					errflg++;
			} else if (strcmp (argv[i], "-type") == 0) {
				if (i >= argc - 1) {
					errflg++;
					continue;
				}
				i++;
				typeval = *argv[i] == 'd' ? S_IFDIR :
					  *argv[i] == 'f' ? S_IFREG :
					  *argv[i] == 'l' ? S_IFLNK : 0;
			} else
				errflg++;
		}
	}
	if (lastpath == 0 || errflg) {
		fprintf (stderr,
		    "usage: %s path-list [predicate-list]\n", argv[0]);
		exit (USERR);
	}
	(void) time (&current_time);
#if defined(_WIN32)
	if (WSAStartup (MAKEWORD (2, 0), &wsadata)) {
		fprintf (stderr, NS052);
		exit (SYERR);
	}
#endif
	for (i = 1; i <= lastpath; i++) {
		path = argv[i];
		if (*path != '/' && strstr (path, ":/") == NULL) {
			if ((p = getenv (CNS_HOME_ENV)) == NULL ||
			    strlen (p) + strlen (path) + 1 > CA_MAXPATHLEN) {
				fprintf (stderr, "%s: invalid path\n", path);
				errflg++;
				continue;
			} else
				sprintf (fullpath, "%s/%s", p, path);
		} else {
			if (strlen (path) > CA_MAXPATHLEN) {
				fprintf (stderr, "%s: %s\n", path,
				    sstrerror(SENAMETOOLONG));
				errflg++;
				continue;
			} else
				strcpy (fullpath, path);
		}
		if (procpath (fullpath)) {
			fprintf (stderr, "%s: %s\n", path, sstrerror(serrno));
			errflg++;
		}
	}
#if defined(_WIN32)
	WSACleanup();
#endif
	if (errflg)
		exit (USERR);
	exit (0);
}

listentry(dir, path, statbuf)
char *dir;
char *path;
struct Cns_filestat *statbuf;
{
	struct group *gr;
	char modestr[11];
	struct passwd *pw;
	static gid_t sav_gid = -1;
	static char sav_gidstr[9];
	time_t ltime;
	static uid_t sav_uid = -1;
	static char sav_uidstr[CA_MAXUSRNAMELEN+1];
	char timestr[13];
	struct tm *tm;
	char tmpbuf[21];

	if (statbuf->status == 'D')
		return (0);
	if (lsflag) {
		printf ("%s ", u64tostr (statbuf->fileid, tmpbuf, 20));
		if (statbuf->filemode & S_IFDIR)
			modestr[0] = 'd';
		else
			modestr[0] = statbuf->status;
		modestr[1] = (statbuf->filemode & S_IRUSR) ? 'r' : '-';
		modestr[2] = (statbuf->filemode & S_IWUSR) ? 'w' : '-';
		if (statbuf->filemode & S_IXUSR)
			if (statbuf->filemode & S_ISUID)
				modestr[3] = 's';
			else
				modestr[3] = 'x';
		else
			modestr[3] = '-';
		modestr[4] = (statbuf->filemode & S_IRGRP) ? 'r' : '-';
		modestr[5] = (statbuf->filemode & S_IWGRP) ? 'w' : '-';
		if (statbuf->filemode & S_IXGRP)
			if (statbuf->filemode & S_ISGID)
				modestr[6] = 's';
			else
				modestr[6] = 'x';
		else
			modestr[6] = '-';
		modestr[7] = (statbuf->filemode & S_IROTH) ? 'r' : '-';
		modestr[8] = (statbuf->filemode & S_IWOTH) ? 'w' : '-';
		if (statbuf->filemode & S_IXOTH)
			if (statbuf->filemode & S_ISVTX)
				modestr[9] = 't';
			else
				modestr[9] = 'x';
		else
			modestr[9] = '-';
		modestr[10] = '\0';
		if (statbuf->uid != sav_uid) {
			sav_uid = statbuf->uid;
			if (pw = getpwuid (sav_uid))
				strcpy (sav_uidstr, pw->pw_name);
			else
				sprintf (sav_uidstr, "%-8u", sav_uid);
		}
		if (statbuf->gid != sav_gid) {
			sav_gid = statbuf->gid;
			if (gr = getgrgid (sav_gid))
				strcpy (sav_gidstr, gr->gr_name);
			else
				sprintf (sav_gidstr, "%-8u", sav_gid);
		}
		ltime = statbuf->mtime;
		tm = localtime (&ltime);
		if (ltime < current_time - SIXMONTHS ||
		    ltime > current_time + 60)
			strftime (timestr, 13, "%b %d  %Y", tm);
		else
			strftime (timestr, 13, "%b %d %H:%M", tm);
		printf ("%s %3d %-8.8s %-8.8s %s %s ",
		    modestr, statbuf->nlink, sav_uidstr, sav_gidstr,
	    u64tostr (statbuf->filesize, tmpbuf, 20), timestr);
	}
	printf ("%s/%s", dir, path);
	printf ("\n");
	return (0);
}

procpath (dir)
char *dir;
{
	int c;
	char curdir[CA_MAXPATHLEN+1];
	struct dirlist {
		char *d_name;
		struct dirlist *next;
	};
	Cns_DIR *dirp;
	struct dirlist *dlc;		/* pointer to current directory in the list */
	struct dirlist *dlf = NULL;	/* pointer to first directory in the list */
	struct dirlist *dll;		/* pointer to last directory in the list */
	struct Cns_direnstat *dxp;
	char fullpath[CA_MAXPATHLEN+1];

	if ((dirp = Cns_opendir (dir)) == NULL)
		return (-1);

	if (Cns_chdir (dir) < 0)
		return (-1);
	while ((dxp = Cns_readdirx (dirp)) != NULL) {
		if (dxp->filemode & S_IFDIR) {
			if ((dlc = (struct dirlist *)
			    malloc (sizeof(struct dirlist))) == NULL ||
			    (dlc->d_name = strdup (dxp->d_name)) == NULL) {
				serrno = errno;
				return (-1);
			}
			dlc->next = 0;
			if (dlf == NULL)
				dlf = dlc;
			else
				dll->next = dlc;
			dll = dlc;
		}
		if (atimeflg) {
			if (atimeflg == '-') {
				 if ((current_time - dxp->atime) / ONEDAY > atimeval)
					continue;
			} else if (atimeflg == '+') {
				 if ((current_time - dxp->atime) / ONEDAY < atimeval)
					continue;
			} else
				 if ((current_time - dxp->atime) / ONEDAY != atimeval)
					continue;
		}
		if (ctimeflg) {
			if (ctimeflg == '-') {
				 if ((current_time - dxp->ctime) / ONEDAY > ctimeval)
					continue;
			} else if (ctimeflg == '+') {
				 if ((current_time - dxp->ctime) / ONEDAY < ctimeval)
					continue;
			} else
				 if ((current_time - dxp->ctime) / ONEDAY != ctimeval)
					continue;
		}
		if (fileid && dxp->fileid != fileid) continue;
		if (typeval && (dxp->filemode & S_IFMT) != typeval) continue;
		if (expstruct && Cregexp_exec (expstruct, dxp->d_name)) continue;
		if (mtimeflg) {
			if (mtimeflg == '-') {
				 if ((current_time - dxp->mtime) / ONEDAY > mtimeval)
					continue;
			} else if (mtimeflg == '+') {
				 if ((current_time - dxp->mtime) / ONEDAY < mtimeval)
					continue;
			} else
				 if ((current_time - dxp->mtime) / ONEDAY != mtimeval)
					continue;
		}
		listentry (dir, dxp->d_name, (struct Cns_filestat *)dxp);
	}
	(void) Cns_closedir (dirp);
	while (dlf) {
		sprintf (curdir, "%s/%s", dir, dlf->d_name);
		if (procpath (curdir) < 0) {
			fprintf (stderr, "%s: %s\n", curdir, sstrerror(serrno));
			errflg++;
		}
		free (dlf->d_name);
		dlc = dlf;
		dlf = dlf->next;
		free (dlc);
	}
	if (Cns_chdir ("..") < 0)
		return (-1);
	return (0);
}