File: gettok.c

package info (click to toggle)
netatalk 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,716 kB
  • sloc: ansic: 85,115; sh: 10,385; perl: 1,703; makefile: 1,363
file content (178 lines) | stat: -rw-r--r-- 3,613 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * $Id: gettok.c,v 1.6 2009-10-13 22:55:37 didg Exp $
 *
 * Copyright (c) 1990,1994 Regents of The University of Michigan.
 * All Rights Reserved.  See COPYRIGHT.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <sys/param.h>

/* STDC check */
#if STDC_HEADERS
#include <string.h>
#else /* STDC_HEADERS */
#ifndef HAVE_STRCHR
#define strchr index
#define strrchr index
#endif /* HAVE_STRCHR */
char *strchr (), *strrchr ();
#ifndef HAVE_MEMCPY
#define memcpy(d,s,n) bcopy ((s), (d), (n))
#define memmove(d,s,n) bcopy ((s), (d), (n))
#endif /* ! HAVE_MEMCPY */
#endif /* STDC_HEADERS */

#include <ctype.h>
#include <pwd.h>

#include <atalk/globals.h>

static char	*l_curr;
static char	*l_end;

void initline( int len, char *line)
{
    l_curr = line;
    l_end = line + len;
}

#define ST_QUOTE	0
#define ST_WORD		1
#define ST_BEGIN	2

int
parseline(int len, char *token)
{
    char	*p, *e;
    int		state;

    state = ST_BEGIN;
    p = token;
    e = token + len;

    for (;;) {
        if ( l_curr > l_end ) {			/* end of line */
            *token = '\0';
            return( -1 );
        }

        switch ( *l_curr ) {
        case '"' :
            if ( state == ST_QUOTE ) {
                state = ST_WORD;
            } else {
                state = ST_QUOTE;
            }
            break;

        case '\0' :
        case '\t' :
        case '\n' :
        case ' ' :
            if ( state == ST_WORD ) {
                *p = '\0';
                return( p - token );
            }
            if ( state != ST_QUOTE ) {
                break;
            }
            /* FALL THROUGH */

        default :
            if ( state == ST_BEGIN ) {
                state = ST_WORD;
            }
            if ( p > e ) {			/* end of token */
                *token = '\0';
                return( -1 );
            }
            *p++ = *l_curr;
            break;
        }

        l_curr++;
    }
}

#ifdef notdef
void parseline(char *token, char *user)
{
    char		*p = pos, *t = token, *u, *q, buf[ MAXPATHLEN ];
    struct passwd	*pwent;
    int			quoted = 0;

    while ( isspace( *p )) {
        p++;
    }

    /*
     * If we've reached the end of the line, or a comment,
     * don't return any more tokens.
     */
    if ( *p == '\0' || *p == '#' ) {
        *token = '\0';
        return;
    }

    if ( *p == '"' ) {
        p++;
        quoted = 1;
    }
    while ( *p != '\0' && ( quoted || !isspace( *p ))) {
        if ( *p == '"' ) {
            if ( quoted ) {
                *t = '\0';
                break;
            }
            quoted = 1;
            p++;
        } else {
            *t++ = *p++;
        }
    }
    pos = p;
    *t = '\0';

    /*
     * We got to the end of the line without closing an open quote
     */
    if ( *p == '\0' && quoted ) {
        *token = '\0';
        return;
    }

    t = token;
    if ( *t == '~' ) {
        t++;
        if ( *t == '\0' || *t == '/' ) {
            u = user;
            if ( *t == '/' ) {
                t++;
            }
        } else {
            u = t;
            if (( q = strchr( t, '/' )) == NULL ) {
                t = "";
            } else {
                *q = '\0';
                t = q + 1;
            }
        }
        if ( u == NULL || ( pwent = getpwnam( u )) == NULL ) {
            *token = '\0';
            return;
        }
        strcpy( buf, pwent->pw_dir );
        if ( *t != '\0' ) {
            strcat( buf, "/" );
            strcat( buf, t );
        }
        strcpy( token, buf );
    }
    return;
}
#endif /* notdef */