blob: 04e436fae1e86d96ac240a9f88f48057d6bae882 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02002/* Copyright (C) 2003 Manuel Novoa III
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +00003 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +00005 */
6
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007/* Nov 6, 2003 Initial version.
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * NOTE: This implementation is quite strict about requiring all
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000010 * field seperators. It also does not allow leading whitespace
11 * except when processing the numeric fields. glibc is more
12 * lenient. See the various glibc difference comments below.
13 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * TODO:
Rob Landley06ec8cf2006-03-03 19:02:50 +000015 * Move to dynamic allocation of (currently statically allocated)
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000016 * buffers; especially for the group-related functions since
17 * large group member lists will cause error returns.
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000018 */
19
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000020#ifndef GETXXKEY_R_FUNC
21#error GETXXKEY_R_FUNC is not defined!
22#endif
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000023
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000024int GETXXKEY_R_FUNC(GETXXKEY_R_KEYTYPE key,
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000025 GETXXKEY_R_ENTTYPE *__restrict resultbuf,
26 char *__restrict buffer, size_t buflen,
27 GETXXKEY_R_ENTTYPE **__restrict result)
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000028{
29 FILE *stream;
30 int rv;
31
32 *result = NULL;
33
Denis Vlasenko5415c852008-07-21 23:05:26 +000034 stream = fopen_for_read(GETXXKEY_R_PATHNAME);
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000035 if (!stream)
36 return errno;
37 while (1) {
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000038 rv = bb__pgsreader(GETXXKEY_R_PARSER, resultbuf, buffer, buflen, stream);
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000039 if (!rv) {
40 if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
41 *result = resultbuf;
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000042 break;
43 }
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000044 } else {
45 if (rv == ENOENT) { /* end-of-file encountered. */
46 rv = 0;
47 }
48 break;
49 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000050 }
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000051 fclose(stream);
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000052
53 return rv;
54}
55
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000056#undef GETXXKEY_R_FUNC
57#undef GETXXKEY_R_PARSER
58#undef GETXXKEY_R_ENTTYPE
59#undef GETXXKEY_R_TEST
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000060#undef GETXXKEY_R_KEYTYPE
61#undef GETXXKEY_R_PATHNAME