17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*299d957dSRainer Orth  * Common Development and Distribution License (the "License").
6*299d957dSRainer Orth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210adc1619Smike_s 
227c478bd9Sstevel@tonic-gate /*
23*299d957dSRainer Orth  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240adc1619Smike_s  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <sys/stat.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
337c478bd9Sstevel@tonic-gate #include <ftw.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
360adc1619Smike_s #include "stdusers.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	MAX_DEPTH	50
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
417c478bd9Sstevel@tonic-gate static int
visit_dir(const char * path,const struct stat * st,int file_type,struct FTW * ft)427c478bd9Sstevel@tonic-gate visit_dir(const char *path, const struct stat *st,
437c478bd9Sstevel@tonic-gate 	int file_type, struct FTW *ft)
447c478bd9Sstevel@tonic-gate {
450adc1619Smike_s 	const char	*uid, *gid;
460adc1619Smike_s 	char	ftype;
477c478bd9Sstevel@tonic-gate 	char	symsrc[MAXPATHLEN];
487c478bd9Sstevel@tonic-gate 	char	buffer[MAXPATHLEN];
497c478bd9Sstevel@tonic-gate 	char	*abs_name;
507c478bd9Sstevel@tonic-gate 	char	name[MAXPATHLEN];
517c478bd9Sstevel@tonic-gate 	char	maj[10], min[10];
527c478bd9Sstevel@tonic-gate 	char	*p;
537c478bd9Sstevel@tonic-gate 	int	c;
547c478bd9Sstevel@tonic-gate 	static int first_time = 1;
557c478bd9Sstevel@tonic-gate 	int	inum;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/*
587c478bd9Sstevel@tonic-gate 	 * The first directory is the current directory '.',
59*299d957dSRainer Orth 	 * this is relevant in our protolist, so I throw it out.
607c478bd9Sstevel@tonic-gate 	 */
617c478bd9Sstevel@tonic-gate 	if (first_time) {
627c478bd9Sstevel@tonic-gate 		first_time = 0;
637c478bd9Sstevel@tonic-gate 		if ((path[0] == '.') && (path[1] == '\0'))
647c478bd9Sstevel@tonic-gate 			return (0);
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	abs_name = (char *)(path + 2);
687c478bd9Sstevel@tonic-gate 	maj[0] = min[0] = symsrc[0] = '-';
697c478bd9Sstevel@tonic-gate 	maj[1] = min[1] = symsrc[1] = '\0';
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	(void) strcpy(name, abs_name);
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * is this a relocatable object?  if so set
747c478bd9Sstevel@tonic-gate 	 * the symsrc appropriately.
757c478bd9Sstevel@tonic-gate 	 *
767c478bd9Sstevel@tonic-gate 	 * All relocatable objects start with /sun or /i86
777c478bd9Sstevel@tonic-gate 	 *
787c478bd9Sstevel@tonic-gate 	 * eg:
797c478bd9Sstevel@tonic-gate 	 *    /sun4d/kadb == kadb
807c478bd9Sstevel@tonic-gate 	 *    /i86pc/kadb == kadb
817c478bd9Sstevel@tonic-gate 	 */
827c478bd9Sstevel@tonic-gate #if defined(sparc)
837c478bd9Sstevel@tonic-gate #define	ARCH_STR "sun"
847c478bd9Sstevel@tonic-gate #elif defined(i386)
857c478bd9Sstevel@tonic-gate #define	ARCH_STR "i86"
867c478bd9Sstevel@tonic-gate #elif defined(__ppc)
877c478bd9Sstevel@tonic-gate #define	ARCH_STR "prep"
887c478bd9Sstevel@tonic-gate #else
897c478bd9Sstevel@tonic-gate #error "Unknown instruction set"
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 	if (strncmp(abs_name, ARCH_STR, 3) == 0) {
927c478bd9Sstevel@tonic-gate 		if (((st->st_mode & S_IFMT) == S_IFDIR) ||
937c478bd9Sstevel@tonic-gate 		    ((st->st_mode & S_IFMT) == S_IFLNK))
947c478bd9Sstevel@tonic-gate 			return (0);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		(void) strcpy(buffer, abs_name);
977c478bd9Sstevel@tonic-gate 		if (p = index(buffer, '/')) {
987c478bd9Sstevel@tonic-gate 			(void) strcpy(symsrc, abs_name);
997c478bd9Sstevel@tonic-gate 			*p++ = '\0';
1007c478bd9Sstevel@tonic-gate 			(void) strcpy(name, p);
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	switch (st->st_mode & S_IFMT) {
1057c478bd9Sstevel@tonic-gate 	case S_IFCHR:
1067c478bd9Sstevel@tonic-gate 		(void) sprintf(maj, "%ld", major(st->st_rdev));
1077c478bd9Sstevel@tonic-gate 		(void) sprintf(min, "%ld", minor(st->st_rdev));
1087c478bd9Sstevel@tonic-gate 		ftype = 'c';
1097c478bd9Sstevel@tonic-gate 		break;
1107c478bd9Sstevel@tonic-gate 	case S_IFDIR:
1117c478bd9Sstevel@tonic-gate 		ftype = 'd';
1127c478bd9Sstevel@tonic-gate 		break;
1137c478bd9Sstevel@tonic-gate 	case S_IFBLK:
1147c478bd9Sstevel@tonic-gate 		(void) sprintf(maj, "%ld", major(st->st_rdev));
1157c478bd9Sstevel@tonic-gate 		(void) sprintf(min, "%ld", minor(st->st_rdev));
1167c478bd9Sstevel@tonic-gate 		ftype = 'b';
1177c478bd9Sstevel@tonic-gate 		break;
1187c478bd9Sstevel@tonic-gate 	case S_IFREG:
1197c478bd9Sstevel@tonic-gate 		ftype = 'f';
1207c478bd9Sstevel@tonic-gate 		break;
1217c478bd9Sstevel@tonic-gate 	case S_IFLNK:
1227c478bd9Sstevel@tonic-gate 		if ((c = readlink(path, symsrc, MAXPATHLEN)) == -1)
1237c478bd9Sstevel@tonic-gate 			perror("readlink");
1247c478bd9Sstevel@tonic-gate 		symsrc[c] = '\0';
1257c478bd9Sstevel@tonic-gate 		ftype = 's';
1267c478bd9Sstevel@tonic-gate 		break;
1277c478bd9Sstevel@tonic-gate 	default:
1287c478bd9Sstevel@tonic-gate 		ftype = '?';
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1320adc1619Smike_s 	uid = stdfindbyvalue(st->st_uid, usernames);
1330adc1619Smike_s 	if (uid == NULL)
1347c478bd9Sstevel@tonic-gate 		uid = "NO_SUCH_UID";
1357c478bd9Sstevel@tonic-gate 
1360adc1619Smike_s 	gid = stdfindbyvalue(st->st_gid, groupnames);
1370adc1619Smike_s 	if (gid == NULL)
1387c478bd9Sstevel@tonic-gate 		gid = "NO_SUCH_GID";
1397c478bd9Sstevel@tonic-gate 	if (st->st_nlink == 1)
1407c478bd9Sstevel@tonic-gate 		inum = 0;
1417c478bd9Sstevel@tonic-gate 	else
1427c478bd9Sstevel@tonic-gate 		inum = st->st_ino;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	(void) printf("%c %-30s %-20s %4lo %-5s %-5s %6d %2ld %2s %2s\n",
145*299d957dSRainer Orth 	    ftype, name, symsrc, st->st_mode % 010000, uid, gid,
146*299d957dSRainer Orth 	    inum, st->st_nlink, maj, min);
1477c478bd9Sstevel@tonic-gate 	return (0);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1517c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (argc != 2) {
1557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: protolist <protodir>\n");
1567c478bd9Sstevel@tonic-gate 		exit(1);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (chdir(argv[1]) < 0) {
1607c478bd9Sstevel@tonic-gate 		perror("chdir");
1617c478bd9Sstevel@tonic-gate 		exit(1);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (nftw(".", visit_dir, MAX_DEPTH, FTW_PHYS) != 0) {
1657c478bd9Sstevel@tonic-gate 		perror("nftw");
1667c478bd9Sstevel@tonic-gate 		exit(1);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (0);
1707c478bd9Sstevel@tonic-gate }
171