Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / ia64 / stand / common / ls.c
blobe14d887c7534bdfadd5a1c2b897884f0008c91e5
1 /*
2 * $NetBSD: ls.c,v 1.2 2006/04/22 07:58:53 cherry Exp $
3 */
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.
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.
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.
40 #include <sys/cdefs.h>
41 /* __FBSDID("$FreeBSD: src/sys/boot/common/ls.c,v 1.11 2003/08/25 23:30:41 obrien Exp $"); */
44 #include <sys/param.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <sys/dirent.h>
49 #include <lib/libsa/stand.h>
50 #include <lib/libsa/loadfile.h>
52 #include "bootstrap.h"
54 static char typestr[] = "?fc?d?b? ?l?s?w";
56 static int ls_getdir(char **pathp);
58 int
59 command_ls(int argc, char *argv[])
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;
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);
85 argv += (optind - 1);
86 argc -= (optind - 1);
88 if (argc < 2) {
89 path = "";
90 } else {
91 path = argv[1];
94 fd = ls_getdir(&path);
95 if (fd == -1) {
96 result = CMD_ERROR;
97 goto out;
100 pager_open();
101 pager_output(path);
102 pager_output("\n");
104 while ((d = readdirfd(fd)) != NULL) {
105 /* if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { */
106 if (verbose) {
107 /* stat the file, if possible */
108 sb.st_size = 0;
109 buf = alloc(strlen(path) + strlen(d->d_name) + 2);
110 sprintf(buf, "%s/%s", path, d->d_name);
111 /* ignore return, could be symlink, etc. */
112 if (stat(buf, &sb))
113 sb.st_size = 0;
114 free(buf);
115 sprintf(lbuf, " %c %8d %s\n", typestr[d->d_type],
116 (int)sb.st_size, d->d_name);
117 } else {
118 sprintf(lbuf, " %c %s\n", typestr[d->d_type], d->d_name);
120 if (pager_output(lbuf))
121 goto out;
122 /* } */
124 out:
125 pager_close();
126 if (fd != -1)
127 close(fd);
128 if (path != NULL)
129 free(path);
130 return(result);
134 * Given (path) containing a vaguely reasonable path specification, return an fd
135 * on the directory, and an allocated copy of the path to the directory.
137 static int
138 ls_getdir(char **pathp)
140 struct stat sb;
141 int fd;
142 const char *cp;
143 char *path, *tail;
145 tail = NULL;
146 fd = -1;
148 /* one extra byte for a possible trailing slash required */
149 path = alloc(strlen(*pathp) + 2);
150 strcpy(path, *pathp);
152 /* Make sure the path is respectable to begin with */
153 if (archsw.arch_getdev(NULL, path, &cp)) {
154 sprintf(command_errbuf, "bad path '%s'", path);
155 goto out;
158 /* If there's no path on the device, assume '/' */
159 if (*cp == 0)
160 strcat(path, "/");
161 fd = open(path, O_RDONLY);
162 if (fd < 0) {
163 sprintf(command_errbuf, "open '%s' failed: %s", path, strerror(errno));
164 goto out;
166 if (fstat(fd, &sb) < 0) {
167 sprintf(command_errbuf, "stat failed: %s", strerror(errno));
168 goto out;
170 if (!S_ISDIR(sb.st_mode)) {
171 sprintf(command_errbuf, "%s: %s", path, strerror(ENOTDIR));
172 goto out;
175 *pathp = path;
176 return(fd);
178 out:
179 free(path);
180 *pathp = NULL;
181 if (fd != -1)
182 close(fd);
183 return(-1);