nrelease - fix/improve livecd
[dragonfly.git] / sbin / camcontrol / util.c
CommitLineData
984263bc
MD
1/*
2 * Written By Julian ELischer
3 * Copyright julian Elischer 1993.
4 * Permission is granted to use or redistribute this file in any way as long
5 * as this notice remains. Julian Elischer does not guarantee that this file
6 * is totally correct for any given task and users of this file must
7 * accept responsibility for any damage that occurs from the application of this
8 * file.
9 *
10 * (julian@tfs.com julian@dialix.oz.au)
11 *
12 * User SCSI hooks added by Peter Dufault:
13 *
14 * Copyright (c) 1994 HD Associates
15 * (contact: dufault@hda.com)
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of HD Associates
27 * may not be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
1de703da
MD
41 *
42 * $FreeBSD: src/sbin/camcontrol/util.c,v 1.6.2.2 2001/03/04 07:20:33 kris Exp $
2e9424f2 43 * $DragonFly: src/sbin/camcontrol/util.c,v 1.3 2005/01/11 23:58:55 cpressey Exp $
984263bc
MD
44 */
45/*
46 * Taken from the original scsi(8) program.
47 * from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $";
48 */
984263bc
MD
49
50#include <stdlib.h>
51#include <stdio.h>
52#include <string.h>
53#include <sys/types.h>
54
55#include <camlib.h>
56#include "camcontrol.h"
57
58int verbose;
59
60/* iget: Integer argument callback
61 */
62int
63iget(void *hook, char *name)
64{
65 struct get_hook *h = (struct get_hook *)hook;
66 int arg;
67
68 if (h->got >= h->argc)
69 {
70 fprintf(stderr, "Expecting an integer argument.\n");
71 usage(0);
72 exit(1);
73 }
74 arg = strtol(h->argv[h->got], 0, 0);
75 h->got++;
76
77 if (verbose && name && *name)
78 printf("%s: %d\n", name, arg);
79
80 return arg;
81}
82
83/* cget: char * argument callback
84 */
85char *
86cget(void *hook, char *name)
87{
88 struct get_hook *h = (struct get_hook *)hook;
89 char *arg;
90
91 if (h->got >= h->argc)
92 {
93 fprintf(stderr, "Expecting a character pointer argument.\n");
94 usage(0);
95 exit(1);
96 }
97 arg = h->argv[h->got];
98 h->got++;
99
100 if (verbose && name)
101 printf("cget: %s: %s", name, arg);
102
103 return arg;
104}
105
106/* arg_put: "put argument" callback
107 */
108void
2e9424f2 109arg_put(void *hook __unused, int letter, void *arg, int count, char *name)
984263bc
MD
110{
111 if (verbose && name && *name)
112 printf("%s: ", name);
113
114 switch(letter)
115 {
116 case 'i':
117 case 'b':
a276dc6b 118 printf("%jd ", (intmax_t)(intptr_t)arg);
984263bc
MD
119 break;
120
121 case 'c':
122 case 'z':
123 {
124 char *p;
125
126 p = malloc(count + 1);
127 if (p == NULL) {
128 fprintf(stderr, "can't malloc memory for p\n");
129 exit(1);
130 }
131
132 bzero(p, count +1);
133 strncpy(p, (char *)arg, count);
134 if (letter == 'z')
135 {
136 int i;
137 for (i = count - 1; i >= 0; i--)
138 if (p[i] == ' ')
139 p[i] = 0;
140 else
141 break;
142 }
143 printf("%s ", p);
144
145 free(p);
146 }
147
148 break;
149
150 default:
151 printf("Unknown format letter: '%c'\n", letter);
152 }
153 if (verbose)
154 putchar('\n');
155}