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