xref: /titanic_50/usr/src/cmd/svr4pkg/libinst/flex_dev.c (revision 49f9b365248ee858ee91baa36eab27c5200f6dca)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <libintl.h>
36 #include <pkglib.h>
37 #include "libadm.h"
38 
39 #define	ERR_CHDIR	"unable to chdir back to <%s>, errno=%d"
40 #define	ERR_GETCWD	"unable to determine the current working directory, " \
41 			"errno=%d"
42 
43 char *
44 flex_device(char *device_name, int dev_ok)
45 {
46 	char		new_device_name[PATH_MAX];
47 	char		*np = device_name;
48 	char		*cwd = NULL;
49 
50 	if (!device_name || !*device_name)		/* NULL or empty */
51 		return (np);
52 
53 	if (dev_ok == 1 && listdev(np) != (char **) NULL) /* device.tab */
54 		return (np);
55 
56 	if (!strncmp(np, "/dev", 4))			/* /dev path */
57 		return (np);
58 
59 	if ((cwd = getcwd(NULL, PATH_MAX)) == NULL) {
60 		progerr(gettext(ERR_GETCWD), errno);
61 		exit(99);
62 	}
63 
64 	if (realpath(np, new_device_name) == NULL) {	/* path */
65 		if (chdir(cwd) == -1) {
66 			progerr(gettext(ERR_CHDIR), cwd, errno);
67 			(void) free(cwd);
68 			exit(99);
69 		}
70 		if (*np != '/' && dev_ok == 2) {
71 			(void) sprintf(new_device_name, "%s/%s", cwd, np);
72 			canonize(new_device_name);
73 			if ((np = strdup(new_device_name)) == NULL)
74 				np = device_name;
75 		}
76 		(void) free(cwd);
77 		return (np);
78 	}
79 
80 	if (strcmp(np, new_device_name)) {
81 		if ((np = strdup(new_device_name)) == NULL)
82 			np = device_name;
83 	}
84 
85 	(void) free(cwd);
86 	return (np);
87 }
88