8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / devmgmt / cmds / listdgrp.c
blob7bcbce8115ce203d6d0a8d5bd4907243996088fe
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * listdgrp.c
36 * Contains
37 * listdgrp Writes on the standard output stream a list of devices
38 * that belong to the specified device group
41 #include <sys/types.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <devmgmt.h>
47 #include <devtab.h>
48 #include <fmtmsg.h>
52 * Local Definitions
53 * TRUE Boolean TRUE value (if not already defined)
54 * FALSE Boolean not-TRUE value (if not already defined)
57 #ifndef TRUE
58 #define TRUE ('t')
59 #endif
61 #ifndef FALSE
62 #define FALSE (0)
63 #endif
66 * Messages:
67 * M_USAGE Command usage error
68 * M_NODGRP Device group not found
69 * M_DGRPTAB Device-group table not found
70 * M_ERROR Internal error
73 #define M_USAGE "usage: listdgrp dgroup"
74 #define M_NODGRP "Device group not found: %s"
75 #define M_DGRPTAB "Cannot open device-group table: %s"
76 #define M_ERROR "Internal error, errno=%d"
80 * Exit codes
81 * EX_OK Exiting okay, no problem
82 * EX_ERROR Some problem with the command
83 * EX_NODGRPTAB Device group table could not be opened
84 * EX_NODGROUP Device group couldn't be found
87 #define EX_OK 0
88 #define EX_ERROR 1
89 #define EX_NODGRPTAB 2
90 #define EX_NODGROUP 3
94 * Macros
95 * stdmsg(r,l,s,t) Write a message in standard format
96 * r Recoverability flag
97 * l Label
98 * s Severity
99 * t Tag
102 #define stdmsg(r,l,s,t) (void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
105 * Global Variables
110 * Static Variables
112 * lbl Buffer for the message label
115 static char lbl[MM_MXLABELLN+1];
116 static char msg[MM_MXTXTLN+1];
119 * listdgrp <dgroup>
121 * List the devices that belong to the device group <dgroup>.
122 * It writes the list to the standard output file (stdout)
123 * in a new-line list.
125 * Returns:
126 * 0 Ok
127 * 1 Syntax or other error
128 * 2 Device table can't be opened
129 * 3 Device group doesn't exist
133 main(int argc, char **argv)
137 * Automatic data
140 char **devices; /* List of devices in the group */
141 char **pp; /* Running pointer to device names */
142 char *cmdname; /* Simple command name */
143 char *dgrptab; /* The device-group table name */
144 char *dgroup; /* Device group to list */
145 int exitcode; /* Value to return to the caller */
146 int sev; /* Message severity */
147 int optchar; /* Option char (from getopt()) */
148 int usageerr; /* TRUE if syntax error on command */
151 /* Build the message label from the (simple) command name */
152 if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
153 else cmdname = argv[0];
154 (void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));
156 /* Only write the text component of a message (this goes away in SVR4.1) */
157 (void) putenv("MSGVERB=text");
160 * Parse the command line:
161 * - Options
162 * - Device group to display
166 * Extract options from the command line
169 /* Initializations */
170 usageerr = FALSE; /* No errors on the command line (yet) */
173 * Loop until all of the command line options have been parced
174 * (and don't let getopt() write messages)
177 opterr = FALSE;
178 while ((optchar = getopt(argc, argv, "")) != EOF) switch (optchar) {
180 /* Default case -- command usage error */
181 case '?':
182 default:
183 usageerr = TRUE;
184 break;
187 /* Check the argument count and extract the device group name */
188 if (usageerr || (optind != argc-1)) usageerr = TRUE;
189 else dgroup = argv[optind];
191 /* If there is a usage error, write an appropriate message and exit */
192 if (usageerr) {
193 stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
194 exit(EX_ERROR);
197 /* Open the device-group file (if there's one to be opened) */
198 if (!_opendgrptab("r")) {
199 if (dgrptab = _dgrptabpath()) {
200 (void) snprintf(msg, sizeof(msg), M_DGRPTAB, dgrptab);
201 exitcode = EX_NODGRPTAB;
202 sev = MM_ERROR;
203 } else {
204 (void) sprintf(msg, M_ERROR, errno);
205 exitcode = EX_ERROR;
206 sev = MM_HALT;
208 stdmsg(MM_NRECOV, lbl, sev, msg);
209 exit(exitcode);
214 * Get the list of devices associated with the device group.
215 * If we get one, write the list to the standard output.
216 * Otherwise, write an appropriate error message
219 exitcode = EX_OK;
220 if (devices = listdgrp(dgroup)) {
221 for (pp = devices ; *pp ; pp++) (void) puts(*pp);
223 else {
224 if (errno == EINVAL) {
225 (void) snprintf(msg, sizeof(msg), M_NODGRP, dgroup);
226 stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
227 exitcode = EX_NODGROUP;
229 else {
230 (void) sprintf(msg, M_ERROR, errno);
231 stdmsg(MM_NRECOV, lbl, MM_HALT, msg);
232 exitcode = EX_ERROR;
236 /* Finished (now wasn't that special?) */
237 return(exitcode);