print.c revision 1.4 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * 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.4 mycroft /*static char sccsid[] = "from: @(#)print.c 5.24 (Berkeley) 10/19/90";*/
39 1.4 mycroft static char rcsid[] = "$Id: print.c,v 1.4 1993/08/01 18:59:29 mycroft Exp $";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #include <sys/param.h>
43 1.1 cgd #include <sys/stat.h>
44 1.1 cgd #include <stdio.h>
45 1.1 cgd #include <grp.h>
46 1.1 cgd #include <pwd.h>
47 1.1 cgd #include <utmp.h>
48 1.1 cgd #include <tzfile.h>
49 1.1 cgd #include "ls.h"
50 1.1 cgd
51 1.1 cgd printscol(stats, num)
52 1.1 cgd register LS *stats;
53 1.1 cgd register int num;
54 1.1 cgd {
55 1.1 cgd for (; num--; ++stats) {
56 1.1 cgd (void)printaname(stats);
57 1.1 cgd (void)putchar('\n');
58 1.1 cgd }
59 1.1 cgd }
60 1.1 cgd
61 1.1 cgd printlong(stats, num)
62 1.1 cgd LS *stats;
63 1.1 cgd register int num;
64 1.1 cgd {
65 1.1 cgd extern int errno;
66 1.1 cgd char modep[15], *user_from_uid(), *group_from_gid(), *strerror();
67 1.1 cgd
68 1.1 cgd if (f_total)
69 1.1 cgd (void)printf("total %lu\n", f_kblocks ?
70 1.1 cgd howmany(stats[0].lstat.st_btotal, 2) :
71 1.1 cgd stats[0].lstat.st_btotal);
72 1.1 cgd for (; num--; ++stats) {
73 1.1 cgd if (f_inode)
74 1.1 cgd (void)printf("%6lu ", stats->lstat.st_ino);
75 1.1 cgd if (f_size)
76 1.1 cgd (void)printf("%4ld ", f_kblocks ?
77 1.1 cgd howmany(stats->lstat.st_blocks, 2) :
78 1.1 cgd stats->lstat.st_blocks);
79 1.1 cgd (void)strmode(stats->lstat.st_mode, modep);
80 1.1 cgd (void)printf("%s %3u %-*s ", modep, stats->lstat.st_nlink,
81 1.1 cgd UT_NAMESIZE, user_from_uid(stats->lstat.st_uid, 0));
82 1.1 cgd if (f_group)
83 1.1 cgd (void)printf("%-*s ", UT_NAMESIZE,
84 1.1 cgd group_from_gid(stats->lstat.st_gid, 0));
85 1.1 cgd if (S_ISCHR(stats->lstat.st_mode) ||
86 1.1 cgd S_ISBLK(stats->lstat.st_mode))
87 1.1 cgd (void)printf("%3d, %3d ", major(stats->lstat.st_rdev),
88 1.1 cgd minor(stats->lstat.st_rdev));
89 1.1 cgd else
90 1.1 cgd (void)printf("%8ld ", stats->lstat.st_size);
91 1.1 cgd if (f_accesstime)
92 1.1 cgd printtime(stats->lstat.st_atime);
93 1.1 cgd else if (f_statustime)
94 1.1 cgd printtime(stats->lstat.st_ctime);
95 1.1 cgd else
96 1.1 cgd printtime(stats->lstat.st_mtime);
97 1.1 cgd (void)printf("%s", stats->name);
98 1.1 cgd if (f_type)
99 1.1 cgd (void)printtype(stats->lstat.st_mode);
100 1.1 cgd if (S_ISLNK(stats->lstat.st_mode))
101 1.1 cgd printlink(stats->name);
102 1.1 cgd (void)putchar('\n');
103 1.1 cgd }
104 1.1 cgd }
105 1.1 cgd
106 1.1 cgd #define TAB 8
107 1.1 cgd
108 1.1 cgd printcol(stats, num)
109 1.1 cgd LS *stats;
110 1.1 cgd int num;
111 1.1 cgd {
112 1.1 cgd extern int termwidth;
113 1.1 cgd register int base, chcnt, cnt, col, colwidth;
114 1.1 cgd int endcol, numcols, numrows, row;
115 1.1 cgd
116 1.1 cgd colwidth = stats[0].lstat.st_maxlen;
117 1.1 cgd if (f_inode)
118 1.1 cgd colwidth += 6;
119 1.1 cgd if (f_size)
120 1.1 cgd colwidth += 5;
121 1.1 cgd if (f_type)
122 1.1 cgd colwidth += 1;
123 1.1 cgd
124 1.1 cgd colwidth = (colwidth + TAB) & ~(TAB - 1);
125 1.1 cgd if (termwidth < 2 * colwidth) {
126 1.1 cgd printscol(stats, num);
127 1.1 cgd return;
128 1.1 cgd }
129 1.1 cgd
130 1.1 cgd numcols = termwidth / colwidth;
131 1.1 cgd numrows = num / numcols;
132 1.1 cgd if (num % numcols)
133 1.1 cgd ++numrows;
134 1.1 cgd
135 1.1 cgd if (f_size && f_total)
136 1.1 cgd (void)printf("total %lu\n", f_kblocks ?
137 1.1 cgd howmany(stats[0].lstat.st_btotal, 2) :
138 1.1 cgd stats[0].lstat.st_btotal);
139 1.1 cgd for (row = 0; row < numrows; ++row) {
140 1.1 cgd endcol = colwidth;
141 1.1 cgd for (base = row, chcnt = col = 0; col < numcols; ++col) {
142 1.1 cgd chcnt += printaname(stats + base);
143 1.1 cgd if ((base += numrows) >= num)
144 1.1 cgd break;
145 1.1 cgd while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
146 1.1 cgd (void)putchar('\t');
147 1.1 cgd chcnt = cnt;
148 1.1 cgd }
149 1.1 cgd endcol += colwidth;
150 1.1 cgd }
151 1.1 cgd putchar('\n');
152 1.1 cgd }
153 1.1 cgd }
154 1.1 cgd
155 1.1 cgd /*
156 1.1 cgd * print [inode] [size] name
157 1.1 cgd * return # of characters printed, no trailing characters
158 1.1 cgd */
159 1.1 cgd printaname(lp)
160 1.1 cgd LS *lp;
161 1.1 cgd {
162 1.1 cgd int chcnt;
163 1.1 cgd
164 1.1 cgd chcnt = 0;
165 1.1 cgd if (f_inode)
166 1.1 cgd chcnt += printf("%5lu ", lp->lstat.st_ino);
167 1.1 cgd if (f_size)
168 1.1 cgd chcnt += printf("%4ld ", f_kblocks ?
169 1.1 cgd howmany(lp->lstat.st_blocks, 2) : lp->lstat.st_blocks);
170 1.1 cgd chcnt += printf("%s", lp->name);
171 1.1 cgd if (f_type)
172 1.1 cgd chcnt += printtype(lp->lstat.st_mode);
173 1.1 cgd return(chcnt);
174 1.1 cgd }
175 1.1 cgd
176 1.1 cgd printtime(ftime)
177 1.1 cgd time_t ftime;
178 1.1 cgd {
179 1.1 cgd int i;
180 1.1 cgd char *longstring, *ctime();
181 1.1 cgd time_t time();
182 1.1 cgd
183 1.1 cgd longstring = ctime((long *)&ftime);
184 1.1 cgd for (i = 4; i < 11; ++i)
185 1.1 cgd (void)putchar(longstring[i]);
186 1.1 cgd
187 1.1 cgd #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
188 1.1 cgd if (f_sectime)
189 1.1 cgd for (i = 11; i < 24; i++)
190 1.1 cgd (void)putchar(longstring[i]);
191 1.1 cgd else if (ftime + SIXMONTHS > time((time_t *)NULL))
192 1.1 cgd for (i = 11; i < 16; ++i)
193 1.1 cgd (void)putchar(longstring[i]);
194 1.1 cgd else {
195 1.1 cgd (void)putchar(' ');
196 1.1 cgd for (i = 20; i < 24; ++i)
197 1.1 cgd (void)putchar(longstring[i]);
198 1.1 cgd }
199 1.1 cgd (void)putchar(' ');
200 1.1 cgd }
201 1.1 cgd
202 1.1 cgd printtype(mode)
203 1.1 cgd mode_t mode;
204 1.1 cgd {
205 1.1 cgd switch(mode & S_IFMT) {
206 1.1 cgd case S_IFDIR:
207 1.1 cgd (void)putchar('/');
208 1.1 cgd return(1);
209 1.1 cgd case S_IFLNK:
210 1.1 cgd (void)putchar('@');
211 1.1 cgd return(1);
212 1.1 cgd case S_IFSOCK:
213 1.1 cgd (void)putchar('=');
214 1.1 cgd return(1);
215 1.1 cgd }
216 1.1 cgd if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
217 1.1 cgd (void)putchar('*');
218 1.1 cgd return(1);
219 1.1 cgd }
220 1.1 cgd return(0);
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd printlink(name)
224 1.1 cgd char *name;
225 1.1 cgd {
226 1.1 cgd int lnklen;
227 1.1 cgd char path[MAXPATHLEN + 1], *strerror();
228 1.1 cgd
229 1.1 cgd if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
230 1.1 cgd (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
231 1.1 cgd return;
232 1.1 cgd }
233 1.1 cgd path[lnklen] = '\0';
234 1.1 cgd (void)printf(" -> %s", path);
235 1.1 cgd }
236