xref: /illumos-gate/usr/src/boot/common/ls.c (revision aecc710ab066150d47e5e9e7269e2e0d69107b4e)
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
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 				/* Avoid double '/' */
121 				if (strcmp(path, "/") == 0) {
122 					(void) asprintf(&buf, "/%s", d->d_name);
123 				} else {
124 					(void) asprintf(&buf, "%s/%s", path,
125 					    d->d_name);
126 				}
127 
128 				if (buf != NULL) {
129 					/*
130 					 * ignore return, could be symlink, etc.
131 					 */
132 					if (stat(buf, &sb) != 0) {
133 						sb.st_size = 0;
134 						sb.st_mode = 0;
135 					}
136 					free(buf);
137 				}
138 			}
139 			if (verbose) {
140 				snprintf(lbuf, sizeof (lbuf), " %c %8d %s\n",
141 				    typestr[d->d_type ?
142 				    d->d_type:sb.st_mode >> 12],
143 				    (int)sb.st_size, d->d_name);
144 			} else {
145 				snprintf(lbuf, sizeof (lbuf), " %c  %s\n",
146 				    typestr[d->d_type ?
147 				    d->d_type:sb.st_mode >> 12], d->d_name);
148 			}
149 			if (pager_output(lbuf))
150 				goto out;
151 		}
152 	}
153 out:
154 	pager_close();
155 	if (fd != -1)
156 		close(fd);
157 	free(path);		/* ls_getdir() did allocate path */
158 	return (result);
159 }
160 
161 /*
162  * Given (path) containing a vaguely reasonable path specification, return an fd
163  * on the directory, and an allocated copy of the path to the directory.
164  */
165 static int
166 ls_getdir(char **pathp)
167 {
168 	struct stat	sb;
169 	int		fd;
170 	const char	*cp;
171 	char	*path;
172 
173 	fd = -1;
174 
175 	/* one extra byte for a possible trailing slash required */
176 	path = malloc(strlen(*pathp) + 2);
177 	if (path == NULL) {
178 		snprintf(command_errbuf, sizeof (command_errbuf),
179 		    "out of memory");
180 		goto out;
181 	}
182 
183 	strcpy(path, *pathp);
184 
185 	/* Make sure the path is respectable to begin with */
186 	if (archsw.arch_getdev(NULL, path, &cp)) {
187 		snprintf(command_errbuf, sizeof (command_errbuf),
188 		    "bad path '%s'", path);
189 		goto out;
190 	}
191 
192 	/* If there's no path on the device, assume '/' */
193 	if (*cp == 0)
194 		strcat(path, "/");
195 
196 	fd = open(path, O_RDONLY);
197 	if (fd < 0) {
198 		snprintf(command_errbuf, sizeof (command_errbuf),
199 		    "open '%s' failed: %s", path, strerror(errno));
200 		goto out;
201 	}
202 	if (fstat(fd, &sb) < 0) {
203 		snprintf(command_errbuf, sizeof (command_errbuf),
204 		    "stat failed: %s", strerror(errno));
205 		goto out;
206 	}
207 	if (!S_ISDIR(sb.st_mode)) {
208 		snprintf(command_errbuf, sizeof (command_errbuf),
209 		    "%s: %s", path, strerror(ENOTDIR));
210 		goto out;
211 	}
212 
213 	*pathp = path;
214 	return (fd);
215 
216 out:
217 	free(path);
218 	*pathp = NULL;
219 	if (fd != -1)
220 		close(fd);
221 	return (-1);
222 }
223