summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libcruft/entlib.c
blob: f88b3f0576bea5ac04d2532ae9fd466305bc9a56 (plain)
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
/*
 * dietlibc/lib/entlib.c - Generic delimited-line parsing library
 *
 * Copyright 2001 Jeff Garzik <jgarzik@mandrakesoft.com>
 *
 * This is a brand new implementation, based on the interface
 * described in man-pages-1.34-1mdk man pages package.
 */


#include "entlib.h" /* this is our only include */


/*
 * __ent_start
 *
 * Allocates and zeroes the module's state structure,
 * and open a handle to /etc/passwd.
 *
 * Returns -1 on failure and sets errno, or zero for success.
 */

int __ent_start(const char *pathname, struct __ent_state **st_ref)
{
	struct __ent_state *st;
	
	if (*st_ref)
		return 0;

	st = calloc(1, sizeof(*st));
	if (!st) {
		errno = ENOMEM;
		return -1;
	}

	st->fd = open(pathname, O_RDONLY);
	if (st->fd == -1) {
		/* errno should have been set by open(2) */
		free(st);
		st = NULL;
		return -1;
	}

	*st_ref = st;
	return 0;
}


/*
 * __ent_get_line
 *
 * Eliminates a previous line from the buffer, if any.
 * Then reads in a new line from /etc/passwd, if necessary.
 *
 * Returns -1 on failure, or zero for success.
 */

int __ent_get_line(struct __ent_state *st)
{
	int rc;

	/* overwrite previous line, by shifting the rest
	 * of the rest to the front of the buffer
	 */
	if (st->bufptr) {
		unsigned int slop = st->buflen - st->bufptr;
		memmove(st->ent_buf, &st->ent_buf[st->bufptr], slop);
		st->bufptr = 0;
		st->buflen = slop;
		st->ent_buf[st->buflen] = 0; /* null terminate */
	}

	if (st->buflen == __ENT_BUFSIZ || strchr(st->ent_buf, '\n'))
		return 0;

	rc = read(st->fd, &st->ent_buf[st->buflen], __ENT_BUFSIZ - st->buflen);
	if (rc < 0)
		return -1;

	st->buflen += rc;
	if (st->buflen == 0)
		return -1;

	return 0;
}


/*
 * __ent_split
 *
 * Splits a string into parts based on a delimiter.
 * Stops processing when \n is reached also.
 *
 * Returns -1 on failure, or zero on success.
 */

int __ent_split(struct __ent_state *st, char **parts,
		int n_parts, int delimiter, int require_exact)
{
	char *s = &st->ent_buf[st->bufptr];
	int idx = 0;

	/* empty list */
	if (!*s) {
		if (!require_exact)
			return 0;
		return -1;
	}

	/* scan through string, sticking string pointers
	 * into parts[] as delimiters are found
	 */
	parts[idx++] = s;
	while (*s) {
		st->bufptr++;
		if (*s == '\n') {
			*s = 0; /* null terminate */
			break;
		}
		if (*s == delimiter) {
			*s = 0; /* null terminate */
			/* boundary error: too many delimiters */
			if (idx == n_parts)
				return -1;
			s++;
			parts[idx++] = s;
		} else {
			s++;
		}
	}

	if (!require_exact)
		return 0;
	return (n_parts == idx) ? 0 : -1;
}


void __ent_set(struct __ent_state *st)
{
	if (!st)
		return;
	st->buflen = st->bufptr = 0;
	lseek(st->fd, 0, SEEK_SET);
}


void __ent_end(struct __ent_state *st)
{
	if (!st)
		return;
	close(st->fd);
	free(st);
}