]> arthur.barton.de Git - netatalk.git/blob - bin/psorder/pa.c
Big configure.in cleanup
[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                 return( NULL );
51         }
52
53         h->cur = h->buf - 1;
54         h->end = h->buf + rc - 1;
55         h->state = PA_NORMAL;
56         h->fd = fd;
57
58         return( h );
59 }
60
61 char *pa_gettok(pa_buf_t *h)
62 {
63         h->state = PA_NORMAL;
64         h->tmp = *(h->cur);
65         *(h->cur) = 0;
66         return( h->mark );
67 }
68
69 char _pa_fixbuf(pa_buf_t *h)
70 {
71         int rc;
72         char *t;
73
74         if ( h->state == PA_NORMAL ) {
75                 *(h->buf) = *(h->cur);
76                 h->cur = h->buf;
77         } else {
78                 bcopy( h->mark, h->buf, h->end - h->mark + 1 );
79                 h->cur = h->buf + ( h->cur - h->mark );
80                 h->end = h->buf + ( h->end - h->mark );
81                 h->mark = h->buf;
82         }
83
84         if ( h->bufsz - (( h->cur - h->buf ) + 1 ) < PA_BUFBLK ) {
85                 t = h->buf;
86                 h->buf = (char *) realloc( h->buf, h->bufsz + PA_BUFBLK );
87                 h->bufsz += PA_BUFBLK;
88                 h->mark = h->buf + ( h->mark - t );
89                 h->cur = h->buf + ( h->cur - t );
90                 h->end = h->buf + ( h->end - t );
91         }
92
93         if (( rc = read( h->fd, h->cur + 1, PA_BUFBLK )) <= 0 ) {
94                 return( (char) 0 );
95         }
96
97         h->end = h->cur + rc;
98         return( *(++(h->cur)) );
99 }