comm: add -Wall
[unleashed.git] / usr / src / cmd / dumpadm / utils.c
blob37710a712988c631a8e765f882c1fc6bba1e7ce7
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 (c) 1998 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
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>
37 #include "utils.h"
39 static const char PNAME_FMT[] = "%s: ";
40 static const char ERRNO_FMT[] = ": %s\n";
42 static const char *pname;
44 /*PRINTFLIKE1*/
45 void
46 warn(const char *format, ...)
48 int err = errno;
49 va_list alist;
51 if (pname != NULL)
52 (void) fprintf(stderr, gettext(PNAME_FMT), pname);
54 va_start(alist, format);
55 (void) vfprintf(stderr, format, alist);
56 va_end(alist);
58 if (strchr(format, '\n') == NULL)
59 (void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
62 /*PRINTFLIKE1*/
63 void
64 die(const char *format, ...)
66 int err = errno;
67 va_list alist;
69 if (pname != NULL)
70 (void) fprintf(stderr, gettext(PNAME_FMT), pname);
72 va_start(alist, format);
73 (void) vfprintf(stderr, format, alist);
74 va_end(alist);
76 if (strchr(format, '\n') == NULL)
77 (void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
79 exit(E_ERROR);
82 const char *
83 getpname(const char *arg0)
85 const char *p = strrchr(arg0, '/');
87 if (p == NULL)
88 p = arg0;
89 else
90 p++;
92 pname = p;
93 return (p);
96 int
97 valid_abspath(const char *p)
99 if (p[0] != '/') {
100 warn(gettext("pathname is not an absolute path -- %s\n"), p);
101 return (0);
104 if (strlen(p) > MAXPATHLEN) {
105 warn(gettext("pathname is too long -- %s\n"), p);
106 return (0);
109 return (1);
113 valid_str2int(const char *p, int *ip)
115 int i;
116 char *q;
118 errno = 0;
119 i = (int)strtol(p, &q, 10);
121 if (errno != 0 || q == p || i < 0 || (*q != '\0' && *q != '\n'))
122 return (0);
124 *ip = i;
125 return (1);
129 valid_str2ull(const char *p, unsigned long long *ullp)
131 long long ll;
132 char *q;
134 errno = 0;
135 ll = strtoll(p, &q, 10);
137 if (errno != 0 || q == p || ll < 0LL || (*q != '\0' && *q != '\n'))
138 return (0);
140 *ullp = (unsigned long long)ll;
141 return (1);