]> arthur.barton.de Git - netatalk.git/blobdiff - etc/papd/printcap.c
- merge branch-netatalk-afp-3x-dev, HEAD was tagged before
[netatalk.git] / etc / papd / printcap.c
index 2459e1abc16b2a9c6f5ddc9a51f2c1d49ce93258..19ed788331cc8351de1a76774a9fe532b0930f33 100644 (file)
@@ -1,9 +1,9 @@
 /*
+ * $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 */
 
 /*
@@ -77,7 +87,7 @@ static char sccsid[] = "@(#)printcap.c        5.7 (Berkeley) 3/4/91";
 #define tnchktc        pnchktc
 #define        tnamatch pnamatch
 #define V6
-#endif
+#endif /* PRINTCAP */
 
 static FILE *pfp = NULL;       /* printcap data base file pointer */
 static char *tbuf;
@@ -94,18 +104,24 @@ char       *getenv();
  * 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);
@@ -132,11 +148,18 @@ getprent( cap, bp)
                                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);
@@ -150,7 +173,7 @@ endprent()
  * 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;
@@ -158,6 +181,7 @@ tgetent( cap, bp, name)
        register int i = 0, cnt = 0;
        char ibuf[BUFSIZ];
        int tf;
+       int skip;
 
        hopcount = 0;
        tbuf = bp;
@@ -176,7 +200,7 @@ tgetent( cap, bp, name)
                        cp2 = getenv("TERM");
                        if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
                                strcpy(bp,cp);
-                               return(tnchktc());
+                               return(tnchktc(cap));
                        } else {
                                tf = open(cap, 0);
                        }
@@ -185,13 +209,14 @@ tgetent( cap, bp, name)
        }
        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);
@@ -203,12 +228,20 @@ tgetent( cap, bp, name)
                        }
                        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;
@@ -222,7 +255,7 @@ tgetent( cap, bp, name)
                 */
                if (tnamatch(name)) {
                        close(tf);
-                       return(tnchktc());
+                       return(tnchktc(cap));
                }
        }
 }
@@ -237,7 +270,7 @@ tgetent( cap, bp, name)
  * 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;
@@ -285,7 +318,7 @@ tnchktc( cap )
  * 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;
@@ -318,7 +351,7 @@ tskip(bp)
 
        while (*bp && *bp != ':')
                bp++;
-       if (*bp == ':')
+       while (*bp && *bp == ':')
                bp++;
        return (bp);
 }
@@ -331,7 +364,7 @@ tskip(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;
@@ -364,7 +397,7 @@ tgetnum(id)
  * 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;
@@ -461,9 +494,10 @@ nextc:
 }
 
 static char *
-decodename(str, area)
+decodename(str, area, bufsize)
        register char *str;
        char **area;
+       int bufsize;
 {
        register char *cp;
        register int c;
@@ -471,7 +505,7 @@ decodename(str, area)
        int i;
 
        cp = *area;
-       while ((c = *str++) && c != ':' && c != '|' ) {
+       while ((c = *str++) && --bufsize && c != ':' && c != '|' ) {
                switch (c) {
 
                case '^':
@@ -506,8 +540,9 @@ nextc:
 }
 
 char *
-getpname( area )
-    char       **area;
+getpname( area, bufsize )
+       char    **area;
+       int     bufsize;
 {
-    return( decodename( tbuf, area ));
+       return( decodename( tbuf, area, bufsize));
 }