print.c revision 13120
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.6 1995/08/08 00:03:04 ache Exp $ 37 */ 38 39#ifndef lint 40static 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 61static int printaname __P((FTSENT *, u_long, u_long)); 62static void printlink __P((FTSENT *)); 63static void printtime __P((time_t)); 64static int printtype __P((u_int)); 65 66#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 67 68void 69printscol(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 82void 83printlong(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 if (minor(sp->st_rdev) > 255) 112 (void)printf("%3d, 0x%08x ", 113 major(sp->st_rdev), minor(sp->st_rdev)); 114 else 115 (void)printf("%3d, %3d ", 116 major(sp->st_rdev), minor(sp->st_rdev)); 117 else if (dp->bcfile) 118 (void)printf("%*s%*qd ", 119 8 - dp->s_size, "", dp->s_size, sp->st_size); 120 else 121 (void)printf("%*qd ", dp->s_size, sp->st_size); 122 if (f_accesstime) 123 printtime(sp->st_atime); 124 else if (f_statustime) 125 printtime(sp->st_ctime); 126 else 127 printtime(sp->st_mtime); 128 (void)printf("%s", p->fts_name); 129 if (f_type) 130 (void)printtype(sp->st_mode); 131 if (S_ISLNK(sp->st_mode)) 132 printlink(p); 133 (void)putchar('\n'); 134 } 135} 136 137#define TAB 8 138 139void 140printcol(dp) 141 DISPLAY *dp; 142{ 143 extern int termwidth; 144 static FTSENT **array; 145 static int lastentries = -1; 146 FTSENT *p; 147 int base, chcnt, cnt, col, colwidth, num; 148 int endcol, numcols, numrows, row; 149 150 /* 151 * Have to do random access in the linked list -- build a table 152 * of pointers. 153 */ 154 if (dp->entries > lastentries) { 155 lastentries = dp->entries; 156 if ((array = 157 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { 158 warn(NULL); 159 printscol(dp); 160 } 161 } 162 for (p = dp->list, num = 0; p; p = p->fts_link) 163 if (p->fts_number != NO_PRINT) 164 array[num++] = p; 165 166 colwidth = dp->maxlen; 167 if (f_inode) 168 colwidth += dp->s_inode + 1; 169 if (f_size) 170 colwidth += dp->s_block + 1; 171 if (f_type) 172 colwidth += 1; 173 174 colwidth = (colwidth + TAB) & ~(TAB - 1); 175 if (termwidth < 2 * colwidth) { 176 printscol(dp); 177 return; 178 } 179 180 numcols = termwidth / colwidth; 181 numrows = num / numcols; 182 if (num % numcols) 183 ++numrows; 184 185 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 186 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 187 for (row = 0; row < numrows; ++row) { 188 endcol = colwidth; 189 for (base = row, chcnt = col = 0; col < numcols; ++col) { 190 chcnt += printaname(array[base], dp->s_inode, 191 dp->s_block); 192 if ((base += numrows) >= num) 193 break; 194 while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol){ 195 (void)putchar('\t'); 196 chcnt = cnt; 197 } 198 endcol += colwidth; 199 } 200 (void)putchar('\n'); 201 } 202} 203 204/* 205 * print [inode] [size] name 206 * return # of characters printed, no trailing characters. 207 */ 208static int 209printaname(p, inodefield, sizefield) 210 FTSENT *p; 211 u_long sizefield, inodefield; 212{ 213 struct stat *sp; 214 int chcnt; 215 216 sp = p->fts_statp; 217 chcnt = 0; 218 if (f_inode) 219 chcnt += printf("%*lu ", (int)inodefield, sp->st_ino); 220 if (f_size) 221 chcnt += printf("%*qd ", 222 (int)sizefield, howmany(sp->st_blocks, blocksize)); 223 chcnt += printf("%s", p->fts_name); 224 if (f_type) 225 chcnt += printtype(sp->st_mode); 226 return (chcnt); 227} 228 229static void 230printtime(ftime) 231 time_t ftime; 232{ 233 int i; 234 char longstring[80]; 235 236 strftime(longstring, sizeof(longstring), "%c", localtime(&ftime)); 237 for (i = 4; i < 11; ++i) 238 (void)putchar(longstring[i]); 239 240#define SIXMONTHS ((365 / 2) * 86400) 241 if (f_sectime) 242 for (i = 11; i < 24; i++) 243 (void)putchar(longstring[i]); 244 else if (ftime + SIXMONTHS > time(NULL)) 245 for (i = 11; i < 16; ++i) 246 (void)putchar(longstring[i]); 247 else { 248 (void)putchar(' '); 249 for (i = 20; i < 24; ++i) 250 (void)putchar(longstring[i]); 251 } 252 (void)putchar(' '); 253} 254 255static int 256printtype(mode) 257 u_int mode; 258{ 259 switch (mode & S_IFMT) { 260 case S_IFDIR: 261 (void)putchar('/'); 262 return (1); 263 case S_IFIFO: 264 (void)putchar('|'); 265 return (1); 266 case S_IFLNK: 267 (void)putchar('@'); 268 return (1); 269 case S_IFSOCK: 270 (void)putchar('='); 271 return (1); 272 } 273 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 274 (void)putchar('*'); 275 return (1); 276 } 277 return (0); 278} 279 280static void 281printlink(p) 282 FTSENT *p; 283{ 284 int lnklen; 285 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; 286 287 if (p->fts_level == FTS_ROOTLEVEL) 288 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 289 else 290 (void)snprintf(name, sizeof(name), 291 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 292 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 293 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 294 return; 295 } 296 path[lnklen] = '\0'; 297 (void)printf(" -> %s", path); 298} 299