xref: /minix3/sys/lib/libsa/ls.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* $NetBSD: ls.c,v 1.4 2012/03/02 12:08:44 tsutsui Exp $ */
258a2b000SEvgeniy Ivanov 
358a2b000SEvgeniy Ivanov /*-
458a2b000SEvgeniy Ivanov  * Copyright (c) 2011
558a2b000SEvgeniy Ivanov  *      The NetBSD Foundation, Inc. All rights reserved.
658a2b000SEvgeniy Ivanov  *
758a2b000SEvgeniy Ivanov  * This code is derived from software contributed to The NetBSD Foundation
858a2b000SEvgeniy Ivanov  * by Martin Husemann.
958a2b000SEvgeniy Ivanov  *
1058a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
1158a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
1258a2b000SEvgeniy Ivanov  * are met:
1358a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
1458a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
1558a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
1658a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
1758a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
1858a2b000SEvgeniy Ivanov  *
1958a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2058a2b000SEvgeniy Ivanov  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2158a2b000SEvgeniy Ivanov  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2258a2b000SEvgeniy Ivanov  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2358a2b000SEvgeniy Ivanov  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2458a2b000SEvgeniy Ivanov  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2558a2b000SEvgeniy Ivanov  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2658a2b000SEvgeniy Ivanov  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2758a2b000SEvgeniy Ivanov  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2858a2b000SEvgeniy Ivanov  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2958a2b000SEvgeniy Ivanov  * POSSIBILITY OF SUCH DAMAGE.
3058a2b000SEvgeniy Ivanov  */
3158a2b000SEvgeniy Ivanov 
3258a2b000SEvgeniy Ivanov /*
3358a2b000SEvgeniy Ivanov  * Copyright (c) 1993
3458a2b000SEvgeniy Ivanov  *	The Regents of the University of California.  All rights reserved.
3558a2b000SEvgeniy Ivanov  *
3658a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
3758a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
3858a2b000SEvgeniy Ivanov  * are met:
3958a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
4058a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
4158a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
4258a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
4358a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
4458a2b000SEvgeniy Ivanov  * 3. Neither the name of the University nor the names of its contributors
4558a2b000SEvgeniy Ivanov  *    may be used to endorse or promote products derived from this software
4658a2b000SEvgeniy Ivanov  *    without specific prior written permission.
4758a2b000SEvgeniy Ivanov  *
4858a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4958a2b000SEvgeniy Ivanov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5058a2b000SEvgeniy Ivanov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5158a2b000SEvgeniy Ivanov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5258a2b000SEvgeniy Ivanov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5358a2b000SEvgeniy Ivanov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5458a2b000SEvgeniy Ivanov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5558a2b000SEvgeniy Ivanov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5658a2b000SEvgeniy Ivanov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5758a2b000SEvgeniy Ivanov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5858a2b000SEvgeniy Ivanov  * SUCH DAMAGE.
5958a2b000SEvgeniy Ivanov  */
6058a2b000SEvgeniy Ivanov 
6158a2b000SEvgeniy Ivanov /*
6258a2b000SEvgeniy Ivanov  * Copyright (c) 1996
6358a2b000SEvgeniy Ivanov  *	Matthias Drochner.  All rights reserved.
6458a2b000SEvgeniy Ivanov  *
6558a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
6658a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
6758a2b000SEvgeniy Ivanov  * are met:
6858a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
6958a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
7058a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
7158a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
7258a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
7358a2b000SEvgeniy Ivanov  *
7458a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7558a2b000SEvgeniy Ivanov  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7658a2b000SEvgeniy Ivanov  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
7758a2b000SEvgeniy Ivanov  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
7858a2b000SEvgeniy Ivanov  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7958a2b000SEvgeniy Ivanov  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8058a2b000SEvgeniy Ivanov  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8158a2b000SEvgeniy Ivanov  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8258a2b000SEvgeniy Ivanov  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8358a2b000SEvgeniy Ivanov  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8458a2b000SEvgeniy Ivanov  */
8558a2b000SEvgeniy Ivanov 
8658a2b000SEvgeniy Ivanov 
8758a2b000SEvgeniy Ivanov #include "stand.h"
8858a2b000SEvgeniy Ivanov #include <sys/stat.h>
8958a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
9058a2b000SEvgeniy Ivanov 
9158a2b000SEvgeniy Ivanov void
9260223321SEvgeniy Ivanov ls(const char *path, void (*funcp)(char* arg))
9358a2b000SEvgeniy Ivanov {
9458a2b000SEvgeniy Ivanov 	int             fd;
9558a2b000SEvgeniy Ivanov 	struct stat     sb;
96*84d9c625SLionel Sambuc 	size_t          size = -1;
9758a2b000SEvgeniy Ivanov 	const char	*fname = 0;
9860223321SEvgeniy Ivanov 	char		*p = NULL;
9958a2b000SEvgeniy Ivanov 	struct open_file *f;
10058a2b000SEvgeniy Ivanov 
10158a2b000SEvgeniy Ivanov 	if ((fd = open(path, 0)) < 0
10258a2b000SEvgeniy Ivanov 	    || fstat(fd, &sb) < 0
10358a2b000SEvgeniy Ivanov 	    || (sb.st_mode & S_IFMT) != S_IFDIR) {
10458a2b000SEvgeniy Ivanov 		/* Path supplied isn't a directory, open parent
10558a2b000SEvgeniy Ivanov 		   directory and list matching files. */
10658a2b000SEvgeniy Ivanov 		if (fd >= 0)
10758a2b000SEvgeniy Ivanov 			close(fd);
10858a2b000SEvgeniy Ivanov 		fname = strrchr(path, '/');
10958a2b000SEvgeniy Ivanov 		if (fname) {
11058a2b000SEvgeniy Ivanov 			size = fname - path;
111f119e637SEvgeniy Ivanov 			fname++;
11258a2b000SEvgeniy Ivanov 			p = alloc(size + 1);
11358a2b000SEvgeniy Ivanov 			if (!p)
11458a2b000SEvgeniy Ivanov 				goto out;
11558a2b000SEvgeniy Ivanov 			memcpy(p, path, size);
11658a2b000SEvgeniy Ivanov 			p[size] = 0;
11758a2b000SEvgeniy Ivanov 			fd = open(p, 0);
11858a2b000SEvgeniy Ivanov 		} else {
11958a2b000SEvgeniy Ivanov 			fd = open("", 0);
12058a2b000SEvgeniy Ivanov 			fname = path;
12158a2b000SEvgeniy Ivanov 		}
12258a2b000SEvgeniy Ivanov 
12358a2b000SEvgeniy Ivanov 		if (fd < 0) {
12458a2b000SEvgeniy Ivanov 			printf("ls: %s\n", strerror(errno));
12558a2b000SEvgeniy Ivanov 			return;
12658a2b000SEvgeniy Ivanov 		}
12758a2b000SEvgeniy Ivanov 		if (fstat(fd, &sb) < 0) {
12858a2b000SEvgeniy Ivanov 			printf("stat: %s\n", strerror(errno));
12958a2b000SEvgeniy Ivanov 			goto out;
13058a2b000SEvgeniy Ivanov 		}
13158a2b000SEvgeniy Ivanov 		if ((sb.st_mode & S_IFMT) != S_IFDIR) {
13258a2b000SEvgeniy Ivanov 			printf("%s: %s\n", path, strerror(ENOTDIR));
13358a2b000SEvgeniy Ivanov 			goto out;
13458a2b000SEvgeniy Ivanov 		}
13558a2b000SEvgeniy Ivanov 	}
13658a2b000SEvgeniy Ivanov 
13758a2b000SEvgeniy Ivanov 	f = &files[fd];
13858a2b000SEvgeniy Ivanov 
13958a2b000SEvgeniy Ivanov #if !defined(LIBSA_NO_FD_CHECKING)
14058a2b000SEvgeniy Ivanov 	if ((unsigned int)fd >= SOPEN_MAX || f->f_flags == 0) {
14158a2b000SEvgeniy Ivanov 		errno = EBADF;
14258a2b000SEvgeniy Ivanov 		goto out;
14358a2b000SEvgeniy Ivanov 	}
14458a2b000SEvgeniy Ivanov #endif
14558a2b000SEvgeniy Ivanov 
14658a2b000SEvgeniy Ivanov #if !defined(LIBSA_NO_RAW_ACCESS)
14758a2b000SEvgeniy Ivanov 	/* operation not defined on raw devices */
14858a2b000SEvgeniy Ivanov 	if (f->f_flags & F_RAW) {
14958a2b000SEvgeniy Ivanov 		errno = EOPNOTSUPP;
15058a2b000SEvgeniy Ivanov 		goto out;
15158a2b000SEvgeniy Ivanov 	}
15258a2b000SEvgeniy Ivanov #endif
15358a2b000SEvgeniy Ivanov 
15458a2b000SEvgeniy Ivanov 	if (FS_LS(f->f_ops) != NULL)
15560223321SEvgeniy Ivanov 		FS_LS(f->f_ops)(f, fname, funcp, p);
15658a2b000SEvgeniy Ivanov 	else
15758a2b000SEvgeniy Ivanov 		printf("no ls support for this file system\n");
15858a2b000SEvgeniy Ivanov 
15958a2b000SEvgeniy Ivanov out:
160f14fb602SLionel Sambuc 	/* LSC: MINIX Modification for correct glob support, beware! */
16160223321SEvgeniy Ivanov 	if (p != NULL)
16260223321SEvgeniy Ivanov 		dealloc(p, size + 1);
16358a2b000SEvgeniy Ivanov 	close(fd);
16458a2b000SEvgeniy Ivanov }
165