summaryrefslogtreecommitdiff
path: root/usr.bin/find/printf.c
blob: c1be04376156d1620c5d978ccdcbb6e055126f60 (plain)
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
/*-
 * Copyright (c) 2023, Netflix, Inc
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <sys/types.h>

#include <err.h>
#include <errno.h>
#include <fts.h>
#include <grp.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "find.h"

/* translate \X to proper escape, or to itself if no special meaning */
static const char *esc = "\a\bcde\fghijklm\nopq\rs\tu\v";

static inline bool
isoct(char c)
{
	return (c >= '0' && c <= '7');
}

static inline bool
isesc(char c)
{
	return (c >= 'a' && c <= 'v' && esc[c - 'a'] != c);
}

static char *
escape(const char *str, bool *flush, bool *warned)
{
	char c;
	int value;
	char *tmpstr;
	size_t tmplen;
	FILE *fp;

	fp = open_memstream(&tmpstr, &tmplen);

	/*
	 * Copy the str string into a new struct sbuf and return that expanding
	 * the different ANSI escape sequences.
	 */
	*flush = false;
	for (c = *str++; c; c = *str++) {
		if (c != '\\') {
			putc(c, fp);
			continue;
		}
		c = *str++;

		/*
		 * User error \ at end of string
		 */
		if (c == '\0') {
			putc('\\', fp);
			break;
		}

		/*
		 * \c terminates output now and is supposed to flush the output
		 * too...
		 */
		if (c == 'c') {
			*flush = true;
			break;
		}

		/*
		 * Is it octal? If so, decode up to 3 octal characters.
		 */
		if (isoct(c)) {
			value = 0;
			for (int i = 3; i-- > 0 && isoct(c);
			     c = *str++) {
				value <<= 3;
				value += c - '0';
			}
			str--;
			putc((char)value, fp);
			continue;
		}

		/*
		 * It's an ANSI X3.159-1989 escape, use the mini-escape lookup
		 * table to translate.
		 */
		if (isesc(c)) {
			putc(esc[c - 'a'], fp);
			continue;
		}

		/*
		 * Otherwise, it's self inserting. gnu find specifically says
		 * not to rely on this behavior though. gnu find will issue
		 * a warning here, while printf(1) won't.
		 */
		if (!*warned) {
			warn("Unknown character %c after \\.", c);
			*warned = true;
		}
		putc(c, fp);
	}
	fclose(fp);

	return (tmpstr);
}

static void
fp_ctime(FILE *fp, time_t t)
{
	char s[26];

	ctime_r(&t, s);
	s[24] = '\0';	/* kill newline, though gnu find info silent on issue */
	fputs(s, fp);
}

/*
 * Assumes all times are displayed in UTC rather than local time, gnu find info
 * page silent on the issue.
 *
 * Also assumes that gnu find doesn't support multiple character escape sequences,
 * which it's info page is also silent on.
 */
static void
fp_strftime(FILE *fp, time_t t, char mod)
{
	struct tm tm;
	char buffer[128];
	char fmt[3] = "% ";

	/*
	 * Gnu libc extension we don't yet support -- seconds since epoch
	 * Used in Linux kernel build, so we kinda have to support it here
	 */
	if (mod == '@')	{
		fprintf(fp, "%ju", (uintmax_t)t);
		return;
	}

	gmtime_r(&t, &tm);
	fmt[1] = mod;
	if (strftime(buffer, sizeof(buffer), fmt, &tm) == 0)
		errx(1, "Format bad or data too long for buffer"); /* Can't really happen ??? */
	fputs(buffer, fp);
}

void
do_printf(PLAN *plan, FTSENT *entry, FILE *fout)
{
	char buf[4096];
	struct stat sb;
	struct stat *sp;
	const char *path, *pend;
	char *all, *fmt;
	ssize_t ret;
	int c;
	bool flush, warned;

	warned = (plan->flags & F_HAS_WARNED) != 0;
	all = fmt = escape(plan->c_data, &flush, &warned);
	if (warned)
		plan->flags |= F_HAS_WARNED;
	for (c = *fmt++; c; c = *fmt++) {
		sp = entry->fts_statp;
		if (c != '%') {
			putc(c, fout);
			continue;
		}
		c = *fmt++;
		/* Style(9) deviation: case order same as gnu find info doc */
		switch (c) {
		case '%':
			putc(c, fout);
			break;
		case 'p': /* Path to file */
			fputs(entry->fts_path, fout);
			break;
		case 'f': /* filename w/o dirs */
			fputs(entry->fts_name, fout);
			break;
		case 'h':
			/*
			 * path, relative to the starting point, of the file, or
			 * '.' if that's empty for some reason.
			 */
			path = entry->fts_path;
			pend = strrchr(path, '/');
			if (pend == NULL)
				putc('.', fout);
			else
				fwrite(path, pend - path, 1, fout);
			break;
		case 'P': /* file with command line arg rm'd -- HOW? fts_parent? */
			errx(1, "%%%c is unimplemented", c);
		case 'H': /* Command line arg -- HOW? */
			errx(1, "%%%c is unimplemented", c);
		case 'g': /* gid human readable */
			fputs(group_from_gid(sp->st_gid, 0), fout);
			break;
		case 'G': /* gid numeric */
			fprintf(fout, "%d", sp->st_gid);
			break;
		case 'u': /* uid human readable */
			fputs(user_from_uid(sp->st_uid, 0), fout);
			break;
		case 'U': /* uid numeric */
			fprintf(fout, "%d", sp->st_uid);
			break;
		case 'm': /* mode in octal */
			fprintf(fout, "%o", sp->st_mode & 07777);
			break;
		case 'M': /* Mode in ls-standard form */
			strmode(sp->st_mode, buf);
			fwrite(buf, 10, 1, fout);
			break;
		case 'k': /* kbytes used by file */
			fprintf(fout, "%jd", (intmax_t)sp->st_blocks / 2);
			break;
		case 'b': /* blocks used by file */
			fprintf(fout, "%jd", (intmax_t)sp->st_blocks);
			break;
		case 's': /* size in bytes of file */
			fprintf(fout, "%ju", (uintmax_t)sp->st_size);
			break;
		case 'S': /* sparseness of file */
			fprintf(fout, "%3.1f",
			    (float)sp->st_blocks * 512 / (float)sp->st_size);
			break;
		case 'd': /* Depth in tree */
			fprintf(fout, "%ld", entry->fts_level);
			break;
		case 'D': /* device number */
			fprintf(fout, "%ju", (uintmax_t)sp->st_dev);
			break;
		case 'F': /* Filesystem type */
			errx(1, "%%%c is unimplemented", c);
		case 'l': /* object of symbolic link */
			ret = readlink(entry->fts_accpath, buf, sizeof(buf));
			if (ret > 0)
				fwrite(buf, ret, 1, fout);
			break;
		case 'i': /* inode # */
			fprintf(fout, "%ju", (uintmax_t)sp->st_ino);
			break;
		case 'n': /* number of hard links */
			fprintf(fout, "%ju", (uintmax_t)sp->st_nlink);
			break;
		case 'Y': /* -type of file, following 'l' types L loop ? error */
			if (S_ISLNK(sp->st_mode)) {
				if (stat(entry->fts_accpath, &sb) != 0) {
					switch (errno) {
					case ELOOP:
						putc('L', fout);
						break;
					case ENOENT:
						putc('N', fout);
						break;
					default:
						putc('?', fout);
						break;
					}
					break;
				}
				sp = &sb;
			}
			/* FALLTHROUGH */
		case 'y': /* -type of file, incl 'l' */
			switch (sp->st_mode & S_IFMT) {
			case S_IFIFO:
				putc('p', fout);
				break;
			case S_IFCHR:
				putc('c', fout);
				break;
			case S_IFDIR:
				putc('d', fout);
				break;
			case S_IFBLK:
				putc('b', fout);
				break;
			case S_IFREG:
				putc('f', fout);
				break;
			case S_IFLNK:
				putc('l', fout);
				break;
			case S_IFSOCK:
				putc('s', fout);
				break;
			case S_IFWHT:
				putc('w', fout);
				break;
			default:
				putc('U', fout);
				break;
			}
			break;
		case 'a': /* access time ctime */
			fp_ctime(fout, sp->st_atime);
			break;
		case 'A': /* access time with next char strftime format */
			fp_strftime(fout, sp->st_atime, *fmt++);
			break;
		case 'B': /* birth time with next char strftime format */
#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
			if (sp->st_birthtime != 0)
				fp_strftime(fout, sp->st_birthtime, *fmt);
#endif
			fmt++;
			break;	/* blank on systems that don't support it */
		case 'c': /* status change time ctime */
			fp_ctime(fout, sp->st_ctime);
			break;
		case 'C': /* status change time with next char strftime format */
			fp_strftime(fout, sp->st_ctime, *fmt++);
			break;
		case 't': /* modification change time ctime */
			fp_ctime(fout, sp->st_mtime);
			break;
		case 'T': /* modification time with next char strftime format */
			fp_strftime(fout, sp->st_mtime, *fmt++);
			break;
		case 'Z': /* empty string for compat SELinux context string */
			break;
		/* Modifier parsing here, but also need to modify above somehow */
		case '#': case '-': case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9': case '.':
			errx(1, "Format modifier %c not yet supported: '%s'", c, all);
		/* Any FeeeBSD-specific modifications here -- none yet */
		default:
			errx(1, "Unknown format %c '%s'", c, all);
		}
	}
	if (flush)
		fflush(fout);
	free(all);
}