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