Home | History | Annotate | Line # | Download | only in tls
tls.c revision 1.4
      1  1.4  christos /*	$NetBSD: tls.c,v 1.4 2010/04/02 15:34:16 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.1       gwr #include <sys/stat.h>
     30  1.1       gwr 
     31  1.1       gwr #include <dirent.h>
     32  1.1       gwr #include <stdio.h>
     33  1.2       mrg #include <stdlib.h>
     34  1.1       gwr #include <time.h>
     35  1.1       gwr 
     36  1.1       gwr int iflag;
     37  1.1       gwr 
     38  1.1       gwr void show_long(char *fname);
     39  1.1       gwr 
     40  1.1       gwr main(argc, argv)
     41  1.1       gwr 	int argc;
     42  1.1       gwr 	char **argv;
     43  1.1       gwr {
     44  1.1       gwr 	DIR *dfp;
     45  1.1       gwr 	struct dirent *d;
     46  1.1       gwr 
     47  1.1       gwr 	/* If given an arg, just cd there first. */
     48  1.1       gwr 	if (argc > 1) {
     49  1.1       gwr 		if (chdir(argv[1])) {
     50  1.1       gwr 			perror(argv[1]);
     51  1.1       gwr 			exit(1);
     52  1.1       gwr 		}
     53  1.1       gwr 	}
     54  1.1       gwr 	if (argc > 2)
     55  1.1       gwr 		fprintf(stderr, "extra args ignored\n");
     56  1.1       gwr 
     57  1.1       gwr 	dfp = opendir(".");
     58  1.1       gwr 	if (dfp == NULL) {
     59  1.1       gwr 		perror("opendir");
     60  1.1       gwr 		return;
     61  1.1       gwr 	}
     62  1.1       gwr 
     63  1.1       gwr 	while ((d = readdir(dfp)) != NULL)
     64  1.1       gwr 		show_long(d->d_name);
     65  1.1       gwr 
     66  1.1       gwr 	closedir(dfp);
     67  1.1       gwr 	exit(0);
     68  1.1       gwr }
     69  1.1       gwr 
     70  1.1       gwr /* XXX - This is system dependent... */
     71  1.1       gwr char ifmt_name[16] = {
     72  1.1       gwr 	'?',	/* 0: nothing */
     73  1.1       gwr 	'P',	/* 1: fifo (pipe) */
     74  1.1       gwr 	'C',	/* 2: chr device */
     75  1.1       gwr 	'?',	/* 3: ? */
     76  1.1       gwr 	'D',	/* 4: dir */
     77  1.1       gwr 	'?',	/* 5: ? */
     78  1.1       gwr 	'B',	/* 6: blk device */
     79  1.1       gwr 	'?',	/* 7: ? */
     80  1.1       gwr 	'F',	/* 8: file */
     81  1.1       gwr 	'?',	/* 9: ? */
     82  1.1       gwr 	'L',	/* A: link */
     83  1.1       gwr 	'?',	/* B: ? */
     84  1.1       gwr 	'S',	/* C: socket */
     85  1.1       gwr 	'?',	/* D: ? */
     86  1.1       gwr 	'W',	/* E: whiteout */
     87  1.1       gwr 	'?' 	/* F: ? */
     88  1.1       gwr };
     89  1.1       gwr 
     90  1.1       gwr void
     91  1.1       gwr show_long(fname)
     92  1.1       gwr 	char *fname;
     93  1.1       gwr {
     94  1.1       gwr 	struct stat st;
     95  1.1       gwr 	int ifmt;
     96  1.1       gwr 	char ifmt_c;
     97  1.1       gwr 	char *date;
     98  1.1       gwr 
     99  1.1       gwr 	if (lstat(fname, &st)) {
    100  1.1       gwr 		perror(fname);
    101  1.1       gwr 		return;
    102  1.1       gwr 	}
    103  1.1       gwr 	ifmt = (st.st_mode >> 12) & 15;
    104  1.1       gwr 	ifmt_c = ifmt_name[ifmt];
    105  1.1       gwr 
    106  1.1       gwr 	if (iflag) {
    107  1.1       gwr 		/* inode number */
    108  1.1       gwr 		printf("%6d ",  st.st_ino);
    109  1.1       gwr 	}
    110  1.1       gwr 
    111  1.1       gwr 	/* fmt/mode */
    112  1.1       gwr 	printf("%c:",   ifmt_c);
    113  1.1       gwr 	printf("%04o ", st.st_mode & 07777);
    114  1.1       gwr 
    115  1.1       gwr 	/* nlinks, uid, gid */
    116  1.1       gwr 	printf("%2d ",   st.st_nlink);
    117  1.1       gwr 	printf("%4d ",  st.st_uid);
    118  1.1       gwr 	printf("%4d ",  st.st_gid);
    119  1.1       gwr 
    120  1.1       gwr 	/* size or major/minor */
    121  1.1       gwr 	if ((ifmt_c == 'B') || (ifmt_c == 'C')) {
    122  1.1       gwr 		printf("%2d, ", major(st.st_rdev));
    123  1.1       gwr 		printf("%3d ",  minor(st.st_rdev));
    124  1.1       gwr 	} else {
    125  1.1       gwr 		printf("%7d ",  (int) st.st_size);
    126  1.1       gwr 	}
    127  1.1       gwr 
    128  1.1       gwr 	/* date */
    129  1.4  christos 	if ((date = ctime(&st.st_mtime)) == NULL)
    130  1.4  christos 		printf("? ");
    131  1.4  christos 	else {
    132  1.4  christos 		date += 4;	/* skip day-of-week */
    133  1.4  christos 		date[12] = '\0';	/* to the minute */
    134  1.4  christos 		printf("%s ", date);
    135  1.4  christos 	}
    136  1.1       gwr 
    137  1.1       gwr 	/* name */
    138  1.1       gwr 	printf("%s", fname);
    139  1.1       gwr 
    140  1.1       gwr 	if (ifmt_c == 'L') {
    141  1.1       gwr 		char linkto[256];
    142  1.1       gwr 		int n;
    143  1.1       gwr 
    144  1.1       gwr 		n = readlink(fname, linkto, sizeof(linkto)-1);
    145  1.1       gwr 		if (n < 0) {
    146  1.1       gwr 			perror(fname);
    147  1.1       gwr 			return;
    148  1.1       gwr 		}
    149  1.1       gwr 		linkto[n] = '\0';
    150  1.1       gwr 		printf(" -> %s", linkto);
    151  1.1       gwr 	}
    152  1.1       gwr 	printf("\n");
    153  1.1       gwr }
    154