]> arthur.barton.de Git - netatalk.git/blob - etc/papd/file.c
62e3c033eb35515fa2f8a010dcc18e535efdfd51
[netatalk.git] / etc / papd / file.c
1 /*
2  * $Id: file.c,v 1.9 2002-01-04 04:45:47 sibaz 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 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     /* success, return 1 */
58     return( 1 );
59 }
60
61 void morespace( pf, data, len )
62     struct papfile      *pf;
63     const char          *data;
64     int                 len;
65 {
66     char                *nbuf;
67     int                 nsize;
68
69     if ( pf->pf_data != pf->pf_buf ) {                  /* pull up */
70         bcopy( pf->pf_data, pf->pf_buf, pf->pf_datalen);
71         pf->pf_data = pf->pf_buf;
72     }
73
74     if ( pf->pf_datalen + len > pf->pf_bufsize ) {      /* make more space */
75         nsize = (( pf->pf_bufsize + len ) / PF_MORESPACE +
76                 (( pf->pf_bufsize + len ) % PF_MORESPACE != 0 )) * PF_MORESPACE;
77         if ( pf->pf_buf ) {
78             if (( nbuf = (char *)realloc( pf->pf_buf, nsize )) == 0 ) {
79                 exit( 1 );
80             }
81         } else {
82             if (( nbuf = (char *)malloc( nsize )) == 0 ) {
83                 exit( 1 );
84             }
85         }
86         pf->pf_bufsize = nsize;
87         pf->pf_data = nbuf + ( pf->pf_data - pf->pf_buf );
88         pf->pf_buf = nbuf;
89     }
90
91     bcopy( data, pf->pf_data + pf->pf_datalen, len );
92     pf->pf_datalen += len;
93 }
94
95
96 void append(pf, data, len)
97     struct papfile      *pf;
98     const char          *data;
99     int                 len;
100 {
101     if ((pf->pf_data + pf->pf_datalen + len) >
102         (pf->pf_buf + pf->pf_bufsize)) {
103                 morespace(pf, data, len);
104     } else {
105         memcpy(pf->pf_data + pf->pf_datalen, data, len);
106         pf->pf_datalen += len;
107     }
108 }
109
110
111 void spoolerror( out, str )
112     struct papfile      *out;
113     char                *str;
114 {
115     char        *pserr1 = "%%[ Error: ";
116     char        *pserr2 = " ]%%\n";
117
118     if ( str == NULL ) {
119         str = "Spooler error.";
120     }
121
122     append( out, pserr1, strlen( pserr1 ));
123     append( out, str, strlen( str ));
124     append( out, pserr2, strlen( pserr2 ));
125 }