usr/src/lib/nsswitch/files/common/getprinter.c
changeset 2847 05bae185f48c
parent 2830 5228d1267a01
child 2849 152a9fea7df7
equal deleted inserted replaced
2846:d2693e8a5243 2847:05bae185f48c
    33 #pragma weak _nss_files__printers_constr = _nss_files_printers_constr
    33 #pragma weak _nss_files__printers_constr = _nss_files_printers_constr
    34 
    34 
    35 #include "files_common.h"
    35 #include "files_common.h"
    36 #include <stdlib.h>
    36 #include <stdlib.h>
    37 #include <strings.h>
    37 #include <strings.h>
       
    38 #include <ctype.h>
    38 
    39 
    39 static int
    40 static int
    40 check_name(nss_XbyY_args_t *argp, const char *line, int linelen)
    41 check_name(nss_XbyY_args_t *argp, const char *line, int linelen)
    41 {
    42 {
    42 
    43 
    64 		}
    65 		}
    65 	}
    66 	}
    66 	return (0);
    67 	return (0);
    67 }
    68 }
    68 
    69 
       
    70 nss_status_t
       
    71 _nss_files_XY_printer(be, args, filter, check)
       
    72 	files_backend_ptr_t	be;
       
    73 	nss_XbyY_args_t		*args;
       
    74 	const char		*filter;	/* advisory, to speed up */
       
    75 						/* string search */
       
    76 	files_XY_check_func	check;	/* NULL means one-shot, for getXXent */
       
    77 {
       
    78 	nss_status_t		res;
       
    79 	int	parsestat;
       
    80 	int (*func)();
       
    81 
       
    82 	if (filter != NULL && *filter == '\0')
       
    83 		return (NSS_NOTFOUND);
       
    84 	if (be->buf == 0 &&
       
    85 		(be->buf = malloc(be->minbuf)) == 0) {
       
    86 		return (NSS_UNAVAIL); /* really panic, malloc failed */
       
    87 	}
       
    88 
       
    89 	if (check != 0 || be->f == 0) {
       
    90 		if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
       
    91 			return (res);
       
    92 		}
       
    93 	}
       
    94 
       
    95 	res = NSS_NOTFOUND;
       
    96 
       
    97 	/*CONSTCOND*/
       
    98 	while (1) {
       
    99 		char		*instr	= be->buf;
       
   100 		int		linelen;
       
   101 
       
   102 		if ((linelen = _nss_files_read_line(be->f, instr,
       
   103 		    be->minbuf)) < 0) {
       
   104 			/* End of file */
       
   105 			args->returnval = 0;
       
   106 			args->returnlen = 0;
       
   107 			break;
       
   108 		}
       
   109 
       
   110 		/* begin at the first non-blank character */
       
   111 		while (isspace(*instr)) {
       
   112 			instr++;
       
   113 			linelen--;
       
   114 		}
       
   115 
       
   116 		/* comment line, skip it. */
       
   117 		if (*instr == '#')
       
   118 			continue;
       
   119 
       
   120 		/* blank line, skip it */
       
   121 		if ((*instr == '\n') || (*instr == '\0'))
       
   122 			continue;
       
   123 
       
   124 		if (filter != 0 && strstr(instr, filter) == 0) {
       
   125 			/*
       
   126 			 * Optimization:  if the entry doesn't contain the
       
   127 			 *   filter string then it can't be the entry we want,
       
   128 			 *   so don't bother looking more closely at it.
       
   129 			 */
       
   130 			continue;
       
   131 		}
       
   132 
       
   133 		args->returnval = 0;
       
   134 		args->returnlen = 0;
       
   135 
       
   136 		if (check != NULL && (*check)(args, instr, linelen) == 0)
       
   137 			continue;
       
   138 
       
   139 		func = args->str2ent;
       
   140 		parsestat = (*func)(instr, linelen, args->buf.result,
       
   141 					args->buf.buffer, args->buf.buflen);
       
   142 
       
   143 		if (parsestat == NSS_STR_PARSE_SUCCESS) {
       
   144 			args->returnval = (args->buf.result != NULL)?
       
   145 					args->buf.result : args->buf.buffer;
       
   146 			args->returnlen = linelen;
       
   147 			res = NSS_SUCCESS;
       
   148 			break;
       
   149 		} else if (parsestat == NSS_STR_PARSE_ERANGE) {
       
   150 			args->erange = 1;
       
   151 			break;
       
   152 		} else if (parsestat == NSS_STR_PARSE_PARSE)
       
   153 			continue;
       
   154 	}
       
   155 
       
   156 	/*
       
   157 	 * stayopen is set to 0 by default in order to close the opened
       
   158 	 * file.  Some applications may break if it is set to 1.
       
   159 	 */
       
   160 	if (check != 0 && !args->stayopen) {
       
   161 		(void) _nss_files_endent(be, 0);
       
   162 	}
       
   163 
       
   164 	return (res);
       
   165 }
       
   166 
       
   167 static nss_status_t
       
   168 getent(be, a)
       
   169 	files_backend_ptr_t	be;
       
   170 	void			*a;
       
   171 {
       
   172 	nss_status_t status;
       
   173 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
       
   174 
       
   175 	return (_nss_files_XY_printer(be, argp, (files_XY_check_func)0,
       
   176 					(const char *)0));
       
   177 }
       
   178 
    69 static nss_status_t
   179 static nss_status_t
    70 getbyname(be, a)
   180 getbyname(be, a)
    71 	files_backend_ptr_t	be;
   181 	files_backend_ptr_t	be;
    72 	void			*a;
   182 	void			*a;
    73 {
   183 {
    74 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
   184 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
    75 
   185 
    76 	return (_nss_files_XY_all(be, argp, 1, argp->key.name,
   186 	return (_nss_files_XY_printer(be, argp, argp->key.name, check_name));
    77 			check_name));
       
    78 }
   187 }
    79 
   188 
    80 static files_backend_op_t printers_ops[] = {
   189 static files_backend_op_t printers_ops[] = {
    81 	_nss_files_destr,
   190 	_nss_files_destr,
    82 	_nss_files_endent,
   191 	_nss_files_endent,
    83 	_nss_files_setent,
   192 	_nss_files_setent,
    84 	_nss_files_getent_rigid,
   193 	getent,
    85 	getbyname
   194 	getbyname
    86 };
   195 };
    87 
   196 
    88 /*ARGSUSED*/
   197 /*ARGSUSED*/
    89 nss_backend_t *
   198 nss_backend_t *