xref: /titanic_52/usr/src/cmd/acctadm/utils.c (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
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/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 char PNAME_FMT[] = "%s: ";
40 static char ERRNO_FMT[] = ": %s\n";
41 
42 static char *pname;
43 
44 /*PRINTFLIKE1*/
45 void
46 warn(char *format, ...)
47 {
48 	int err = errno;
49 	va_list alist;
50 	if (pname != NULL)
51 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
52 	va_start(alist, format);
53 	(void) vfprintf(stderr, format, alist);
54 	va_end(alist);
55 	if (strchr(format, '\n') == NULL)
56 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
57 }
58 
59 /*PRINTFLIKE1*/
60 void
61 die(char *format, ...)
62 {
63 	int err = errno;
64 	va_list alist;
65 
66 	if (pname != NULL)
67 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
68 	va_start(alist, format);
69 	(void) vfprintf(stderr, format, alist);
70 	va_end(alist);
71 	if (strchr(format, '\n') == NULL)
72 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
73 	exit(E_ERROR);
74 }
75 
76 char *
77 setprogname(char *arg0)
78 {
79 	char *p = strrchr(arg0, '/');
80 
81 	if (p == NULL)
82 		p = arg0;
83 	else
84 		p++;
85 	pname = p;
86 	return (pname);
87 }
88 
89 int
90 valid_abspath(char *p)
91 {
92 	if (p[0] != '/') {
93 		die(gettext("%s is not an absolute pathname\n"), p);
94 	}
95 	if (strlen(p) > MAXPATHLEN) {
96 		errno = ENAMETOOLONG;
97 		return (0);
98 	}
99 	return (1);
100 }
101