print.c revision 1.1.1.2 1 1.1 cgd /*
2 1.1.1.2 mycroft * Copyright (c) 1989, 1993, 1994
3 1.1.1.2 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Michael Fischbein.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1.1.2 mycroft static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include <sys/param.h>
42 1.1 cgd #include <sys/stat.h>
43 1.1.1.2 mycroft
44 1.1.1.2 mycroft #include <err.h>
45 1.1.1.2 mycroft #include <errno.h>
46 1.1.1.2 mycroft #include <fts.h>
47 1.1 cgd #include <grp.h>
48 1.1 cgd #include <pwd.h>
49 1.1.1.2 mycroft #include <stdio.h>
50 1.1.1.2 mycroft #include <stdlib.h>
51 1.1.1.2 mycroft #include <string.h>
52 1.1.1.2 mycroft #include <time.h>
53 1.1 cgd #include <tzfile.h>
54 1.1.1.2 mycroft #include <unistd.h>
55 1.1.1.2 mycroft #include <utmp.h>
56 1.1.1.2 mycroft
57 1.1 cgd #include "ls.h"
58 1.1.1.2 mycroft #include "extern.h"
59 1.1 cgd
60 1.1.1.2 mycroft static int printaname __P((FTSENT *, u_long, u_long));
61 1.1.1.2 mycroft static void printlink __P((FTSENT *));
62 1.1.1.2 mycroft static void printtime __P((time_t));
63 1.1.1.2 mycroft static int printtype __P((u_int));
64 1.1.1.2 mycroft
65 1.1.1.2 mycroft #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
66 1.1.1.2 mycroft
67 1.1.1.2 mycroft void
68 1.1.1.2 mycroft printscol(dp)
69 1.1.1.2 mycroft DISPLAY *dp;
70 1.1 cgd {
71 1.1.1.2 mycroft FTSENT *p;
72 1.1.1.2 mycroft
73 1.1.1.2 mycroft for (p = dp->list; p; p = p->fts_link) {
74 1.1.1.2 mycroft if (IS_NOPRINT(p))
75 1.1.1.2 mycroft continue;
76 1.1.1.2 mycroft (void)printaname(p, dp->s_inode, dp->s_block);
77 1.1 cgd (void)putchar('\n');
78 1.1 cgd }
79 1.1 cgd }
80 1.1 cgd
81 1.1.1.2 mycroft void
82 1.1.1.2 mycroft printlong(dp)
83 1.1.1.2 mycroft DISPLAY *dp;
84 1.1.1.2 mycroft {
85 1.1.1.2 mycroft struct stat *sp;
86 1.1.1.2 mycroft FTSENT *p;
87 1.1.1.2 mycroft NAMES *np;
88 1.1.1.2 mycroft char buf[20];
89 1.1.1.2 mycroft
90 1.1.1.2 mycroft if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
91 1.1.1.2 mycroft (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
92 1.1.1.2 mycroft
93 1.1.1.2 mycroft for (p = dp->list; p; p = p->fts_link) {
94 1.1.1.2 mycroft if (IS_NOPRINT(p))
95 1.1.1.2 mycroft continue;
96 1.1.1.2 mycroft sp = p->fts_statp;
97 1.1 cgd if (f_inode)
98 1.1.1.2 mycroft (void)printf("%*lu ", dp->s_inode, sp->st_ino);
99 1.1 cgd if (f_size)
100 1.1.1.2 mycroft (void)printf("%*qd ",
101 1.1.1.2 mycroft dp->s_block, howmany(sp->st_blocks, blocksize));
102 1.1.1.2 mycroft (void)strmode(sp->st_mode, buf);
103 1.1.1.2 mycroft np = p->fts_pointer;
104 1.1.1.2 mycroft (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
105 1.1.1.2 mycroft sp->st_nlink, dp->s_user, np->user, dp->s_group,
106 1.1.1.2 mycroft np->group);
107 1.1.1.2 mycroft if (f_flags)
108 1.1.1.2 mycroft (void)printf("%-*s ", dp->s_flags, np->flags);
109 1.1.1.2 mycroft if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
110 1.1.1.2 mycroft (void)printf("%3d, %3d ",
111 1.1.1.2 mycroft major(sp->st_rdev), minor(sp->st_rdev));
112 1.1.1.2 mycroft else if (dp->bcfile)
113 1.1.1.2 mycroft (void)printf("%*s%*qd ",
114 1.1.1.2 mycroft 8 - dp->s_size, "", dp->s_size, sp->st_size);
115 1.1 cgd else
116 1.1.1.2 mycroft (void)printf("%*qd ", dp->s_size, sp->st_size);
117 1.1 cgd if (f_accesstime)
118 1.1.1.2 mycroft printtime(sp->st_atime);
119 1.1 cgd else if (f_statustime)
120 1.1.1.2 mycroft printtime(sp->st_ctime);
121 1.1 cgd else
122 1.1.1.2 mycroft printtime(sp->st_mtime);
123 1.1.1.2 mycroft (void)printf("%s", p->fts_name);
124 1.1 cgd if (f_type)
125 1.1.1.2 mycroft (void)printtype(sp->st_mode);
126 1.1.1.2 mycroft if (S_ISLNK(sp->st_mode))
127 1.1.1.2 mycroft printlink(p);
128 1.1 cgd (void)putchar('\n');
129 1.1 cgd }
130 1.1 cgd }
131 1.1 cgd
132 1.1 cgd #define TAB 8
133 1.1 cgd
134 1.1.1.2 mycroft void
135 1.1.1.2 mycroft printcol(dp)
136 1.1.1.2 mycroft DISPLAY *dp;
137 1.1 cgd {
138 1.1 cgd extern int termwidth;
139 1.1.1.2 mycroft static FTSENT **array;
140 1.1.1.2 mycroft static int lastentries = -1;
141 1.1.1.2 mycroft FTSENT *p;
142 1.1.1.2 mycroft int base, chcnt, cnt, col, colwidth, num;
143 1.1 cgd int endcol, numcols, numrows, row;
144 1.1 cgd
145 1.1.1.2 mycroft /*
146 1.1.1.2 mycroft * Have to do random access in the linked list -- build a table
147 1.1.1.2 mycroft * of pointers.
148 1.1.1.2 mycroft */
149 1.1.1.2 mycroft if (dp->entries > lastentries) {
150 1.1.1.2 mycroft lastentries = dp->entries;
151 1.1.1.2 mycroft if ((array =
152 1.1.1.2 mycroft realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
153 1.1.1.2 mycroft warn(NULL);
154 1.1.1.2 mycroft printscol(dp);
155 1.1.1.2 mycroft }
156 1.1.1.2 mycroft }
157 1.1.1.2 mycroft for (p = dp->list, num = 0; p; p = p->fts_link)
158 1.1.1.2 mycroft if (p->fts_number != NO_PRINT)
159 1.1.1.2 mycroft array[num++] = p;
160 1.1.1.2 mycroft
161 1.1.1.2 mycroft colwidth = dp->maxlen;
162 1.1 cgd if (f_inode)
163 1.1.1.2 mycroft colwidth += dp->s_inode + 1;
164 1.1 cgd if (f_size)
165 1.1.1.2 mycroft colwidth += dp->s_block + 1;
166 1.1 cgd if (f_type)
167 1.1 cgd colwidth += 1;
168 1.1 cgd
169 1.1 cgd colwidth = (colwidth + TAB) & ~(TAB - 1);
170 1.1 cgd if (termwidth < 2 * colwidth) {
171 1.1.1.2 mycroft printscol(dp);
172 1.1 cgd return;
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd numcols = termwidth / colwidth;
176 1.1 cgd numrows = num / numcols;
177 1.1 cgd if (num % numcols)
178 1.1 cgd ++numrows;
179 1.1 cgd
180 1.1.1.2 mycroft if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
181 1.1.1.2 mycroft (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
182 1.1 cgd for (row = 0; row < numrows; ++row) {
183 1.1 cgd endcol = colwidth;
184 1.1 cgd for (base = row, chcnt = col = 0; col < numcols; ++col) {
185 1.1.1.2 mycroft chcnt += printaname(array[base], dp->s_inode,
186 1.1.1.2 mycroft dp->s_block);
187 1.1 cgd if ((base += numrows) >= num)
188 1.1 cgd break;
189 1.1 cgd while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
190 1.1 cgd (void)putchar('\t');
191 1.1 cgd chcnt = cnt;
192 1.1 cgd }
193 1.1 cgd endcol += colwidth;
194 1.1 cgd }
195 1.1.1.2 mycroft (void)putchar('\n');
196 1.1 cgd }
197 1.1 cgd }
198 1.1 cgd
199 1.1 cgd /*
200 1.1 cgd * print [inode] [size] name
201 1.1.1.2 mycroft * return # of characters printed, no trailing characters.
202 1.1 cgd */
203 1.1.1.2 mycroft static int
204 1.1.1.2 mycroft printaname(p, inodefield, sizefield)
205 1.1.1.2 mycroft FTSENT *p;
206 1.1.1.2 mycroft u_long sizefield, inodefield;
207 1.1 cgd {
208 1.1.1.2 mycroft struct stat *sp;
209 1.1 cgd int chcnt;
210 1.1 cgd
211 1.1.1.2 mycroft sp = p->fts_statp;
212 1.1 cgd chcnt = 0;
213 1.1 cgd if (f_inode)
214 1.1.1.2 mycroft chcnt += printf("%*lu ", (int)inodefield, sp->st_ino);
215 1.1 cgd if (f_size)
216 1.1.1.2 mycroft chcnt += printf("%*qd ",
217 1.1.1.2 mycroft (int)sizefield, howmany(sp->st_blocks, blocksize));
218 1.1.1.2 mycroft chcnt += printf("%s", p->fts_name);
219 1.1 cgd if (f_type)
220 1.1.1.2 mycroft chcnt += printtype(sp->st_mode);
221 1.1.1.2 mycroft return (chcnt);
222 1.1 cgd }
223 1.1 cgd
224 1.1.1.2 mycroft static void
225 1.1 cgd printtime(ftime)
226 1.1 cgd time_t ftime;
227 1.1 cgd {
228 1.1 cgd int i;
229 1.1.1.2 mycroft char *longstring;
230 1.1 cgd
231 1.1.1.2 mycroft longstring = ctime(&ftime);
232 1.1 cgd for (i = 4; i < 11; ++i)
233 1.1 cgd (void)putchar(longstring[i]);
234 1.1 cgd
235 1.1 cgd #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
236 1.1 cgd if (f_sectime)
237 1.1 cgd for (i = 11; i < 24; i++)
238 1.1 cgd (void)putchar(longstring[i]);
239 1.1.1.2 mycroft else if (ftime + SIXMONTHS > time(NULL))
240 1.1 cgd for (i = 11; i < 16; ++i)
241 1.1 cgd (void)putchar(longstring[i]);
242 1.1 cgd else {
243 1.1 cgd (void)putchar(' ');
244 1.1 cgd for (i = 20; i < 24; ++i)
245 1.1 cgd (void)putchar(longstring[i]);
246 1.1 cgd }
247 1.1 cgd (void)putchar(' ');
248 1.1 cgd }
249 1.1 cgd
250 1.1.1.2 mycroft static int
251 1.1 cgd printtype(mode)
252 1.1.1.2 mycroft u_int mode;
253 1.1 cgd {
254 1.1.1.2 mycroft switch (mode & S_IFMT) {
255 1.1 cgd case S_IFDIR:
256 1.1 cgd (void)putchar('/');
257 1.1.1.2 mycroft return (1);
258 1.1.1.2 mycroft case S_IFIFO:
259 1.1.1.2 mycroft (void)putchar('|');
260 1.1.1.2 mycroft return (1);
261 1.1 cgd case S_IFLNK:
262 1.1 cgd (void)putchar('@');
263 1.1.1.2 mycroft return (1);
264 1.1 cgd case S_IFSOCK:
265 1.1 cgd (void)putchar('=');
266 1.1.1.2 mycroft return (1);
267 1.1 cgd }
268 1.1 cgd if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
269 1.1 cgd (void)putchar('*');
270 1.1.1.2 mycroft return (1);
271 1.1 cgd }
272 1.1.1.2 mycroft return (0);
273 1.1 cgd }
274 1.1 cgd
275 1.1.1.2 mycroft static void
276 1.1.1.2 mycroft printlink(p)
277 1.1.1.2 mycroft FTSENT *p;
278 1.1 cgd {
279 1.1 cgd int lnklen;
280 1.1.1.2 mycroft char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
281 1.1 cgd
282 1.1.1.2 mycroft if (p->fts_level == FTS_ROOTLEVEL)
283 1.1.1.2 mycroft (void)snprintf(name, sizeof(name), "%s", p->fts_name);
284 1.1.1.2 mycroft else
285 1.1.1.2 mycroft (void)snprintf(name, sizeof(name),
286 1.1.1.2 mycroft "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
287 1.1.1.2 mycroft if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
288 1.1 cgd (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
289 1.1 cgd return;
290 1.1 cgd }
291 1.1 cgd path[lnklen] = '\0';
292 1.1 cgd (void)printf(" -> %s", path);
293 1.1 cgd }
294