]> arthur.barton.de Git - netatalk.git/blob - etc/papd/file.c
markline: return an error if the buffer is too big and there's no end of line
[netatalk.git] / etc / papd / file.c
1 /*
2  * $Id: file.c,v 1.10 2009-02-02 10:31:32 didg Exp $
3  *
4  * Copyright (c) 1990,1994 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <atalk/logger.h>
13 #include <sys/param.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "file.h"
19
20 /* 
21 */
22 int markline( pf, start, linelength, crlflength )
23     char                **start;
24     int                 *linelength, *crlflength;
25     struct papfile      *pf;
26 {
27     char                *p;
28
29     if ( pf->pf_datalen == 0 && ( pf->pf_state & PF_EOF )) {
30         return( 0 );
31     }
32
33     *start = pf->pf_data;
34
35     /* get a line */
36     for ( *linelength=0; *linelength < pf->pf_datalen; (*linelength)++) {
37         if (pf->pf_data[*linelength] == '\n' ||
38             pf->pf_data[*linelength] == '\r') {
39             break;
40         }
41     }
42
43     if ( *linelength >= pf->pf_datalen ) {
44         if ( pf->pf_state & PF_EOF ) {
45             append( pf, "\n", 1 );
46         } else if (*linelength < 1024) {
47             return( -1 );
48         }
49     }
50
51     p = pf->pf_data + *linelength;
52
53     *crlflength=0;
54     while(*crlflength < pf->pf_datalen-*linelength && 
55     (p[*crlflength]=='\r' || p[*crlflength]=='\n')) {
56         (*crlflength)++;
57     }
58     
59     if (!*crlflength) {
60         /* line is way too long, something fishy is going on, give up */
61         LOG(log_error, logtype_papd, "markline: no crlf in comment, give up" );
62         return( -2 );
63     }
64
65     /* success, return 1 */
66     return( 1 );
67 }
68
69 void morespace( pf, data, len )
70     struct papfile      *pf;
71     const char          *data;
72     int                 len;
73 {
74     char                *nbuf;
75     int                 nsize;
76
77     if ( pf->pf_data != pf->pf_buf ) {                  /* pull up */
78         bcopy( pf->pf_data, pf->pf_buf, pf->pf_datalen);
79         pf->pf_data = pf->pf_buf;
80     }
81
82     if ( pf->pf_datalen + len > pf->pf_bufsize ) {      /* make more space */
83         nsize = (( pf->pf_bufsize + len ) / PF_MORESPACE +
84                 (( pf->pf_bufsize + len ) % PF_MORESPACE != 0 )) * PF_MORESPACE;
85         if ( pf->pf_buf ) {
86             if (( nbuf = (char *)realloc( pf->pf_buf, nsize )) == 0 ) {
87                 exit( 1 );
88             }
89         } else {
90             if (( nbuf = (char *)malloc( nsize )) == 0 ) {
91                 exit( 1 );
92             }
93         }
94         pf->pf_bufsize = nsize;
95         pf->pf_data = nbuf + ( pf->pf_data - pf->pf_buf );
96         pf->pf_buf = nbuf;
97     }
98
99     bcopy( data, pf->pf_data + pf->pf_datalen, len );
100     pf->pf_datalen += len;
101 }
102
103
104 void append(pf, data, len)
105     struct papfile      *pf;
106     const char          *data;
107     int                 len;
108 {
109     if ((pf->pf_data + pf->pf_datalen + len) >
110         (pf->pf_buf + pf->pf_bufsize)) {
111                 morespace(pf, data, len);
112     } else {
113         memcpy(pf->pf_data + pf->pf_datalen, data, len);
114         pf->pf_datalen += len;
115     }
116 }
117
118
119 void spoolerror( out, str )
120     struct papfile      *out;
121     char                *str;
122 {
123     char        *pserr1 = "%%[ Error: ";
124     char        *pserr2 = " ]%%\n";
125
126     if ( str == NULL ) {
127         str = "Spooler error.";
128     }
129
130     append( out, pserr1, strlen( pserr1 ));
131     append( out, str, strlen( str ));
132     append( out, pserr2, strlen( pserr2 ));
133 }