]> arthur.barton.de Git - netatalk.git/blob - bin/psorder/pa.c
Makefile.am patches from Sebastian Rittau (srittau@jroger.in-berlin.de)
[netatalk.git] / bin / psorder / pa.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation, and that the name of The University
10  * of Michigan not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. This software is supplied as is without expressed or
13  * implied warranties of any kind.
14  *
15  *      Research Systems Unix Group
16  *      The University of Michigan
17  *      c/o Mike Clark
18  *      535 W. William Street
19  *      Ann Arbor, Michigan
20  *      +1-313-763-0525
21  *      netatalk@itd.umich.edu
22  */
23
24 /* This is used with pa.h */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdlib.h>
31
32 #include "pa.h"
33
34 pa_buf_t *pa_init( fd )
35         int fd;
36 {
37         pa_buf_t *h;
38         int rc;
39
40         h = (pa_buf_t *) malloc( sizeof( pa_buf_t ));
41         h->buf = (char *) malloc( PA_BUFBLK * 2 );
42         h->bufsz = PA_BUFBLK * 2;
43
44         if (( rc = read( fd, h->buf, PA_BUFBLK )) < 0 ) {
45                 return( 0 );
46         }
47
48         h->cur = h->buf - 1;
49         h->end = h->buf + rc - 1;
50         h->state = PA_NORMAL;
51         h->fd = fd;
52
53         return( h );
54 }
55
56 char *pa_gettok( h )
57         pa_buf_t *h;
58 {
59         h->state = PA_NORMAL;
60         h->tmp = *(h->cur);
61         *(h->cur) = 0;
62         return( h->mark );
63 }
64
65 char _pa_fixbuf( h )
66         pa_buf_t *h;
67 {
68         int rc;
69         char *t;
70
71         if ( h->state == PA_NORMAL ) {
72                 *(h->buf) = *(h->cur);
73                 h->cur = h->buf;
74         } else {
75                 bcopy( h->mark, h->buf, h->end - h->mark + 1 );
76                 h->cur = h->buf + ( h->cur - h->mark );
77                 h->end = h->buf + ( h->end - h->mark );
78                 h->mark = h->buf;
79         }
80
81         if ( h->bufsz - (( h->cur - h->buf ) + 1 ) < PA_BUFBLK ) {
82                 t = h->buf;
83                 h->buf = (char *) realloc( h->buf, h->bufsz + PA_BUFBLK );
84                 h->bufsz += PA_BUFBLK;
85                 h->mark = h->buf + ( h->mark - t );
86                 h->cur = h->buf + ( h->cur - t );
87                 h->end = h->buf + ( h->end - t );
88         }
89
90         if (( rc = read( h->fd, h->cur + 1, PA_BUFBLK )) <= 0 ) {
91                 return( (char) 0 );
92         }
93
94         h->end = h->cur + rc;
95         return( *(++(h->cur)) );
96 }