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