xref: /titanic_53/usr/src/cmd/allocate/mkdevalloc.c (revision 7883e825152992a0f20c3f52f7c4bb902a56205c)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*7883e825Spaulson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * scan /dev directory for mountable objects and construct device_allocate
317c478bd9Sstevel@tonic-gate  * file for allocate....
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * devices are:
347c478bd9Sstevel@tonic-gate  *	tape (cartridge)
357c478bd9Sstevel@tonic-gate  *		/dev/rst*
367c478bd9Sstevel@tonic-gate  *		/dev/nrst*
377c478bd9Sstevel@tonic-gate  *		/dev/rmt/...
387c478bd9Sstevel@tonic-gate  *	audio
397c478bd9Sstevel@tonic-gate  *		/dev/audio
407c478bd9Sstevel@tonic-gate  *		/dev/audioctl
417c478bd9Sstevel@tonic-gate  *		/dev/sound/...
427c478bd9Sstevel@tonic-gate  *	floppy
437c478bd9Sstevel@tonic-gate  *		/dev/diskette
447c478bd9Sstevel@tonic-gate  *		/dev/fd*
457c478bd9Sstevel@tonic-gate  *		/dev/rdiskette
467c478bd9Sstevel@tonic-gate  *		/dev/rfd*
477c478bd9Sstevel@tonic-gate  *	CD
487c478bd9Sstevel@tonic-gate  *		/dev/sr*
497c478bd9Sstevel@tonic-gate  *		/dev/nsr*
507c478bd9Sstevel@tonic-gate  *		/dev/dsk/c?t?d0s?
517c478bd9Sstevel@tonic-gate  *		/dev/rdsk/c?t?d0s?
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/types.h>	/* for stat(2), etc. */
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <dirent.h>	/* for readdir(3), etc. */
577c478bd9Sstevel@tonic-gate #include <unistd.h>	/* for readlink(2) */
587c478bd9Sstevel@tonic-gate #include <string.h>	/* for strcpy(3), etc. */
597c478bd9Sstevel@tonic-gate #include <strings.h>	/* for bcopy(3C), etc. */
607c478bd9Sstevel@tonic-gate #include <stdio.h>	/* for perror(3) */
617c478bd9Sstevel@tonic-gate #include <stdlib.h>	/* for atoi(3) */
627c478bd9Sstevel@tonic-gate #include <locale.h>
637c478bd9Sstevel@tonic-gate #include <libintl.h>
647c478bd9Sstevel@tonic-gate #include <auth_attr.h>
657c478bd9Sstevel@tonic-gate #include <auth_list.h>
667c478bd9Sstevel@tonic-gate #include "allocate.h"   /* for SECLIB */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
697c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	DELTA	5	/* array size delta when full */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* "/dev/rst...", "/dev/nrst...", "/dev/rmt/..." */
757c478bd9Sstevel@tonic-gate struct tape {
767c478bd9Sstevel@tonic-gate 	char	*name;
777c478bd9Sstevel@tonic-gate 	char	*device;
787c478bd9Sstevel@tonic-gate 	int	number;
797c478bd9Sstevel@tonic-gate } *tape;
807c478bd9Sstevel@tonic-gate #define	DFLT_NTAPE  10		/* size of initial array */
817c478bd9Sstevel@tonic-gate #define	SIZE_OF_RST  3		/* |rmt| */
827c478bd9Sstevel@tonic-gate #define	SIZE_OF_NRST 4		/* |nrmt| */
837c478bd9Sstevel@tonic-gate #define	SIZE_OF_TMP  4		/* |/tmp| */
847c478bd9Sstevel@tonic-gate #define	SIZE_OF_RMT  8		/* |/dev/rmt| */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* "/dev/audio", "/dev/audioctl", "/dev/sound/..." */
877c478bd9Sstevel@tonic-gate struct audio {
887c478bd9Sstevel@tonic-gate 	char	*name;
897c478bd9Sstevel@tonic-gate 	char	*device;
907c478bd9Sstevel@tonic-gate 	int	number;
917c478bd9Sstevel@tonic-gate } *audio;
927c478bd9Sstevel@tonic-gate #define	DFLT_NAUDIO   10	/* size of initial array */
937c478bd9Sstevel@tonic-gate #define	SIZE_OF_SOUND 10	/* |/dev/sound| */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /* "/dev/sr", "/dev/nsr", "/dev/dsk/c?t?d0s?", "/dev/rdsk/c?t?d0s?" */
967c478bd9Sstevel@tonic-gate struct cd {
977c478bd9Sstevel@tonic-gate 	char	*name;
987c478bd9Sstevel@tonic-gate 	char	*device;
997c478bd9Sstevel@tonic-gate 	int	id;
1007c478bd9Sstevel@tonic-gate 	int	controller;
1017c478bd9Sstevel@tonic-gate 	int	number;
1027c478bd9Sstevel@tonic-gate } *cd;
1037c478bd9Sstevel@tonic-gate #define	DFLT_NCD    10		/* size of initial array */
1047c478bd9Sstevel@tonic-gate #define	SIZE_OF_SR   2		/* |sr| */
1057c478bd9Sstevel@tonic-gate #define	SIZE_OF_RSR  3		/* |rsr| */
1067c478bd9Sstevel@tonic-gate #define	SIZE_OF_DSK  8		/* |/dev/dsk| */
1077c478bd9Sstevel@tonic-gate #define	SIZE_OF_RDSK 9		/* |/dev/rdsk| */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* "/dev/fd0*", "/dev/rfd0*", "/dev/fd1*", "/dev/rfd1*" */
1117c478bd9Sstevel@tonic-gate struct fp {
1127c478bd9Sstevel@tonic-gate 	char *name;
1137c478bd9Sstevel@tonic-gate 	char *device;
1147c478bd9Sstevel@tonic-gate 	int number;
1157c478bd9Sstevel@tonic-gate } *fp;
1167c478bd9Sstevel@tonic-gate #define	DFLT_NFP    10		/* size of initial array */
1177c478bd9Sstevel@tonic-gate #define	SIZE_OF_FD0  3		/* |fd0| */
1187c478bd9Sstevel@tonic-gate #define	SIZE_OF_RFD0 4		/* |rfd0| */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static void dotape();
1217c478bd9Sstevel@tonic-gate static void doaudio();
1227c478bd9Sstevel@tonic-gate static void dofloppy();
1237c478bd9Sstevel@tonic-gate static void docd();
1247c478bd9Sstevel@tonic-gate static void initmem();
1257c478bd9Sstevel@tonic-gate static int  expandmem(int, void **, int);
1267c478bd9Sstevel@tonic-gate static void no_memory(void);
1277c478bd9Sstevel@tonic-gate 
128*7883e825Spaulson int
129*7883e825Spaulson main(void)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1327c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	initmem();		/* initialize memory */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	dotape();		/* do tape */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	doaudio();		/* do audio */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	dofloppy();		/* do floppy */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	docd();			/* do cd */
143*7883e825Spaulson 
144*7883e825Spaulson 	return (0);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static void
1487c478bd9Sstevel@tonic-gate dotape()
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	DIR *dirp;
1517c478bd9Sstevel@tonic-gate 	struct dirent *dep;	/* directory entry pointer */
1527c478bd9Sstevel@tonic-gate 	int	i, j, n;
1537c478bd9Sstevel@tonic-gate 	char	*nm;		/* name/device of special device */
1547c478bd9Sstevel@tonic-gate 	char	linkvalue[2048];	/* symlink value */
1557c478bd9Sstevel@tonic-gate 	struct stat stat;	/* determine if it's a symlink */
1567c478bd9Sstevel@tonic-gate 	int	sz;		/* size of symlink value */
1577c478bd9Sstevel@tonic-gate 	char	*cp;		/* pointer into string */
1587c478bd9Sstevel@tonic-gate 	int	ntape;		/* max array size */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	ntape = DFLT_NTAPE;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * look for rst* and nrst*
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev")) == NULL) {
1677c478bd9Sstevel@tonic-gate 		perror(gettext("open /dev failure"));
1687c478bd9Sstevel@tonic-gate 		exit(1);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	i = 0;
1727c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
1737c478bd9Sstevel@tonic-gate 		/* ignore if neither rst* nor nrst* */
1747c478bd9Sstevel@tonic-gate 		if (strncmp(dep->d_name, "rst", SIZE_OF_RST) &&
1757c478bd9Sstevel@tonic-gate 		    strncmp(dep->d_name, "nrst", SIZE_OF_NRST))
1767c478bd9Sstevel@tonic-gate 			continue;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
1797c478bd9Sstevel@tonic-gate 		if (i == ntape) {
1807c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
1817c478bd9Sstevel@tonic-gate 			ntape = expandmem(i, (void **)&tape,
1827c478bd9Sstevel@tonic-gate 					sizeof (struct tape));
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		/* save name (/dev + / + d_name + \0) */
1867c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
1877c478bd9Sstevel@tonic-gate 		if (nm == NULL)
1887c478bd9Sstevel@tonic-gate 			no_memory();
1897c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/");
1907c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
1917c478bd9Sstevel@tonic-gate 		tape[i].name = nm;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		/* ignore if not symbolic link (note i not incremented) */
1947c478bd9Sstevel@tonic-gate 		if (lstat(tape[i].name, &stat) < 0) {
1957c478bd9Sstevel@tonic-gate 			perror("stat(2) failed ");
1967c478bd9Sstevel@tonic-gate 			exit(1);
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 		if ((stat.st_mode & S_IFMT) != S_IFLNK)
1997c478bd9Sstevel@tonic-gate 			continue;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		/* get name from symbolic link */
2027c478bd9Sstevel@tonic-gate 		if ((sz = readlink(tape[i].name, linkvalue,
2037c478bd9Sstevel@tonic-gate 				sizeof (linkvalue))) < 0)
2047c478bd9Sstevel@tonic-gate 			continue;
2057c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(sz + 1);
2067c478bd9Sstevel@tonic-gate 		if (nm == NULL)
2077c478bd9Sstevel@tonic-gate 			no_memory();
2087c478bd9Sstevel@tonic-gate 		(void) strncpy(nm, linkvalue, sz);
2097c478bd9Sstevel@tonic-gate 		nm[sz] = '\0';
2107c478bd9Sstevel@tonic-gate 		tape[i].device = nm;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/* get device number */
2137c478bd9Sstevel@tonic-gate 		cp = strrchr(tape[i].device, '/');
2147c478bd9Sstevel@tonic-gate 		cp++;				/* advance to device # */
2157c478bd9Sstevel@tonic-gate 		(void) sscanf(cp, "%d", &tape[i].number);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		i++;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * scan /dev/rmt and add entry to table
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev/rmt")) == NULL) {
2277c478bd9Sstevel@tonic-gate 		perror(gettext("open /dev failure"));
2287c478bd9Sstevel@tonic-gate 		exit(1);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
2327c478bd9Sstevel@tonic-gate 		/* skip . .. etc... */
2337c478bd9Sstevel@tonic-gate 		if (strncmp(dep->d_name, ".", 1) == NULL)
2347c478bd9Sstevel@tonic-gate 			continue;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
2377c478bd9Sstevel@tonic-gate 		if (i == ntape) {
2387c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
2397c478bd9Sstevel@tonic-gate 			ntape = expandmem(i, (void **)&tape,
2407c478bd9Sstevel@tonic-gate 					sizeof (struct tape));
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		/* save name (/dev/rmt + / + d_name + \0) */
2447c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_RMT + 1 + strlen(dep->d_name) + 1);
2457c478bd9Sstevel@tonic-gate 		if (nm == NULL)
2467c478bd9Sstevel@tonic-gate 			no_memory();
2477c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/rmt/");
2487c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
2497c478bd9Sstevel@tonic-gate 		tape[i].name = nm;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		/* save device name (rmt/ + d_name + \0) */
2527c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_TMP + strlen(dep->d_name) + 1);
2537c478bd9Sstevel@tonic-gate 		if (nm == NULL)
2547c478bd9Sstevel@tonic-gate 			no_memory();
2557c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "rmt/");
2567c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
2577c478bd9Sstevel@tonic-gate 		tape[i].device = nm;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		(void) sscanf(dep->d_name, "%d", &tape[i].number);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		i++;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 	n = i;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* remove duplicate entries */
2687c478bd9Sstevel@tonic-gate 	for (i = 0; i < n - 1; i++) {
2697c478bd9Sstevel@tonic-gate 		for (j = i + 1; j < n; j++) {
2707c478bd9Sstevel@tonic-gate 			if (strcmp(tape[i].device, tape[j].device))
2717c478bd9Sstevel@tonic-gate 				continue;
2727c478bd9Sstevel@tonic-gate 			tape[j].number = -1;
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* print out device_allocate entries for tape devices */
2777c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
2787c478bd9Sstevel@tonic-gate 		for (j = 0; j < n; j++) {
2797c478bd9Sstevel@tonic-gate 			if (tape[j].number == i) {
2807c478bd9Sstevel@tonic-gate 				(void) printf(
2817c478bd9Sstevel@tonic-gate 					"st%d;st;reserved;reserved;%s;",
2827c478bd9Sstevel@tonic-gate 					i, DEFAULT_DEV_ALLOC_AUTH);
2837c478bd9Sstevel@tonic-gate 				(void) printf("%s%s\n", SECLIB, "/st_clean");
2847c478bd9Sstevel@tonic-gate 				break;
2857c478bd9Sstevel@tonic-gate 			}
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static void
2917c478bd9Sstevel@tonic-gate doaudio()
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	DIR *dirp;
2947c478bd9Sstevel@tonic-gate 	struct dirent *dep;	/* directory entry pointer */
2957c478bd9Sstevel@tonic-gate 	int	i, j, n;
2967c478bd9Sstevel@tonic-gate 	char	*nm;		/* name/device of special device */
2977c478bd9Sstevel@tonic-gate 	char	linkvalue[2048];	/* symlink value */
2987c478bd9Sstevel@tonic-gate 	struct stat stat;	/* determine if it's a symlink */
2997c478bd9Sstevel@tonic-gate 	int	sz;		/* size of symlink value */
3007c478bd9Sstevel@tonic-gate 	char	*cp;		/* pointer into string */
3017c478bd9Sstevel@tonic-gate 	int	naudio;		/* max array size */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	naudio = DFLT_NAUDIO;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev")) == NULL) {
3067c478bd9Sstevel@tonic-gate 		perror(gettext("open /dev failure"));
3077c478bd9Sstevel@tonic-gate 		exit(1);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	i = 0;
3117c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
3127c478bd9Sstevel@tonic-gate 		if (strcmp(dep->d_name, "audio") &&
3137c478bd9Sstevel@tonic-gate 		    strcmp(dep->d_name, "audioctl"))
3147c478bd9Sstevel@tonic-gate 			continue;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
3177c478bd9Sstevel@tonic-gate 		if (i == naudio) {
3187c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
3197c478bd9Sstevel@tonic-gate 			naudio = expandmem(i, (void **)&audio,
3207c478bd9Sstevel@tonic-gate 					sizeof (struct audio));
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		/* save name (/dev + 1 + d_name + \0) */
3247c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
3257c478bd9Sstevel@tonic-gate 		if (nm == NULL)
3267c478bd9Sstevel@tonic-gate 			no_memory();
3277c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/");
3287c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
3297c478bd9Sstevel@tonic-gate 		audio[i].name = nm;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		/* ignore if not symbolic link (note i not incremented) */
3327c478bd9Sstevel@tonic-gate 		if (lstat(audio[i].name, &stat) < 0) {
3337c478bd9Sstevel@tonic-gate 			perror(gettext("stat(2) failed "));
3347c478bd9Sstevel@tonic-gate 			exit(1);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		if ((stat.st_mode & S_IFMT) != S_IFLNK)
3377c478bd9Sstevel@tonic-gate 			continue;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		/* get name from symbolic link */
3407c478bd9Sstevel@tonic-gate 		if ((sz = readlink(audio[i].name, linkvalue,
3417c478bd9Sstevel@tonic-gate 				sizeof (linkvalue))) < 0)
3427c478bd9Sstevel@tonic-gate 			continue;
3437c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(sz + 1);
3447c478bd9Sstevel@tonic-gate 		if (nm == NULL)
3457c478bd9Sstevel@tonic-gate 			no_memory();
3467c478bd9Sstevel@tonic-gate 		(void) strncpy(nm, linkvalue, sz);
3477c478bd9Sstevel@tonic-gate 		nm[sz] = '\0';
3487c478bd9Sstevel@tonic-gate 		audio[i].device = nm;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		cp = strrchr(audio[i].device, '/');
3517c478bd9Sstevel@tonic-gate 		cp++;				/* advance to device # */
3527c478bd9Sstevel@tonic-gate 		(void) sscanf(cp, "%d", &audio[i].number);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		i++;
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev/sound")) == NULL) {
3607c478bd9Sstevel@tonic-gate 		goto skip;
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
3647c478bd9Sstevel@tonic-gate 		/* skip . .. etc... */
3657c478bd9Sstevel@tonic-gate 		if (strncmp(dep->d_name, ".", 1) == NULL)
3667c478bd9Sstevel@tonic-gate 			continue;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
3697c478bd9Sstevel@tonic-gate 		if (i == naudio) {
3707c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
3717c478bd9Sstevel@tonic-gate 			naudio = expandmem(i, (void **)&audio,
3727c478bd9Sstevel@tonic-gate 					sizeof (struct audio));
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		/* save name (/dev/sound + / + d_name + \0) */
3767c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_SOUND + 1 +
3777c478bd9Sstevel@tonic-gate 		    strlen(dep->d_name) + 1);
3787c478bd9Sstevel@tonic-gate 		if (nm == NULL)
3797c478bd9Sstevel@tonic-gate 			no_memory();
3807c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/sound/");
3817c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
3827c478bd9Sstevel@tonic-gate 		audio[i].name = nm;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_SOUND + 1 +
3857c478bd9Sstevel@tonic-gate 		    strlen(dep->d_name) + 1);
3867c478bd9Sstevel@tonic-gate 		if (nm == NULL)
3877c478bd9Sstevel@tonic-gate 			no_memory();
3887c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/sound/");
3897c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
3907c478bd9Sstevel@tonic-gate 		audio[i].device = nm;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		(void) sscanf(dep->d_name, "%d", &audio[i].number);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		i++;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate skip:
4007c478bd9Sstevel@tonic-gate 	n = i;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/* remove duplicate entries */
4037c478bd9Sstevel@tonic-gate 	for (i = 0; i < n - 1; i++) {
4047c478bd9Sstevel@tonic-gate 		for (j = i + 1; j < n; j++) {
4057c478bd9Sstevel@tonic-gate 			if (strcmp(audio[i].device, audio[j].device))
4067c478bd9Sstevel@tonic-gate 				continue;
4077c478bd9Sstevel@tonic-gate 			audio[j].number = -1;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* print out device_allocate entries for tape devices */
4127c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
4137c478bd9Sstevel@tonic-gate 		for (j = 0; j < n; j++) {
4147c478bd9Sstevel@tonic-gate 			if (audio[j].number == i) {
4157c478bd9Sstevel@tonic-gate 				(void) printf("audio;audio;");
4167c478bd9Sstevel@tonic-gate 				(void) printf("reserved;reserved;%s;",
4177c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_ALLOC_AUTH);
4187c478bd9Sstevel@tonic-gate 				(void) printf("%s%s\n", SECLIB, "/audio_clean");
4197c478bd9Sstevel@tonic-gate 				break;
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate static void
4267c478bd9Sstevel@tonic-gate dofloppy()
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	DIR *dirp;
4297c478bd9Sstevel@tonic-gate 	struct dirent *dep;	/* directory entry pointer */
4307c478bd9Sstevel@tonic-gate 	int i, j, n;
4317c478bd9Sstevel@tonic-gate 	char *nm;		/* name/device of special device */
4327c478bd9Sstevel@tonic-gate 	char linkvalue[2048];	/* symlink value */
4337c478bd9Sstevel@tonic-gate 	struct stat stat;	/* determine if it's a symlink */
4347c478bd9Sstevel@tonic-gate 	int sz;			/* size of symlink value */
4357c478bd9Sstevel@tonic-gate 	char *cp;		/* pointer into string */
4367c478bd9Sstevel@tonic-gate 	int nfp;		/* max array size */
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	nfp = DFLT_NFP;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * look for fd* and rfd*
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev")) == NULL) {
4457c478bd9Sstevel@tonic-gate 		perror(gettext("open /dev failure"));
4467c478bd9Sstevel@tonic-gate 		exit(1);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	i = 0;
4507c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
4517c478bd9Sstevel@tonic-gate 		/* ignore if neither rst* nor nrst* */
4527c478bd9Sstevel@tonic-gate 		if (strncmp(dep->d_name, "fd0", SIZE_OF_FD0) &&
4537c478bd9Sstevel@tonic-gate 		    strncmp(dep->d_name, "rfd0", SIZE_OF_RFD0) &&
4547c478bd9Sstevel@tonic-gate 		    strncmp(dep->d_name, "fd1", SIZE_OF_FD0) &&
4557c478bd9Sstevel@tonic-gate 		    strncmp(dep->d_name, "rfd0", SIZE_OF_RFD0))
4567c478bd9Sstevel@tonic-gate 			continue;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
4597c478bd9Sstevel@tonic-gate 		if (i == nfp) {
4607c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
4617c478bd9Sstevel@tonic-gate 			nfp = expandmem(i, (void **)&fp, sizeof (struct fp));
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		/* save name (/dev + 1 + d_name + \0) */
4657c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
4667c478bd9Sstevel@tonic-gate 		if (nm == NULL)
4677c478bd9Sstevel@tonic-gate 			no_memory();
4687c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/");
4697c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
4707c478bd9Sstevel@tonic-gate 		fp[i].name = nm;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		/* ignore if not symbolic link (note i not incremented) */
4737c478bd9Sstevel@tonic-gate 		if (lstat(fp[i].name, &stat) < 0) {
4747c478bd9Sstevel@tonic-gate 			perror(gettext("stat(2) failed "));
4757c478bd9Sstevel@tonic-gate 			exit(1);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		if ((stat.st_mode&S_IFMT) != S_IFLNK)
4787c478bd9Sstevel@tonic-gate 			continue;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		/* get name from symbolic link */
4817c478bd9Sstevel@tonic-gate 		if ((sz = readlink(fp[i].name, linkvalue,
4827c478bd9Sstevel@tonic-gate 		    sizeof (linkvalue))) < 0)
4837c478bd9Sstevel@tonic-gate 			continue;
4847c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(sz+1);
4857c478bd9Sstevel@tonic-gate 		if (nm == NULL)
4867c478bd9Sstevel@tonic-gate 			no_memory();
4877c478bd9Sstevel@tonic-gate 		(void) strncpy(nm, linkvalue, sz);
4887c478bd9Sstevel@tonic-gate 		nm[sz] = '\0';
4897c478bd9Sstevel@tonic-gate 		fp[i].device = nm;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		/* get device number */
4927c478bd9Sstevel@tonic-gate 		cp = strchr(fp[i].name, 'd');
4937c478bd9Sstevel@tonic-gate 		cp++;				/* advance to device # */
4947c478bd9Sstevel@tonic-gate 		cp = strchr(cp, 'd');
4957c478bd9Sstevel@tonic-gate 		cp++;				/* advance to device # */
4967c478bd9Sstevel@tonic-gate 		(void) sscanf(cp, "%d", &fp[i].number);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		i++;
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	n = i;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* print out device_allocate entries for tape devices */
5067c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
5077c478bd9Sstevel@tonic-gate 	    for (j = 0; j < n; j++) {
5087c478bd9Sstevel@tonic-gate 		if (fp[j].number == i) {
5097c478bd9Sstevel@tonic-gate 		    (void) printf("fd%d;fd;reserved;reserved;%s;",
5107c478bd9Sstevel@tonic-gate 			i, DEFAULT_DEV_ALLOC_AUTH);
5117c478bd9Sstevel@tonic-gate 		    (void) printf("/etc/security/lib/fd_clean\n");
5127c478bd9Sstevel@tonic-gate 		    break;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	    }
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate static void
5197c478bd9Sstevel@tonic-gate docd()
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	DIR *dirp;
5227c478bd9Sstevel@tonic-gate 	struct dirent *dep;	/* directory entry pointer */
5237c478bd9Sstevel@tonic-gate 	int	i, j, n;
5247c478bd9Sstevel@tonic-gate 	char	*nm;		/* name/device of special device */
5257c478bd9Sstevel@tonic-gate 	char	linkvalue[2048];	/* symlink value */
5267c478bd9Sstevel@tonic-gate 	struct stat stat;	/* determine if it's a symlink */
5277c478bd9Sstevel@tonic-gate 	int	sz;		/* size of symlink value */
5287c478bd9Sstevel@tonic-gate 	char	*cp;		/* pointer into string */
5297c478bd9Sstevel@tonic-gate 	int	id;		/* disk id */
5307c478bd9Sstevel@tonic-gate 	int	ctrl;		/* disk controller */
5317c478bd9Sstevel@tonic-gate 	int	ncd;		/* max array size */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	ncd = DFLT_NCD;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * look for sr* and rsr*
5377c478bd9Sstevel@tonic-gate 	 */
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev")) == NULL) {
5407c478bd9Sstevel@tonic-gate 		perror(gettext("open /dev failure"));
5417c478bd9Sstevel@tonic-gate 		exit(1);
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	i = 0;
5457c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
5467c478bd9Sstevel@tonic-gate 		/* ignore if neither sr* nor rsr* */
5477c478bd9Sstevel@tonic-gate 		if (strncmp(dep->d_name, "sr", SIZE_OF_SR) &&
5487c478bd9Sstevel@tonic-gate 		    strncmp(dep->d_name, "rsr", SIZE_OF_RSR))
5497c478bd9Sstevel@tonic-gate 			continue;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
5527c478bd9Sstevel@tonic-gate 		if (i == ncd) {
5537c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
5547c478bd9Sstevel@tonic-gate 			ncd = expandmem(i, (void **)&cd, sizeof (struct cd));
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		/* save name (/dev + / + d_name + \0) */
5587c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
5597c478bd9Sstevel@tonic-gate 		if (nm == NULL)
5607c478bd9Sstevel@tonic-gate 			no_memory();
5617c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/");
5627c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
5637c478bd9Sstevel@tonic-gate 		cd[i].name = nm;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		/* save id # */
5667c478bd9Sstevel@tonic-gate 		if (dep->d_name[0] == 'r')
5677c478bd9Sstevel@tonic-gate 			(void) sscanf(dep->d_name, "rsr%d", &cd[i].id);
5687c478bd9Sstevel@tonic-gate 		else
5697c478bd9Sstevel@tonic-gate 			(void) sscanf(dep->d_name, "sr%d", &cd[i].id);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		/* ignore if not symbolic link (note i not incremented) */
5727c478bd9Sstevel@tonic-gate 		if (lstat(cd[i].name, &stat) < 0) {
5737c478bd9Sstevel@tonic-gate 			perror(gettext("stat(2) failed "));
5747c478bd9Sstevel@tonic-gate 			exit(1);
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 		if ((stat.st_mode & S_IFMT) != S_IFLNK)
5777c478bd9Sstevel@tonic-gate 			continue;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		/* get name from symbolic link */
5807c478bd9Sstevel@tonic-gate 		if ((sz = readlink(cd[i].name, linkvalue, sizeof (linkvalue))) <
5817c478bd9Sstevel@tonic-gate 		    0)
5827c478bd9Sstevel@tonic-gate 			continue;
5837c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(sz + 1);
5847c478bd9Sstevel@tonic-gate 		if (nm == NULL)
5857c478bd9Sstevel@tonic-gate 			no_memory();
5867c478bd9Sstevel@tonic-gate 		(void) strncpy(nm, linkvalue, sz);
5877c478bd9Sstevel@tonic-gate 		nm[sz] = '\0';
5887c478bd9Sstevel@tonic-gate 		cd[i].device = nm;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		cp = strrchr(cd[i].device, '/');
5917c478bd9Sstevel@tonic-gate 		cp++;				/* advance to device # */
5927c478bd9Sstevel@tonic-gate 		(void) sscanf(cp, "c%dt%d", &cd[i].controller, &cd[i].number);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		i++;
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	n = i;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * scan /dev/dsk for cd devices
6027c478bd9Sstevel@tonic-gate 	 */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev/dsk")) == NULL) {
6057c478bd9Sstevel@tonic-gate 		perror("gettext(open /dev/dsk failure)");
6067c478bd9Sstevel@tonic-gate 		exit(1);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
6107c478bd9Sstevel@tonic-gate 		/* skip . .. etc... */
6117c478bd9Sstevel@tonic-gate 		if (strncmp(dep->d_name, ".", 1) == NULL)
6127c478bd9Sstevel@tonic-gate 			continue;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		/* get device # (disk #) */
6157c478bd9Sstevel@tonic-gate 		if (sscanf(dep->d_name, "c%dt%d", &ctrl, &id) <= 0)
6167c478bd9Sstevel@tonic-gate 			continue;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		/* see if this is one of the cd special devices */
6197c478bd9Sstevel@tonic-gate 		for (j = 0; j < n; j++) {
6207c478bd9Sstevel@tonic-gate 			if (cd[j].number == id && cd[j].controller == ctrl)
6217c478bd9Sstevel@tonic-gate 				goto found;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 		continue;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		/* add new entry to table (/dev/dsk + / + d_name + \0) */
6267c478bd9Sstevel@tonic-gate found:
6277c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
6287c478bd9Sstevel@tonic-gate 		if (i == ncd) {
6297c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
6307c478bd9Sstevel@tonic-gate 			ncd = expandmem(i, (void **)&cd, sizeof (struct cd));
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_DSK + 1 + strlen(dep->d_name) + 1);
6347c478bd9Sstevel@tonic-gate 		if (nm == NULL)
6357c478bd9Sstevel@tonic-gate 			no_memory();
6367c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/dsk/");
6377c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
6387c478bd9Sstevel@tonic-gate 		cd[i].name = nm;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		cd[i].id = cd[j].id;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		cd[i].device = "";
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		cd[i].number = id;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		i++;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/*
6527c478bd9Sstevel@tonic-gate 	 * scan /dev/rdsk for cd devices
6537c478bd9Sstevel@tonic-gate 	 */
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev/rdsk")) == NULL) {
6567c478bd9Sstevel@tonic-gate 		perror(gettext("open /dev/dsk failure"));
6577c478bd9Sstevel@tonic-gate 		exit(1);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	while (dep = readdir(dirp)) {
6617c478bd9Sstevel@tonic-gate 		/* skip . .. etc... */
6627c478bd9Sstevel@tonic-gate 		if (strncmp(dep->d_name, ".", 1) == NULL)
6637c478bd9Sstevel@tonic-gate 			continue;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		/* get device # (disk #) */
6667c478bd9Sstevel@tonic-gate 		if (sscanf(dep->d_name, "c%dt%d", &ctrl, &id) != 2)
6677c478bd9Sstevel@tonic-gate 			continue;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		/* see if this is one of the cd special devices */
6707c478bd9Sstevel@tonic-gate 		for (j = 0; j < n; j++) {
6717c478bd9Sstevel@tonic-gate 			if (cd[j].number == id && cd[j].controller == ctrl)
6727c478bd9Sstevel@tonic-gate 				goto found1;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 		continue;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		/* add new entry to table (/dev/rdsk + / + d_name + \0) */
6777c478bd9Sstevel@tonic-gate found1:
6787c478bd9Sstevel@tonic-gate 		/* if array full, then expand it */
6797c478bd9Sstevel@tonic-gate 		if (i == ncd) {
6807c478bd9Sstevel@tonic-gate 			/* will exit(1) if insufficient memory */
6817c478bd9Sstevel@tonic-gate 			ncd = expandmem(i, (void **)&cd, sizeof (struct cd));
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		nm = (char *)malloc(SIZE_OF_RDSK + 1 + strlen(dep->d_name) + 1);
6857c478bd9Sstevel@tonic-gate 		if (nm == NULL)
6867c478bd9Sstevel@tonic-gate 			no_memory();
6877c478bd9Sstevel@tonic-gate 		(void) strcpy(nm, "/dev/rdsk/");
6887c478bd9Sstevel@tonic-gate 		(void) strcat(nm, dep->d_name);
6897c478bd9Sstevel@tonic-gate 		cd[i].name = nm;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		cd[i].id = cd[j].id;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		cd[i].device = "";
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		cd[i].number = id;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		cd[i].controller = ctrl;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		i++;
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	n = i;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/* print out device_maps entries for tape devices */
7077c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
7087c478bd9Sstevel@tonic-gate 		for (j = 0; j < n; j++) {
7097c478bd9Sstevel@tonic-gate 			if (cd[j].id == i) {
7107c478bd9Sstevel@tonic-gate 				(void) printf(
7117c478bd9Sstevel@tonic-gate 					"sr%d;sr;reserved;reserved;%s;",
7127c478bd9Sstevel@tonic-gate 					i, DEFAULT_DEV_ALLOC_AUTH);
7137c478bd9Sstevel@tonic-gate 				(void) printf("%s%s\n", SECLIB, "/sr_clean");
7147c478bd9Sstevel@tonic-gate 				break;
7157c478bd9Sstevel@tonic-gate 			}
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate /* set default array sizes */
7217c478bd9Sstevel@tonic-gate static void
7227c478bd9Sstevel@tonic-gate initmem()
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	tape  = (struct tape *)calloc(DFLT_NTAPE, sizeof (struct tape));
7257c478bd9Sstevel@tonic-gate 	audio = (struct audio *)calloc(DFLT_NAUDIO, sizeof (struct audio));
7267c478bd9Sstevel@tonic-gate 	cd    = (struct cd *)calloc(DFLT_NCD, sizeof (struct cd));
7277c478bd9Sstevel@tonic-gate 	fp    = (struct fp *)calloc(DFLT_NFP, sizeof (struct fp));
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (tape == NULL || audio == NULL || cd == NULL || fp == NULL)
7307c478bd9Sstevel@tonic-gate 		no_memory();
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /* note n will be # elments in array (and could be 0) */
7347c478bd9Sstevel@tonic-gate static int
7357c478bd9Sstevel@tonic-gate expandmem(int n, void **array, int size)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	void *old = *array;
7387c478bd9Sstevel@tonic-gate 	void *new;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/* get new array space (n + DELTA) */
7417c478bd9Sstevel@tonic-gate 	new = (void *)calloc(n + DELTA,  size);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (new == NULL) {
7447c478bd9Sstevel@tonic-gate 		perror("memory allocation failed");
7457c478bd9Sstevel@tonic-gate 		exit(1);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/* copy old array into new space */
7497c478bd9Sstevel@tonic-gate 	bcopy(old, new, n * size);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/* now release old arrary */
7527c478bd9Sstevel@tonic-gate 	free(old);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	*array = new;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	return (n + DELTA);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate static void
7607c478bd9Sstevel@tonic-gate no_memory(void)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s\n", "mkdevalloc",
7637c478bd9Sstevel@tonic-gate 	    gettext("out of memory"));
7647c478bd9Sstevel@tonic-gate 	exit(1);
7657c478bd9Sstevel@tonic-gate 	/* NOT REACHED */
7667c478bd9Sstevel@tonic-gate }
767