ls.c revision 1.4 1 1.4 tsutsui /* $NetBSD: ls.c,v 1.4 2012/03/02 12:08:44 tsutsui Exp $ */
2 1.3 tsutsui
3 1.1 brezak /*-
4 1.3 tsutsui * Copyright (c) 2011
5 1.3 tsutsui * The NetBSD Foundation, Inc. All rights reserved.
6 1.3 tsutsui *
7 1.3 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.3 tsutsui * by Martin Husemann.
9 1.3 tsutsui *
10 1.3 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.3 tsutsui * modification, are permitted provided that the following conditions
12 1.3 tsutsui * are met:
13 1.3 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.3 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.3 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.3 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.3 tsutsui * documentation and/or other materials provided with the distribution.
18 1.3 tsutsui *
19 1.3 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3 tsutsui * POSSIBILITY OF SUCH DAMAGE.
30 1.3 tsutsui */
31 1.3 tsutsui
32 1.3 tsutsui /*
33 1.1 brezak * Copyright (c) 1993
34 1.1 brezak * The Regents of the University of California. All rights reserved.
35 1.1 brezak *
36 1.1 brezak * Redistribution and use in source and binary forms, with or without
37 1.1 brezak * modification, are permitted provided that the following conditions
38 1.1 brezak * are met:
39 1.1 brezak * 1. Redistributions of source code must retain the above copyright
40 1.1 brezak * notice, this list of conditions and the following disclaimer.
41 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 brezak * notice, this list of conditions and the following disclaimer in the
43 1.1 brezak * documentation and/or other materials provided with the distribution.
44 1.3 tsutsui * 3. Neither the name of the University nor the names of its contributors
45 1.1 brezak * may be used to endorse or promote products derived from this software
46 1.1 brezak * without specific prior written permission.
47 1.1 brezak *
48 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 brezak * SUCH DAMAGE.
59 1.1 brezak */
60 1.1 brezak
61 1.3 tsutsui /*
62 1.3 tsutsui * Copyright (c) 1996
63 1.3 tsutsui * Matthias Drochner. All rights reserved.
64 1.3 tsutsui *
65 1.3 tsutsui * Redistribution and use in source and binary forms, with or without
66 1.3 tsutsui * modification, are permitted provided that the following conditions
67 1.3 tsutsui * are met:
68 1.3 tsutsui * 1. Redistributions of source code must retain the above copyright
69 1.3 tsutsui * notice, this list of conditions and the following disclaimer.
70 1.3 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
71 1.3 tsutsui * notice, this list of conditions and the following disclaimer in the
72 1.3 tsutsui * documentation and/or other materials provided with the distribution.
73 1.3 tsutsui *
74 1.3 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
75 1.3 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76 1.3 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
77 1.3 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
78 1.3 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
79 1.3 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
80 1.3 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
81 1.3 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
82 1.3 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
83 1.3 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
84 1.3 tsutsui */
85 1.3 tsutsui
86 1.3 tsutsui
87 1.1 brezak #include "stand.h"
88 1.3 tsutsui #include <sys/stat.h>
89 1.3 tsutsui #include <lib/libkern/libkern.h>
90 1.1 brezak
91 1.3 tsutsui void
92 1.3 tsutsui ls(const char *path)
93 1.1 brezak {
94 1.3 tsutsui int fd;
95 1.3 tsutsui struct stat sb;
96 1.3 tsutsui size_t size;
97 1.3 tsutsui const char *fname = 0;
98 1.3 tsutsui char *p;
99 1.3 tsutsui struct open_file *f;
100 1.3 tsutsui
101 1.3 tsutsui if ((fd = open(path, 0)) < 0
102 1.3 tsutsui || fstat(fd, &sb) < 0
103 1.3 tsutsui || (sb.st_mode & S_IFMT) != S_IFDIR) {
104 1.3 tsutsui /* Path supplied isn't a directory, open parent
105 1.3 tsutsui directory and list matching files. */
106 1.3 tsutsui if (fd >= 0)
107 1.3 tsutsui close(fd);
108 1.3 tsutsui fname = strrchr(path, '/');
109 1.3 tsutsui if (fname) {
110 1.3 tsutsui size = fname - path;
111 1.4 tsutsui fname++;
112 1.3 tsutsui p = alloc(size + 1);
113 1.3 tsutsui if (!p)
114 1.3 tsutsui goto out;
115 1.3 tsutsui memcpy(p, path, size);
116 1.3 tsutsui p[size] = 0;
117 1.3 tsutsui fd = open(p, 0);
118 1.3 tsutsui dealloc(p, size + 1);
119 1.3 tsutsui } else {
120 1.3 tsutsui fd = open("", 0);
121 1.3 tsutsui fname = path;
122 1.3 tsutsui }
123 1.1 brezak
124 1.3 tsutsui if (fd < 0) {
125 1.3 tsutsui printf("ls: %s\n", strerror(errno));
126 1.3 tsutsui return;
127 1.1 brezak }
128 1.3 tsutsui if (fstat(fd, &sb) < 0) {
129 1.3 tsutsui printf("stat: %s\n", strerror(errno));
130 1.3 tsutsui goto out;
131 1.3 tsutsui }
132 1.3 tsutsui if ((sb.st_mode & S_IFMT) != S_IFDIR) {
133 1.3 tsutsui printf("%s: %s\n", path, strerror(ENOTDIR));
134 1.3 tsutsui goto out;
135 1.1 brezak }
136 1.1 brezak }
137 1.1 brezak
138 1.3 tsutsui f = &files[fd];
139 1.1 brezak
140 1.3 tsutsui #if !defined(LIBSA_NO_FD_CHECKING)
141 1.3 tsutsui if ((unsigned int)fd >= SOPEN_MAX || f->f_flags == 0) {
142 1.3 tsutsui errno = EBADF;
143 1.3 tsutsui goto out;
144 1.3 tsutsui }
145 1.3 tsutsui #endif
146 1.3 tsutsui
147 1.3 tsutsui #if !defined(LIBSA_NO_RAW_ACCESS)
148 1.3 tsutsui /* operation not defined on raw devices */
149 1.3 tsutsui if (f->f_flags & F_RAW) {
150 1.3 tsutsui errno = EOPNOTSUPP;
151 1.3 tsutsui goto out;
152 1.3 tsutsui }
153 1.3 tsutsui #endif
154 1.1 brezak
155 1.3 tsutsui if (FS_LS(f->f_ops) != NULL)
156 1.3 tsutsui FS_LS(f->f_ops)(f, fname);
157 1.3 tsutsui else
158 1.3 tsutsui printf("no ls support for this file system\n");
159 1.1 brezak
160 1.3 tsutsui out:
161 1.3 tsutsui close(fd);
162 1.1 brezak }
163