GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ls/print.c Lines: 101 176 57.4 %
Date: 2017-11-07 Branches: 65 167 38.9 %

Line Branch Exec Source
1
/*	$OpenBSD: print.c,v 1.37 2016/08/16 16:09:24 krw Exp $	*/
2
/*	$NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $	*/
3
4
/*
5
 * Copyright (c) 1989, 1993, 1994
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * This code is derived from software contributed to Berkeley by
9
 * Michael Fischbein.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 */
35
36
#include <sys/types.h>
37
#include <sys/stat.h>
38
39
#include <err.h>
40
#include <errno.h>
41
#include <fts.h>
42
#include <grp.h>
43
#include <pwd.h>
44
#include <stdio.h>
45
#include <stdlib.h>
46
#include <string.h>
47
#include <time.h>
48
#include <unistd.h>
49
#include <limits.h>
50
#include <util.h>
51
52
#include "ls.h"
53
#include "extern.h"
54
55
static int	printaname(FTSENT *, int, int);
56
static void	printlink(FTSENT *);
57
static void	printsize(int, off_t);
58
static void	printtime(time_t);
59
static int	printtype(mode_t);
60
static int	compute_columns(DISPLAY *, int *);
61
62
#define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
63
64
#define	DATELEN		64
65
66
#define	SECSPERDAY	(24 * 60 * 60)
67
#define	SIXMONTHS	(SECSPERDAY * 365 / 2)
68
69
void
70
printscol(DISPLAY *dp)
71
{
72
	FTSENT *p;
73
74
10232332
	for (p = dp->list; p; p = p->fts_link) {
75
5103335
		if (IS_NOPRINT(p))
76
			continue;
77
5103335
		(void)printaname(p, dp->s_inode, dp->s_block);
78
10206670
		(void)putchar('\n');
79
	}
80
8554
}
81
82
void
83
printlong(DISPLAY *dp)
84
{
85
	struct stat *sp;
86
	FTSENT *p;
87
	NAMES *np;
88
226
	char buf[20];
89
90

155
	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
91
42
		(void)printf("total %llu\n", howmany(dp->btotal, blocksize));
92
93
79410
	for (p = dp->list; p; p = p->fts_link) {
94
39592
		if (IS_NOPRINT(p))
95
			continue;
96
39592
		sp = p->fts_statp;
97
39592
		if (f_inode)
98
			(void)printf("%*llu ", dp->s_inode,
99
			    (unsigned long long)sp->st_ino);
100
39592
		if (f_size)
101
76
			(void)printf("%*lld ", dp->s_block,
102
38
			    howmany((long long)sp->st_blocks, blocksize));
103
39592
		(void)strmode(sp->st_mode, buf);
104
39592
		np = p->fts_pointer;
105
39592
		(void)printf("%s %*u ", buf, dp->s_nlink, sp->st_nlink);
106
39592
		if (!f_grouponly)
107
39592
			(void)printf("%-*s  ", dp->s_user, np->user);
108
39592
		(void)printf("%-*s  ", dp->s_group, np->group);
109
39592
		if (f_flags)
110
			(void)printf("%-*s ", dp->s_flags, np->flags);
111

79184
		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
112
			(void)printf("%3d, %3d ",
113
			    major(sp->st_rdev), minor(sp->st_rdev));
114
39592
		else if (dp->bcfile)
115
			(void)printf("%*s%*lld ",
116
			    8 - dp->s_size, "", dp->s_size,
117
			    (long long)sp->st_size);
118
		else
119
39592
			printsize(dp->s_size, sp->st_size);
120
39592
		if (f_accesstime)
121
			printtime(sp->st_atime);
122
39592
		else if (f_statustime)
123
			printtime(sp->st_ctime);
124
		else
125
39592
			printtime(sp->st_mtime);
126
39592
		(void)mbsprint(p->fts_name, 1);
127

79184
		if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
128
			(void)printtype(sp->st_mode);
129
39592
		if (S_ISLNK(sp->st_mode))
130
1
			printlink(p);
131
79184
		(void)putchar('\n');
132
	}
133
113
}
134
135
static int
136
compute_columns(DISPLAY *dp, int *pnum)
137
{
138
	int colwidth;
139
	extern int termwidth;
140
	int mywidth;
141
142
66
	colwidth = dp->maxlen;
143
33
	if (f_inode)
144
		colwidth += dp->s_inode + 1;
145
33
	if (f_size)
146
		colwidth += dp->s_block + 1;
147
33
	if (f_type || f_typedir)
148
		colwidth += 1;
149
150
33
	colwidth += 1;
151
33
	mywidth = termwidth + 1;	/* no extra space for last column */
152
153
33
	if (mywidth < 2 * colwidth) {
154
8
		printscol(dp);
155
8
		return (0);
156
	}
157
158
25
	*pnum = mywidth / colwidth;
159
25
	return (mywidth / *pnum);		/* spread out if possible */
160
33
}
161
162
void
163
printcol(DISPLAY *dp)
164
{
165
	static FTSENT **array;
166
	static int lastentries = -1;
167
	FTSENT *p;
168
	int base, chcnt, col, colwidth, num;
169
66
	int numcols, numrows, row;
170
171
33
	if ((colwidth = compute_columns(dp, &numcols)) == 0)
172
8
		return;
173
	/*
174
	 * Have to do random access in the linked list -- build a table
175
	 * of pointers.
176
	 */
177
25
	if (dp->entries > lastentries) {
178
		FTSENT **a;
179
180
25
		if ((a = reallocarray(array, dp->entries, sizeof(FTSENT *))) ==
181
		    NULL) {
182
			free(array);
183
			array = NULL;
184
			dp->entries = 0;
185
			lastentries = -1;
186
			warn(NULL);
187
			printscol(dp);
188
			return;
189
		}
190
25
		lastentries = dp->entries;
191
25
		array = a;
192
25
	}
193
1486
	for (p = dp->list, num = 0; p; p = p->fts_link)
194
718
		if (p->fts_number != NO_PRINT)
195
718
			array[num++] = p;
196
197
25
	numrows = num / numcols;
198
25
	if (num % numcols)
199
21
		++numrows;
200
201

50
	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
202
		(void)printf("total %llu\n", howmany(dp->btotal, blocksize));
203
286
	for (row = 0; row < numrows; ++row) {
204
118
		for (base = row, col = 0;;) {
205
718
			chcnt = printaname(array[base], dp->s_inode, dp->s_block);
206
718
			if ((base += numrows) >= num)
207
				break;
208
600
			if (++col == numcols)
209
				break;
210
6114
			while (chcnt++ < colwidth)
211
11028
				putchar(' ');
212
		}
213
236
		(void)putchar('\n');
214
	}
215
58
}
216
217
/*
218
 * print [inode] [size] name
219
 * return # of characters printed, no trailing characters.
220
 */
221
static int
222
printaname(FTSENT *p, int inodefield, int sizefield)
223
{
224
	struct stat *sp;
225
	int chcnt;
226
227
10208106
	sp = p->fts_statp;
228
	chcnt = 0;
229
5104053
	if (f_inode)
230
18
		chcnt += printf("%*llu ", inodefield,
231
18
		    (unsigned long long)sp->st_ino);
232
5104053
	if (f_size)
233
		chcnt += printf("%*lld ", sizefield,
234
		    howmany((long long)sp->st_blocks, blocksize));
235
5104053
	chcnt += mbsprint(p->fts_name, 1);
236

10208106
	if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
237
		chcnt += printtype(sp->st_mode);
238
5104053
	return (chcnt);
239
}
240
241
static void
242
printtime(time_t ftime)
243
{
244
39592
	char f_date[DATELEN];
245
	static time_t now;
246
	static int now_set = 0;
247
248
39592
	if (! now_set) {
249
113
		now = time(NULL);
250
113
		now_set = 1;
251
113
	}
252
253
	/*
254
	 * convert time to string, and print
255
	 */
256

197960
	if (strftime(f_date, sizeof(f_date), f_sectime ? "%b %e %H:%M:%S %Y" :
257
118596
	    (ftime <= now - SIXMONTHS || ftime > now) ? "%b %e  %Y" :
258
79184
	    "%b %e %H:%M", localtime(&ftime)) == 0)
259
		f_date[0] = '\0';
260
261
39592
	printf("%s ", f_date);
262
39592
}
263
264
void
265
printacol(DISPLAY *dp)
266
{
267
	FTSENT *p;
268
	int chcnt, col, colwidth;
269
	int numcols;
270
271
	if ( (colwidth = compute_columns(dp, &numcols)) == 0)
272
		return;
273
274
	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
275
		(void)printf("total %llu\n", howmany(dp->btotal, blocksize));
276
	col = 0;
277
	for (p = dp->list; p; p = p->fts_link) {
278
		if (IS_NOPRINT(p))
279
			continue;
280
		if (col >= numcols) {
281
			col = 0;
282
			(void)putchar('\n');
283
		}
284
		chcnt = printaname(p, dp->s_inode, dp->s_block);
285
		col++;
286
		if (col < numcols)
287
			while (chcnt++ < colwidth)
288
				(void)putchar(' ');
289
	}
290
	(void)putchar('\n');
291
}
292
293
void
294
printstream(DISPLAY *dp)
295
{
296
	extern int termwidth;
297
	FTSENT *p;
298
	int col;
299
	int extwidth;
300
301
	extwidth = 0;
302
	if (f_inode)
303
		extwidth += dp->s_inode + 1;
304
	if (f_size)
305
		extwidth += dp->s_block + 1;
306
	if (f_type)
307
		extwidth += 1;
308
309
	for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
310
		if (IS_NOPRINT(p))
311
			continue;
312
		if (col > 0) {
313
			(void)putchar(','), col++;
314
			if (col + 1 + extwidth + mbsprint(p->fts_name, 0) >=
315
			    termwidth)
316
				(void)putchar('\n'), col = 0;
317
			else
318
				(void)putchar(' '), col++;
319
		}
320
		col += printaname(p, dp->s_inode, dp->s_block);
321
	}
322
	(void)putchar('\n');
323
}
324
325
static int
326
printtype(mode_t mode)
327
{
328
	switch (mode & S_IFMT) {
329
	case S_IFDIR:
330
		(void)putchar('/');
331
		return (1);
332
	case S_IFIFO:
333
		(void)putchar('|');
334
		return (1);
335
	case S_IFLNK:
336
		(void)putchar('@');
337
		return (1);
338
	case S_IFSOCK:
339
		(void)putchar('=');
340
		return (1);
341
	}
342
	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
343
		(void)putchar('*');
344
		return (1);
345
	}
346
	return (0);
347
}
348
349
static void
350
printlink(FTSENT *p)
351
{
352
	int lnklen;
353
2
	char name[PATH_MAX], path[PATH_MAX];
354
355
1
	if (p->fts_level == FTS_ROOTLEVEL)
356
		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
357
	else
358
1
		(void)snprintf(name, sizeof(name),
359
1
		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
360
1
	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
361
		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
362
		return;
363
	}
364
1
	path[lnklen] = '\0';
365
1
	(void)printf(" -> ");
366
1
	(void)mbsprint(path, 1);
367
2
}
368
369
static void
370
printsize(int width, off_t bytes)
371
{
372
79184
	char ret[FMT_SCALED_STRSIZE];
373
374

39592
	if ((f_humanval) && (fmt_scaled(bytes, ret) != -1)) {
375
		(void)printf("%*s ", width, ret);
376
		return;
377
	}
378
39592
	(void)printf("%*lld ", width, (long long)bytes);
379
79184
}