]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
503b54c9 | 2 | * Banner routines for the CUPS scheduler. |
ef416fc2 | 3 | * |
503b54c9 MS |
4 | * Copyright 2007-2011 by Apple Inc. |
5 | * Copyright 1997-2006 by Easy Software Products. | |
ef416fc2 | 6 | * |
e3101897 | 7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 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 | ||
e1d6a774 | 22 | static void add_banner(const char *name, const char *filename); |
fa73b229 | 23 | static int compare_banners(const cupsd_banner_t *b0, |
24 | const cupsd_banner_t *b1); | |
e1d6a774 | 25 | static void free_banners(void); |
ef416fc2 | 26 | |
27 | ||
28 | /* | |
29 | * 'cupsdFindBanner()' - Find a named banner. | |
30 | */ | |
31 | ||
32 | cupsd_banner_t * /* O - Pointer to banner or NULL */ | |
33 | cupsdFindBanner(const char *name) /* I - Name of banner */ | |
34 | { | |
35 | cupsd_banner_t key; /* Search key */ | |
36 | ||
37 | ||
bd7854cb | 38 | key.name = (char *)name; |
ef416fc2 | 39 | |
fa73b229 | 40 | return ((cupsd_banner_t *)cupsArrayFind(Banners, &key)); |
41 | } | |
42 | ||
43 | ||
ef416fc2 | 44 | /* |
45 | * 'cupsdLoadBanners()' - Load all available banner files... | |
46 | */ | |
47 | ||
48 | void | |
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 | ||
e1d6a774 | 61 | free_banners(); |
ef416fc2 | 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 | ||
fa73b229 | 78 | Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL); |
79 | ||
ef416fc2 | 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 | ||
bd7854cb | 92 | if (dent->filename[0] == '~' || |
93 | dent->filename[strlen(dent->filename) - 1] == '~') | |
ef416fc2 | 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 | ||
e1d6a774 | 106 | add_banner(dent->filename, filename); |
ef416fc2 | 107 | } |
108 | ||
109 | /* | |
fa73b229 | 110 | * Close the directory... |
ef416fc2 | 111 | */ |
112 | ||
113 | cupsDirClose(dir); | |
ef416fc2 | 114 | } |
115 | ||
116 | ||
e1d6a774 | 117 | /* |
118 | * 'add_banner()' - Add a banner to the array. | |
119 | */ | |
120 | ||
121 | static void | |
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, | |
91c84a35 MS |
136 | "add_banner: Banner \"%s\" (\"%s\") is of an unknown file " |
137 | "type - skipping!", name, filename); | |
e1d6a774 | 138 | return; |
139 | } | |
140 | ||
141 | /* | |
142 | * Allocate memory... | |
143 | */ | |
144 | ||
91c84a35 MS |
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 | } | |
e1d6a774 | 152 | |
153 | /* | |
154 | * Copy the new banner data over... | |
155 | */ | |
156 | ||
91c84a35 MS |
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 | ||
e1d6a774 | 166 | temp->filetype = filetype; |
167 | ||
168 | cupsArrayAdd(Banners, temp); | |
169 | } | |
170 | ||
171 | ||
ef416fc2 | 172 | /* |
fa73b229 | 173 | * 'compare_banners()' - Compare two banners. |
ef416fc2 | 174 | */ |
175 | ||
176 | static int /* O - -1 if name0 < name1, etc. */ | |
fa73b229 | 177 | compare_banners( |
178 | const cupsd_banner_t *b0, /* I - First banner */ | |
179 | const cupsd_banner_t *b1) /* I - Second banner */ | |
ef416fc2 | 180 | { |
88f9aafc | 181 | return (_cups_strcasecmp(b0->name, b1->name)); |
ef416fc2 | 182 | } |
183 | ||
184 | ||
185 | /* | |
e1d6a774 | 186 | * 'free_banners()' - Free all banners. |
187 | */ | |
188 | ||
07725fee | 189 | static void |
e1d6a774 | 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 | } |