]> arthur.barton.de Git - netatalk.git/blob - etc/papd/file.c
CVS id tags, define fixes, code beautification
[netatalk.git] / etc / papd / file.c
1 /*
2  * $Id: file.c,v 1.6 2001-06-25 20:13:45 rufustfirefly 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 <sys/syslog.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 int markline( pf, start, linelength, crlflength )
21     char                **start;
22     int                 *linelength, *crlflength;
23     struct papfile      *pf;
24 {
25     char                *p;
26
27     if ( pf->pf_datalen == 0 && ( pf->pf_state & PF_EOF )) {
28         return( 0 );
29     }
30
31     *start = pf->pf_data;
32
33     /* get a line */
34     for ( *linelength=0; *linelength < pf->pf_datalen; (*linelength)++) {
35         if (pf->pf_data[*linelength] == '\n' ||
36             pf->pf_data[*linelength] == '\r') {
37             break;
38         }
39     }
40
41     if ( *linelength >= pf->pf_datalen ) {
42         if ( pf->pf_state & PF_EOF ) {
43             append( pf, "\n", 1 );
44         } else if (*linelength < 1024) {
45             return( -1 );
46         }
47     }
48
49     p = pf->pf_data + *linelength;
50
51     *crlflength=0;
52     while(*crlflength < pf->pf_datalen-*linelength && 
53     (p[*crlflength]=='\r' || p[*crlflength]=='\n')) {
54         (*crlflength)++;
55     }
56
57     return *linelength;
58 }
59
60 void morespace( pf, data, len )
61     struct papfile      *pf;
62     const char          *data;
63     int                 len;
64 {
65     char                *nbuf;
66     int                 nsize;
67
68     if ( pf->pf_data != pf->pf_buf ) {                  /* pull up */
69         bcopy( pf->pf_data, pf->pf_buf, pf->pf_datalen);
70         pf->pf_data = pf->pf_buf;
71     }
72
73     if ( pf->pf_datalen + len > pf->pf_bufsize ) {      /* make more space */
74         nsize = (( pf->pf_bufsize + len ) / PF_MORESPACE +
75                 (( pf->pf_bufsize + len ) % PF_MORESPACE != 0 )) * PF_MORESPACE;
76         if ( pf->pf_buf ) {
77             if (( nbuf = (char *)realloc( pf->pf_buf, nsize )) == 0 ) {
78                 exit( 1 );
79             }
80         } else {
81             if (( nbuf = (char *)malloc( nsize )) == 0 ) {
82                 exit( 1 );
83             }
84         }
85         pf->pf_bufsize = nsize;
86         pf->pf_data = nbuf + ( pf->pf_data - pf->pf_buf );
87         pf->pf_buf = nbuf;
88     }
89
90     bcopy( data, pf->pf_data + pf->pf_datalen, len );
91     pf->pf_datalen += len;
92 }
93
94
95 void append(pf, data, len)
96     struct papfile      *pf;
97     const char          *data;
98     int                 len;
99 {
100     if ((pf->pf_data + pf->pf_datalen + len) >
101         (pf->pf_buf + pf->pf_bufsize)) {
102                 morespace(pf, data, len);
103     } else {
104         memcpy(pf->pf_data + pf->pf_datalen, data, len);
105         pf->pf_datalen += len;
106     }
107 }
108
109
110 void spoolerror( out, str )
111     struct papfile      *out;
112     char                *str;
113 {
114     char        *pserr1 = "%%[ Error: ";
115     char        *pserr2 = " ]%%\n";
116
117     if ( str == NULL ) {
118         str = "Spooler error.";
119     }
120
121     append( out, pserr1, strlen( pserr1 ));
122     append( out, str, strlen( str ));
123     append( out, pserr2, strlen( pserr2 ));
124 }