xref: /illumos-gate/usr/src/cmd/lp/lib/lp/isterminfo.c (revision 268ffd3a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "fcntl.h"
267c478bd9Sstevel@tonic-gate #include "errno.h"
277c478bd9Sstevel@tonic-gate #include "string.h"
287c478bd9Sstevel@tonic-gate #include "unistd.h"
297c478bd9Sstevel@tonic-gate #include "search.h"
307c478bd9Sstevel@tonic-gate #include "stdlib.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "lp.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
357c478bd9Sstevel@tonic-gate typedef void **		A2;
367c478bd9Sstevel@tonic-gate typedef int		(*A3)(const void *, const void *);
377c478bd9Sstevel@tonic-gate #else
387c478bd9Sstevel@tonic-gate typedef char **		A2;
397c478bd9Sstevel@tonic-gate typedef int		(*A3)();
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Define the following if you want to do a stronger check:
447c478bd9Sstevel@tonic-gate  * that a type names a valid entry in the Terminfo database.
457c478bd9Sstevel@tonic-gate  * The stronger check reads the entry and verifies the magic
467c478bd9Sstevel@tonic-gate  * number in the header. The weaker check is to see if we
477c478bd9Sstevel@tonic-gate  * have read access to the file. The weaker check will be a
487c478bd9Sstevel@tonic-gate  * tad faster.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate /* #define STRONG_CHECK	1 */	/* */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Define the following if you want to cache hits and/or misses.
547c478bd9Sstevel@tonic-gate  * One reason for NOT caching is to guard against crazies who try
557c478bd9Sstevel@tonic-gate  * submitting print requests with goofball types, or every valid type
567c478bd9Sstevel@tonic-gate  * under the sun. Since Terminfo is limited, the hit cache is effectively
577c478bd9Sstevel@tonic-gate  * limited, so it shouldn't be a problem searching the cache (the search
587c478bd9Sstevel@tonic-gate  * is binary) but the cache can become big. The miss cache, on the other
597c478bd9Sstevel@tonic-gate  * hand, could cause a problem. This problem can become severe, so
607c478bd9Sstevel@tonic-gate  * consider limiting the size of the cache (see below).
617c478bd9Sstevel@tonic-gate  * Another reason for NOT caching is to pick up changes in the Terminfo
627c478bd9Sstevel@tonic-gate  * database. The "terminfo" type is not likely to be an oft used feature,
637c478bd9Sstevel@tonic-gate  * though, so this shouldn't be a big problem.
647c478bd9Sstevel@tonic-gate  * The reason FOR caching is to limit the number of file system accesses.
657c478bd9Sstevel@tonic-gate  * This routine is called OFTEN, so limiting the number of open() or
667c478bd9Sstevel@tonic-gate  * access() calls is a good idea.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define CACHE_HITS 1	/* */
697c478bd9Sstevel@tonic-gate #define CACHE_MISSES 1	/* */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Define the following if you want to limit the sizes of the caches.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate #define HIT_CACHE_LIMIT		100	/* */
757c478bd9Sstevel@tonic-gate #define MISS_CACHE_LIMIT	100	/* */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #if	defined(CACHE_HITS)
787c478bd9Sstevel@tonic-gate static char		*hit_cache	= 0;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate # if	defined(HIT_CACHE_LIMIT)
817c478bd9Sstevel@tonic-gate static int		hit_cache_size	= 0;
827c478bd9Sstevel@tonic-gate # endif
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #if	defined(CACHE_MISSES)
867c478bd9Sstevel@tonic-gate static char		*miss_cache	= 0;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate # if	defined(MISS_CACHE_LIMIT)
897c478bd9Sstevel@tonic-gate static int		miss_cache_size	= 0;
907c478bd9Sstevel@tonic-gate # endif
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
947c478bd9Sstevel@tonic-gate static int		_isterminfo ( char * , char * );
957c478bd9Sstevel@tonic-gate #else
967c478bd9Sstevel@tonic-gate static int		_isterminfo();
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /**
1007c478bd9Sstevel@tonic-gate  ** isterminfo() - SEE IF TYPE IS IN TERMINFO DATABASE
1017c478bd9Sstevel@tonic-gate  **/
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
1047c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
isterminfo(char * type)1057c478bd9Sstevel@tonic-gate isterminfo (
1067c478bd9Sstevel@tonic-gate 	char *			type
1077c478bd9Sstevel@tonic-gate )
1087c478bd9Sstevel@tonic-gate #else
1097c478bd9Sstevel@tonic-gate isterminfo (type)
1107c478bd9Sstevel@tonic-gate 	char			*type;
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	register int		ret;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	static char		*envTERMINFO	= 0;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (!type || !*type)
1197c478bd9Sstevel@tonic-gate 		return (0);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #if	defined(CACHE_HITS)
1227c478bd9Sstevel@tonic-gate 	if (tfind(type, (A2)&hit_cache, (A3)strcmp))
1237c478bd9Sstevel@tonic-gate 		return (1);
1247c478bd9Sstevel@tonic-gate #endif
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #if	defined(CACHE_MISSES)
1277c478bd9Sstevel@tonic-gate 	if (tfind(type, (A2)&miss_cache, (A3)strcmp))
1287c478bd9Sstevel@tonic-gate 		return (0);
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (!envTERMINFO)
1327c478bd9Sstevel@tonic-gate 		envTERMINFO = getenv("TERMNIFO");
1337c478bd9Sstevel@tonic-gate 	if (
1347c478bd9Sstevel@tonic-gate 		envTERMINFO
1357c478bd9Sstevel@tonic-gate 	     && _isterminfo(type, envTERMINFO)
1367c478bd9Sstevel@tonic-gate #if	defined(TERMINFO)
1377c478bd9Sstevel@tonic-gate 	     || _isterminfo(type, TERMINFO)
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate 	) {
1407c478bd9Sstevel@tonic-gate 		ret = 1;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #if	defined(CACHE_HITS)
1437c478bd9Sstevel@tonic-gate # if	defined(HIT_CACHE_LIMIT)
1447c478bd9Sstevel@tonic-gate 		if (hit_cache_size++ < HIT_CACHE_LIMIT)
1457c478bd9Sstevel@tonic-gate # endif
1467c478bd9Sstevel@tonic-gate 			(void)tsearch (Strdup(type), (A2)&hit_cache, (A3)strcmp);
1477c478bd9Sstevel@tonic-gate #endif
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	} else {
1507c478bd9Sstevel@tonic-gate 		ret = 0;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #if	defined(CACHE_MISSES)
1537c478bd9Sstevel@tonic-gate # if	defined(MISS_CACHE_LIMIT)
1547c478bd9Sstevel@tonic-gate 		if (miss_cache_size++ < MISS_CACHE_LIMIT)
1557c478bd9Sstevel@tonic-gate # endif
1567c478bd9Sstevel@tonic-gate 			(void)tsearch (Strdup(type), (A2)&miss_cache, (A3)strcmp);
1577c478bd9Sstevel@tonic-gate #endif
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	return (ret);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /**
1637c478bd9Sstevel@tonic-gate  ** _isterminfo()
1647c478bd9Sstevel@tonic-gate  **/
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static int
1677c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_isterminfo(char * type,char * parent)1687c478bd9Sstevel@tonic-gate _isterminfo (
1697c478bd9Sstevel@tonic-gate 	char *			type,
1707c478bd9Sstevel@tonic-gate 	char *			parent
1717c478bd9Sstevel@tonic-gate )
1727c478bd9Sstevel@tonic-gate #else
1737c478bd9Sstevel@tonic-gate _isterminfo (type, parent)
1747c478bd9Sstevel@tonic-gate 	char			*type,
1757c478bd9Sstevel@tonic-gate 				*parent;
1767c478bd9Sstevel@tonic-gate #endif
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	char			*path,
179*268ffd3aSRichard Lowe 				type_letter[]	= "X";
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	int			ret;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate #if	defined(STRONG_CHECK)
1847c478bd9Sstevel@tonic-gate 	int			fd;
1857c478bd9Sstevel@tonic-gate #endif
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	type_letter[0] = type[0];
1897c478bd9Sstevel@tonic-gate 	if (!(path = makepath(parent, type_letter, type, (char *)0)))
1907c478bd9Sstevel@tonic-gate 		return (0);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #if	defined(STRONG_CHECK)
1937c478bd9Sstevel@tonic-gate 	if (!(fd = Open(path, O_RDONLY))) {
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		/*
1967c478bd9Sstevel@tonic-gate 		 * If we can't open the TERMINFO file because we
1977c478bd9Sstevel@tonic-gate 		 * don't have any open channels left, let's err on
1987c478bd9Sstevel@tonic-gate 		 * the side of likelihood--if the file can be
1997c478bd9Sstevel@tonic-gate 		 * accessed, figure that it's okay.
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		if (errno == EMFILE && Access(path, R_OK) == 0)
2027c478bd9Sstevel@tonic-gate 			ret = 1;
2037c478bd9Sstevel@tonic-gate 		else
2047c478bd9Sstevel@tonic-gate 			ret = 0;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	} else {
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		char			buf[2];
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (Read(fd, buf, 2) == 2 && buf[0] == 26 && buf[1] == 1)
2117c478bd9Sstevel@tonic-gate 			ret = 1;
2127c478bd9Sstevel@tonic-gate 		else
2137c478bd9Sstevel@tonic-gate 			ret = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		Close (fd);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate #else
2197c478bd9Sstevel@tonic-gate 	ret = (Access(path, R_OK) == 0);
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	Free (path);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (ret);
2257c478bd9Sstevel@tonic-gate }
226