xref: /illumos-gate/usr/src/cmd/lofiadm/utils.c (revision 4703203d)
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) 1999-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <sys/statvfs.h>
33 #include <sys/sysmacros.h>
34 #include <libintl.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <errno.h>
40 
41 #include "utils.h"
42 
43 static const char PNAME_FMT[] = "%s: ";
44 static const char ERRNO_FMT[] = ": %s\n";
45 
46 static const char *pname;
47 
48 /*PRINTFLIKE1*/
49 static void
50 warn(const char *format, ...)
51 {
52 	int err = errno;
53 	va_list alist;
54 
55 	if (pname != NULL)
56 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
57 
58 	va_start(alist, format);
59 	(void) vfprintf(stderr, format, alist);
60 	va_end(alist);
61 
62 	if (strchr(format, '\n') == NULL)
63 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
64 }
65 
66 /*PRINTFLIKE1*/
67 void
68 die(const char *format, ...)
69 {
70 	int err = errno;
71 	va_list alist;
72 
73 	if (pname != NULL)
74 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
75 
76 	va_start(alist, format);
77 	(void) vfprintf(stderr, format, alist);
78 	va_end(alist);
79 
80 	if (strchr(format, '\n') == NULL)
81 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
82 
83 	exit(E_ERROR);
84 }
85 
86 const char *
87 getpname(const char *arg0)
88 {
89 	const char *p = strrchr(arg0, '/');
90 
91 	if (p == NULL)
92 		p = arg0;
93 	else
94 		p++;
95 
96 	pname = p;
97 	return (p);
98 }
99 
100 int
101 valid_abspath(const char *p)
102 {
103 	if (p[0] != '/') {
104 		warn(gettext("pathname is not an absolute path -- %s\n"), p);
105 		return (0);
106 	}
107 
108 	if (strlen(p) > MAXPATHLEN) {
109 		warn(gettext("pathname is too long -- %s\n"), p);
110 		return (0);
111 	}
112 
113 	return (1);
114 }
115