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