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