File: cmd_find.c

package info (click to toggle)
sash 3.7-7.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 380 kB
  • ctags: 306
  • sloc: ansic: 6,232; sh: 148; makefile: 35
file content (374 lines) | stat: -rw-r--r-- 6,144 bytes parent folder | download | duplicates (5)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * Copyright (c) 2002 by David I. Bell
 * Permission is granted to use, distribute, or modify this source,
 * provided that this copyright notice remains intact.
 *
 * The "find" built-in command.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>

#include "sash.h"


#ifdef	S_ISLNK
#define	LSTAT	lstat
#else
#define	LSTAT	stat
#endif


#define	MAX_NAME_SIZE	(1024 * 10)


/*
 * Items that can be specified to restrict the output.
 */
static	BOOL		xdevFlag;
static	dev_t		xdevDevice;
static	long		fileSize;
static	const char *	filePattern;
static	const char *	fileType;


/*
 * Recursive routine to examine the files in a directory.
 */
static	void	examineDirectory(const char * path);
static	BOOL	testFile(const char * fullName, const struct stat * statBuf);



/*
 * Find files from the specified directory path.
 * This is limited to just printing their file names.
 */
void
do_find(int argc, const char ** argv)
{
	const char *	cp;
	const char *	path;
	struct stat	statBuf;

	argc--;
	argv++;

	xdevFlag = FALSE;
	fileType = NULL;
	filePattern = NULL;
	fileSize = 0;

	if ((argc <= 0) || (**argv == '-'))
	{
		fprintf(stderr, "No path specified\n");

		return;
	}

	path = *argv++;
	argc--;

	while (argc > 0)
	{
		argc--;
		cp = *argv++;

		if (strcmp(cp, "-xdev") == 0)
			xdevFlag = TRUE;
		else if (strcmp(cp, "-type") == 0)
		{
			if ((argc <= 0) || (**argv == '-'))
			{
				fprintf(stderr, "Missing type string\n");

				return;
			}

			argc--;
			fileType = *argv++;
		}
		else if (strcmp(cp, "-name") == 0)
		{
			if ((argc <= 0) || (**argv == '-'))
			{
				fprintf(stderr, "Missing file name\n");

				return;
			}

			argc--;
			filePattern = *argv++;
		}
		else if (strcmp(cp, "-size") == 0)
		{
			if ((argc <= 0) || (**argv == '-'))
			{
				fprintf(stderr, "Missing file size\n");

				return;
			}

			argc--;
			cp = *argv++;

			fileSize = 0;

			while (isDecimal(*cp))
				fileSize = fileSize * 10 + (*cp++ - '0');

			if (*cp || (fileSize < 0))
			{
				fprintf(stderr, "Bad file size specified\n");

				return;
			}
		}
		else
		{
			if (*cp != '-')
				fprintf(stderr, "Missing dash in option\n");
			else
				fprintf(stderr, "Unknown option\n");

			return;
		}
	}

	/*
	 * Get information about the path and make sure that it
	 * is a directory.
	 */
	if (stat(path, &statBuf) < 0)
	{
		fprintf(stderr, "Cannot stat \"%s\": %s\n", path,
			strerror(errno));

		return;
	}

	if (!S_ISDIR(statBuf.st_mode))
	{
		fprintf(stderr, "Path \"%s\" is not a directory\n", path);

		return;
	}

	/*
	 * Remember the device that this directory is on in case we need it.
	 */
	xdevDevice = statBuf.st_dev;

	/*
	 * If the directory meets the specified criteria, then print it out.
	 */
	if (testFile(path, &statBuf))
		printf("%s\n", path);

	/*
	 * Now examine the files in the directory.
	 */
	examineDirectory(path);
}


/*
 * Recursive routine to examine the files in a directory.
 */
static void
examineDirectory(const char * path)
{
	DIR *		dir;
	BOOL		needSlash;
	struct dirent *	entry;
	struct stat	statBuf;
	char		fullName[MAX_NAME_SIZE];

	/*
	 * Open the directory.
	 */
	dir = opendir(path);

	if (dir == NULL)
	{
		fprintf(stderr, "Cannot read directory \"%s\": %s\n",
			path, strerror(errno));

		return;
	}

	/*
	 * See if a slash is needed.
	 */
	needSlash = (*path && (path[strlen(path) - 1] != '/'));

	/*
	 * Read all of the directory entries and check them,
	 * except for the current and parent directory entries.
	 */
	while (!intFlag && ((entry = readdir(dir)) != NULL))
	{
		if ((strcmp(entry->d_name, ".") == 0) ||
			(strcmp(entry->d_name, "..") == 0))
		{
			continue;
		}

		/*
		 * Build the full path name.
		 */
		strcpy(fullName, path);

		if (needSlash)
			strcat(fullName, "/");

		strcat(fullName, entry->d_name);

		/*
		 * Find out about this file.
		 */
		if (LSTAT(fullName, &statBuf) < 0)
		{
			fprintf(stderr, "Cannot stat \"%s\": %s\n",
				fullName, strerror(errno));

			continue;
		}

		/*
		 * If this file matches the criteria that was
		 * specified then print its name out.
		 */
		if (testFile(fullName, &statBuf))
			printf("%s\n", fullName);

		/*
		 * If this is a directory and we are allowed to cross
		 * mount points or the directory is still on the same
		 * device, then examine it's files too.
		 */
		if (S_ISDIR(statBuf.st_mode) &&
			(!xdevFlag || (statBuf.st_dev == xdevDevice)))
		{
			examineDirectory(fullName);
		}
	}

	closedir(dir);
}


/*
 * Test a file name having the specified status to see if it should
 * be acted on.  Returns TRUE if the file name has been selected.
 */
static BOOL
testFile(const char * fullName, const struct stat * statBuf)
{
	const char *	cp;
	const char *	entryName;
	BOOL		wantType;
	int		mode;

	mode = statBuf->st_mode;

	/*
	 * Check the file type if it was specified.
	 */
	if (fileType != NULL)
	{
		wantType = FALSE;

		for (cp = fileType; *cp; cp++)
		{
			switch (*cp)
			{
				case 'f':
					if (S_ISREG(mode))
						wantType = TRUE;
					break;

				case 'd':
					if (S_ISDIR(mode))
						wantType = TRUE;

					break;

				case 'p':
					if (S_ISFIFO(mode))
						wantType = TRUE;

					break;

				case 'c':
					if (S_ISCHR(mode))
						wantType = TRUE;

					break;

				case 'b':
					if (S_ISBLK(mode))
						wantType = TRUE;

					break;

				case 's':
					if (S_ISSOCK(mode))
						wantType = TRUE;

					break;

#ifdef	S_ISLNK
				case 'l':
					if (S_ISLNK(mode))
						wantType = TRUE;

					break;
#endif
				default:
					break;
			}
		}

		if (!wantType)
			return FALSE;
	}

	/*
	 * Check the file size if it was specified.
	 * This check only lets regular files and directories through.
	 */
	if (fileSize > 0)
	{
		if (!S_ISREG(mode) && !S_ISDIR(mode))
			return FALSE;

		if (statBuf->st_size < fileSize)
			return FALSE;
	}

	/*
	 * Check the file name pattern if it was specified.
	 */
	if (filePattern != NULL)
	{
		entryName = strrchr(fullName, '/');

		if (entryName)
			entryName++;
		else
			entryName = fullName;

		if (!match(entryName, filePattern))
			return FALSE;
	}

	/*
	 * This file name is wanted.
	 */
	return TRUE;
}

/* END CODE */