Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / nsswitch / files / common / bootparams_getbyname.c
blobe1e88b1ced79aacebf81dfcd85d56651a8d2ba84
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * files/bootparams_getbyname.c -- "files" backend for
26 * nsswitch "bootparams" database.
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 static const char *bootparams = "/etc/bootparams";
33 #include "files_common.h"
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <strings.h>
38 static nss_status_t _nss_files_XY_bootparams(files_backend_ptr_t,
39 nss_XbyY_args_t *, const char *);
41 static nss_status_t
42 getbyname(be, a)
43 files_backend_ptr_t be;
44 void *a;
46 nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
47 nss_status_t res;
49 /* bootparams_getbyname() has not set/endent; rewind on each call */
50 if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
51 return (res);
53 return (_nss_files_XY_bootparams(be, argp, argp->key.name));
56 static files_backend_op_t bootparams_ops[] = {
57 _nss_files_destr,
58 getbyname
61 /*ARGSUSED*/
62 nss_backend_t *
63 _nss_files_bootparams_constr(dummy1, dummy2, dummy3)
64 const char *dummy1, *dummy2, *dummy3;
66 return (_nss_files_constr(bootparams_ops,
67 sizeof (bootparams_ops) / sizeof (bootparams_ops[0]),
68 bootparams,
69 NSS_LINELEN_BOOTPARAMS,
70 NULL));
74 * bootparams has the hostname as part of the data in the file, but the other
75 * backends don't include it in the data passed to the backend. For this
76 * reason, we process everything here and don't bother calling the backend.
78 /*ARGSUSED*/
79 static nss_status_t
80 _nss_files_XY_bootparams(be, args, filter)
81 files_backend_ptr_t be;
82 nss_XbyY_args_t *args;
83 const char *filter;
85 * filter not useful here since the key
86 * we are looking for is the first "word"
87 * on the line and we can be fast enough.
90 nss_status_t res;
92 if (be->buf == 0 &&
93 (be->buf = (char *)malloc(be->minbuf)) == 0) {
94 (void) _nss_files_endent(be, 0);
95 return (NSS_UNAVAIL); /* really panic, malloc failed */
98 res = NSS_NOTFOUND;
100 /*CONSTCOND*/
101 while (1) {
102 char *instr = be->buf;
103 char *p, *host, *limit;
104 int linelen;
107 * _nss_files_read_line does process the '\' that are used
108 * in /etc/bootparams for continuation and gives one long
109 * buffer.
111 * linelen counts the characters up to but excluding the '\n'
113 if ((linelen = _nss_files_read_line(be->f, instr,
114 be->minbuf)) < 0) {
115 /* End of file */
116 args->returnval = 0;
117 args->returnlen = 0;
118 break;
122 * we need to strip off the host name before returning it.
124 p = instr;
125 limit = p + linelen;
127 /* Skip over leading whitespace */
128 while (p < limit && isspace(*p)) {
129 p++;
131 host = p;
133 /* Skip over the hostname */
134 while (p < limit && !isspace(*p)) {
135 p++;
137 *p++ = '\0';
139 if (strcasecmp(args->key.name, host) != 0) {
140 continue;
143 /* Skip over whitespace between name and first datum */
144 while (p < limit && isspace(*p)) {
145 p++;
147 if (p >= limit) {
148 /* Syntax error -- no data! Just skip it. */
149 continue;
152 linelen -= (p - instr);
153 if (args->buf.buflen <= linelen) { /* not enough buffer */
154 args->erange = 1;
155 break;
157 (void) memcpy(args->buf.buffer, p, linelen);
158 args->buf.buffer[linelen] = '\0';
159 args->returnval = args->buf.result;
160 args->returnlen = linelen;
161 res = NSS_SUCCESS;
162 break;
164 (void) _nss_files_endent(be, 0);
165 return (res);