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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*644Sakaplan * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate 310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 320Sstevel@tonic-gate 330Sstevel@tonic-gate /* 340Sstevel@tonic-gate * getdev.c 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Contains 370Sstevel@tonic-gate * getdev Writes on the standard output stream a list of devices 380Sstevel@tonic-gate * that match certain criteria. 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate #include <sys/types.h> 410Sstevel@tonic-gate #include <stdio.h> 420Sstevel@tonic-gate #include <errno.h> 430Sstevel@tonic-gate #include <stdlib.h> 440Sstevel@tonic-gate #include <string.h> 450Sstevel@tonic-gate #include <fmtmsg.h> 460Sstevel@tonic-gate #include <devmgmt.h> 470Sstevel@tonic-gate #include <devtab.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate 500Sstevel@tonic-gate /* 510Sstevel@tonic-gate * Local Definitions 520Sstevel@tonic-gate * TRUE Boolean TRUE value 530Sstevel@tonic-gate * FALSE Boolean FALSE value 540Sstevel@tonic-gate * EX_OK Exit Value if all went well 550Sstevel@tonic-gate * EX_ERROR Exit Value if an error occurred 560Sstevel@tonic-gate * EX_DEVTAB Exit Value if the device table couldn't be opened 570Sstevel@tonic-gate 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate 600Sstevel@tonic-gate #ifndef TRUE 610Sstevel@tonic-gate #define TRUE (1) 620Sstevel@tonic-gate #endif 630Sstevel@tonic-gate 640Sstevel@tonic-gate #ifndef FALSE 650Sstevel@tonic-gate #define FALSE (0) 660Sstevel@tonic-gate #endif 670Sstevel@tonic-gate 680Sstevel@tonic-gate #define EX_OK 0 690Sstevel@tonic-gate #define EX_ERROR 1 700Sstevel@tonic-gate #define EX_DEVTAB 2 710Sstevel@tonic-gate 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * Messages: 750Sstevel@tonic-gate * M_USAGE Usage error 760Sstevel@tonic-gate * M_DEVTAB Can't open the device table 770Sstevel@tonic-gate * M_NODEV Device not found in the device table 780Sstevel@tonic-gate * M_ERROR Unexpected or internal error 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate 810Sstevel@tonic-gate #define M_USAGE "usage: getdev [-ae] [criterion [...]] [device [...]]" 820Sstevel@tonic-gate #define M_DEVTAB "Cannot open the device table: %s" 830Sstevel@tonic-gate #define M_NODEV "Device not found in the device table: %s" 840Sstevel@tonic-gate #define M_ERROR "Internal error, errno=%d" 850Sstevel@tonic-gate 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * Local (Static) Definitions and macros 890Sstevel@tonic-gate * buildcriterialist() Builds the criteria list from the command-line 900Sstevel@tonic-gate * builddevlist() Builds the device list from the command-line 910Sstevel@tonic-gate * lbl Buffer for the standard message label 920Sstevel@tonic-gate * txt Buffer for the standard message text 930Sstevel@tonic-gate * stdmsg(r,l,s,t) Write a standard message: 940Sstevel@tonic-gate * r Recoverability flag 950Sstevel@tonic-gate * l Standard label 960Sstevel@tonic-gate * s Severity 970Sstevel@tonic-gate * t Text 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate 1000Sstevel@tonic-gate static char **buildcriterialist(); 1010Sstevel@tonic-gate static char **builddevlist(); 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; 1040Sstevel@tonic-gate static char txt[MM_MXTXTLN+1]; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG) 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* 1090Sstevel@tonic-gate * getdev [-ae] [criterion [...]] [device [...]] 1100Sstevel@tonic-gate * 1110Sstevel@tonic-gate * This command generates a list of devices that match the 1120Sstevel@tonic-gate * specified criteria. 1130Sstevel@tonic-gate * 1140Sstevel@tonic-gate * Options: 1150Sstevel@tonic-gate * -a A device must meet all of the criteria to be 1160Sstevel@tonic-gate * included in the generated list instead of just 1170Sstevel@tonic-gate * one of the criteria (the "and" flag) 1180Sstevel@tonic-gate * -e Exclude the devices mentioned from the generated 1190Sstevel@tonic-gate * list. If this flag is omitted, the devices in the 1200Sstevel@tonic-gate * list are selected from the devices mentioned. 1210Sstevel@tonic-gate * 1220Sstevel@tonic-gate * Arguments: 1230Sstevel@tonic-gate * criterion An <attr><op><value> expression that describes 1240Sstevel@tonic-gate * a device attribute. 1250Sstevel@tonic-gate * <attr> is a device attribute 1260Sstevel@tonic-gate * <op> may be = != : !: indicating equals, does not 1270Sstevel@tonic-gate * equal, is defined, and is not defined 1280Sstevel@tonic-gate * respectively 1290Sstevel@tonic-gate * <value> is the attribute value. Currently, the only 1300Sstevel@tonic-gate * value supported for the : and !: operators 1310Sstevel@tonic-gate * is * 1320Sstevel@tonic-gate * device A device to select for or exclude from the generated 1330Sstevel@tonic-gate * list 1340Sstevel@tonic-gate * 1350Sstevel@tonic-gate * Exit values: 1360Sstevel@tonic-gate * EX_OK All went well 1370Sstevel@tonic-gate * EX_ERROR An error (syntax, internal, or resource) occurred 1380Sstevel@tonic-gate * EX_DEVTAB The device-table could not be opened for reading 1390Sstevel@tonic-gate */ 1400Sstevel@tonic-gate 141*644Sakaplan int 142*644Sakaplan main(int argc, char **argv) 1430Sstevel@tonic-gate { 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * Automatic data 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate char **arglist; /* List of arguments */ 1500Sstevel@tonic-gate char **criterialist; /* List of criteria */ 1510Sstevel@tonic-gate char **devicelist; /* List of devices to search/ignore */ 1520Sstevel@tonic-gate char **fitdevlist; /* List of devices that fit criteria */ 1530Sstevel@tonic-gate char *cmdname; /* Simple command name */ 1540Sstevel@tonic-gate char *device; /* Device name in the list */ 1550Sstevel@tonic-gate char *devtab; /* The device table name */ 1560Sstevel@tonic-gate int exitcode; /* Value to return to the caller */ 1570Sstevel@tonic-gate int sev; /* Message severity */ 1580Sstevel@tonic-gate int optchar; /* Option character (from getopt()) */ 1590Sstevel@tonic-gate int andflag; /* TRUE if criteria are to be anded */ 1600Sstevel@tonic-gate int excludeflag; /* TRUE if exclude "devices" lists */ 1610Sstevel@tonic-gate int options; /* Options to pass to getdev() */ 1620Sstevel@tonic-gate int usageerr; /* TRUE if syntax error */ 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* Build the message label from the (simple) command name */ 1660Sstevel@tonic-gate if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++; 1670Sstevel@tonic-gate else cmdname = argv[0]; 1680Sstevel@tonic-gate (void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl)); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* Write text-component of messages only (goes away in SVR4.1) */ 1710Sstevel@tonic-gate (void) putenv("MSGVERB=text"); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * Parse the command line: 1750Sstevel@tonic-gate * - Options 1760Sstevel@tonic-gate * - Selection criteria 1770Sstevel@tonic-gate * - Devices to include or exclude 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * Extract options from the command line 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* Initializations */ 1850Sstevel@tonic-gate andflag = FALSE; /* No -a -- Or criteria data */ 1860Sstevel@tonic-gate excludeflag = FALSE; /* No -e -- Include only mentioned devices */ 1870Sstevel@tonic-gate usageerr = FALSE; /* No errors on the command line (yet) */ 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Loop until all of the command line options have been parced 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate opterr = FALSE; /* Don't let getopt() write messages */ 1930Sstevel@tonic-gate while ((optchar = getopt(argc, argv, "ae")) != EOF) switch (optchar) { 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* -a List devices that fit all of the criteria listed */ 1960Sstevel@tonic-gate case 'a': 1970Sstevel@tonic-gate if (andflag) usageerr = TRUE; 1980Sstevel@tonic-gate else andflag = TRUE; 1990Sstevel@tonic-gate break; 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate /* -e Exclude those devices mentioned on the command line */ 2020Sstevel@tonic-gate case 'e': 2030Sstevel@tonic-gate if (excludeflag) usageerr = TRUE; 2040Sstevel@tonic-gate else excludeflag = TRUE; 2050Sstevel@tonic-gate break; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* Default case -- command usage error */ 2080Sstevel@tonic-gate case '?': 2090Sstevel@tonic-gate default: 2100Sstevel@tonic-gate usageerr = TRUE; 2110Sstevel@tonic-gate break; 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* If there is a usage error, write an appropriate message and exit */ 2150Sstevel@tonic-gate if (usageerr) { 2160Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE); 2170Sstevel@tonic-gate exit(EX_ERROR); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* Open the device file (if there's one to be opened) */ 2210Sstevel@tonic-gate if (!_opendevtab("r")) { 2220Sstevel@tonic-gate if (devtab = _devtabpath()) { 2230Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab); 2240Sstevel@tonic-gate sev = MM_ERROR; 2250Sstevel@tonic-gate exitcode = EX_DEVTAB; 2260Sstevel@tonic-gate } else { 2270Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 2280Sstevel@tonic-gate sev = MM_HALT; 2290Sstevel@tonic-gate exitcode = EX_ERROR; 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, txt); 2320Sstevel@tonic-gate exit(exitcode); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* Build the list of criteria and devices */ 2360Sstevel@tonic-gate arglist = argv + optind; 2370Sstevel@tonic-gate criterialist = buildcriterialist(arglist); 2380Sstevel@tonic-gate devicelist = builddevlist(arglist); 2390Sstevel@tonic-gate options = (excludeflag?DTAB_EXCLUDEFLAG:0)|(andflag?DTAB_ANDCRITERIA:0); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * Get the list of devices that meets the criteria requested. If we 2430Sstevel@tonic-gate * got a list (that might be empty), write that list to the standard 2440Sstevel@tonic-gate * output file (stdout). 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate exitcode = 0; 2480Sstevel@tonic-gate if (!(fitdevlist = getdev(devicelist, criterialist, options))) { 2490Sstevel@tonic-gate exitcode = 1; 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate else for (device = *fitdevlist++ ; device ; device = *fitdevlist++) 2520Sstevel@tonic-gate (void) puts(device); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* Finished */ 2550Sstevel@tonic-gate return(exitcode); 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * char **buildcriterialist(arglist) 2600Sstevel@tonic-gate * char **arglist 2610Sstevel@tonic-gate * 2620Sstevel@tonic-gate * Build a list of pointers to the criterion on the command-line 2630Sstevel@tonic-gate * 2640Sstevel@tonic-gate * Arguments: 2650Sstevel@tonic-gate * arglist The list of arguments on the command-line 2660Sstevel@tonic-gate * 2670Sstevel@tonic-gate * Returns: char ** 2680Sstevel@tonic-gate * The address of the first item of the list of criterion on the 2690Sstevel@tonic-gate * command-line. This is a pointer to malloc()ed space. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate static char ** 2730Sstevel@tonic-gate buildcriterialist(arglist) 2740Sstevel@tonic-gate char **arglist; /* Pointer to the list of argument pointers */ 2750Sstevel@tonic-gate { 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * Automatic data 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate char **pp; /* Pointer to a criteria */ 2810Sstevel@tonic-gate char **allocbuf; /* Pointer to the allocated data */ 2820Sstevel@tonic-gate int ncriteria; /* Number of criteria found */ 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Search the argument list, looking for the end of the list or 2870Sstevel@tonic-gate * the first thing that's not a criteria. (A criteria is a 2880Sstevel@tonic-gate * character-string that contains a colon (':') or an equal-sign ('=') 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate pp = arglist; 2920Sstevel@tonic-gate ncriteria = 0; 2930Sstevel@tonic-gate while (*pp && (strchr(*pp, '=') || strchr(*pp, ':'))) { 2940Sstevel@tonic-gate ncriteria++; 2950Sstevel@tonic-gate pp++; 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate if (ncriteria > 0) { 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate /* Allocate space for the list of criteria pointers */ 3010Sstevel@tonic-gate allocbuf = (char **) malloc((ncriteria+1)*sizeof(char **)); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * Build the list of criteria arguments 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate pp = allocbuf; /* Beginning of the list */ 3070Sstevel@tonic-gate while (*arglist && /* If there's more to do ... */ 3080Sstevel@tonic-gate (strchr(*arglist, '=') || /* and it's a = criterion ... */ 3090Sstevel@tonic-gate strchr(*arglist, ':'))) /* or it's a : criterion ... */ 3100Sstevel@tonic-gate *pp++ = *arglist++; /* Include it in the list */ 3110Sstevel@tonic-gate *pp = (char *) NULL; /* Terminate the list */ 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate } else allocbuf = (char **) NULL; /* NO criteria */ 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate return (allocbuf); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate /* 3200Sstevel@tonic-gate * char **builddevlist(arglist) 3210Sstevel@tonic-gate * char **arglist 3220Sstevel@tonic-gate * 3230Sstevel@tonic-gate * Builds a list of pointers to the devices mentioned on the command- 3240Sstevel@tonic-gate * line and returns the address of that list. 3250Sstevel@tonic-gate * 3260Sstevel@tonic-gate * Arguments: 3270Sstevel@tonic-gate * arglist The address of the list of arguments to the 3280Sstevel@tonic-gate * getdev command. 3290Sstevel@tonic-gate * 3300Sstevel@tonic-gate * Returns: char ** 3310Sstevel@tonic-gate * A pointer to the first item in the list of pointers to devices 3320Sstevel@tonic-gate * specified on the command-line 3330Sstevel@tonic-gate */ 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate static char ** 3360Sstevel@tonic-gate builddevlist(arglist) 3370Sstevel@tonic-gate char **arglist; /* Pointer to the list of pointers to args */ 3380Sstevel@tonic-gate { 3390Sstevel@tonic-gate /* 3400Sstevel@tonic-gate * Automatic data 3410Sstevel@tonic-gate */ 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate /* 3440Sstevel@tonic-gate * Search the argument list, looking for the end of the list or the 3450Sstevel@tonic-gate * first thing that's not a criteria. It is the first device in the 3460Sstevel@tonic-gate * list of devices (if any). 3470Sstevel@tonic-gate */ 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate while (*arglist && (strchr(*arglist, '=') || strchr(*arglist, ':'))) arglist++; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate /* Return a pointer to the argument list. */ 3520Sstevel@tonic-gate return(*arglist?arglist:(char **) NULL); 3530Sstevel@tonic-gate } 354