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