xref: /illumos-gate/usr/src/cmd/dumpadm/utils.c (revision d17be682a2c70b4505d43c830bbd2603da11918d)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <sys/param.h>
28 #include <libintl.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <errno.h>
34 
35 #include "utils.h"
36 
37 static const char PNAME_FMT[] = "%s: ";
38 static const char ERRNO_FMT[] = ": %s\n";
39 
40 static const char *pname;
41 
42 /*PRINTFLIKE1*/
43 void
44 warn(const char *format, ...)
45 {
46 	int err = errno;
47 	va_list alist;
48 
49 	if (pname != NULL)
50 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
51 
52 	va_start(alist, format);
53 	(void) vfprintf(stderr, format, alist);
54 	va_end(alist);
55 
56 	if (strchr(format, '\n') == NULL)
57 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
58 }
59 
60 /*PRINTFLIKE1*/
61 void
62 die(const char *format, ...)
63 {
64 	int err = errno;
65 	va_list alist;
66 
67 	if (pname != NULL)
68 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
69 
70 	va_start(alist, format);
71 	(void) vfprintf(stderr, format, alist);
72 	va_end(alist);
73 
74 	if (strchr(format, '\n') == NULL)
75 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
76 
77 	exit(E_ERROR);
78 }
79 
80 const char *
81 getpname(const char *arg0)
82 {
83 	const char *p = strrchr(arg0, '/');
84 
85 	if (p == NULL)
86 		p = arg0;
87 	else
88 		p++;
89 
90 	pname = p;
91 	return (p);
92 }
93 
94 int
95 valid_abspath(const char *p)
96 {
97 	if (p[0] != '/') {
98 		warn(gettext("pathname is not an absolute path -- %s\n"), p);
99 		return (0);
100 	}
101 
102 	if (strlen(p) > MAXPATHLEN) {
103 		warn(gettext("pathname is too long -- %s\n"), p);
104 		return (0);
105 	}
106 
107 	return (1);
108 }
109 
110 int
111 valid_str2int(const char *p, int *ip)
112 {
113 	int i;
114 	char *q;
115 
116 	errno = 0;
117 	i = (int)strtol(p, &q, 10);
118 
119 	if (errno != 0 || q == p || i < 0 || (*q != '\0' && *q != '\n'))
120 		return (0);
121 
122 	*ip = i;
123 	return (1);
124 }
125 
126 int
127 valid_str2ull(const char *p, unsigned long long *ullp)
128 {
129 	long long ll;
130 	char *q;
131 
132 	errno = 0;
133 	ll = strtoll(p, &q, 10);
134 
135 	if (errno != 0 || q == p || ll < 0LL || (*q != '\0' && *q != '\n'))
136 		return (0);
137 
138 	*ullp = (unsigned long long)ll;
139 	return (1);
140 }
141