]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/banners.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / banners.c
1 /*
2 * "$Id: banners.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * Banner routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 *
15 * Contents:
16 *
17 * cupsdFindBanner() - Find a named banner.
18 * cupsdLoadBanners() - Load all available banner files...
19 * add_banner() - Add a banner to the array.
20 * compare_banners() - Compare two banners.
21 * free_banners() - Free all banners.
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
36 static void add_banner(const char *name, const char *filename);
37 static int compare_banners(const cupsd_banner_t *b0,
38 const cupsd_banner_t *b1);
39 static void free_banners(void);
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
52 key.name = (char *)name;
53
54 return ((cupsd_banner_t *)cupsArrayFind(Banners, &key));
55 }
56
57
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
75 free_banners();
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
92 Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL);
93
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
106 if (dent->filename[0] == '~' ||
107 dent->filename[strlen(dent->filename) - 1] == '~')
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
120 add_banner(dent->filename, filename);
121 }
122
123 /*
124 * Close the directory...
125 */
126
127 cupsDirClose(dir);
128 }
129
130
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,
150 "add_banner: Banner \"%s\" (\"%s\") is of an unknown file type - skipping!",
151 name, filename);
152 return;
153 }
154
155 /*
156 * Allocate memory...
157 */
158
159 temp = calloc(1, sizeof(cupsd_banner_t));
160
161 /*
162 * Copy the new banner data over...
163 */
164
165 temp->name = strdup(name);
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. */
177 compare_banners(
178 const cupsd_banner_t *b0, /* I - First banner */
179 const cupsd_banner_t *b1) /* I - Second banner */
180 {
181 return (strcasecmp(b0->name, b1->name));
182 }
183
184
185 /*
186 * 'free_banners()' - Free all banners.
187 */
188
189 static 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
207
208 /*
209 * End of "$Id: banners.c 6649 2007-07-11 21:46:42Z mike $".
210 */