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