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