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