]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id: util.c 5305 2006-03-18 03:05:12Z 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 | ||
e1d6a774 | 197 | #if 0 /* Not currently used */ |
ef416fc2 | 198 | /* |
199 | * 'cupsdSendIPPInteger()' - Send an integer attribute. | |
200 | */ | |
201 | ||
202 | void | |
203 | cupsdSendIPPInteger( | |
204 | ipp_tag_t value_tag, /* I - Value tag */ | |
205 | const char *name, /* I - Attribute name */ | |
206 | int value) /* I - Attribute value */ | |
207 | { | |
208 | size_t len; /* Length of attribute name */ | |
209 | ||
210 | ||
211 | /* | |
212 | * Send IPP integer value: value tag (1 byte), name length (2 bytes), | |
213 | * name string (without nul), and value (4 bytes)... | |
214 | */ | |
215 | ||
216 | putchar(value_tag); | |
217 | ||
218 | len = strlen(name); | |
219 | putchar(len >> 8); | |
220 | putchar(len); | |
221 | ||
222 | fputs(name, stdout); | |
223 | ||
224 | putchar(value >> 24); | |
225 | putchar(value >> 16); | |
226 | putchar(value >> 8); | |
227 | putchar(value); | |
228 | } | |
e1d6a774 | 229 | #endif /* 0 */ |
ef416fc2 | 230 | |
231 | ||
232 | /* | |
233 | * 'cupsdSendIPPString()' - Send a string attribute. | |
234 | */ | |
235 | ||
236 | void | |
237 | cupsdSendIPPString( | |
238 | ipp_tag_t value_tag, /* I - Value tag */ | |
239 | const char *name, /* I - Attribute name */ | |
240 | const char *value) /* I - Attribute value */ | |
241 | { | |
242 | size_t len; /* Length of attribute name */ | |
243 | ||
244 | ||
245 | /* | |
246 | * Send IPP string value: value tag (1 byte), name length (2 bytes), | |
247 | * name string (without nul), value length (2 bytes), and value string | |
248 | * (without nul)... | |
249 | */ | |
250 | ||
251 | putchar(value_tag); | |
252 | ||
253 | len = strlen(name); | |
254 | putchar(len >> 8); | |
255 | putchar(len); | |
256 | ||
257 | fputs(name, stdout); | |
258 | ||
259 | len = strlen(value); | |
260 | putchar(len >> 8); | |
261 | putchar(len); | |
262 | ||
263 | fputs(value, stdout); | |
264 | } | |
265 | ||
266 | ||
267 | /* | |
268 | * 'cupsdSendIPPTrailer()' - Send the end-of-message tag. | |
269 | */ | |
270 | ||
271 | void | |
272 | cupsdSendIPPTrailer(void) | |
273 | { | |
274 | putchar(IPP_TAG_END); | |
275 | fflush(stdout); | |
276 | } | |
277 | ||
278 | ||
279 | /* | |
f7faf1f5 | 280 | * End of "$Id: util.c 5305 2006-03-18 03:05:12Z mike $". |
ef416fc2 | 281 | */ |