xref: /freebsd/stand/common/ls.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
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 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 
47 #include <stand.h>
48 #include <string.h>
49 
50 #include "bootstrap.h"
51 
52 static char typestr[] = "?fc?d?b? ?l?s?w";
53 
54 static int	ls_getdir(char **pathp);
55 
56 COMMAND_SET(ls, "ls", "list files", command_ls);
57 
58 static int
59 command_ls(int argc, char *argv[])
60 {
61     int		fd;
62     struct stat	sb;
63     struct	dirent *d;
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     if (stat(path, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
95 	if (verbose) {
96 	    printf(" %c %8d %s\n",
97 		typestr[sb.st_mode >> 12],
98 		(int)sb.st_size, path);
99 	} else {
100 	    printf(" %c  %s\n",
101 		typestr[sb.st_mode >> 12], path);
102 	}
103 	return (CMD_OK);
104     }
105 
106     fd = ls_getdir(&path);
107     if (fd == -1) {
108 	result = CMD_ERROR;
109 	goto out;
110     }
111     pager_open();
112     pager_output(path);
113     pager_output("\n");
114 
115     while ((d = readdirfd(fd)) != NULL) {
116 	if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
117 	    if (d->d_type == 0 || verbose) {
118 		/* stat the file, if possible */
119 		sb.st_size = 0;
120 		sb.st_mode = 0;
121 		buf = malloc(strlen(path) + strlen(d->d_name) + 2);
122 		if (buf != NULL) {
123 		    sprintf(buf, "%s/%s", path, d->d_name);
124 		    /* ignore return, could be symlink, etc. */
125 		    if (stat(buf, &sb)) {
126 			sb.st_size = 0;
127 			sb.st_mode = 0;
128 		    }
129 		    free(buf);
130 		}
131 	    }
132 	    if (verbose) {
133 		snprintf(lbuf, sizeof(lbuf), " %c %8d %s\n",
134 		    typestr[d->d_type? d->d_type:sb.st_mode >> 12],
135 		    (int)sb.st_size, d->d_name);
136 	    } else {
137 		snprintf(lbuf, sizeof(lbuf), " %c  %s\n",
138 		    typestr[d->d_type? d->d_type:sb.st_mode >> 12], d->d_name);
139 	    }
140 	    if (pager_output(lbuf))
141 		goto out;
142 	}
143     }
144  out:
145     pager_close();
146     if (fd != -1)
147 	close(fd);
148     free(path);		/* ls_getdir() did allocate path */
149     return (result);
150 }
151 
152 /*
153  * Given (path) containing a vaguely reasonable path specification, return an fd
154  * on the directory, and an allocated copy of the path to the directory.
155  */
156 static int
157 ls_getdir(char **pathp)
158 {
159     struct stat	sb;
160     int		fd;
161     const char	*cp;
162     char	*path;
163 
164     fd = -1;
165 
166     /* one extra byte for a possible trailing slash required */
167     path = malloc(strlen(*pathp) + 2);
168     if (path == NULL) {
169 	snprintf(command_errbuf, sizeof (command_errbuf),
170 	    "out of memory");
171 	goto out;
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