ptg10805159
802 Communicating with a Network Printer Chapter 21
The next file we will look at isutil.c,the file containing utility routines.
1#include "apue.h"
2#include "print.h"
3#include <ctype.h>
4#include <sys/select.h>
5#define MAXCFGLINE 512
6#define MAXKWLEN 16
7#define MAXFMTLEN 16
8/*
9*Get the address list for the given host and service and
10 * return through ailistpp. Returns 0 on success or an error
11 * code on failure. Note that we do not set errno if we
12 * encounter an error.
13 *
14 * LOCKING: none.
15 */
16 int
17 getaddrlist(const char *host, const char *service,
18 struct addrinfo **ailistpp)
19 {
20 int err;
21 struct addrinfo hint;
22 hint.ai_flags=AI_CANONNAME;
23 hint.ai_family=AF_INET;
24 hint.ai_socktype=SOCK_STREAM;
25 hint.ai_protocol=0;
26 hint.ai_addrlen=0;
27 hint.ai_canonname=NULL;
28 hint.ai_addr=NULL;
29 hint.ai_next=NULL;
30 err=getaddrinfo(host, service, &hint, ailistpp);
31 return(err);
32 }
[1 – 7] We first define the limits needed by the functions in this file.MAXCFGLINEis
the maximum size of a line in the printer configuration file,MAXKWLENis the
maximum size of a keyword in the configuration file, andMAXFMTLENis the
maximum size of the format string we pass tosscanf.
[8 – 32] The first function is getaddrlist.It is a wrapper for getaddrinfo
(Section 16.3.3), since we always call getaddrinfo with the same hint
structure. Note that we do not need mutex locking in this function. The
LOCKINGcomment at the beginning of each function is intended only for
documenting multithreaded locking. This comment lists the assumptions, if
any,that aremade regarding the locking, tells which locks the function might
acquire or release, and tells which locks must be held to call the function.