15c51f12Moriah Waterland/*
25c51f12Moriah Waterland * CDDL HEADER START
35c51f12Moriah Waterland *
45c51f12Moriah Waterland * The contents of this file are subject to the terms of the
55c51f12Moriah Waterland * Common Development and Distribution License (the "License").
65c51f12Moriah Waterland * You may not use this file except in compliance with the License.
75c51f12Moriah Waterland *
85c51f12Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f12Moriah Waterland * or http://www.opensolaris.org/os/licensing.
105c51f12Moriah Waterland * See the License for the specific language governing permissions
115c51f12Moriah Waterland * and limitations under the License.
125c51f12Moriah Waterland *
135c51f12Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
145c51f12Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f12Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
165c51f12Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
175c51f12Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f12Moriah Waterland *
195c51f12Moriah Waterland * CDDL HEADER END
205c51f12Moriah Waterland */
215c51f12Moriah Waterland
225c51f12Moriah Waterland/*
235c51f12Moriah Waterland * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
245c51f12Moriah Waterland * Use is subject to license terms.
255c51f12Moriah Waterland */
265c51f12Moriah Waterland
275c51f12Moriah Waterland
285c51f12Moriah Waterland
295c51f12Moriah Waterland#include <stdio.h>
305c51f12Moriah Waterland#include <stdlib.h>
315c51f12Moriah Waterland#include <unistd.h>
325c51f12Moriah Waterland#include <limits.h>
335c51f12Moriah Waterland#include <string.h>
345c51f12Moriah Waterland#include <errno.h>
355c51f12Moriah Waterland#include <libintl.h>
365c51f12Moriah Waterland#include <pkglib.h>
375c51f12Moriah Waterland#include "libadm.h"
385c51f12Moriah Waterland
395c51f12Moriah Waterland#define	ERR_CHDIR	"unable to chdir back to <%s>, errno=%d"
405c51f12Moriah Waterland#define	ERR_GETCWD	"unable to determine the current working directory, " \
415c51f12Moriah Waterland			"errno=%d"
425c51f12Moriah Waterland
435c51f12Moriah Waterlandchar *
445c51f12Moriah Waterlandflex_device(char *device_name, int dev_ok)
455c51f12Moriah Waterland{
465c51f12Moriah Waterland	char		new_device_name[PATH_MAX];
475c51f12Moriah Waterland	char		*np = device_name;
485c51f12Moriah Waterland	char		*cwd = NULL;
495c51f12Moriah Waterland
505c51f12Moriah Waterland	if (!device_name || !*device_name)		/* NULL or empty */
515c51f12Moriah Waterland		return (np);
525c51f12Moriah Waterland
535c51f12Moriah Waterland	if (dev_ok == 1 && listdev(np) != (char **) NULL) /* device.tab */
545c51f12Moriah Waterland		return (np);
555c51f12Moriah Waterland
565c51f12Moriah Waterland	if (!strncmp(np, "/dev", 4))			/* /dev path */
575c51f12Moriah Waterland		return (np);
585c51f12Moriah Waterland
595c51f12Moriah Waterland	if ((cwd = getcwd(NULL, PATH_MAX)) == NULL) {
605c51f12Moriah Waterland		progerr(gettext(ERR_GETCWD), errno);
615c51f12Moriah Waterland		exit(99);
625c51f12Moriah Waterland	}
635c51f12Moriah Waterland
645c51f12Moriah Waterland	if (realpath(np, new_device_name) == NULL) {	/* path */
655c51f12Moriah Waterland		if (chdir(cwd) == -1) {
665c51f12Moriah Waterland			progerr(gettext(ERR_CHDIR), cwd, errno);
675c51f12Moriah Waterland			(void) free(cwd);
685c51f12Moriah Waterland			exit(99);
695c51f12Moriah Waterland		}
705c51f12Moriah Waterland		if (*np != '/' && dev_ok == 2) {
715c51f12Moriah Waterland			(void) sprintf(new_device_name, "%s/%s", cwd, np);
725c51f12Moriah Waterland			canonize(new_device_name);
735c51f12Moriah Waterland			if ((np = strdup(new_device_name)) == NULL)
745c51f12Moriah Waterland				np = device_name;
755c51f12Moriah Waterland		}
765c51f12Moriah Waterland		(void) free(cwd);
775c51f12Moriah Waterland		return (np);
785c51f12Moriah Waterland	}
795c51f12Moriah Waterland
805c51f12Moriah Waterland	if (strcmp(np, new_device_name)) {
815c51f12Moriah Waterland		if ((np = strdup(new_device_name)) == NULL)
825c51f12Moriah Waterland			np = device_name;
835c51f12Moriah Waterland	}
845c51f12Moriah Waterland
855c51f12Moriah Waterland	(void) free(cwd);
865c51f12Moriah Waterland	return (np);
875c51f12Moriah Waterland}