xref: /titanic_52/usr/src/cmd/lofiadm/utils.c (revision 579df0ad79cbf73b8473820d721b202c79d12d34)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*579df0adSaalok  * Common Development and Distribution License (the "License").
6*579df0adSaalok  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*579df0adSaalok  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*579df0adSaalok  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <stdarg.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
39*579df0adSaalok #include <dlfcn.h>
40*579df0adSaalok #include <link.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "utils.h"
437c478bd9Sstevel@tonic-gate 
44*579df0adSaalok static void *lib_hdl = NULL;
45*579df0adSaalok 
467c478bd9Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: ";
477c478bd9Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n";
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static const char *pname;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
527c478bd9Sstevel@tonic-gate static void
537c478bd9Sstevel@tonic-gate warn(const char *format, ...)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	int err = errno;
567c478bd9Sstevel@tonic-gate 	va_list alist;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (pname != NULL)
597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	va_start(alist, format);
627c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
637c478bd9Sstevel@tonic-gate 	va_end(alist);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
707c478bd9Sstevel@tonic-gate void
717c478bd9Sstevel@tonic-gate die(const char *format, ...)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	int err = errno;
747c478bd9Sstevel@tonic-gate 	va_list alist;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (pname != NULL)
777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	va_start(alist, format);
807c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
817c478bd9Sstevel@tonic-gate 	va_end(alist);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate const char *
907c478bd9Sstevel@tonic-gate getpname(const char *arg0)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	const char *p = strrchr(arg0, '/');
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (p == NULL)
957c478bd9Sstevel@tonic-gate 		p = arg0;
967c478bd9Sstevel@tonic-gate 	else
977c478bd9Sstevel@tonic-gate 		p++;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	pname = p;
1007c478bd9Sstevel@tonic-gate 	return (p);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
1047c478bd9Sstevel@tonic-gate valid_abspath(const char *p)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	if (p[0] != '/') {
1077c478bd9Sstevel@tonic-gate 		warn(gettext("pathname is not an absolute path -- %s\n"), p);
1087c478bd9Sstevel@tonic-gate 		return (0);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (strlen(p) > MAXPATHLEN) {
1127c478bd9Sstevel@tonic-gate 		warn(gettext("pathname is too long -- %s\n"), p);
1137c478bd9Sstevel@tonic-gate 		return (0);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	return (1);
1177c478bd9Sstevel@tonic-gate }
118*579df0adSaalok 
119*579df0adSaalok /*
120*579df0adSaalok  * Wrapper for dlopen'ing a library.
121*579df0adSaalok  * The caller must call closelib() once
122*579df0adSaalok  * access to the library is no longer needed.
123*579df0adSaalok  */
124*579df0adSaalok void *
125*579df0adSaalok openlib(const char *lib)
126*579df0adSaalok {
127*579df0adSaalok 	lib_hdl = dlopen(lib, RTLD_LAZY);
128*579df0adSaalok 	return (lib_hdl);
129*579df0adSaalok }
130*579df0adSaalok 
131*579df0adSaalok void
132*579df0adSaalok closelib()
133*579df0adSaalok {
134*579df0adSaalok 	if (lib_hdl != NULL)
135*579df0adSaalok 		(void) dlclose(lib_hdl);
136*579df0adSaalok }
137