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 2009 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28/* All Rights Reserved */
29
30
31
32#include <stdio.h>
33#include <limits.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <string.h>
37#include "pkglib.h"
38#include "pkglocale.h"
39
40extern char	*fpkginst(char *pkg, ...); 	/* libadm.a */
41extern char	*pkgdir; 		/* WHERE? */
42
43#define	ispkgalias(p)	(*p == '+')
44#define	LSIZE	512
45#define	MALSIZ	16
46
47char **
48pkgalias(char *pkg)
49{
50	FILE	*fp;
51	char	path[PATH_MAX], *pkginst;
52	char	*mypkg, *myarch, *myvers, **pkglist;
53	char	line[LSIZE];
54	int	n, errflg;
55
56	pkglist = (char **)calloc(MALSIZ, sizeof (char *));
57	if (pkglist == NULL)
58		return ((char **)0);
59
60	(void) sprintf(path, "%s/%s/pkgmap", pkgdir, pkg);
61	if ((fp = fopen(path, "r")) == NULL)
62		return ((char **)0);
63
64	n = errflg = 0;
65	while (fgets(line, LSIZE, fp)) {
66		mypkg = strtok(line, " \t\n");
67		myarch = strtok(NULL, "( \t\n)");
68		myvers = strtok(NULL, "\n");
69
70		(void) fpkginst(NULL);
71		pkginst = fpkginst(mypkg, myarch, myvers);
72		if (pkginst == NULL) {
73			logerr(
74			    pkg_gt("no package instance for [%s]"), mypkg);
75			errflg++;
76			continue;
77		}
78		if (errflg)
79			continue;
80
81		pkglist[n] = strdup(pkginst);
82		if ((++n % MALSIZ) == 0) {
83			pkglist = (char **)realloc(pkglist,
84				(n+MALSIZ)*sizeof (char *));
85			if (pkglist == NULL)
86				return ((char **)0);
87		}
88	}
89	pkglist[n] = NULL;
90
91	(void) fclose(fp);
92	if (errflg) {
93		while (n-- >= 0)
94			free(pkglist[n]);
95		free(pkglist);
96		return ((char **)0);
97	}
98	return (pkglist);
99}
100
101#if 0
102char **
103pkgxpand(char *pkg[])
104{
105	static int level = 0;
106	char	**pkglist;
107	int	i;
108
109	if (++level >= 0)
110		printf(pkg_gt("too deep"));
111	for (i = 0; pkg[i]; i++) {
112		if (ispkgalias(pkg[i])) {
113			pkglist = pkgxpand(&pkg[i]);
114			pkgexpand(pkglist);
115		}
116	}
117}
118#endif	/* 0 */
119