]> 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 4719 2005-09-28 21:12:44Z mike $"
3 *
4 * Banner routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 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 * cupsdAddBanner() - Add a banner to the array.
27 * cupsdFindBanner() - Find a named banner.
28 * cupsdLoadBanners() - Load all available banner files...
29 * compare() - Compare two banners.
30 */
31
32 /*
33 * Include necessary headers...
34 */
35
36 #include "cupsd.h"
37 #include <cups/dir.h>
38
39
40 /*
41 * Local functions...
42 */
43
44 static int compare(const cupsd_banner_t *b0, const cupsd_banner_t *b1);
45
46
47 /*
48 * 'cupsdAddBanner()' - Add a banner to the array.
49 */
50
51 void
52 cupsdAddBanner(const char *name, /* I - Name of banner */
53 const char *filename) /* I - Filename for banner */
54 {
55 mime_type_t *filetype; /* Filetype */
56 cupsd_banner_t *temp; /* New banner data */
57
58
59 /*
60 * See what the filetype is...
61 */
62
63 if ((filetype = mimeFileType(MimeDatabase, filename, NULL)) == NULL)
64 {
65 cupsdLogMessage(CUPSD_LOG_WARN,
66 "cupsdAddBanner: Banner \"%s\" (\"%s\") is of an unknown file type - skipping!",
67 name, filename);
68 return;
69 }
70
71 /*
72 * Allocate memory...
73 */
74
75 if (NumBanners == 0)
76 temp = malloc(sizeof(cupsd_banner_t));
77 else
78 temp = realloc(Banners, sizeof(cupsd_banner_t) * (NumBanners + 1));
79
80 if (temp == NULL)
81 {
82 cupsdLogMessage(CUPSD_LOG_ERROR,
83 "cupsdAddBanner: Ran out of memory adding a banner!");
84 return;
85 }
86
87 /*
88 * Copy the new banner data over...
89 */
90
91 Banners = temp;
92 temp += NumBanners;
93 NumBanners ++;
94
95 memset(temp, 0, sizeof(cupsd_banner_t));
96 strlcpy(temp->name, name, sizeof(temp->name));
97 temp->filetype = filetype;
98 }
99
100
101 /*
102 * 'cupsdFindBanner()' - Find a named banner.
103 */
104
105 cupsd_banner_t * /* O - Pointer to banner or NULL */
106 cupsdFindBanner(const char *name) /* I - Name of banner */
107 {
108 cupsd_banner_t key; /* Search key */
109
110
111 strlcpy(key.name, name, sizeof(key.name));
112
113 return ((cupsd_banner_t *)bsearch(&key, Banners, NumBanners, sizeof(cupsd_banner_t),
114 (int (*)(const void *, const void *))compare));
115 }
116
117
118 /*
119 * 'cupsdLoadBanners()' - Load all available banner files...
120 */
121
122 void
123 cupsdLoadBanners(const char *d) /* I - Directory to search */
124 {
125 cups_dir_t *dir; /* Directory pointer */
126 cups_dentry_t *dent; /* Directory entry */
127 char filename[1024], /* Name of banner */
128 *ext; /* Pointer to extension */
129
130
131 /*
132 * Free old banner info...
133 */
134
135 if (NumBanners)
136 {
137 free(Banners);
138 NumBanners = 0;
139 }
140
141 /*
142 * Try opening the banner directory...
143 */
144
145 if ((dir = cupsDirOpen(d)) == NULL)
146 {
147 cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdLoadBanners: Unable to open banner directory \"%s\": %s",
148 d, strerror(errno));
149 return;
150 }
151
152 /*
153 * Read entries, skipping directories and backup files.
154 */
155
156 while ((dent = cupsDirRead(dir)) != NULL)
157 {
158 /*
159 * Check the file to make sure it isn't a directory or a backup
160 * file of some sort...
161 */
162
163 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
164
165 if (S_ISDIR(dent->fileinfo.st_mode))
166 continue;
167
168 if (dent->filename[0] == '~')
169 continue;
170
171 if ((ext = strrchr(dent->filename, '.')) != NULL)
172 if (!strcmp(ext, ".bck") ||
173 !strcmp(ext, ".bak") ||
174 !strcmp(ext, ".sav"))
175 continue;
176
177 /*
178 * Must be a valid file; add it!
179 */
180
181 cupsdAddBanner(dent->filename, filename);
182 }
183
184 /*
185 * Close the directory and sort as needed...
186 */
187
188 cupsDirClose(dir);
189
190 if (NumBanners > 1)
191 qsort(Banners, NumBanners, sizeof(cupsd_banner_t),
192 (int (*)(const void *, const void *))compare);
193 }
194
195
196 /*
197 * 'compare()' - Compare two banners.
198 */
199
200 static int /* O - -1 if name0 < name1, etc. */
201 compare(const cupsd_banner_t *b0, /* I - First banner */
202 const cupsd_banner_t *b1) /* I - Second banner */
203 {
204 return (strcasecmp(b0->name, b1->name));
205 }
206
207
208 /*
209 * End of "$Id: banners.c 4719 2005-09-28 21:12:44Z mike $".
210 */