Home | History | Annotate | Line # | Download | only in common
ls.c revision 1.1.2.2
      1  1.1.2.2  yamt /*
      2  1.1.2.2  yamt  * $NetBSD: ls.c,v 1.1.2.2 2006/04/11 11:53:40 yamt Exp $
      3  1.1.2.2  yamt  */
      4  1.1.2.2  yamt 
      5  1.1.2.2  yamt /*-
      6  1.1.2.2  yamt  * Copyright (c) 1993
      7  1.1.2.2  yamt  *	The Regents of the University of California.  All rights reserved.
      8  1.1.2.2  yamt  * Copyright (c) 1996
      9  1.1.2.2  yamt  *	Matthias Drochner.  All rights reserved.
     10  1.1.2.2  yamt  *
     11  1.1.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     12  1.1.2.2  yamt  * modification, are permitted provided that the following conditions
     13  1.1.2.2  yamt  * are met:
     14  1.1.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     15  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     16  1.1.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     18  1.1.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     19  1.1.2.2  yamt  * 3. All advertising materials mentioning features or use of this software
     20  1.1.2.2  yamt  *    must display the following acknowledgement:
     21  1.1.2.2  yamt  *	This product includes software developed by the University of
     22  1.1.2.2  yamt  *	California, Berkeley and its contributors.
     23  1.1.2.2  yamt  * 4. Neither the name of the University nor the names of its contributors
     24  1.1.2.2  yamt  *    may be used to endorse or promote products derived from this software
     25  1.1.2.2  yamt  *    without specific prior written permission.
     26  1.1.2.2  yamt  *
     27  1.1.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1.2.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1.2.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1.2.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1.2.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1.2.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1.2.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1.2.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1.2.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1.2.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1.2.2  yamt  * SUCH DAMAGE.
     38  1.1.2.2  yamt  */
     39  1.1.2.2  yamt 
     40  1.1.2.2  yamt #include <sys/cdefs.h>
     41  1.1.2.2  yamt 
     42  1.1.2.2  yamt #include <sys/param.h>
     43  1.1.2.2  yamt #include <ufs/ufs/dinode.h>
     44  1.1.2.2  yamt #include <ufs/ufs/dir.h>
     45  1.1.2.2  yamt #include <sys/dirent.h>
     46  1.1.2.2  yamt 
     47  1.1.2.2  yamt #include <lib/libsa/stand.h>
     48  1.1.2.2  yamt 
     49  1.1.2.2  yamt #include "bootstrap.h"
     50  1.1.2.2  yamt 
     51  1.1.2.2  yamt static char typestr[] = "?fc?d?b? ?l?s?w";
     52  1.1.2.2  yamt 
     53  1.1.2.2  yamt static int	ls_getdir(char **pathp);
     54  1.1.2.2  yamt 
     55  1.1.2.2  yamt int
     56  1.1.2.2  yamt command_ls(int argc, char *argv[])
     57  1.1.2.2  yamt {
     58  1.1.2.2  yamt     int		fd;
     59  1.1.2.2  yamt     struct stat	sb;
     60  1.1.2.2  yamt     struct 	dirent *d;
     61  1.1.2.2  yamt     char	*buf, *path;
     62  1.1.2.2  yamt     char	lbuf[128];		/* one line */
     63  1.1.2.2  yamt     int		result, ch;
     64  1.1.2.2  yamt     int		verbose;
     65  1.1.2.2  yamt 
     66  1.1.2.2  yamt     result = CMD_OK;
     67  1.1.2.2  yamt     fd = -1;
     68  1.1.2.2  yamt     verbose = 0;
     69  1.1.2.2  yamt     optind = 1;
     70  1.1.2.2  yamt     optreset = 1;
     71  1.1.2.2  yamt     while ((ch = getopt(argc, argv, "l")) != -1) {
     72  1.1.2.2  yamt 	switch(ch) {
     73  1.1.2.2  yamt 	case 'l':
     74  1.1.2.2  yamt 	    verbose = 1;
     75  1.1.2.2  yamt 	    break;
     76  1.1.2.2  yamt 	case '?':
     77  1.1.2.2  yamt 	default:
     78  1.1.2.2  yamt 	    /* getopt has already reported an error */
     79  1.1.2.2  yamt 	    return(CMD_OK);
     80  1.1.2.2  yamt 	}
     81  1.1.2.2  yamt     }
     82  1.1.2.2  yamt     argv += (optind - 1);
     83  1.1.2.2  yamt     argc -= (optind - 1);
     84  1.1.2.2  yamt 
     85  1.1.2.2  yamt     if (argc < 2) {
     86  1.1.2.2  yamt 	path = "";
     87  1.1.2.2  yamt     } else {
     88  1.1.2.2  yamt 	path = argv[1];
     89  1.1.2.2  yamt     }
     90  1.1.2.2  yamt 
     91  1.1.2.2  yamt     fd = ls_getdir(&path);
     92  1.1.2.2  yamt     if (fd == -1) {
     93  1.1.2.2  yamt 	result = CMD_ERROR;
     94  1.1.2.2  yamt 	goto out;
     95  1.1.2.2  yamt     }
     96  1.1.2.2  yamt 
     97  1.1.2.2  yamt     pager_open();
     98  1.1.2.2  yamt     pager_output(path);
     99  1.1.2.2  yamt     pager_output("\n");
    100  1.1.2.2  yamt 
    101  1.1.2.2  yamt     while ((d = readdirfd(fd)) != NULL) {
    102  1.1.2.2  yamt /*	if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { */
    103  1.1.2.2  yamt 	    if (verbose) {
    104  1.1.2.2  yamt 		/* stat the file, if possible */
    105  1.1.2.2  yamt 		sb.st_size = 0;
    106  1.1.2.2  yamt 		buf = alloc(strlen(path) + strlen(d->d_name) + 2);
    107  1.1.2.2  yamt 		sprintf(buf, "%s/%s", path, d->d_name);
    108  1.1.2.2  yamt 		/* ignore return, could be symlink, etc. */
    109  1.1.2.2  yamt 		if (stat(buf, &sb))
    110  1.1.2.2  yamt 		    sb.st_size = 0;
    111  1.1.2.2  yamt 		free(buf);
    112  1.1.2.2  yamt 		sprintf(lbuf, " %c %8d %s\n", typestr[d->d_type],
    113  1.1.2.2  yamt 		    (int)sb.st_size, d->d_name);
    114  1.1.2.2  yamt 	    } else {
    115  1.1.2.2  yamt 	      sprintf(lbuf, " %c  %s\n", typestr[d->d_type], d->d_name);
    116  1.1.2.2  yamt 	    }
    117  1.1.2.2  yamt 	    if (pager_output(lbuf))
    118  1.1.2.2  yamt 		goto out;
    119  1.1.2.2  yamt 	    /*	} */
    120  1.1.2.2  yamt     }
    121  1.1.2.2  yamt  out:
    122  1.1.2.2  yamt     pager_close();
    123  1.1.2.2  yamt     if (fd != -1)
    124  1.1.2.2  yamt 	close(fd);
    125  1.1.2.2  yamt     if (path != NULL)
    126  1.1.2.2  yamt 	free(path);
    127  1.1.2.2  yamt     return(result);
    128  1.1.2.2  yamt }
    129  1.1.2.2  yamt 
    130  1.1.2.2  yamt /*
    131  1.1.2.2  yamt  * Given (path) containing a vaguely reasonable path specification, return an fd
    132  1.1.2.2  yamt  * on the directory, and an allocated copy of the path to the directory.
    133  1.1.2.2  yamt  */
    134  1.1.2.2  yamt static int
    135  1.1.2.2  yamt ls_getdir(char **pathp)
    136  1.1.2.2  yamt {
    137  1.1.2.2  yamt     struct stat	sb;
    138  1.1.2.2  yamt     int		fd;
    139  1.1.2.2  yamt     const char	*cp;
    140  1.1.2.2  yamt     char	*path, *tail;
    141  1.1.2.2  yamt 
    142  1.1.2.2  yamt     tail = NULL;
    143  1.1.2.2  yamt     fd = -1;
    144  1.1.2.2  yamt 
    145  1.1.2.2  yamt     /* one extra byte for a possible trailing slash required */
    146  1.1.2.2  yamt     path = alloc(strlen(*pathp) + 2);
    147  1.1.2.2  yamt     strcpy(path, *pathp);
    148  1.1.2.2  yamt 
    149  1.1.2.2  yamt     /* Make sure the path is respectable to begin with */
    150  1.1.2.2  yamt     if (archsw.arch_getdev(NULL, path, &cp)) {
    151  1.1.2.2  yamt 	sprintf(command_errbuf, "bad path '%s'", path);
    152  1.1.2.2  yamt 	goto out;
    153  1.1.2.2  yamt     }
    154  1.1.2.2  yamt 
    155  1.1.2.2  yamt     /* If there's no path on the device, assume '/' */
    156  1.1.2.2  yamt     if (*cp == 0)
    157  1.1.2.2  yamt 	strcat(path, "/");
    158  1.1.2.2  yamt     fd = open(path, O_RDONLY);
    159  1.1.2.2  yamt     if (fd < 0) {
    160  1.1.2.2  yamt 	sprintf(command_errbuf, "open '%s' failed: %s", path, strerror(errno));
    161  1.1.2.2  yamt 	goto out;
    162  1.1.2.2  yamt     }
    163  1.1.2.2  yamt     if (fstat(fd, &sb) < 0) {
    164  1.1.2.2  yamt 	sprintf(command_errbuf, "stat failed: %s", strerror(errno));
    165  1.1.2.2  yamt 	goto out;
    166  1.1.2.2  yamt     }
    167  1.1.2.2  yamt     if (!S_ISDIR(sb.st_mode)) {
    168  1.1.2.2  yamt 	sprintf(command_errbuf, "%s: %s", path, strerror(ENOTDIR));
    169  1.1.2.2  yamt 	goto out;
    170  1.1.2.2  yamt     }
    171  1.1.2.2  yamt 
    172  1.1.2.2  yamt     *pathp = path;
    173  1.1.2.2  yamt     return(fd);
    174  1.1.2.2  yamt 
    175  1.1.2.2  yamt  out:
    176  1.1.2.2  yamt     free(path);
    177  1.1.2.2  yamt     *pathp = NULL;
    178  1.1.2.2  yamt     if (fd != -1)
    179  1.1.2.2  yamt 	close(fd);
    180  1.1.2.2  yamt     return(-1);
    181  1.1.2.2  yamt }
    182