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 #include <sys/types.h> 34*0Sstevel@tonic-gate #include <sys/param.h> 35*0Sstevel@tonic-gate #include <stdio.h> 36*0Sstevel@tonic-gate #include <errno.h> 37*0Sstevel@tonic-gate #include <stdlib.h> 38*0Sstevel@tonic-gate #include <string.h> 39*0Sstevel@tonic-gate #include <fmtmsg.h> 40*0Sstevel@tonic-gate #include <devmgmt.h> 41*0Sstevel@tonic-gate #include <devtab.h> 42*0Sstevel@tonic-gate #include <values.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * Local definitions 47*0Sstevel@tonic-gate * TRUE Boolean TRUE value 48*0Sstevel@tonic-gate * FALSE Boolean FALSE value 49*0Sstevel@tonic-gate * TOKDELIMS Char string of delimiters for lists 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #ifndef TRUE 53*0Sstevel@tonic-gate #define TRUE ('t') 54*0Sstevel@tonic-gate #endif 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #ifndef FALSE 57*0Sstevel@tonic-gate #define FALSE 0 58*0Sstevel@tonic-gate #endif 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate #define TOKDELIMS ", \t\n" 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate /* 64*0Sstevel@tonic-gate * Exit codes: 65*0Sstevel@tonic-gate * EX_OK Exit code for all went well 66*0Sstevel@tonic-gate * EX_ERROR Exit code for something failed 67*0Sstevel@tonic-gate * EX_TABLES A table couldn't be accessed 68*0Sstevel@tonic-gate * EX_NOALLOC Exit code for allocation failed 69*0Sstevel@tonic-gate */ 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate #define EX_OK 0 72*0Sstevel@tonic-gate #define EX_ERROR 1 73*0Sstevel@tonic-gate #define EX_TABLES 2 74*0Sstevel@tonic-gate #define EX_NOALLOC 3 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate /* 77*0Sstevel@tonic-gate * Messages: 78*0Sstevel@tonic-gate * M_USAGE Usage error 79*0Sstevel@tonic-gate * M_INVKEY Invalid key specified 80*0Sstevel@tonic-gate * M_ERROR Some strange error 81*0Sstevel@tonic-gate * M_UNABLE A list of devices is unavailable 82*0Sstevel@tonic-gate * M_DEVTAB Can't access device table (for reading) 83*0Sstevel@tonic-gate * M_RSVTAB Can't access device reservation table (for r/w) 84*0Sstevel@tonic-gate * M_NODEV A list of devices is invalid 85*0Sstevel@tonic-gate */ 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate #define M_USAGE "usage: devreserv [key [devicelist [...]]]" 88*0Sstevel@tonic-gate #define M_INVKEY "Invalid key: %s" 89*0Sstevel@tonic-gate #define M_ERROR "Internal error, errno=%d" 90*0Sstevel@tonic-gate #define M_UNABLE "Cannot reserve devices" 91*0Sstevel@tonic-gate #define M_DEVTAB "Cannot open the device table: %s" 92*0Sstevel@tonic-gate #define M_RSVTAB "Cannot open the device-reservation table: %s" 93*0Sstevel@tonic-gate #define M_NODEV M_UNABLE 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate /* 97*0Sstevel@tonic-gate * Local functions and static data 98*0Sstevel@tonic-gate * 99*0Sstevel@tonic-gate * buildreqlist() Builds the list of requested devices for devreserv() 100*0Sstevel@tonic-gate * freereqlist() Free space allocated to the list of requested devices 101*0Sstevel@tonic-gate * ndevsin() Get number of elements in a list 102*0Sstevel@tonic-gate * stdmsg(r,l,s,m) Standard message generation 103*0Sstevel@tonic-gate * r Recoverability flag 104*0Sstevel@tonic-gate * l Label 105*0Sstevel@tonic-gate * s Severity 106*0Sstevel@tonic-gate * m Message 107*0Sstevel@tonic-gate * 108*0Sstevel@tonic-gate * lbl Buffer for the label-component of a message 109*0Sstevel@tonic-gate * txt Buffer for the text-component of a message 110*0Sstevel@tonic-gate */ 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate static char ***buildreqlist(); 113*0Sstevel@tonic-gate static void freereqlist(); 114*0Sstevel@tonic-gate static int ndevsin(); 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate #define stdmsg(r,l,s,m) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,m,MM_NULLACT,MM_NULLTAG) 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate static char lbl[MM_MXLABELLN+1]; 119*0Sstevel@tonic-gate static char txt[MM_MXTXTLN+1]; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* 122*0Sstevel@tonic-gate * devreserv [key [devlist [devlist [...]]]] 123*0Sstevel@tonic-gate * 124*0Sstevel@tonic-gate * This command reserves sets of devices known to the OA&M device 125*0Sstevel@tonic-gate * management system. It reserves a device from each of the device 126*0Sstevel@tonic-gate * lists presented to it, reserving them on the key (<key>). If no 127*0Sstevel@tonic-gate * device-lists are provided, the command lists those devices reserved 128*0Sstevel@tonic-gate * on the given key (<key>). If no key (<key>) is provided, the 129*0Sstevel@tonic-gate * command lists all devices currently reserved. 130*0Sstevel@tonic-gate * 131*0Sstevel@tonic-gate * Options: None 132*0Sstevel@tonic-gate * 133*0Sstevel@tonic-gate * Arguments: 134*0Sstevel@tonic-gate * key Key to lock the devices on 135*0Sstevel@tonic-gate * devlist A comma-, space-, or tab-list containing devices 136*0Sstevel@tonic-gate * (pathnames or aliases). For typical shells, space- 137*0Sstevel@tonic-gate * and tab-lists should be quoted or the separator should 138*0Sstevel@tonic-gate * be somehow escaped. 139*0Sstevel@tonic-gate * 140*0Sstevel@tonic-gate * Command Values: 141*0Sstevel@tonic-gate * EX_OK 0 Device(s) successfully allocated 142*0Sstevel@tonic-gate * EX_ERROR 1 A syntax or other error occurred 143*0Sstevel@tonic-gate * EX_TABLES 2 Either the device-table or the device- 144*0Sstevel@tonic-gate * reservation table couldn't be opened as needed 145*0Sstevel@tonic-gate * EX_NOALLOC 3 The device-reservation request couldn't be 146*0Sstevel@tonic-gate * fulfilled. 147*0Sstevel@tonic-gate */ 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate main(argc, argv) 150*0Sstevel@tonic-gate int argc; /* Arg count */ 151*0Sstevel@tonic-gate char *argv[]; /* Arg vector */ 152*0Sstevel@tonic-gate { 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate /* Automatics */ 155*0Sstevel@tonic-gate char ***reqlist; /* * to list of lists */ 156*0Sstevel@tonic-gate char **argp; /* Ptr to current argument */ 157*0Sstevel@tonic-gate char **alloclist; /* List of allocated devices */ 158*0Sstevel@tonic-gate char **pp; /* Temp ptr to char ptrs */ 159*0Sstevel@tonic-gate struct reservdev **rsvd; /* Ptr to list of rsvd devs */ 160*0Sstevel@tonic-gate struct reservdev **plk; /* Running ptr to locks */ 161*0Sstevel@tonic-gate char *p; /* Temp char ptr */ 162*0Sstevel@tonic-gate char *devtab; /* Device table pathname */ 163*0Sstevel@tonic-gate char *rsvtab; /* Dev-rsv tbl pathname */ 164*0Sstevel@tonic-gate int argcount; /* Number of args on cmd */ 165*0Sstevel@tonic-gate long lkey; /* Key for locking (long) */ 166*0Sstevel@tonic-gate int key; /* Key for locking */ 167*0Sstevel@tonic-gate int exitcode; /* Value to return */ 168*0Sstevel@tonic-gate int sev; /* Message severity */ 169*0Sstevel@tonic-gate int syntaxerr; /* Flag, TRUE if syntax error */ 170*0Sstevel@tonic-gate int c; /* Option character */ 171*0Sstevel@tonic-gate int i; /* Temp counter */ 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate /* 175*0Sstevel@tonic-gate * Initializations 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate /* Build a message label */ 179*0Sstevel@tonic-gate if (p = strrchr(argv[0], '/')) p++; 180*0Sstevel@tonic-gate else p = argv[0]; 181*0Sstevel@tonic-gate (void) strlcat(strcpy(lbl, "UX:"), p, sizeof(lbl)); 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate /* 185*0Sstevel@tonic-gate * Allow only the text component of messages to be written 186*0Sstevel@tonic-gate * (this will probably go away in SVR4.1) 187*0Sstevel@tonic-gate */ 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate (void) putenv("MSGVERB=text"); 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate /* 193*0Sstevel@tonic-gate * Parse the options from the command line 194*0Sstevel@tonic-gate */ 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate opterr = 0; 197*0Sstevel@tonic-gate syntaxerr = FALSE; 198*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "")) != EOF) switch(c) { 199*0Sstevel@tonic-gate default: 200*0Sstevel@tonic-gate syntaxerr = FALSE; 201*0Sstevel@tonic-gate break; 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate /* If there's (an obvious) syntax error, write a message and quit */ 205*0Sstevel@tonic-gate if (syntaxerr) { 206*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE); 207*0Sstevel@tonic-gate exit(EX_ERROR); 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate /* Argument initializations */ 211*0Sstevel@tonic-gate argcount = argc - optind; 212*0Sstevel@tonic-gate argp = &argv[optind]; 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate /* 216*0Sstevel@tonic-gate * devreserv 217*0Sstevel@tonic-gate * 218*0Sstevel@tonic-gate * If euid == 0, write a list of all currently allocated devices. 219*0Sstevel@tonic-gate */ 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate if (argcount == 0) { 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate /* Get the list of reserved devices */ 224*0Sstevel@tonic-gate if (rsvd = reservdev()) { 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate /* Write the list of reserved devices with the key 227*0Sstevel@tonic-gate * that the device was locked on. The key should go 228*0Sstevel@tonic-gate * in column 16, but separate it from the alias with at 229*0Sstevel@tonic-gate * least one space */ 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate exitcode = EX_OK; 232*0Sstevel@tonic-gate for (plk = rsvd ; *plk ; plk++) { 233*0Sstevel@tonic-gate if ((i = fputs((*plk)->devname, stdout)) >= 0) do 234*0Sstevel@tonic-gate (void) fputc(' ', stdout); 235*0Sstevel@tonic-gate while (++i < 16); 236*0Sstevel@tonic-gate (void) fprintf(stdout, "%ld\n", (*plk)->key); 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate } else { 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* Problems getting the list of reserved devices */ 242*0Sstevel@tonic-gate if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) { 243*0Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab); 244*0Sstevel@tonic-gate exitcode = EX_TABLES; 245*0Sstevel@tonic-gate sev = MM_ERROR; 246*0Sstevel@tonic-gate } else { 247*0Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 248*0Sstevel@tonic-gate exitcode = EX_ERROR; 249*0Sstevel@tonic-gate sev = MM_HALT; 250*0Sstevel@tonic-gate } 251*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, txt); 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate /* Finished */ 255*0Sstevel@tonic-gate exit(exitcode); 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate /* 260*0Sstevel@tonic-gate * devreserv key 261*0Sstevel@tonic-gate * 262*0Sstevel@tonic-gate * Generate a list of the devices allocated on a specific key. 263*0Sstevel@tonic-gate */ 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate if (argcount == 1) { 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate /* Extract the key from the command */ 268*0Sstevel@tonic-gate lkey = strtol(*argp, &p, 10); 269*0Sstevel@tonic-gate if (*p || (lkey <= 0) || (lkey > MAXINT)) { 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate /* <key> argument invalid */ 272*0Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_INVKEY, *argp); 273*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, txt); 274*0Sstevel@tonic-gate exitcode = EX_ERROR; 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate } else { 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate key = (int) lkey; 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate /* Get the list of reserved devices ... */ 281*0Sstevel@tonic-gate if (rsvd = reservdev()) { 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate /* For each reserved device, write the alias to stdout */ 284*0Sstevel@tonic-gate exitcode = EX_OK; 285*0Sstevel@tonic-gate for (plk = rsvd ; *plk ; plk++) { 286*0Sstevel@tonic-gate if ((*plk)->key == key) (void) puts((*plk)->devname); 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate } else { 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate /* Problems getting the list of reserved devices */ 292*0Sstevel@tonic-gate if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) { 293*0Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab); 294*0Sstevel@tonic-gate exitcode = EX_TABLES; 295*0Sstevel@tonic-gate sev = MM_ERROR; 296*0Sstevel@tonic-gate } else { 297*0Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 298*0Sstevel@tonic-gate exitcode = EX_ERROR; 299*0Sstevel@tonic-gate sev = MM_HALT; 300*0Sstevel@tonic-gate } 301*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, txt); 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate /* Finished */ 306*0Sstevel@tonic-gate exit(exitcode); 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate /* 311*0Sstevel@tonic-gate * devreserv key devlist [...] 312*0Sstevel@tonic-gate * 313*0Sstevel@tonic-gate * Reserve specific devices 314*0Sstevel@tonic-gate */ 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate /* Open the device file (if there's one to be opened) */ 317*0Sstevel@tonic-gate if (!_opendevtab("r")) { 318*0Sstevel@tonic-gate if (devtab = _devtabpath()) { 319*0Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab); 320*0Sstevel@tonic-gate exitcode = EX_TABLES; 321*0Sstevel@tonic-gate sev = MM_ERROR; 322*0Sstevel@tonic-gate } else { 323*0Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 324*0Sstevel@tonic-gate exitcode = EX_ERROR; 325*0Sstevel@tonic-gate sev = MM_HALT; 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, sev, txt); 328*0Sstevel@tonic-gate exit(exitcode); 329*0Sstevel@tonic-gate } 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate /* Extract the key from the command */ 332*0Sstevel@tonic-gate lkey = strtol(*argp, &p, 10); 333*0Sstevel@tonic-gate if (*p || (lkey <= 0) || (lkey > MAXINT)) { 334*0Sstevel@tonic-gate (void) snprintf(txt, sizeof(txt), M_INVKEY, *argp); 335*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, txt); 336*0Sstevel@tonic-gate exit(EX_ERROR); 337*0Sstevel@tonic-gate } 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate key = (int) lkey; 340*0Sstevel@tonic-gate argp++; 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate /* Build the device request list from the command arguments */ 343*0Sstevel@tonic-gate if (reqlist = buildreqlist(argp)) { 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate /* Attempt to allocate the devices */ 346*0Sstevel@tonic-gate if (alloclist = devreserv(key, reqlist)) { 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gate /* 349*0Sstevel@tonic-gate * For each allocated device, write the alias to stdout 350*0Sstevel@tonic-gate * and free the space allocated for the string. 351*0Sstevel@tonic-gate */ 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate for (pp = alloclist; *pp; pp++) { 354*0Sstevel@tonic-gate (void) puts(*pp); 355*0Sstevel@tonic-gate free(*pp); 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate /* Free the list of allocated devices */ 359*0Sstevel@tonic-gate free((char *) alloclist); 360*0Sstevel@tonic-gate exitcode = EX_OK; 361*0Sstevel@tonic-gate } 362*0Sstevel@tonic-gate else { 363*0Sstevel@tonic-gate /* Device allocation failed */ 364*0Sstevel@tonic-gate if (errno == EAGAIN) { 365*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_UNABLE); 366*0Sstevel@tonic-gate exitcode = EX_NOALLOC; 367*0Sstevel@tonic-gate } else if (errno == ENODEV) { 368*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_ERROR, M_NODEV); 369*0Sstevel@tonic-gate exitcode = EX_NOALLOC; 370*0Sstevel@tonic-gate } else { 371*0Sstevel@tonic-gate (void) sprintf(txt, M_ERROR, errno); 372*0Sstevel@tonic-gate stdmsg(MM_NRECOV, lbl, MM_HALT, txt); 373*0Sstevel@tonic-gate exitcode = EX_ERROR; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate } 376*0Sstevel@tonic-gate freereqlist(reqlist); 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate /* Exit with the appropriate code */ 381*0Sstevel@tonic-gate return(exitcode); 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate /* 385*0Sstevel@tonic-gate * char ***buildreqlist(args) 386*0Sstevel@tonic-gate * char **args 387*0Sstevel@tonic-gate * 388*0Sstevel@tonic-gate * Build the list of lists of devices to request, as described by the 389*0Sstevel@tonic-gate * arguments on the command line. 390*0Sstevel@tonic-gate * 391*0Sstevel@tonic-gate * Arguments: 392*0Sstevel@tonic-gate * char **args The address of the first argument of the list of 393*0Sstevel@tonic-gate * lists of devices to allocate. (This list is 394*0Sstevel@tonic-gate * terminated with a (char *) NULL.) 395*0Sstevel@tonic-gate * 396*0Sstevel@tonic-gate * Returns: char *** 397*0Sstevel@tonic-gate * A pointer to a list containing addresses of lists of pointers to 398*0Sstevel@tonic-gate * character-strings, as expected by "devreserv()" 399*0Sstevel@tonic-gate * 400*0Sstevel@tonic-gate * Notes: 401*0Sstevel@tonic-gate * - Assuming that strtok() won't return "". If it does, the 402*0Sstevel@tonic-gate * parsing algorithm needs to be enhanced a bit to eliminate 403*0Sstevel@tonic-gate * these cases. 404*0Sstevel@tonic-gate */ 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate static char *** 407*0Sstevel@tonic-gate buildreqlist(args) 408*0Sstevel@tonic-gate char **args; 409*0Sstevel@tonic-gate { 410*0Sstevel@tonic-gate /* Local automatic data */ 411*0Sstevel@tonic-gate char ***addrlist; /* Addr of space for ptrs to lists */ 412*0Sstevel@tonic-gate char ***ppp; /* Pointer to pointers to pointers */ 413*0Sstevel@tonic-gate char **pp; /* Pointer to pointers */ 414*0Sstevel@tonic-gate char **qq; /* Pointer to pointers */ 415*0Sstevel@tonic-gate int noerror; /* FLAG, TRUE if all's well */ 416*0Sstevel@tonic-gate int i; /* Counter */ 417*0Sstevel@tonic-gate int n; /* Another counter */ 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate /* Count the number of lists we have to work with */ 421*0Sstevel@tonic-gate i = 1; 422*0Sstevel@tonic-gate for (pp = args ; *pp ; pp++) i++; 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate /* If we can allocate space for the list of lists ... */ 426*0Sstevel@tonic-gate if (addrlist = (char ***) malloc(i*sizeof(char **))) { 427*0Sstevel@tonic-gate 428*0Sstevel@tonic-gate /* Parse each list, putting that list in the list of lists */ 429*0Sstevel@tonic-gate ppp = addrlist; 430*0Sstevel@tonic-gate noerror = TRUE; 431*0Sstevel@tonic-gate for (pp = args ; noerror && *pp ; pp++) { 432*0Sstevel@tonic-gate n = ndevsin(*pp, TOKDELIMS); 433*0Sstevel@tonic-gate if (*ppp = (char **) malloc((n+1)*sizeof(char *))) { 434*0Sstevel@tonic-gate qq = *ppp++; 435*0Sstevel@tonic-gate if (*qq++ = strtok(*pp, TOKDELIMS)) 436*0Sstevel@tonic-gate while (*qq++ = strtok((char *) NULL, TOKDELIMS)); 437*0Sstevel@tonic-gate } else noerror = FALSE; 438*0Sstevel@tonic-gate } 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate /* If there was an error, clean up the malloc()s we've made */ 441*0Sstevel@tonic-gate if (!noerror) { 442*0Sstevel@tonic-gate freereqlist(addrlist); 443*0Sstevel@tonic-gate addrlist = (char ***) NULL; 444*0Sstevel@tonic-gate } 445*0Sstevel@tonic-gate } 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate /* Return ptr to the list of addresses of lists (or NULL if none) */ 448*0Sstevel@tonic-gate return(addrlist); 449*0Sstevel@tonic-gate } 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate /* 452*0Sstevel@tonic-gate * void freereqlist(list) 453*0Sstevel@tonic-gate * char ***list 454*0Sstevel@tonic-gate * 455*0Sstevel@tonic-gate * This function frees the space allocated to the list of lists 456*0Sstevel@tonic-gate * referenced by <list> 457*0Sstevel@tonic-gate * 458*0Sstevel@tonic-gate * Arguments: 459*0Sstevel@tonic-gate * char ***list Address of the list of lists 460*0Sstevel@tonic-gate * 461*0Sstevel@tonic-gate * Returns: void 462*0Sstevel@tonic-gate */ 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gate static void 465*0Sstevel@tonic-gate freereqlist(list) 466*0Sstevel@tonic-gate char ***list; 467*0Sstevel@tonic-gate { 468*0Sstevel@tonic-gate char ***ppp; 469*0Sstevel@tonic-gate if (list) { 470*0Sstevel@tonic-gate for (ppp = list ; *ppp ; ppp++) free((char *) *ppp); 471*0Sstevel@tonic-gate free((char *) list); 472*0Sstevel@tonic-gate } 473*0Sstevel@tonic-gate } 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gate /* 476*0Sstevel@tonic-gate * int ndevsin(list, delims) 477*0Sstevel@tonic-gate * char *list 478*0Sstevel@tonic-gate * char *delims 479*0Sstevel@tonic-gate * 480*0Sstevel@tonic-gate * This function determines how many tokens are in the list <list>. 481*0Sstevel@tonic-gate * The tokens are delimited by fields of characters in the string 482*0Sstevel@tonic-gate * <delims>. It returns the number of tokens in the list. 483*0Sstevel@tonic-gate * 484*0Sstevel@tonic-gate * Arguments: 485*0Sstevel@tonic-gate * char *list The <delims>list of tokens to scan 486*0Sstevel@tonic-gate * char *delims The list of delimiters that define the list 487*0Sstevel@tonic-gate * 488*0Sstevel@tonic-gate * Returns: int 489*0Sstevel@tonic-gate * The number of elements in the list. 490*0Sstevel@tonic-gate * 491*0Sstevel@tonic-gate * Notes: 492*0Sstevel@tonic-gate * - This function does not recognize "null" elements. For example, 493*0Sstevel@tonic-gate * a,b,,,,c,,d contains 4 elememts (if delims contains a ',') 494*0Sstevel@tonic-gate */ 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate static int 497*0Sstevel@tonic-gate ndevsin(list, delims) 498*0Sstevel@tonic-gate char *list; /* List to scan */ 499*0Sstevel@tonic-gate char *delims; /* Delimiters */ 500*0Sstevel@tonic-gate { 501*0Sstevel@tonic-gate char *p; /* Running character pointer */ 502*0Sstevel@tonic-gate int count; /* Number of tokens seen so far */ 503*0Sstevel@tonic-gate int tokflag; /* TRUE if we're parsing a token */ 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate count = 0; /* None seen yet */ 506*0Sstevel@tonic-gate tokflag = FALSE; /* Not in a token */ 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate /* Scan the character-string containing the list of tokens */ 509*0Sstevel@tonic-gate for (p = list ; *p ; p++) { 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate /* If a delimiter, we're not in a token */ 512*0Sstevel@tonic-gate if (strchr(delims, *p)) tokflag = FALSE; 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate /* Otherwise, if we weren't in a token, we've found one */ 515*0Sstevel@tonic-gate else if (!tokflag) { 516*0Sstevel@tonic-gate tokflag = TRUE; 517*0Sstevel@tonic-gate count++; 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate /* Return the number of elements in the list */ 522*0Sstevel@tonic-gate return(count); 523*0Sstevel@tonic-gate } 524