10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*8782Sro@techfak.uni-bielefeld.de * Common Development and Distribution License (the "License").
6*8782Sro@techfak.uni-bielefeld.de * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211384Smike_s
220Sstevel@tonic-gate /*
23*8782Sro@techfak.uni-bielefeld.de * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
241384Smike_s * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/mkdev.h>
330Sstevel@tonic-gate #include <ftw.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <stdlib.h>
361384Smike_s #include "stdusers.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate #define MAX_DEPTH 50
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*ARGSUSED2*/
410Sstevel@tonic-gate static int
visit_dir(const char * path,const struct stat * st,int file_type,struct FTW * ft)420Sstevel@tonic-gate visit_dir(const char *path, const struct stat *st,
430Sstevel@tonic-gate int file_type, struct FTW *ft)
440Sstevel@tonic-gate {
451384Smike_s const char *uid, *gid;
461384Smike_s char ftype;
470Sstevel@tonic-gate char symsrc[MAXPATHLEN];
480Sstevel@tonic-gate char buffer[MAXPATHLEN];
490Sstevel@tonic-gate char *abs_name;
500Sstevel@tonic-gate char name[MAXPATHLEN];
510Sstevel@tonic-gate char maj[10], min[10];
520Sstevel@tonic-gate char *p;
530Sstevel@tonic-gate int c;
540Sstevel@tonic-gate static int first_time = 1;
550Sstevel@tonic-gate int inum;
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * The first directory is the current directory '.',
59*8782Sro@techfak.uni-bielefeld.de * this is relevant in our protolist, so I throw it out.
600Sstevel@tonic-gate */
610Sstevel@tonic-gate if (first_time) {
620Sstevel@tonic-gate first_time = 0;
630Sstevel@tonic-gate if ((path[0] == '.') && (path[1] == '\0'))
640Sstevel@tonic-gate return (0);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate abs_name = (char *)(path + 2);
680Sstevel@tonic-gate maj[0] = min[0] = symsrc[0] = '-';
690Sstevel@tonic-gate maj[1] = min[1] = symsrc[1] = '\0';
700Sstevel@tonic-gate
710Sstevel@tonic-gate (void) strcpy(name, abs_name);
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate * is this a relocatable object? if so set
740Sstevel@tonic-gate * the symsrc appropriately.
750Sstevel@tonic-gate *
760Sstevel@tonic-gate * All relocatable objects start with /sun or /i86
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * eg:
790Sstevel@tonic-gate * /sun4d/kadb == kadb
800Sstevel@tonic-gate * /i86pc/kadb == kadb
810Sstevel@tonic-gate */
820Sstevel@tonic-gate #if defined(sparc)
830Sstevel@tonic-gate #define ARCH_STR "sun"
840Sstevel@tonic-gate #elif defined(i386)
850Sstevel@tonic-gate #define ARCH_STR "i86"
860Sstevel@tonic-gate #elif defined(__ppc)
870Sstevel@tonic-gate #define ARCH_STR "prep"
880Sstevel@tonic-gate #else
890Sstevel@tonic-gate #error "Unknown instruction set"
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate if (strncmp(abs_name, ARCH_STR, 3) == 0) {
920Sstevel@tonic-gate if (((st->st_mode & S_IFMT) == S_IFDIR) ||
930Sstevel@tonic-gate ((st->st_mode & S_IFMT) == S_IFLNK))
940Sstevel@tonic-gate return (0);
950Sstevel@tonic-gate
960Sstevel@tonic-gate (void) strcpy(buffer, abs_name);
970Sstevel@tonic-gate if (p = index(buffer, '/')) {
980Sstevel@tonic-gate (void) strcpy(symsrc, abs_name);
990Sstevel@tonic-gate *p++ = '\0';
1000Sstevel@tonic-gate (void) strcpy(name, p);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate switch (st->st_mode & S_IFMT) {
1050Sstevel@tonic-gate case S_IFCHR:
1060Sstevel@tonic-gate (void) sprintf(maj, "%ld", major(st->st_rdev));
1070Sstevel@tonic-gate (void) sprintf(min, "%ld", minor(st->st_rdev));
1080Sstevel@tonic-gate ftype = 'c';
1090Sstevel@tonic-gate break;
1100Sstevel@tonic-gate case S_IFDIR:
1110Sstevel@tonic-gate ftype = 'd';
1120Sstevel@tonic-gate break;
1130Sstevel@tonic-gate case S_IFBLK:
1140Sstevel@tonic-gate (void) sprintf(maj, "%ld", major(st->st_rdev));
1150Sstevel@tonic-gate (void) sprintf(min, "%ld", minor(st->st_rdev));
1160Sstevel@tonic-gate ftype = 'b';
1170Sstevel@tonic-gate break;
1180Sstevel@tonic-gate case S_IFREG:
1190Sstevel@tonic-gate ftype = 'f';
1200Sstevel@tonic-gate break;
1210Sstevel@tonic-gate case S_IFLNK:
1220Sstevel@tonic-gate if ((c = readlink(path, symsrc, MAXPATHLEN)) == -1)
1230Sstevel@tonic-gate perror("readlink");
1240Sstevel@tonic-gate symsrc[c] = '\0';
1250Sstevel@tonic-gate ftype = 's';
1260Sstevel@tonic-gate break;
1270Sstevel@tonic-gate default:
1280Sstevel@tonic-gate ftype = '?';
1290Sstevel@tonic-gate break;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1321384Smike_s uid = stdfindbyvalue(st->st_uid, usernames);
1331384Smike_s if (uid == NULL)
1340Sstevel@tonic-gate uid = "NO_SUCH_UID";
1350Sstevel@tonic-gate
1361384Smike_s gid = stdfindbyvalue(st->st_gid, groupnames);
1371384Smike_s if (gid == NULL)
1380Sstevel@tonic-gate gid = "NO_SUCH_GID";
1390Sstevel@tonic-gate if (st->st_nlink == 1)
1400Sstevel@tonic-gate inum = 0;
1410Sstevel@tonic-gate else
1420Sstevel@tonic-gate inum = st->st_ino;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate (void) printf("%c %-30s %-20s %4lo %-5s %-5s %6d %2ld %2s %2s\n",
145*8782Sro@techfak.uni-bielefeld.de ftype, name, symsrc, st->st_mode % 010000, uid, gid,
146*8782Sro@techfak.uni-bielefeld.de inum, st->st_nlink, maj, min);
1470Sstevel@tonic-gate return (0);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate int
main(int argc,char * argv[])1510Sstevel@tonic-gate main(int argc, char *argv[])
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate if (argc != 2) {
1550Sstevel@tonic-gate (void) fprintf(stderr, "usage: protolist <protodir>\n");
1560Sstevel@tonic-gate exit(1);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (chdir(argv[1]) < 0) {
1600Sstevel@tonic-gate perror("chdir");
1610Sstevel@tonic-gate exit(1);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (nftw(".", visit_dir, MAX_DEPTH, FTW_PHYS) != 0) {
1650Sstevel@tonic-gate perror("nftw");
1660Sstevel@tonic-gate exit(1);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate return (0);
1700Sstevel@tonic-gate }
171