1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate /* 34*0Sstevel@tonic-gate * getdev.c 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * Contains 37*0Sstevel@tonic-gate * getdev Writes on the standard output stream a list of devices 38*0Sstevel@tonic-gate * that match certain criteria. 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate #include <sys/types.h> 41*0Sstevel@tonic-gate #include <stdio.h> 42*0Sstevel@tonic-gate #include <errno.h> 43*0Sstevel@tonic-gate #include <stdlib.h> 44*0Sstevel@tonic-gate #include <string.h> 45*0Sstevel@tonic-gate #include <fmtmsg.h> 46*0Sstevel@tonic-gate #include <devmgmt.h> 47*0Sstevel@tonic-gate #include <devtab.h> 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate /* 51*0Sstevel@tonic-gate * Local Definitions 52*0Sstevel@tonic-gate * TRUE Boolean TRUE value 53*0Sstevel@tonic-gate * FALSE Boolean FALSE value 54*0Sstevel@tonic-gate * EX_OK Exit Value if all went well 55*0Sstevel@tonic-gate * EX_ERROR Exit Value if an error occurred 56*0Sstevel@tonic-gate * EX_DEVTAB Exit Value if the device table couldn't be opened 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate */ 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate #ifndef TRUE 61*0Sstevel@tonic-gate #define TRUE (1) 62*0Sstevel@tonic-gate #endif 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate #ifndef FALSE 65*0Sstevel@tonic-gate #define FALSE (0) 66*0Sstevel@tonic-gate #endif 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate #define EX_OK 0 69*0Sstevel@tonic-gate #define EX_ERROR 1 70*0Sstevel@tonic-gate #define EX_DEVTAB 2 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate /* 74*0Sstevel@tonic-gate * Messages: 75*0Sstevel@tonic-gate * M_USAGE Usage error 76*0Sstevel@tonic-gate * M_DEVTAB Can't open the device table 77*0Sstevel@tonic-gate * M_NODEV Device not found in the device table 78*0Sstevel@tonic-gate * M_ERROR Unexpected or internal error 79*0Sstevel@tonic-gate */ 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate #define M_USAGE "usage: getdev [-ae] [criterion [...]] [device [...]]" 82*0Sstevel@tonic-gate #define M_DEVTAB "Cannot open the device table: %s" 83*0Sstevel@tonic-gate #define M_NODEV "Device not found in the device table: %s" 84*0Sstevel@tonic-gate #define M_ERROR "Internal error, errno=%d" 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate /* 88*0Sstevel@tonic-gate * Local (Static) Definitions and macros 89*0Sstevel@tonic-gate * buildcriterialist() Builds the criteria list from the command-line 90*0Sstevel@tonic-gate * builddevlist() Builds the device list from the command-line 91*0Sstevel@tonic-gate * lbl Buffer for the standard message label 92*0Sstevel@tonic-gate * txt Buffer for the standard message text 93*0Sstevel@tonic-gate * stdmsg(r,l,s,t) Write a standard message: 94*0Sstevel@tonic-gate * r Recoverability flag 95*0Sstevel@tonic-gate * l Standard label 96*0Sstevel@tonic-gate * s Severity 97*0Sstevel@tonic-gate * t Text 98*0Sstevel@tonic-gate */ 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate static char **buildcriterialist(); 101*0Sstevel@tonic-gate static char **builddevlist(); 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; 104*0Sstevel@tonic-gate static char txt[MM_MXTXTLN+1]; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG) 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* 109*0Sstevel@tonic-gate * getdev [-ae] [criterion [...]] [device [...]] 110*0Sstevel@tonic-gate * 111*0Sstevel@tonic-gate * This command generates a list of devices that match the 112*0Sstevel@tonic-gate * specified criteria. 113*0Sstevel@tonic-gate * 114*0Sstevel@tonic-gate * Options: 115*0Sstevel@tonic-gate * -a A device must meet all of the criteria to be 116*0Sstevel@tonic-gate * included in the generated list instead of just 117*0Sstevel@tonic-gate * one of the criteria (the "and" flag) 118*0Sstevel@tonic-gate * -e Exclude the devices mentioned from the generated 119*0Sstevel@tonic-gate * list. If this flag is omitted, the devices in the 120*0Sstevel@tonic-gate * list are selected from the devices mentioned. 121*0Sstevel@tonic-gate * 122*0Sstevel@tonic-gate * Arguments: 123*0Sstevel@tonic-gate * criterion An <attr><op><value> expression that describes 124*0Sstevel@tonic-gate * a device attribute. 125*0Sstevel@tonic-gate * <attr> is a device attribute 126*0Sstevel@tonic-gate * <op> may be = != : !: indicating equals, does not 127*0Sstevel@tonic-gate * equal, is defined, and is not defined 128*0Sstevel@tonic-gate * respectively 129*0Sstevel@tonic-gate * <value> is the attribute value. Currently, the only 130*0Sstevel@tonic-gate * value supported for the : and !: operators 131*0Sstevel@tonic-gate * is * 132*0Sstevel@tonic-gate * device A device to select for or exclude from the generated 133*0Sstevel@tonic-gate * list 134*0Sstevel@tonic-gate * 135*0Sstevel@tonic-gate * Exit values: 136*0Sstevel@tonic-gate * EX_OK All went well 137*0Sstevel@tonic-gate * EX_ERROR An error (syntax, internal, or resource) occurred 138*0Sstevel@tonic-gate * EX_DEVTAB The device-table could not be opened for reading 139*0Sstevel@tonic-gate */ 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate main(argc, argv) 142*0Sstevel@tonic-gate int argc; /* Number of items on the command line */ 143*0Sstevel@tonic-gate char **argv; /* List of pointers to the arguments */ 144*0Sstevel@tonic-gate { 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate /* 147*0Sstevel@tonic-gate * Automatic data 148*0Sstevel@tonic-gate */ 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate char **arglist; /* List of arguments */ 151*0Sstevel@tonic-gate char **criterialist; /* List of criteria */ 152*0Sstevel@tonic-gate char **devicelist; /* List of devices to search/ignore */ 153*0Sstevel@tonic-gate char **fitdevlist; /* List of devices that fit criteria */ 154*0Sstevel@tonic-gate char *cmdname; /* Simple command name */ 155*0Sstevel@tonic-gate char *device; /* Device name in the list */ 156*0Sstevel@tonic-gate char *devtab; /* The device table name */ 157*0Sstevel@tonic-gate int exitcode; /* Value to return to the caller */ 158*0Sstevel@tonic-gate int sev; /* Message severity */ 159*0Sstevel@tonic-gate int optchar; /* Option character (from getopt()) */ 160*0Sstevel@tonic-gate int andflag; /* TRUE if criteria are to be anded */ 161*0Sstevel@tonic-gate int excludeflag; /* TRUE if exclude "devices" lists */ 162*0Sstevel@tonic-gate int options; /* Options to pass to getdev() */ 163*0Sstevel@tonic-gate int usageerr; /* TRUE if syntax error */ 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /* Build the message label from the (simple) command name */ 167*0Sstevel@tonic-gate if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++; 168*0Sstevel@tonic-gate else cmdname = argv[0]; 169*0Sstevel@tonic-gate (void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl)); 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate /* Write text-component of messages only (goes away in SVR4.1) */ 172*0Sstevel@tonic-gate (void) putenv("MSGVERB=text"); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate /* 175*0Sstevel@tonic-gate * Parse the command line: 176*0Sstevel@tonic-gate * - Options 177*0Sstevel@tonic-gate * - Selection criteria 178*0Sstevel@tonic-gate * - Devices to include or exclude 179*0Sstevel@tonic-gate */ 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* 182*0Sstevel@tonic-gate * Extract options from the command line 183*0Sstevel@tonic-gate */ 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate /* Initializations */ 186*0Sstevel@tonic-gate andflag = FALSE; /* No -a -- Or criteria data */ 187*0Sstevel@tonic-gate excludeflag = FALSE; /* No -e -- Include only mentioned devices */ 188*0Sstevel@tonic-gate usageerr = FALSE; /* No errors on the command line (yet) */ 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate /* 191*0Sstevel@tonic-gate * Loop until all of the command line options have been parced 192*0Sstevel@tonic-gate */ 193*0Sstevel@tonic-gate opterr = FALSE; /* Don't let getopt() write messages */ 194*0Sstevel@tonic-gate while ((optchar = getopt(argc, argv, "ae")) != EOF) switch (optchar) { 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate /* -a List devices that fit all of the criteria listed */ 197*0Sstevel@tonic-gate case 'a': 198*0Sstevel@tonic-gate if (andflag) usageerr = TRUE; 199*0Sstevel@tonic-gate else andflag = TRUE; 200*0Sstevel@tonic-gate break; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate /* -e Exclude those devices mentioned on the command line */ 203*0Sstevel@tonic-gate case 'e': 204*0Sstevel@tonic-gate if (excludeflag) usageerr = TRUE; 205*0Sstevel@tonic-gate else excludeflag = TRUE; 206*0Sstevel@tonic-gate break; 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate /* Default case -- command usage error */ 209*0Sstevel@tonic-gate case '?': 210*0Sstevel@tonic-gate default: 211*0Sstevel@tonic-gate usageerr = TRUE; 212*0Sstevel@tonic-gate break; 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate /* If there is a usage error, write an appropriate message and exit */ 216*0Sstevel@tonic-gate if (usageerr) { 217*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE); 218*0Sstevel@tonic-gate exit(EX_ERROR); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /* Open the device file (if there's one to be opened) */ 222*0Sstevel@tonic-gate if (!_opendevtab("r")) { 223*0Sstevel@tonic-gate if (devtab = _devtabpath()) { 224*0Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab); 225*0Sstevel@tonic-gate sev = MM_ERROR; 226*0Sstevel@tonic-gate exitcode = EX_DEVTAB; 227*0Sstevel@tonic-gate } else { 228*0Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 229*0Sstevel@tonic-gate sev = MM_HALT; 230*0Sstevel@tonic-gate exitcode = EX_ERROR; 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, txt); 233*0Sstevel@tonic-gate exit(exitcode); 234*0Sstevel@tonic-gate } 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate /* Build the list of criteria and devices */ 237*0Sstevel@tonic-gate arglist = argv + optind; 238*0Sstevel@tonic-gate criterialist = buildcriterialist(arglist); 239*0Sstevel@tonic-gate devicelist = builddevlist(arglist); 240*0Sstevel@tonic-gate options = (excludeflag?DTAB_EXCLUDEFLAG:0)|(andflag?DTAB_ANDCRITERIA:0); 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate /* 243*0Sstevel@tonic-gate * Get the list of devices that meets the criteria requested. If we 244*0Sstevel@tonic-gate * got a list (that might be empty), write that list to the standard 245*0Sstevel@tonic-gate * output file (stdout). 246*0Sstevel@tonic-gate */ 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate exitcode = 0; 249*0Sstevel@tonic-gate if (!(fitdevlist = getdev(devicelist, criterialist, options))) { 250*0Sstevel@tonic-gate exitcode = 1; 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate else for (device = *fitdevlist++ ; device ; device = *fitdevlist++) 253*0Sstevel@tonic-gate (void) puts(device); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate /* Finished */ 256*0Sstevel@tonic-gate return(exitcode); 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate /* 260*0Sstevel@tonic-gate * char **buildcriterialist(arglist) 261*0Sstevel@tonic-gate * char **arglist 262*0Sstevel@tonic-gate * 263*0Sstevel@tonic-gate * Build a list of pointers to the criterion on the command-line 264*0Sstevel@tonic-gate * 265*0Sstevel@tonic-gate * Arguments: 266*0Sstevel@tonic-gate * arglist The list of arguments on the command-line 267*0Sstevel@tonic-gate * 268*0Sstevel@tonic-gate * Returns: char ** 269*0Sstevel@tonic-gate * The address of the first item of the list of criterion on the 270*0Sstevel@tonic-gate * command-line. This is a pointer to malloc()ed space. 271*0Sstevel@tonic-gate */ 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate static char ** 274*0Sstevel@tonic-gate buildcriterialist(arglist) 275*0Sstevel@tonic-gate char **arglist; /* Pointer to the list of argument pointers */ 276*0Sstevel@tonic-gate { 277*0Sstevel@tonic-gate /* 278*0Sstevel@tonic-gate * Automatic data 279*0Sstevel@tonic-gate */ 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate char **pp; /* Pointer to a criteria */ 282*0Sstevel@tonic-gate char **allocbuf; /* Pointer to the allocated data */ 283*0Sstevel@tonic-gate int ncriteria; /* Number of criteria found */ 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate /* 287*0Sstevel@tonic-gate * Search the argument list, looking for the end of the list or 288*0Sstevel@tonic-gate * the first thing that's not a criteria. (A criteria is a 289*0Sstevel@tonic-gate * character-string that contains a colon (':') or an equal-sign ('=') 290*0Sstevel@tonic-gate */ 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate pp = arglist; 293*0Sstevel@tonic-gate ncriteria = 0; 294*0Sstevel@tonic-gate while (*pp && (strchr(*pp, '=') || strchr(*pp, ':'))) { 295*0Sstevel@tonic-gate ncriteria++; 296*0Sstevel@tonic-gate pp++; 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate if (ncriteria > 0) { 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate /* Allocate space for the list of criteria pointers */ 302*0Sstevel@tonic-gate allocbuf = (char **) malloc((ncriteria+1)*sizeof(char **)); 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate /* 305*0Sstevel@tonic-gate * Build the list of criteria arguments 306*0Sstevel@tonic-gate */ 307*0Sstevel@tonic-gate pp = allocbuf; /* Beginning of the list */ 308*0Sstevel@tonic-gate while (*arglist && /* If there's more to do ... */ 309*0Sstevel@tonic-gate (strchr(*arglist, '=') || /* and it's a = criterion ... */ 310*0Sstevel@tonic-gate strchr(*arglist, ':'))) /* or it's a : criterion ... */ 311*0Sstevel@tonic-gate *pp++ = *arglist++; /* Include it in the list */ 312*0Sstevel@tonic-gate *pp = (char *) NULL; /* Terminate the list */ 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate } else allocbuf = (char **) NULL; /* NO criteria */ 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate return (allocbuf); 318*0Sstevel@tonic-gate } 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate /* 321*0Sstevel@tonic-gate * char **builddevlist(arglist) 322*0Sstevel@tonic-gate * char **arglist 323*0Sstevel@tonic-gate * 324*0Sstevel@tonic-gate * Builds a list of pointers to the devices mentioned on the command- 325*0Sstevel@tonic-gate * line and returns the address of that list. 326*0Sstevel@tonic-gate * 327*0Sstevel@tonic-gate * Arguments: 328*0Sstevel@tonic-gate * arglist The address of the list of arguments to the 329*0Sstevel@tonic-gate * getdev command. 330*0Sstevel@tonic-gate * 331*0Sstevel@tonic-gate * Returns: char ** 332*0Sstevel@tonic-gate * A pointer to the first item in the list of pointers to devices 333*0Sstevel@tonic-gate * specified on the command-line 334*0Sstevel@tonic-gate */ 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate static char ** 337*0Sstevel@tonic-gate builddevlist(arglist) 338*0Sstevel@tonic-gate char **arglist; /* Pointer to the list of pointers to args */ 339*0Sstevel@tonic-gate { 340*0Sstevel@tonic-gate /* 341*0Sstevel@tonic-gate * Automatic data 342*0Sstevel@tonic-gate */ 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gate /* 345*0Sstevel@tonic-gate * Search the argument list, looking for the end of the list or the 346*0Sstevel@tonic-gate * first thing that's not a criteria. It is the first device in the 347*0Sstevel@tonic-gate * list of devices (if any). 348*0Sstevel@tonic-gate */ 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate while (*arglist && (strchr(*arglist, '=') || strchr(*arglist, ':'))) arglist++; 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate /* Return a pointer to the argument list. */ 353*0Sstevel@tonic-gate return(*arglist?arglist:(char **) NULL); 354*0Sstevel@tonic-gate } 355