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