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