about summary refs log tree commit diff stats
path: root/WWW/Library/Implementation/HTAAFile.c
blob: a55b2f899c42ef3ac51d20d8d7e349e9cb4db341 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* MODULE							HTAAFile.c
**		FILE ROUTINES FOR AUTHENTICATION
**		(PASSWD AND GROUP FILES) AND
**		ACCESS CONTROL LIST (.www_acl)
** AUTHORS:
**	AL	Ari Luotonen	luotonen@dxcern.cern.ch
**
** HISTORY:
**
**
** BUGS:
**
**
*/


#ifndef HTUTILS_H
#include "HTUtils.h"
#endif /* HTUTILS_H */

#include "tcp.h"		/* Macro FROMASCII() */
/*#include <stdio.h> included by HTUtils.h -- FM *//* FILE */
#include <string.h>
#include "HTAAUtil.h"		/* Common utilities used in AA */
#include "HTAAFile.h"		/* Implemented here */

#include "LYLeaks.h"

#define SPACE			' '
#define TAB			'\t'



/* PUBLIC						HTAAFile_nextRec()
**		GO TO THE BEGINNING OF THE NEXT RECORD
** ON ENTRY:
**	fp	is the file from which records are read from.
**
** ON EXIT:
**	returns	nothing. File read pointer is located at the beginning
**		of the next record. Handles continuation lines
**		(lines ending in comma indicate a following
**		continuation line).
**
*/
PUBLIC void HTAAFile_nextRec ARGS1(FILE *, fp)
{
    int ch = getc(fp);
    int last = (char)0;

    do {
	while (ch != EOF  &&  ch != CR  &&  ch != LF) {
	    if (ch != ' '  && ch != '\t')
		last = ch;		/* Last non-whitespace */
	    ch = getc(fp);		/* Skip until end-of-line */
	}
	while (ch != EOF &&
	       (ch == CR  ||  ch == LF))/*Skip carriage returns and linefeeds*/
	    ch = getc(fp);
	if (ch != EOF)
	    ungetc(ch, fp);
    } while (last == ',' && ch != EOF);	/* Skip also continuation lines */
}


/* PRIVATE							read_item()
**		READ AN ITEM FROM A PASSWORD, GROUP
**		OR ACCESS CONTROL LIST FILE
**		i.e. either a field, or a list item.
** ON ENTRY:
**	fp		is the file to read the characters from
**	contents	is the character array to put the characters
**	reading_list	if TRUE, read a list item (ends either in
**			acomma or acolon),
**			if FALSE, read a field (ends in acolon).
**	max_len		is the maximum number of characters that may
**			be read (i.e. the size of dest minus one for
**			terminating null).
** ON EXIT:
**	returns		the terminating character
**			(i.e. either separator or CR or LF or EOF).
**	contents	contains a null-terminated string representing
**			the read field.
** NOTE 1:
**			Ignores leading and trailing blanks and tabs.
** NOTE 2:
**			If the item is more than max_len characters
**			long, the rest of the characters in that item
**			are ignored.  However, contents is always
**			null-terminated!
*/
PRIVATE int read_item ARGS4(FILE *,	fp,
			    char *,	contents,
			    BOOL,	reading_list,
			    int,	max_len)
{
    char * dest = contents;
    char * end = contents;
    int cnt = 0;
    int ch = getc(fp);

    while (SPACE == ch || TAB == ch)	/* Skip spaces and tabs */
	ch = getc(fp);

    while (ch != FIELD_SEPARATOR &&
	   (!reading_list || ch != LIST_SEPARATOR) &&
	   ch != CR  &&  ch != LF  &&  ch != EOF  &&  cnt < max_len) {
	*(dest++) = ch;
	cnt++;
	if (ch != SPACE && ch != TAB)
	    end = dest;
	ch = getc(fp);
    } /* while not eol or eof or too many read */

    if (cnt == max_len)	{
	/* If the field was too long (or exactly maximum) ignore the rest */
	while (ch != FIELD_SEPARATOR &&
	       (!reading_list || ch != LIST_SEPARATOR) &&
	       ch != CR  &&  ch != LF  &&  ch != EOF)
	    ch = getc(fp);
    }

    if (ch == CR || ch == LF)
	ungetc(ch, fp);	/* Push back the record separator (NL or LF) */

    /* Terminate the string, truncating trailing whitespace off.
    ** Otherwise (if whitespace would be included), here would
    ** be *dest='\0'; and  cnt -= ... would be left out.
    */
    *end = '\0';
    cnt -= dest-end;

    return ch;		/* Return the terminating character */
}



/* PUBLIC						HTAAFile_readField()
**		READ A FIELD FROM A PASSWORD, GROUP
**		OR ACCESS CONTROL LIST FILE
**		i.e. an item terminated by colon,
**		end-of-line, or end-of-file. 
** ON ENTRY:
**	fp		is the file to read the characters from
**	contents	is the character array to put the characters
**	max_len		is the maximum number of characters that may
**			be read (i.e. the size of dest minus one for
**			terminating null).
** ON EXIT:
**	returns		the terminating character
**			(i.e. either separator or CR or LF or EOF).
**	contents	contains a null-terminated string representing
**			the read field.
** NOTE 1:
**			Ignores leading and trailing blanks and tabs.
** NOTE 2:
**			If the field is more than max_len characters
**			long, the rest of the characters in that item
**			are ignored.  However, contents is always
**			null-terminated!
*/
PUBLIC int HTAAFile_readField ARGS3(FILE *, fp,
				    char *, contents,
				    int,    max_len)
{
    return read_item(fp, contents, NO, max_len);
}




/* PUBLIC						HTAAFile_readList()
**
**			READ A LIST OF STRINGS SEPARATED BY COMMAS
**			(FROM A GROUP OR ACCESS CONTROL LIST FILE)
** ON ENTRY:
**	fp		is a pointer to the input file.
**	result		is the list to which append the read items.
**	max_len		is the maximum number of characters in each
**			list entry (extra characters are ignored).
** ON EXIT:
**	returns		the number of items read.
**
*/
PUBLIC int HTAAFile_readList ARGS3(FILE *,	fp,
				   HTList *,	result,
				   int,		max_len)
{
    char *item = NULL;
    int terminator;
    int cnt = 0;

    do {
	if (!item  &&  !(item = (char*)malloc(max_len+1)))
	    outofmem(__FILE__, "HTAAFile_readList");
	terminator = read_item(fp, item, YES, max_len);
	if (strlen(item) > 0) {
	    cnt++;
	    HTList_addObject(result, (void*)item);
	    item = NULL;
	}
    } while (terminator != FIELD_SEPARATOR  &&
	     terminator != CR  &&  terminator != LF  &&
	     terminator != EOF);

    FREE(item);	/* This was not needed */
    return cnt;
}