/*
+ * $Id: printcap.c,v 1.10 2005-04-28 20:49:49 bfernhomberg Exp $
+ *
* Copyright (c) 1990,1994 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
- */
-
-/*
+ *
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved.
*
* SUCH DAMAGE.
*/
-#ifndef lint
-static char sccsid[] = "@(#)printcap.c 5.7 (Berkeley) 3/4/91";
-#endif /* not lint */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
#include <ctype.h>
#include <stdio.h>
#include <string.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif /* HAVE_UNISTD_H */
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
#include <atalk/paths.h>
+#include "printcap.h"
+
#ifndef BUFSIZ
#define BUFSIZ 1024
-#endif
+#endif /* ! BUFSIZ */
#define MAXHOP 32 /* max number of tc= indirections */
/*
#define tnchktc pnchktc
#define tnamatch pnamatch
#define V6
-#endif
+#endif /* PRINTCAP */
static FILE *pfp = NULL; /* printcap data base file pointer */
static char *tbuf;
* Added a "cap" parameter, so we can use these calls for printcap
* and papd.conf.
*/
-getprent( cap, bp)
+int getprent( cap, bp, bufsize )
register char *cap;
register char *bp;
+ register int bufsize;
{
- register int c, skip = 0;
+ register int c, skip = 0, i;
if (pfp == NULL && (pfp = fopen( cap, "r")) == NULL)
return(-1);
tbuf = bp;
+ i = 0;
for (;;) {
switch (c = getc(pfp)) {
case EOF:
+ if (bp != tbuf) {
+ *bp = '\0';
+ return(1);
+ }
fclose(pfp);
pfp = NULL;
return(0);
return(1);
}
*bp++ = c;
+ if (++i >= bufsize) {
+ write(2, "config file too large\n", 22);
+ fclose(pfp);
+ pfp = NULL;
+ *bp = '\0';
+ return(1);
+ }
}
}
}
-endprent()
+void endprent()
{
if (pfp != NULL)
fclose(pfp);
* Added a "cap" parameter, so we can use these calls for printcap
* and papd.conf.
*/
-tgetent( cap, bp, name)
+int tgetent( cap, bp, name)
char *cap, *bp, *name;
{
register char *cp;
register int i = 0, cnt = 0;
char ibuf[BUFSIZ];
int tf;
+ int skip;
hopcount = 0;
tbuf = bp;
cp2 = getenv("TERM");
if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
strcpy(bp,cp);
- return(tnchktc());
+ return(tnchktc(cap));
} else {
tf = open(cap, 0);
}
}
if (tf==0)
tf = open(cap, 0);
-#else
+#else /* V6 */
tf = open(cap, 0);
-#endif
+#endif /* V6 */
if (tf < 0)
return (-1);
for (;;) {
cp = bp;
+ skip = 0;
for (;;) {
if (i == cnt) {
cnt = read(tf, ibuf, BUFSIZ);
}
c = ibuf[i++];
if (c == '\n') {
- if (cp > bp && cp[-1] == '\\'){
+ if (!skip && cp > bp && cp[-1] == '\\') {
cp--;
continue;
}
- break;
+ skip = 0;
+ if (cp == bp)
+ continue;
+ else
+ break;
}
+ if (c == '#' && cp == bp)
+ skip++;
+ if (skip)
+ continue;
if (cp >= bp+BUFSIZ) {
write(2,"Termcap entry too long\n", 23);
break;
*/
if (tnamatch(name)) {
close(tf);
- return(tnchktc());
+ return(tnchktc(cap));
}
}
}
* Added a "cap" parameter, so we can use these calls for printcap
* and papd.conf.
*/
-tnchktc( cap )
+int tnchktc( cap )
char *cap;
{
register char *p, *q;
* against each such name. The normal : terminator after the last
* name (before the first field) stops us.
*/
-tnamatch(np)
+int tnamatch(np)
char *np;
{
register char *Np, *Bp;
while (*bp && *bp != ':')
bp++;
- if (*bp == ':')
+ while (*bp && *bp == ':')
bp++;
return (bp);
}
* a # character. If the option is not found we return -1.
* Note that we handle octal numbers beginning with 0.
*/
-tgetnum(id)
+int tgetnum(id)
char *id;
{
register int i, base;
* of the buffer. Return 1 if we find the option, or 0 if it is
* not given.
*/
-tgetflag(id)
+int tgetflag(id)
char *id;
{
register char *bp = tbuf;
}
static char *
-decodename(str, area)
+decodename(str, area, bufsize)
register char *str;
char **area;
+ int bufsize;
{
register char *cp;
register int c;
int i;
cp = *area;
- while ((c = *str++) && c != ':' && c != '|' ) {
+ while ((c = *str++) && --bufsize && c != ':' && c != '|' ) {
switch (c) {
case '^':
}
char *
-getpname( area )
- char **area;
+getpname( area, bufsize )
+ char **area;
+ int bufsize;
{
- return( decodename( tbuf, area ));
+ return( decodename( tbuf, area, bufsize));
}