File: cmd_file.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 (233 lines) | stat: -rw-r--r-- 3,365 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
/*
 * 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 "file" built-in command.
 */

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

#include "sash.h"


static const char *	checkFile(const char * name);


void
do_file(int argc, const char ** argv)
{
	const char *	name;
	const char *	info;

	argc--;
	argv++;

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

		info = checkFile(name);

		if (info == NULL)
			info = "No information available";

		printf("%s: %s\n", name, info);
	}
}


/*
 * Examine the specified file and return a static string which
 * describes the file type.  Returns NULL on a failure.
 */
static const char *
checkFile(const char * name)
{
	int		mode;
	int		fd;
	int		cc;
	int		i;
	int		ch;
	int		badCount;
	char *		cp;
	struct	stat	statBuf;
	char		data[8192];
	static	char	info[1024];

	cp = info;
	*cp = '\0';

	if (lstat(name, &statBuf) < 0)
	{
		if (errno == ENOENT)
			return "non-existent";

		sprintf(cp, "stat failed: %s", strerror(errno));

		return info;
	}

	/*
	 * Check the file type.
	 */
	mode = statBuf.st_mode;

	if (S_ISDIR(mode))
		return "directory";

	if (S_ISCHR(mode))
		return "character device";

	if (S_ISBLK(mode))
		return "block device";

	if (S_ISFIFO(mode))
		return "named pipe";

#ifdef	S_ISLNK
	if (S_ISLNK(mode))
		return "symbolic link";
#endif

#ifdef	S_ISSOCK
	if (S_ISSOCK(mode))
		return "socket";
#endif

	/*
	 * If the file is not a regular file mention that.
	 */
	if (!S_ISREG(mode))
	{
		sprintf(cp, "unknown mode 0x%x, \n", mode);

		cp += strlen(cp);
	}

	/*
	 * Check for an executable file.
	 */
	if ((mode & (S_IEXEC | S_IXGRP | S_IXOTH)) != 0)
	{
		strcpy(cp, "executable, ");

		cp += strlen(cp);
	}

	/*
	 * The file is a normal file.
	 * Open it if we can and read in the first block.
	 */
	fd = open(name, O_RDONLY);

	if (fd < 0)
	{
		sprintf(cp, "unreadable: %s", strerror(errno));

		return info;
	}

	cc = read(fd, data, sizeof(data));

	if (cc < 0)
	{
		sprintf(cp, "read error: %s", strerror(errno));

		(void) close(fd);

		return info;
	}

	(void) close(fd);

	/*
	 * Check for an empty file.
	 */
	if (cc == 0)
	{
		strcpy(cp, "empty file");

		return info;
	}

	/*
	 * Check for a script file.
	 */
	if ((cc > 2) && (data[0] == '#') && (data[1] == '!'))
	{
		char *	begin;
		char *	end;

		data[sizeof(data) - 1] = '\0';

		begin = &data[2];

		while (*begin == ' ')
			begin++;

		end = begin;

		while (*end && (*end != ' ') && (*end != '\n'))
			end++;

		*end = '\0';

		sprintf(cp, "script for \"%s\"", begin);

		return info;
	}

	/*
	 * Check for special binary data types.
	 */
	if ((data[0] == '\037') && (data[1] == '\235'))
		return "compressed file";

	if ((data[0] == '\037') && (data[1] == '\213'))
		return "GZIP file";

	if ((data[0] == '\177') && (memcmp(&data[1], "ELF", 3) == 0))
	{
		strcpy(cp, "ELF program");

		return info;
	}

	/*
	 * Check for binary data.
	 */
	badCount = 0;

	for (i = 0; i < cc; i++)
	{
		ch = data[i];

		if ((ch == '\n') || (ch == '\t'))
			continue;

		if (isspace(ch) || isprint(ch))
			continue;

		badCount++;
	}

	if (badCount != 0)
	{
		strcpy(cp, "binary");

		return info;
	}

	/*
	 * It is just a text file.
	 */
	strcpy(cp, "text file");

	return info;
}

/* END CODE */