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