xref: /dragonfly/stand/boot/common/ls.c (revision 655933d6)
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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/boot/common/ls.c,v 1.11 2003/08/25 23:30:41 obrien Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <vfs/ufs/dinode.h>
40 #include <vfs/ufs/dir.h>
41 
42 #include <stand.h>
43 #include <string.h>
44 
45 #include "bootstrap.h"
46 
47 static char typestr[] = "?fc?d?b? ?l?s?w";
48 
49 static int	ls_getdir(char **pathp);
50 
51 COMMAND_SET(ls, "ls", "list files", command_ls);
52 
53 static int
54 command_ls(int argc, char *argv[])
55 {
56     int		fd;
57     struct stat	sb;
58     struct 	dirent *d;
59     char	*buf, *path;
60     char	lbuf[128];		/* one line */
61     int		result, ch;
62     int		verbose;
63 
64     result = CMD_OK;
65     fd = -1;
66     verbose = 0;
67     optind = 1;
68     optreset = 1;
69     while ((ch = getopt(argc, argv, "l")) != -1) {
70 	switch(ch) {
71 	case 'l':
72 	    verbose = 1;
73 	    break;
74 	case '?':
75 	default:
76 	    /* getopt has already reported an error */
77 	    return(CMD_OK);
78 	}
79     }
80     argv += (optind - 1);
81     argc -= (optind - 1);
82 
83     if (argc < 2) {
84 	path = "";
85     } else {
86 	path = argv[1];
87     }
88 
89     fd = ls_getdir(&path);
90     if (fd == -1) {
91 	result = CMD_ERROR;
92 	goto out;
93     }
94     pager_open();
95     pager_output(path);
96     pager_output("\n");
97 
98     while ((d = readdirfd(fd)) != NULL) {
99 	if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
100 	    if (verbose) {
101 		/* stat the file, if possible */
102 		sb.st_size = 0;
103 		buf = malloc(strlen(path) + strlen(d->d_name) + 2);
104 		if (strlen(path) == 0)
105 		    sprintf(buf, "%s", d->d_name);
106 		else
107 		    sprintf(buf, "%s/%s", path, d->d_name);
108 		/* ignore return, could be symlink, etc. */
109 		if (rel_stat(buf, &sb))
110 		    sb.st_size = 0;
111 		free(buf);
112 		sprintf(lbuf, " %c %8d %s\n", typestr[d->d_type],
113 		    (int)sb.st_size, d->d_name);
114 	    } else {
115 		sprintf(lbuf, " %c  %s\n", typestr[d->d_type], d->d_name);
116 	    }
117 	    if (pager_output(lbuf))
118 		goto out;
119 	}
120     }
121  out:
122     pager_close();
123     if (fd != -1)
124 	close(fd);
125     if (path != NULL)
126 	free(path);
127     return(result);
128 }
129 
130 /*
131  * Given (path) containing a vaguely reasonable path specification, return an fd
132  * on the directory, and an allocated copy of the path to the directory.
133  */
134 static int
135 ls_getdir(char **pathp)
136 {
137     struct stat	sb;
138     int		fd;
139     const char	*cp;
140     char	*path;
141 
142     fd = -1;
143 
144     /* one extra byte for a possible trailing slash required */
145     path = malloc(strlen(*pathp) + 2);
146     strcpy(path, *pathp);
147 
148     /* Make sure the path is respectable to begin with */
149     if (archsw.arch_getdev(NULL, path, &cp)) {
150 	snprintf(command_errbuf, sizeof(command_errbuf),
151 	    "bad path '%s'", path);
152 	goto out;
153     }
154 
155     fd = rel_open(cp, NULL, O_RDONLY);
156     if (fd < 0) {
157 	snprintf(command_errbuf, sizeof(command_errbuf),
158 	    "open '%s' failed: %s", path, strerror(errno));
159 	goto out;
160     }
161     if (fstat(fd, &sb) < 0) {
162 	snprintf(command_errbuf, sizeof(command_errbuf),
163 	    "stat failed: %s", strerror(errno));
164 	goto out;
165     }
166     if (!S_ISDIR(sb.st_mode)) {
167 	snprintf(command_errbuf, sizeof(command_errbuf),
168 	    "%s: %s", path, strerror(ENOTDIR));
169 	goto out;
170     }
171 
172     *pathp = path;
173     return(fd);
174 
175  out:
176     free(path);
177     *pathp = NULL;
178     if (fd != -1)
179 	close(fd);
180     return(-1);
181 }
182