xref: /illumos-gate/usr/src/boot/common/ls.c (revision 22028508)
1199767f8SToomas Soome /*
2199767f8SToomas Soome  * $NetBSD: ls.c,v 1.3 1997/06/13 13:48:47 drochner Exp $
3199767f8SToomas Soome  */
4199767f8SToomas Soome 
5bab26a4cSToomas Soome /*
6199767f8SToomas Soome  * Copyright (c) 1993
7199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
8199767f8SToomas Soome  * Copyright (c) 1996
9199767f8SToomas Soome  *	Matthias Drochner.  All rights reserved.
10199767f8SToomas Soome  *
11199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
12199767f8SToomas Soome  * modification, are permitted provided that the following conditions
13199767f8SToomas Soome  * are met:
14199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
15199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
16199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
17199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
18199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
19199767f8SToomas Soome  * 3. All advertising materials mentioning features or use of this software
20199767f8SToomas Soome  *    must display the following acknowledgement:
21199767f8SToomas Soome  *	This product includes software developed by the University of
22199767f8SToomas Soome  *	California, Berkeley and its contributors.
23199767f8SToomas Soome  * 4. Neither the name of the University nor the names of its contributors
24199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
25199767f8SToomas Soome  *    without specific prior written permission.
26199767f8SToomas Soome  *
27199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37199767f8SToomas Soome  * SUCH DAMAGE.
38199767f8SToomas Soome  */
39199767f8SToomas Soome 
40199767f8SToomas Soome #include <sys/cdefs.h>
41199767f8SToomas Soome 
42199767f8SToomas Soome #include <sys/param.h>
43199767f8SToomas Soome #include <ufs/ufs/dinode.h>
44199767f8SToomas Soome #include <ufs/ufs/dir.h>
45199767f8SToomas Soome 
46199767f8SToomas Soome #include <stand.h>
47199767f8SToomas Soome #include <string.h>
48199767f8SToomas Soome 
49199767f8SToomas Soome #include "bootstrap.h"
50199767f8SToomas Soome 
51199767f8SToomas Soome static char typestr[] = "?fc?d?b? ?l?s?w";
52199767f8SToomas Soome 
53199767f8SToomas Soome static int	ls_getdir(char **pathp);
54199767f8SToomas Soome 
55199767f8SToomas Soome COMMAND_SET(ls, "ls", "list files", command_ls);
56199767f8SToomas Soome 
57199767f8SToomas Soome static int
command_ls(int argc,char * argv[])58199767f8SToomas Soome command_ls(int argc, char *argv[])
59199767f8SToomas Soome {
60199767f8SToomas Soome     int		fd;
61199767f8SToomas Soome     struct stat	sb;
62bab26a4cSToomas Soome     struct	dirent *d;
63199767f8SToomas Soome     char	*buf, *path;
64199767f8SToomas Soome     char	lbuf[128];		/* one line */
65199767f8SToomas Soome     int		result, ch;
66199767f8SToomas Soome     int		verbose;
67bab26a4cSToomas Soome 
68199767f8SToomas Soome     result = CMD_OK;
69199767f8SToomas Soome     fd = -1;
70199767f8SToomas Soome     verbose = 0;
71199767f8SToomas Soome     optind = 1;
72199767f8SToomas Soome     optreset = 1;
73199767f8SToomas Soome     while ((ch = getopt(argc, argv, "l")) != -1) {
74bab26a4cSToomas Soome 	switch (ch) {
75199767f8SToomas Soome 	case 'l':
76199767f8SToomas Soome 	    verbose = 1;
77199767f8SToomas Soome 	    break;
78199767f8SToomas Soome 	case '?':
79199767f8SToomas Soome 	default:
80199767f8SToomas Soome 	    /* getopt has already reported an error */
81bab26a4cSToomas Soome 	    return (CMD_OK);
82199767f8SToomas Soome 	}
83199767f8SToomas Soome     }
84199767f8SToomas Soome     argv += (optind - 1);
85199767f8SToomas Soome     argc -= (optind - 1);
86199767f8SToomas Soome 
87199767f8SToomas Soome     if (argc < 2) {
88199767f8SToomas Soome 	path = "";
89199767f8SToomas Soome     } else {
90199767f8SToomas Soome 	path = argv[1];
91199767f8SToomas Soome     }
92199767f8SToomas Soome 
93199767f8SToomas Soome     if (stat(path, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
94199767f8SToomas Soome 	if (verbose) {
95199767f8SToomas Soome 	    printf(" %c %8d %s\n",
96199767f8SToomas Soome 	    typestr[sb.st_mode >> 12],
97199767f8SToomas Soome 	    (int)sb.st_size, path);
98199767f8SToomas Soome 	} else {
99199767f8SToomas Soome 	    printf(" %c  %s\n",
100199767f8SToomas Soome 	    typestr[sb.st_mode >> 12], path);
101199767f8SToomas Soome 	}
102199767f8SToomas Soome 	return (CMD_OK);
103199767f8SToomas Soome     }
104199767f8SToomas Soome 
105199767f8SToomas Soome     fd = ls_getdir(&path);
106199767f8SToomas Soome     if (fd == -1) {
107199767f8SToomas Soome 	result = CMD_ERROR;
108199767f8SToomas Soome 	goto out;
109199767f8SToomas Soome     }
110199767f8SToomas Soome     pager_open();
111199767f8SToomas Soome     pager_output(path);
112199767f8SToomas Soome     pager_output("\n");
113199767f8SToomas Soome 
114199767f8SToomas Soome     while ((d = readdirfd(fd)) != NULL) {
115199767f8SToomas Soome 	if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
116199767f8SToomas Soome 	    if (d->d_type == 0 || verbose) {
117199767f8SToomas Soome 		/* stat the file, if possible */
118199767f8SToomas Soome 		sb.st_size = 0;
119199767f8SToomas Soome 		sb.st_mode = 0;
120199767f8SToomas Soome 		buf = malloc(strlen(path) + strlen(d->d_name) + 2);
121bab26a4cSToomas Soome 		if (buf != NULL) {
122bab26a4cSToomas Soome 		    sprintf(buf, "%s/%s", path, d->d_name);
123bab26a4cSToomas Soome 		    /* ignore return, could be symlink, etc. */
124bab26a4cSToomas Soome 		    if (stat(buf, &sb)) {
125bab26a4cSToomas Soome 			sb.st_size = 0;
126bab26a4cSToomas Soome 			sb.st_mode = 0;
127bab26a4cSToomas Soome 		    }
128bab26a4cSToomas Soome 		    free(buf);
129bab26a4cSToomas Soome 		}
130199767f8SToomas Soome 	    }
131199767f8SToomas Soome 	    if (verbose) {
132bab26a4cSToomas Soome 		snprintf(lbuf, sizeof (lbuf), " %c %8d %s\n",
133199767f8SToomas Soome 		    typestr[d->d_type? d->d_type:sb.st_mode >> 12],
134199767f8SToomas Soome 		    (int)sb.st_size, d->d_name);
135199767f8SToomas Soome 	    } else {
136bab26a4cSToomas Soome 		snprintf(lbuf, sizeof (lbuf), " %c  %s\n",
137199767f8SToomas Soome 		    typestr[d->d_type? d->d_type:sb.st_mode >> 12], d->d_name);
138199767f8SToomas Soome 	    }
139199767f8SToomas Soome 	    if (pager_output(lbuf))
140199767f8SToomas Soome 		goto out;
141199767f8SToomas Soome 	}
142199767f8SToomas Soome     }
143199767f8SToomas Soome  out:
144199767f8SToomas Soome     pager_close();
145199767f8SToomas Soome     if (fd != -1)
146199767f8SToomas Soome 	close(fd);
147bab26a4cSToomas Soome     free(path);		/* ls_getdir() did allocate path */
148bab26a4cSToomas Soome     return (result);
149199767f8SToomas Soome }
150199767f8SToomas Soome 
151199767f8SToomas Soome /*
152199767f8SToomas Soome  * Given (path) containing a vaguely reasonable path specification, return an fd
153199767f8SToomas Soome  * on the directory, and an allocated copy of the path to the directory.
154199767f8SToomas Soome  */
155199767f8SToomas Soome static int
ls_getdir(char ** pathp)156199767f8SToomas Soome ls_getdir(char **pathp)
157199767f8SToomas Soome {
158199767f8SToomas Soome     struct stat	sb;
159199767f8SToomas Soome     int		fd;
160199767f8SToomas Soome     const char	*cp;
161199767f8SToomas Soome     char	*path;
162bab26a4cSToomas Soome 
163199767f8SToomas Soome     fd = -1;
164199767f8SToomas Soome 
165199767f8SToomas Soome     /* one extra byte for a possible trailing slash required */
166199767f8SToomas Soome     path = malloc(strlen(*pathp) + 2);
167bab26a4cSToomas Soome     if (path == NULL) {
168bab26a4cSToomas Soome 	snprintf(command_errbuf, sizeof (command_errbuf),
169bab26a4cSToomas Soome 	    "out of memory");
170bab26a4cSToomas Soome 	goto out;
171bab26a4cSToomas Soome     }
172bab26a4cSToomas Soome 
173199767f8SToomas Soome     strcpy(path, *pathp);
174199767f8SToomas Soome 
175199767f8SToomas Soome     /* Make sure the path is respectable to begin with */
176199767f8SToomas Soome     if (archsw.arch_getdev(NULL, path, &cp)) {
177199767f8SToomas Soome 	snprintf(command_errbuf, sizeof (command_errbuf),
178199767f8SToomas Soome 	    "bad path '%s'", path);
179199767f8SToomas Soome 	goto out;
180199767f8SToomas Soome     }
181bab26a4cSToomas Soome 
182199767f8SToomas Soome     /* If there's no path on the device, assume '/' */
183199767f8SToomas Soome     if (*cp == 0)
184199767f8SToomas Soome 	strcat(path, "/");
185199767f8SToomas Soome 
186199767f8SToomas Soome     fd = open(path, O_RDONLY);
187199767f8SToomas Soome     if (fd < 0) {
188199767f8SToomas Soome 	snprintf(command_errbuf, sizeof (command_errbuf),
189199767f8SToomas Soome 	    "open '%s' failed: %s", path, strerror(errno));
190199767f8SToomas Soome 	goto out;
191199767f8SToomas Soome     }
192199767f8SToomas Soome     if (fstat(fd, &sb) < 0) {
193199767f8SToomas Soome 	snprintf(command_errbuf, sizeof (command_errbuf),
194199767f8SToomas Soome 	    "stat failed: %s", strerror(errno));
195199767f8SToomas Soome 	goto out;
196199767f8SToomas Soome     }
197199767f8SToomas Soome     if (!S_ISDIR(sb.st_mode)) {
198199767f8SToomas Soome 	snprintf(command_errbuf, sizeof (command_errbuf),
199199767f8SToomas Soome 	    "%s: %s", path, strerror(ENOTDIR));
200199767f8SToomas Soome 	goto out;
201199767f8SToomas Soome     }
202199767f8SToomas Soome 
203199767f8SToomas Soome     *pathp = path;
204bab26a4cSToomas Soome     return (fd);
205199767f8SToomas Soome 
206199767f8SToomas Soome  out:
207199767f8SToomas Soome     free(path);
208199767f8SToomas Soome     *pathp = NULL;
209199767f8SToomas Soome     if (fd != -1)
210199767f8SToomas Soome 	close(fd);
211bab26a4cSToomas Soome     return (-1);
212199767f8SToomas Soome }
213