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