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