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