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