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