]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
3d8365b8 | 2 | * "$Id: util.c 6535 2007-05-16 20:35:05Z mike $" |
ef416fc2 | 3 | * |
4 | * Mini-daemon utility functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2005 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cupsdCompareNames() - Compare two names. | |
27 | * cupsdSendIPPGroup() - Send a group tag. | |
28 | * cupsdSendIPPHeader() - Send the IPP response header. | |
29 | * cupsdSendIPPInteger() - Send an integer attribute. | |
30 | * cupsdSendIPPString() - Send a string attribute. | |
31 | * cupsdSendIPPTrailer() - Send the end-of-message tag. | |
32 | */ | |
33 | ||
34 | /* | |
35 | * Include necessary headers... | |
36 | */ | |
37 | ||
38 | #include "util.h" | |
39 | ||
40 | ||
41 | /* | |
42 | * 'cupsdCompareNames()' - Compare two names. | |
43 | * | |
44 | * This function basically does a strcasecmp() of the two strings, | |
45 | * but is also aware of numbers so that "a2" < "a100". | |
46 | */ | |
47 | ||
48 | int /* O - Result of comparison */ | |
49 | cupsdCompareNames(const char *s, /* I - First string */ | |
50 | const char *t) /* I - Second string */ | |
51 | { | |
52 | int diff, /* Difference between digits */ | |
53 | digits; /* Number of digits */ | |
54 | ||
55 | ||
56 | /* | |
57 | * Loop through both names, returning only when a difference is | |
58 | * seen. Also, compare whole numbers rather than just characters, too! | |
59 | */ | |
60 | ||
61 | while (*s && *t) | |
62 | { | |
63 | if (isdigit(*s & 255) && isdigit(*t & 255)) | |
64 | { | |
65 | /* | |
66 | * Got a number; start by skipping leading 0's... | |
67 | */ | |
68 | ||
69 | while (*s == '0') | |
70 | s ++; | |
71 | while (*t == '0') | |
72 | t ++; | |
73 | ||
74 | /* | |
75 | * Skip equal digits... | |
76 | */ | |
77 | ||
78 | while (isdigit(*s & 255) && *s == *t) | |
79 | { | |
80 | s ++; | |
81 | t ++; | |
82 | } | |
83 | ||
84 | /* | |
85 | * Bounce out if *s and *t aren't both digits... | |
86 | */ | |
87 | ||
88 | if (isdigit(*s & 255) && !isdigit(*t & 255)) | |
89 | return (1); | |
90 | else if (!isdigit(*s & 255) && isdigit(*t & 255)) | |
91 | return (-1); | |
92 | else if (!isdigit(*s & 255) || !isdigit(*t & 255)) | |
93 | continue; | |
94 | ||
95 | if (*s < *t) | |
96 | diff = -1; | |
97 | else | |
98 | diff = 1; | |
99 | ||
100 | /* | |
101 | * Figure out how many more digits there are... | |
102 | */ | |
103 | ||
104 | digits = 0; | |
105 | s ++; | |
106 | t ++; | |
107 | ||
108 | while (isdigit(*s & 255)) | |
109 | { | |
110 | digits ++; | |
111 | s ++; | |
112 | } | |
113 | ||
114 | while (isdigit(*t & 255)) | |
115 | { | |
116 | digits --; | |
117 | t ++; | |
118 | } | |
119 | ||
120 | /* | |
121 | * Return if the number or value of the digits is different... | |
122 | */ | |
123 | ||
124 | if (digits < 0) | |
125 | return (-1); | |
126 | else if (digits > 0) | |
127 | return (1); | |
128 | else if (diff) | |
129 | return (diff); | |
130 | } | |
131 | else if (tolower(*s) < tolower(*t)) | |
132 | return (-1); | |
133 | else if (tolower(*s) > tolower(*t)) | |
134 | return (1); | |
135 | else | |
136 | { | |
137 | s ++; | |
138 | t ++; | |
139 | } | |
140 | } | |
141 | ||
142 | /* | |
143 | * Return the results of the final comparison... | |
144 | */ | |
145 | ||
146 | if (*s) | |
147 | return (1); | |
148 | else if (*t) | |
149 | return (-1); | |
150 | else | |
151 | return (0); | |
152 | } | |
153 | ||
154 | ||
155 | /* | |
156 | * 'cupsdSendIPPGroup()' - Send a group tag. | |
157 | */ | |
158 | ||
159 | void | |
160 | cupsdSendIPPGroup(ipp_tag_t group_tag) /* I - Group tag */ | |
161 | { | |
162 | /* | |
163 | * Send IPP group tag (1 byte)... | |
164 | */ | |
165 | ||
166 | putchar(group_tag); | |
167 | } | |
168 | ||
169 | ||
170 | /* | |
171 | * 'cupsdSendIPPHeader()' - Send the IPP response header. | |
172 | */ | |
173 | ||
174 | void | |
175 | cupsdSendIPPHeader( | |
176 | ipp_status_t status_code, /* I - Status code */ | |
177 | int request_id) /* I - Request ID */ | |
178 | { | |
179 | /* | |
180 | * Send IPP/1.1 response header: version number (2 bytes), status code | |
181 | * (2 bytes), and request ID (4 bytes)... | |
182 | */ | |
183 | ||
184 | putchar(1); | |
185 | putchar(1); | |
186 | ||
187 | putchar(status_code >> 8); | |
188 | putchar(status_code); | |
189 | ||
190 | putchar(request_id >> 24); | |
191 | putchar(request_id >> 16); | |
192 | putchar(request_id >> 8); | |
193 | putchar(request_id); | |
194 | } | |
195 | ||
196 | ||
197 | /* | |
198 | * 'cupsdSendIPPInteger()' - Send an integer attribute. | |
199 | */ | |
200 | ||
201 | void | |
202 | cupsdSendIPPInteger( | |
203 | ipp_tag_t value_tag, /* I - Value tag */ | |
204 | const char *name, /* I - Attribute name */ | |
205 | int value) /* I - Attribute value */ | |
206 | { | |
207 | size_t len; /* Length of attribute name */ | |
208 | ||
209 | ||
210 | /* | |
211 | * Send IPP integer value: value tag (1 byte), name length (2 bytes), | |
3d8365b8 | 212 | * name string (without nul), value length (2 bytes), and value (4 bytes)... |
ef416fc2 | 213 | */ |
214 | ||
215 | putchar(value_tag); | |
216 | ||
217 | len = strlen(name); | |
218 | putchar(len >> 8); | |
219 | putchar(len); | |
220 | ||
221 | fputs(name, stdout); | |
222 | ||
3d8365b8 | 223 | putchar(0); |
224 | putchar(4); | |
225 | ||
ef416fc2 | 226 | putchar(value >> 24); |
227 | putchar(value >> 16); | |
228 | putchar(value >> 8); | |
229 | putchar(value); | |
230 | } | |
231 | ||
232 | ||
233 | /* | |
234 | * 'cupsdSendIPPString()' - Send a string attribute. | |
235 | */ | |
236 | ||
237 | void | |
238 | cupsdSendIPPString( | |
239 | ipp_tag_t value_tag, /* I - Value tag */ | |
240 | const char *name, /* I - Attribute name */ | |
241 | const char *value) /* I - Attribute value */ | |
242 | { | |
243 | size_t len; /* Length of attribute name */ | |
244 | ||
245 | ||
246 | /* | |
247 | * Send IPP string value: value tag (1 byte), name length (2 bytes), | |
248 | * name string (without nul), value length (2 bytes), and value string | |
249 | * (without nul)... | |
250 | */ | |
251 | ||
252 | putchar(value_tag); | |
253 | ||
254 | len = strlen(name); | |
255 | putchar(len >> 8); | |
256 | putchar(len); | |
257 | ||
258 | fputs(name, stdout); | |
259 | ||
260 | len = strlen(value); | |
261 | putchar(len >> 8); | |
262 | putchar(len); | |
263 | ||
264 | fputs(value, stdout); | |
265 | } | |
266 | ||
267 | ||
268 | /* | |
269 | * 'cupsdSendIPPTrailer()' - Send the end-of-message tag. | |
270 | */ | |
271 | ||
272 | void | |
273 | cupsdSendIPPTrailer(void) | |
274 | { | |
275 | putchar(IPP_TAG_END); | |
276 | fflush(stdout); | |
277 | } | |
278 | ||
279 | ||
280 | /* | |
3d8365b8 | 281 | * End of "$Id: util.c 6535 2007-05-16 20:35:05Z mike $". |
ef416fc2 | 282 | */ |