xref: /illumos-gate/usr/src/cmd/svr4pkg/libinst/flex_dev.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland 
28*5c51f124SMoriah Waterland 
29*5c51f124SMoriah Waterland #include <stdio.h>
30*5c51f124SMoriah Waterland #include <stdlib.h>
31*5c51f124SMoriah Waterland #include <unistd.h>
32*5c51f124SMoriah Waterland #include <limits.h>
33*5c51f124SMoriah Waterland #include <string.h>
34*5c51f124SMoriah Waterland #include <errno.h>
35*5c51f124SMoriah Waterland #include <libintl.h>
36*5c51f124SMoriah Waterland #include <pkglib.h>
37*5c51f124SMoriah Waterland #include "libadm.h"
38*5c51f124SMoriah Waterland 
39*5c51f124SMoriah Waterland #define	ERR_CHDIR	"unable to chdir back to <%s>, errno=%d"
40*5c51f124SMoriah Waterland #define	ERR_GETCWD	"unable to determine the current working directory, " \
41*5c51f124SMoriah Waterland 			"errno=%d"
42*5c51f124SMoriah Waterland 
43*5c51f124SMoriah Waterland char *
44*5c51f124SMoriah Waterland flex_device(char *device_name, int dev_ok)
45*5c51f124SMoriah Waterland {
46*5c51f124SMoriah Waterland 	char		new_device_name[PATH_MAX];
47*5c51f124SMoriah Waterland 	char		*np = device_name;
48*5c51f124SMoriah Waterland 	char		*cwd = NULL;
49*5c51f124SMoriah Waterland 
50*5c51f124SMoriah Waterland 	if (!device_name || !*device_name)		/* NULL or empty */
51*5c51f124SMoriah Waterland 		return (np);
52*5c51f124SMoriah Waterland 
53*5c51f124SMoriah Waterland 	if (dev_ok == 1 && listdev(np) != (char **) NULL) /* device.tab */
54*5c51f124SMoriah Waterland 		return (np);
55*5c51f124SMoriah Waterland 
56*5c51f124SMoriah Waterland 	if (!strncmp(np, "/dev", 4))			/* /dev path */
57*5c51f124SMoriah Waterland 		return (np);
58*5c51f124SMoriah Waterland 
59*5c51f124SMoriah Waterland 	if ((cwd = getcwd(NULL, PATH_MAX)) == NULL) {
60*5c51f124SMoriah Waterland 		progerr(gettext(ERR_GETCWD), errno);
61*5c51f124SMoriah Waterland 		exit(99);
62*5c51f124SMoriah Waterland 	}
63*5c51f124SMoriah Waterland 
64*5c51f124SMoriah Waterland 	if (realpath(np, new_device_name) == NULL) {	/* path */
65*5c51f124SMoriah Waterland 		if (chdir(cwd) == -1) {
66*5c51f124SMoriah Waterland 			progerr(gettext(ERR_CHDIR), cwd, errno);
67*5c51f124SMoriah Waterland 			(void) free(cwd);
68*5c51f124SMoriah Waterland 			exit(99);
69*5c51f124SMoriah Waterland 		}
70*5c51f124SMoriah Waterland 		if (*np != '/' && dev_ok == 2) {
71*5c51f124SMoriah Waterland 			(void) sprintf(new_device_name, "%s/%s", cwd, np);
72*5c51f124SMoriah Waterland 			canonize(new_device_name);
73*5c51f124SMoriah Waterland 			if ((np = strdup(new_device_name)) == NULL)
74*5c51f124SMoriah Waterland 				np = device_name;
75*5c51f124SMoriah Waterland 		}
76*5c51f124SMoriah Waterland 		(void) free(cwd);
77*5c51f124SMoriah Waterland 		return (np);
78*5c51f124SMoriah Waterland 	}
79*5c51f124SMoriah Waterland 
80*5c51f124SMoriah Waterland 	if (strcmp(np, new_device_name)) {
81*5c51f124SMoriah Waterland 		if ((np = strdup(new_device_name)) == NULL)
82*5c51f124SMoriah Waterland 			np = device_name;
83*5c51f124SMoriah Waterland 	}
84*5c51f124SMoriah Waterland 
85*5c51f124SMoriah Waterland 	(void) free(cwd);
86*5c51f124SMoriah Waterland 	return (np);
87*5c51f124SMoriah Waterland }
88