xref: /onnv-gate/usr/src/lib/libbc/libc/sys/common/_open.c (revision 11798:1e7f1f154004)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53224Sraf  * Common Development and Distribution License (the "License").
63224Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213224Sraf 
220Sstevel@tonic-gate /*
23*11798SRoger.Faulkner@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <fcntl.h>
29722Smuffin #include <string.h>
300Sstevel@tonic-gate #include <mntent.h>
313224Sraf #include <sys/syscall.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <dirent.h>
370Sstevel@tonic-gate #include "s5sysmacros.h"
380Sstevel@tonic-gate #include "compat.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	PRINTER_DIR		"/etc/lp/printers/"
410Sstevel@tonic-gate #define	PRINTER_CONFIG_FILE	"/configuration"
420Sstevel@tonic-gate #define	MNT_LINE_MAX 1024
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	GETTOK(xx, ll) \
450Sstevel@tonic-gate 	if ((xx = strtok(ll, sepstr)) == NULL) \
460Sstevel@tonic-gate 		return (-1); \
470Sstevel@tonic-gate 	if (strcmp(xx, dash) == 0) \
480Sstevel@tonic-gate 		xx = NULL
490Sstevel@tonic-gate 
500Sstevel@tonic-gate char *mktemp();
51722Smuffin 
520Sstevel@tonic-gate static void getPrinterInfo(char *, FILE *);
530Sstevel@tonic-gate static char sepstr[] = " \t\n";
540Sstevel@tonic-gate static char dash[] = "-";
55722Smuffin static int	open_printcap(void);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /* SVR4/SunOS 5.0 equivalent modes */
580Sstevel@tonic-gate #define	N_O_NDELAY	0x04
590Sstevel@tonic-gate #define	N_O_SYNC	0x10
600Sstevel@tonic-gate #define	N_O_NONBLOCK	0x80
610Sstevel@tonic-gate #define	N_O_CREAT	0x100
620Sstevel@tonic-gate #define	N_O_TRUNC	0x200
630Sstevel@tonic-gate #define	N_O_EXCL	0x400
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /* Mask corresponding to the bits above in SunOS 4.x */
660Sstevel@tonic-gate #define	FLAGS_MASK	(O_SYNC|O_NONBLOCK|O_CREAT|O_TRUNC|O_EXCL \
670Sstevel@tonic-gate 			|_FNDELAY|_FNBIO)
680Sstevel@tonic-gate 
690Sstevel@tonic-gate int
open_com(char * path,int flags,int mode)700Sstevel@tonic-gate open_com(char *path, int flags, int mode)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	int fd, fd2, pathl, inspt, ret = 0;
730Sstevel@tonic-gate 	int nflags = flags;
740Sstevel@tonic-gate 	char loc[] = "/lib/locale";
750Sstevel@tonic-gate 	char *loct = NULL;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (flags & FLAGS_MASK) {
780Sstevel@tonic-gate 		nflags = flags & ~FLAGS_MASK;
790Sstevel@tonic-gate 		if (flags & O_SYNC)
800Sstevel@tonic-gate 			nflags |= N_O_SYNC;
810Sstevel@tonic-gate 		if (flags & (_FNDELAY|O_NONBLOCK)) {
820Sstevel@tonic-gate 			nflags |= N_O_NONBLOCK;
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 		if (flags & O_CREAT)
850Sstevel@tonic-gate 			nflags |= N_O_CREAT;
860Sstevel@tonic-gate 		if (flags & O_TRUNC)
870Sstevel@tonic-gate 			nflags |= N_O_TRUNC;
880Sstevel@tonic-gate 		if (flags & O_EXCL)
890Sstevel@tonic-gate 			nflags |= N_O_EXCL;
900Sstevel@tonic-gate 		if (flags & _FNBIO)
910Sstevel@tonic-gate 			nflags |= N_O_NDELAY;
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /* change path from ..../lib/locale/.... to ..../lib/oldlocale/.... XXX */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if ((loct = (char *)_strstr(path, loc)) != NULL) { /* /lib/locale ?  */
970Sstevel@tonic-gate 		char locbuf[MAXPATHLEN+100];	  /* to hold new locale path */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		pathl = strlen(path);
1000Sstevel@tonic-gate 		inspt = pathl - strlen(loct) + 5;	/* pos to add "old" */
1010Sstevel@tonic-gate 		(void) strncpy(locbuf, path, inspt); /* copy  path upto lib */
1020Sstevel@tonic-gate 		locbuf[inspt] = '\0';		/* make it a string */
1030Sstevel@tonic-gate 		strcat(locbuf, "old");		/* add "old" */
1040Sstevel@tonic-gate 		strcat(locbuf, loct+5);		/* add remainer of path */
105*11798SRoger.Faulkner@Sun.COM 		return (_syscall(SYS_openat, AT_FDCWD, locbuf, nflags, mode));
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1083224Sraf 	if (strcmp(path, "/etc/mtab") == 0)
1090Sstevel@tonic-gate 		return (open_mnt("/etc/mnttab", "mtab", nflags, mode));
1103224Sraf 
1113224Sraf 	if (strcmp(path, "/etc/fstab") == 0)
1120Sstevel@tonic-gate 		return (open_mnt("/etc/vfstab", "fstab", nflags, mode));
1133224Sraf 
1143224Sraf 	if (strcmp(path, "/etc/printcap") == 0) {
115*11798SRoger.Faulkner@Sun.COM 		if ((fd = _syscall(SYS_openat, AT_FDCWD, path, nflags, mode))
116*11798SRoger.Faulkner@Sun.COM 		    >= 0)
1173224Sraf 			return (fd);
1180Sstevel@tonic-gate 		return (open_printcap());
1193224Sraf 	}
1203224Sraf 
1213224Sraf 	if (strcmp(path, "/etc/utmp") == 0 ||
1223224Sraf 	    strcmp(path, "/var/adm/utmp") == 0) {
123*11798SRoger.Faulkner@Sun.COM 		fd = _syscall(SYS_openat,
124*11798SRoger.Faulkner@Sun.COM 		    AT_FDCWD, "/var/adm/utmpx", nflags, mode);
1253224Sraf 		if (fd >= 0)
1263224Sraf 			fd_add(fd, UTMPX_MAGIC_FLAG);
1273224Sraf 		return (fd);
1283224Sraf 	}
1293224Sraf 
1303224Sraf 	if (strcmp(path, "/var/adm/wtmp") == 0) {
131*11798SRoger.Faulkner@Sun.COM 		fd = _syscall(SYS_openat,
132*11798SRoger.Faulkner@Sun.COM 		    AT_FDCWD, "/var/adm/wtmpx", nflags, mode);
1333224Sraf 		if (fd >= 0)
1343224Sraf 			fd_add(fd, UTMPX_MAGIC_FLAG);
1353224Sraf 		return (fd);
1363224Sraf 	}
1373224Sraf 
138*11798SRoger.Faulkner@Sun.COM 	return (_syscall(SYS_openat, AT_FDCWD, path, nflags, mode));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate int
open_mnt(char * fname,char * tname,int flags,int mode)1420Sstevel@tonic-gate open_mnt(char *fname, char *tname, int flags, int mode)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	FILE *fd_in, *fd_out;
1450Sstevel@tonic-gate 	FILE *_fopen();
1460Sstevel@tonic-gate 	char tmp_name[64];
1470Sstevel@tonic-gate 	char line[MNT_LINE_MAX];
1480Sstevel@tonic-gate 	int fd;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if ((fd_in = _fopen(fname, "r")) == NULL)
1510Sstevel@tonic-gate 		return (-1);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	sprintf(tmp_name, "%s%s%s", "/tmp/", tname, "XXXXXX");
1540Sstevel@tonic-gate 	mktemp(tmp_name);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if ((fd_out = _fopen(tmp_name, "a+")) == NULL) {
1570Sstevel@tonic-gate 		fclose(fd_in);
1580Sstevel@tonic-gate 		return (-1);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	while (getmntline(line, fd_in) != -1) {
1620Sstevel@tonic-gate 		if (strcmp(fname, "/etc/mnttab") == 0) {
1630Sstevel@tonic-gate 			if (putmline(line, fd_out) == -1) {
1640Sstevel@tonic-gate 				fclose(fd_in);
1650Sstevel@tonic-gate 				fclose(fd_out);
1660Sstevel@tonic-gate 				return (-1);
1670Sstevel@tonic-gate 			}
1683224Sraf 		} else {	/* processing vfstab */
1690Sstevel@tonic-gate 			if (putfline(line, fd_out) == -1) {
1700Sstevel@tonic-gate 				fclose(fd_in);
1710Sstevel@tonic-gate 				fclose(fd_out);
1720Sstevel@tonic-gate 				return (-1);
1730Sstevel@tonic-gate 			}
1743224Sraf 		}
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (feof(fd_in)) {
1780Sstevel@tonic-gate 		fclose(fd_in);
1790Sstevel@tonic-gate 		fclose(fd_out);
1800Sstevel@tonic-gate 
181*11798SRoger.Faulkner@Sun.COM 		fd = _syscall(SYS_openat, AT_FDCWD, tmp_name, O_RDONLY);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		if (fd == -1 || unlink(tmp_name) == -1)
1840Sstevel@tonic-gate 			return (-1);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		return (fd);
1870Sstevel@tonic-gate 	} else {
1880Sstevel@tonic-gate 		fclose(fd_in);
1890Sstevel@tonic-gate 		fclose(fd_out);
1900Sstevel@tonic-gate 		return (-1);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate int
getmntline(char * lp,FILE * fp)1950Sstevel@tonic-gate getmntline(char *lp, FILE *fp)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	int ret;
1980Sstevel@tonic-gate 	char *cp;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	while ((lp = fgets(lp, MNT_LINE_MAX, fp)) != NULL) {
2010Sstevel@tonic-gate 		if (strlen(lp) == MNT_LINE_MAX-1 && lp[MNT_LINE_MAX-2] != '\n')
2020Sstevel@tonic-gate 			return (-1);
2030Sstevel@tonic-gate 		for (cp = lp; *cp == ' ' || *cp == '\t'; cp++)
2040Sstevel@tonic-gate 			;
2050Sstevel@tonic-gate 		if (*cp != '#' && *cp != '\n')
2060Sstevel@tonic-gate 			return (0);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	return (-1);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate int
putmline(char * line,FILE * fp)2120Sstevel@tonic-gate putmline(char *line, FILE *fp)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	struct mntent mnt;
2150Sstevel@tonic-gate 	char *buf;
2160Sstevel@tonic-gate 	char *devnumstr = 0;	/* the device number, in (hex) ascii */
2170Sstevel@tonic-gate 	char *remainder;	/* remainder of mnt_opts string, after devnum */
2180Sstevel@tonic-gate 	unsigned long devnum;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	GETTOK(mnt.mnt_fsname, line);
2210Sstevel@tonic-gate 	GETTOK(mnt.mnt_dir, NULL);
2220Sstevel@tonic-gate 	GETTOK(mnt.mnt_type, NULL);
2230Sstevel@tonic-gate 	GETTOK(mnt.mnt_opts, NULL);
2240Sstevel@tonic-gate 	GETTOK(buf, NULL);
2250Sstevel@tonic-gate 	mnt.mnt_freq = 0;
2260Sstevel@tonic-gate 	mnt.mnt_passno = 0;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (strtok(NULL, sepstr) != NULL)
2290Sstevel@tonic-gate 		return (-1);
2300Sstevel@tonic-gate 	if (strcmp(mnt.mnt_type, "ufs") == 0) {
2310Sstevel@tonic-gate 		mnt.mnt_type = "4.2";
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * the device number, if present, follows the '='
2360Sstevel@tonic-gate 	 * in the mnt_opts string.
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (mnt.mnt_opts != NULL)
2400Sstevel@tonic-gate 		devnumstr = (char *)strchr(mnt.mnt_opts, '=');
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (!devnumstr) {
2430Sstevel@tonic-gate 		/* no device number on this line */
2440Sstevel@tonic-gate 		fprintf(fp, "%s %s %s %s %d %d\n",
2453224Sraf 		    mnt.mnt_fsname, mnt.mnt_dir, mnt.mnt_type,
2463224Sraf 		    mnt.mnt_opts, mnt.mnt_freq, mnt.mnt_passno);
2470Sstevel@tonic-gate 	} else {
2480Sstevel@tonic-gate 		/* found the device number, convert it to 4.x format */
2490Sstevel@tonic-gate 		devnum = strtol(&devnumstr[1], (char **)NULL, 16);
2500Sstevel@tonic-gate 		remainder = (char *)strchr(&devnumstr[1], ' ');
2510Sstevel@tonic-gate 		devnumstr[1] = 0;	/* null terminate mnt_opts after '=' */
2520Sstevel@tonic-gate 		devnum = cmpdev(devnum);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		fprintf(fp, "%s %s %s %s%4x%s %d %d\n",
2553224Sraf 		    mnt.mnt_fsname, mnt.mnt_dir, mnt.mnt_type,
2563224Sraf 		    mnt.mnt_opts, devnum, remainder ? remainder : "",
2573224Sraf 		    mnt.mnt_freq, mnt.mnt_passno);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	return (0);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate int
putfline(char * line,FILE * fp)2640Sstevel@tonic-gate putfline(char *line, FILE *fp)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	struct mntent mnt;
2670Sstevel@tonic-gate 	char *buf;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	GETTOK(mnt.mnt_fsname, line);
2700Sstevel@tonic-gate 	GETTOK(buf, NULL);
2710Sstevel@tonic-gate 	GETTOK(mnt.mnt_dir, NULL);
2720Sstevel@tonic-gate 	if (mnt.mnt_dir == NULL && strcmp(mnt.mnt_fsname, "/dev/root") == 0)
2730Sstevel@tonic-gate 		mnt.mnt_dir = "/";
2740Sstevel@tonic-gate 	GETTOK(mnt.mnt_type, NULL);
2750Sstevel@tonic-gate 	GETTOK(buf, NULL);
2760Sstevel@tonic-gate 	GETTOK(buf, NULL);
2770Sstevel@tonic-gate 	GETTOK(mnt.mnt_opts, NULL);
2780Sstevel@tonic-gate 	if (mnt.mnt_opts == NULL)
2790Sstevel@tonic-gate 		mnt.mnt_opts = "rw";
2800Sstevel@tonic-gate 	mnt.mnt_freq = 0;
2810Sstevel@tonic-gate 	mnt.mnt_passno = 0;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (strtok(NULL, sepstr) != NULL)
2840Sstevel@tonic-gate 		return (-1);
2850Sstevel@tonic-gate 	if (strcmp(mnt.mnt_type, "ufs") == 0) {
2860Sstevel@tonic-gate 		mnt.mnt_type = "4.2";
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	fprintf(fp, "%s %s %s %s %d %d\n",
2903224Sraf 	    mnt.mnt_fsname, mnt.mnt_dir, mnt.mnt_type,
2913224Sraf 	    mnt.mnt_opts, mnt.mnt_freq, mnt.mnt_passno);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	return (0);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate FILE *
_fopen(char * file,char * mode)2970Sstevel@tonic-gate _fopen(char *file, char *mode)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	extern FILE *_findiop();
3000Sstevel@tonic-gate 	FILE *iop;
3010Sstevel@tonic-gate 
302722Smuffin 	int    plus, oflag, fd;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	iop = _findiop();
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (iop == NULL || file == NULL || file[0] == '\0')
3070Sstevel@tonic-gate 		return (NULL);
3080Sstevel@tonic-gate 	plus = (mode[1] == '+');
3090Sstevel@tonic-gate 	switch (mode[0]) {
3100Sstevel@tonic-gate 	case 'w':
3110Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | N_O_TRUNC | N_O_CREAT;
3120Sstevel@tonic-gate 		break;
3130Sstevel@tonic-gate 	case 'a':
3140Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | N_O_CREAT;
3150Sstevel@tonic-gate 		break;
3160Sstevel@tonic-gate 	case 'r':
3170Sstevel@tonic-gate 		oflag = plus ? O_RDWR : O_RDONLY;
3180Sstevel@tonic-gate 		break;
3190Sstevel@tonic-gate 	default:
3200Sstevel@tonic-gate 		return (NULL);
3210Sstevel@tonic-gate 	}
322*11798SRoger.Faulkner@Sun.COM 	if ((fd = _syscall(SYS_openat, AT_FDCWD, file, oflag, 0666)) < 0)
3230Sstevel@tonic-gate 		return (NULL);
3240Sstevel@tonic-gate 	iop->_cnt = 0;
3250Sstevel@tonic-gate 	iop->_file = fd;
3260Sstevel@tonic-gate 	iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
3270Sstevel@tonic-gate 	if (mode[0] == 'a')   {
3280Sstevel@tonic-gate 		if ((lseek(fd, 0L, 2)) < 0)  {
3290Sstevel@tonic-gate 			(void) close(fd);
3300Sstevel@tonic-gate 			return (NULL);
3310Sstevel@tonic-gate 		}
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	iop->_base = iop->_ptr = NULL;
3340Sstevel@tonic-gate 	iop->_bufsiz = 0;
3350Sstevel@tonic-gate 	return (iop);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static int
open_printcap(void)339722Smuffin open_printcap(void)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	FILE		*fd;
3420Sstevel@tonic-gate 	FILE		*_fopen();
3430Sstevel@tonic-gate 	char		tmp_name[] = "/tmp/printcap.XXXXXX";
3440Sstevel@tonic-gate 	int		tmp_file;
3450Sstevel@tonic-gate 	DIR		*printerDir;
3460Sstevel@tonic-gate 	struct dirent	*entry;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	mktemp(tmp_name);
3490Sstevel@tonic-gate 	if ((fd = _fopen(tmp_name, "a+")) == NULL)
3500Sstevel@tonic-gate 		return (-1);
3510Sstevel@tonic-gate 	fprintf(fd, "# Derived from lp(1) configuration information for BCP\n");
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if ((printerDir = opendir(PRINTER_DIR)) != NULL) {
3540Sstevel@tonic-gate 		while ((entry = readdir(printerDir)) != NULL)
3550Sstevel@tonic-gate 			if (entry->d_name[0] != '.')
3560Sstevel@tonic-gate 				getPrinterInfo(entry->d_name, fd);
3570Sstevel@tonic-gate 		closedir(printerDir);
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 	fclose(fd);
3600Sstevel@tonic-gate 
361*11798SRoger.Faulkner@Sun.COM 	tmp_file = _syscall(SYS_openat, AT_FDCWD, tmp_name, O_RDONLY);
3620Sstevel@tonic-gate 	if (tmp_file == -1 || unlink(tmp_name) == -1)
3630Sstevel@tonic-gate 		return (-1);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	return (tmp_file);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate static void
getPrinterInfo(char * printerName,FILE * fd)3690Sstevel@tonic-gate getPrinterInfo(char *printerName, FILE *fd)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	char			*fullPath;
3720Sstevel@tonic-gate 	char			*str;
3730Sstevel@tonic-gate 	char			*p;
3740Sstevel@tonic-gate 	char			*c;
3750Sstevel@tonic-gate 	struct stat		buf;
3760Sstevel@tonic-gate 	int			config_fd;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	fullPath = (char *)malloc(strlen(PRINTER_DIR) + strlen(printerName) +
3790Sstevel@tonic-gate 	    strlen(PRINTER_CONFIG_FILE) + 1);
3800Sstevel@tonic-gate 	strcpy(fullPath, PRINTER_DIR);
3810Sstevel@tonic-gate 	strcat(fullPath, printerName);
3820Sstevel@tonic-gate 	strcat(fullPath, PRINTER_CONFIG_FILE);
3830Sstevel@tonic-gate 
384*11798SRoger.Faulkner@Sun.COM 	if ((config_fd = _syscall(SYS_openat, AT_FDCWD, fullPath, O_RDONLY))
385*11798SRoger.Faulkner@Sun.COM 	    == -1) {
3860Sstevel@tonic-gate 		free(fullPath);
3870Sstevel@tonic-gate 		return;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	if ((fstat(config_fd, &buf)) != 0 ||
3900Sstevel@tonic-gate 	    (str = (char *)malloc(buf.st_size + 2)) == NULL) {
3910Sstevel@tonic-gate 		free(fullPath);
3920Sstevel@tonic-gate 		close(config_fd);
3930Sstevel@tonic-gate 		return;
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	if ((read(config_fd, str, buf.st_size)) != buf.st_size) {
3960Sstevel@tonic-gate 		free(fullPath);
3970Sstevel@tonic-gate 		free(str);
3980Sstevel@tonic-gate 		close(config_fd);
3990Sstevel@tonic-gate 		return;
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 	p = &str[buf.st_size];
4020Sstevel@tonic-gate 	p[0] = '\n';
4030Sstevel@tonic-gate 	p[1] = '\0';
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	fprintf(fd, "%s:", printerName);
4060Sstevel@tonic-gate 	if ((p = (char *)_strstr(str, "Remote")) != NULL) {
4070Sstevel@tonic-gate 		/* remote printer */
4080Sstevel@tonic-gate 		p = (char *)strchr(p, ' ') + 1;
4090Sstevel@tonic-gate 		c = (char *)strchr(p, '\n');
4100Sstevel@tonic-gate 		*c = '\0';
4110Sstevel@tonic-gate 		fprintf(fd, "lp=:rm=%s:rp=%s:\n", p, printerName);
4120Sstevel@tonic-gate 	} else if ((p = (char *)_strstr(str, "Device")) != NULL) {
4130Sstevel@tonic-gate 		/* local printer */
4140Sstevel@tonic-gate 		p = (char *)strchr(p, ' ') + 1;
4150Sstevel@tonic-gate 		c = (char *)strchr(p, '\n');
4160Sstevel@tonic-gate 		*c = '\0';
4170Sstevel@tonic-gate 		fprintf(fd, "lp=%s:\n", p);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 	free(fullPath);
4200Sstevel@tonic-gate 	free(str);
4210Sstevel@tonic-gate 	close(config_fd);
4220Sstevel@tonic-gate }
423