8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / lofiadm / utils.c
blob6b94155ab0a80e2b189f18daa965c9726ab05bb3
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <sys/statvfs.h>
30 #include <sys/sysmacros.h>
31 #include <libintl.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <dlfcn.h>
38 #include <link.h>
40 #include "utils.h"
42 static void *lib_hdl = NULL;
44 static const char PNAME_FMT[] = "%s: ";
45 static const char ERRNO_FMT[] = ": %s\n";
47 static const char *pname;
49 /*PRINTFLIKE1*/
50 void
51 warn(const char *format, ...)
53 int err = errno;
54 va_list alist;
56 if (pname != NULL)
57 (void) fprintf(stderr, gettext(PNAME_FMT), pname);
59 va_start(alist, format);
60 (void) vfprintf(stderr, format, alist);
61 va_end(alist);
63 if (strchr(format, '\n') == NULL)
64 (void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
67 /*PRINTFLIKE1*/
68 void
69 die(const char *format, ...)
71 int err = errno;
72 va_list alist;
74 if (pname != NULL)
75 (void) fprintf(stderr, gettext(PNAME_FMT), pname);
77 va_start(alist, format);
78 (void) vfprintf(stderr, format, alist);
79 va_end(alist);
81 if (strchr(format, '\n') == NULL)
82 (void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
83 exit(E_ERROR);
86 const char *
87 getpname(const char *arg0)
89 const char *p = strrchr(arg0, '/');
91 if (p == NULL)
92 p = arg0;
93 else
94 p++;
96 pname = p;
97 return (p);
101 valid_abspath(const char *p)
103 if (p[0] != '/') {
104 warn(gettext("pathname is not an absolute path -- %s\n"), p);
105 return (0);
108 if (strlen(p) > MAXPATHLEN) {
109 warn(gettext("pathname is too long -- %s\n"), p);
110 return (0);
113 return (1);
117 * Wrapper for dlopen'ing a library.
118 * The caller must call closelib() once
119 * access to the library is no longer needed.
121 void *
122 openlib(const char *lib)
124 lib_hdl = dlopen(lib, RTLD_LAZY);
125 return (lib_hdl);
128 void
129 closelib()
131 if (lib_hdl != NULL)
132 (void) dlclose(lib_hdl);