]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: banners.c 7221 2008-01-16 22:20:08Z mike $" |
ef416fc2 | 3 | * |
4 | * Banner routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
fa73b229 | 7 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
ef416fc2 | 17 | * cupsdFindBanner() - Find a named banner. |
18 | * cupsdLoadBanners() - Load all available banner files... | |
e1d6a774 | 19 | * add_banner() - Add a banner to the array. |
fa73b229 | 20 | * compare_banners() - Compare two banners. |
e1d6a774 | 21 | * free_banners() - Free all banners. |
ef416fc2 | 22 | */ |
23 | ||
24 | /* | |
25 | * Include necessary headers... | |
26 | */ | |
27 | ||
28 | #include "cupsd.h" | |
29 | #include <cups/dir.h> | |
30 | ||
31 | ||
32 | /* | |
33 | * Local functions... | |
34 | */ | |
35 | ||
e1d6a774 | 36 | static void add_banner(const char *name, const char *filename); |
fa73b229 | 37 | static int compare_banners(const cupsd_banner_t *b0, |
38 | const cupsd_banner_t *b1); | |
e1d6a774 | 39 | static void free_banners(void); |
ef416fc2 | 40 | |
41 | ||
42 | /* | |
43 | * 'cupsdFindBanner()' - Find a named banner. | |
44 | */ | |
45 | ||
46 | cupsd_banner_t * /* O - Pointer to banner or NULL */ | |
47 | cupsdFindBanner(const char *name) /* I - Name of banner */ | |
48 | { | |
49 | cupsd_banner_t key; /* Search key */ | |
50 | ||
51 | ||
bd7854cb | 52 | key.name = (char *)name; |
ef416fc2 | 53 | |
fa73b229 | 54 | return ((cupsd_banner_t *)cupsArrayFind(Banners, &key)); |
55 | } | |
56 | ||
57 | ||
ef416fc2 | 58 | /* |
59 | * 'cupsdLoadBanners()' - Load all available banner files... | |
60 | */ | |
61 | ||
62 | void | |
63 | cupsdLoadBanners(const char *d) /* I - Directory to search */ | |
64 | { | |
65 | cups_dir_t *dir; /* Directory pointer */ | |
66 | cups_dentry_t *dent; /* Directory entry */ | |
67 | char filename[1024], /* Name of banner */ | |
68 | *ext; /* Pointer to extension */ | |
69 | ||
70 | ||
71 | /* | |
72 | * Free old banner info... | |
73 | */ | |
74 | ||
e1d6a774 | 75 | free_banners(); |
ef416fc2 | 76 | |
77 | /* | |
78 | * Try opening the banner directory... | |
79 | */ | |
80 | ||
81 | if ((dir = cupsDirOpen(d)) == NULL) | |
82 | { | |
83 | cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdLoadBanners: Unable to open banner directory \"%s\": %s", | |
84 | d, strerror(errno)); | |
85 | return; | |
86 | } | |
87 | ||
88 | /* | |
89 | * Read entries, skipping directories and backup files. | |
90 | */ | |
91 | ||
fa73b229 | 92 | Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL); |
93 | ||
ef416fc2 | 94 | while ((dent = cupsDirRead(dir)) != NULL) |
95 | { | |
96 | /* | |
97 | * Check the file to make sure it isn't a directory or a backup | |
98 | * file of some sort... | |
99 | */ | |
100 | ||
101 | snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename); | |
102 | ||
103 | if (S_ISDIR(dent->fileinfo.st_mode)) | |
104 | continue; | |
105 | ||
bd7854cb | 106 | if (dent->filename[0] == '~' || |
107 | dent->filename[strlen(dent->filename) - 1] == '~') | |
ef416fc2 | 108 | continue; |
109 | ||
110 | if ((ext = strrchr(dent->filename, '.')) != NULL) | |
111 | if (!strcmp(ext, ".bck") || | |
112 | !strcmp(ext, ".bak") || | |
113 | !strcmp(ext, ".sav")) | |
114 | continue; | |
115 | ||
116 | /* | |
117 | * Must be a valid file; add it! | |
118 | */ | |
119 | ||
e1d6a774 | 120 | add_banner(dent->filename, filename); |
ef416fc2 | 121 | } |
122 | ||
123 | /* | |
fa73b229 | 124 | * Close the directory... |
ef416fc2 | 125 | */ |
126 | ||
127 | cupsDirClose(dir); | |
ef416fc2 | 128 | } |
129 | ||
130 | ||
e1d6a774 | 131 | /* |
132 | * 'add_banner()' - Add a banner to the array. | |
133 | */ | |
134 | ||
135 | static void | |
136 | add_banner(const char *name, /* I - Name of banner */ | |
137 | const char *filename) /* I - Filename for banner */ | |
138 | { | |
139 | mime_type_t *filetype; /* Filetype */ | |
140 | cupsd_banner_t *temp; /* New banner data */ | |
141 | ||
142 | ||
143 | /* | |
144 | * See what the filetype is... | |
145 | */ | |
146 | ||
147 | if ((filetype = mimeFileType(MimeDatabase, filename, NULL, NULL)) == NULL) | |
148 | { | |
149 | cupsdLogMessage(CUPSD_LOG_WARN, | |
91c84a35 MS |
150 | "add_banner: Banner \"%s\" (\"%s\") is of an unknown file " |
151 | "type - skipping!", name, filename); | |
e1d6a774 | 152 | return; |
153 | } | |
154 | ||
155 | /* | |
156 | * Allocate memory... | |
157 | */ | |
158 | ||
91c84a35 MS |
159 | if ((temp = calloc(1, sizeof(cupsd_banner_t))) == NULL) |
160 | { | |
161 | cupsdLogMessage(CUPSD_LOG_WARN, | |
162 | "add_banner: Unable to allocate memory for banner \"%s\" - " | |
163 | "skipping!", name); | |
164 | return; | |
165 | } | |
e1d6a774 | 166 | |
167 | /* | |
168 | * Copy the new banner data over... | |
169 | */ | |
170 | ||
91c84a35 MS |
171 | if ((temp->name = strdup(name)) == NULL) |
172 | { | |
173 | cupsdLogMessage(CUPSD_LOG_WARN, | |
174 | "add_banner: Unable to allocate memory for banner \"%s\" - " | |
175 | "skipping!", name); | |
176 | free(temp); | |
177 | return; | |
178 | } | |
179 | ||
e1d6a774 | 180 | temp->filetype = filetype; |
181 | ||
182 | cupsArrayAdd(Banners, temp); | |
183 | } | |
184 | ||
185 | ||
ef416fc2 | 186 | /* |
fa73b229 | 187 | * 'compare_banners()' - Compare two banners. |
ef416fc2 | 188 | */ |
189 | ||
190 | static int /* O - -1 if name0 < name1, etc. */ | |
fa73b229 | 191 | compare_banners( |
192 | const cupsd_banner_t *b0, /* I - First banner */ | |
193 | const cupsd_banner_t *b1) /* I - Second banner */ | |
ef416fc2 | 194 | { |
195 | return (strcasecmp(b0->name, b1->name)); | |
196 | } | |
197 | ||
198 | ||
199 | /* | |
e1d6a774 | 200 | * 'free_banners()' - Free all banners. |
201 | */ | |
202 | ||
07725fee | 203 | static void |
e1d6a774 | 204 | free_banners(void) |
205 | { | |
206 | cupsd_banner_t *temp; /* Current banner */ | |
207 | ||
208 | ||
209 | for (temp = (cupsd_banner_t *)cupsArrayFirst(Banners); | |
210 | temp; | |
211 | temp = (cupsd_banner_t *)cupsArrayNext(Banners)) | |
212 | { | |
213 | free(temp->name); | |
214 | free(temp); | |
215 | } | |
216 | ||
217 | cupsArrayDelete(Banners); | |
218 | Banners = NULL; | |
219 | } | |
220 | ||
221 | ||
222 | /* | |
75bd9771 | 223 | * End of "$Id: banners.c 7221 2008-01-16 22:20:08Z mike $". |
ef416fc2 | 224 | */ |