1 1.6 christos /* $NetBSD: tls.c,v 1.6 2011/05/20 02:12:39 christos Exp $ */ 2 1.1 gwr 3 1.1 gwr /* 4 1.1 gwr * Copyright (c) 1995 Gordon W. Ross 5 1.1 gwr * All rights reserved. 6 1.1 gwr * 7 1.1 gwr * Redistribution and use in source and binary forms, with or without 8 1.1 gwr * modification, are permitted provided that the following conditions 9 1.1 gwr * are met: 10 1.1 gwr * 1. Redistributions of source code must retain the above copyright 11 1.1 gwr * notice, this list of conditions and the following disclaimer. 12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 gwr * notice, this list of conditions and the following disclaimer in the 14 1.1 gwr * documentation and/or other materials provided with the distribution. 15 1.1 gwr * 16 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 gwr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 gwr */ 27 1.1 gwr 28 1.1 gwr #include <sys/types.h> 29 1.6 christos #include <sys/param.h> 30 1.1 gwr #include <sys/stat.h> 31 1.1 gwr 32 1.1 gwr #include <dirent.h> 33 1.5 tsutsui #include <err.h> 34 1.1 gwr #include <stdio.h> 35 1.2 mrg #include <stdlib.h> 36 1.6 christos #include <unistd.h> 37 1.1 gwr #include <time.h> 38 1.5 tsutsui #include <unistd.h> 39 1.1 gwr 40 1.6 christos static int iflag; 41 1.1 gwr 42 1.6 christos static void show_long(const char *); 43 1.1 gwr 44 1.5 tsutsui int 45 1.5 tsutsui main(int argc, char *argv[]) 46 1.1 gwr { 47 1.1 gwr DIR *dfp; 48 1.1 gwr struct dirent *d; 49 1.1 gwr 50 1.1 gwr /* If given an arg, just cd there first. */ 51 1.1 gwr if (argc > 1) { 52 1.6 christos if (chdir(argv[1])) 53 1.6 christos err(1, "chdir `%s'", argv[1]); 54 1.1 gwr } 55 1.1 gwr if (argc > 2) 56 1.1 gwr fprintf(stderr, "extra args ignored\n"); 57 1.1 gwr 58 1.1 gwr dfp = opendir("."); 59 1.6 christos if (dfp == NULL) 60 1.5 tsutsui err(EXIT_FAILURE, "opendir"); 61 1.1 gwr 62 1.1 gwr while ((d = readdir(dfp)) != NULL) 63 1.1 gwr show_long(d->d_name); 64 1.1 gwr 65 1.1 gwr closedir(dfp); 66 1.6 christos return EXIT_SUCCESS; 67 1.1 gwr } 68 1.1 gwr 69 1.1 gwr /* XXX - This is system dependent... */ 70 1.6 christos static const char ifmt_name[16] = { 71 1.1 gwr '?', /* 0: nothing */ 72 1.1 gwr 'P', /* 1: fifo (pipe) */ 73 1.1 gwr 'C', /* 2: chr device */ 74 1.1 gwr '?', /* 3: ? */ 75 1.1 gwr 'D', /* 4: dir */ 76 1.1 gwr '?', /* 5: ? */ 77 1.1 gwr 'B', /* 6: blk device */ 78 1.1 gwr '?', /* 7: ? */ 79 1.1 gwr 'F', /* 8: file */ 80 1.1 gwr '?', /* 9: ? */ 81 1.1 gwr 'L', /* A: link */ 82 1.1 gwr '?', /* B: ? */ 83 1.1 gwr 'S', /* C: socket */ 84 1.1 gwr '?', /* D: ? */ 85 1.1 gwr 'W', /* E: whiteout */ 86 1.1 gwr '?' /* F: ? */ 87 1.1 gwr }; 88 1.1 gwr 89 1.6 christos static void 90 1.6 christos show_long(const char *fname) 91 1.1 gwr { 92 1.1 gwr struct stat st; 93 1.1 gwr int ifmt; 94 1.1 gwr char ifmt_c; 95 1.1 gwr char *date; 96 1.1 gwr 97 1.1 gwr if (lstat(fname, &st)) { 98 1.6 christos warn("lstat `%s'", fname); 99 1.1 gwr return; 100 1.1 gwr } 101 1.1 gwr ifmt = (st.st_mode >> 12) & 15; 102 1.1 gwr ifmt_c = ifmt_name[ifmt]; 103 1.1 gwr 104 1.1 gwr if (iflag) { 105 1.1 gwr /* inode number */ 106 1.5 tsutsui printf("%6d ", (int)st.st_ino); /* assume small fs */ 107 1.1 gwr } 108 1.1 gwr 109 1.1 gwr /* fmt/mode */ 110 1.1 gwr printf("%c:", ifmt_c); 111 1.1 gwr printf("%04o ", st.st_mode & 07777); 112 1.1 gwr 113 1.1 gwr /* nlinks, uid, gid */ 114 1.1 gwr printf("%2d ", st.st_nlink); 115 1.1 gwr printf("%4d ", st.st_uid); 116 1.1 gwr printf("%4d ", st.st_gid); 117 1.1 gwr 118 1.1 gwr /* size or major/minor */ 119 1.1 gwr if ((ifmt_c == 'B') || (ifmt_c == 'C')) { 120 1.1 gwr printf("%2d, ", major(st.st_rdev)); 121 1.1 gwr printf("%3d ", minor(st.st_rdev)); 122 1.1 gwr } else { 123 1.1 gwr printf("%7d ", (int) st.st_size); 124 1.1 gwr } 125 1.1 gwr 126 1.1 gwr /* date */ 127 1.4 christos if ((date = ctime(&st.st_mtime)) == NULL) 128 1.4 christos printf("? "); 129 1.4 christos else { 130 1.4 christos date += 4; /* skip day-of-week */ 131 1.4 christos date[12] = '\0'; /* to the minute */ 132 1.4 christos printf("%s ", date); 133 1.4 christos } 134 1.1 gwr 135 1.1 gwr /* name */ 136 1.1 gwr printf("%s", fname); 137 1.1 gwr 138 1.1 gwr if (ifmt_c == 'L') { 139 1.6 christos char linkto[MAXPATHLEN]; 140 1.1 gwr int n; 141 1.1 gwr 142 1.1 gwr n = readlink(fname, linkto, sizeof(linkto)-1); 143 1.1 gwr if (n < 0) { 144 1.6 christos warn("readlink `%s'", fname); 145 1.1 gwr return; 146 1.1 gwr } 147 1.1 gwr linkto[n] = '\0'; 148 1.1 gwr printf(" -> %s", linkto); 149 1.1 gwr } 150 1.1 gwr printf("\n"); 151 1.1 gwr } 152