]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/banners.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / banners.c
CommitLineData
ef416fc2 1/*
07725fee 2 * "$Id: banners.c 5948 2006-09-12 13:58:39Z mike $"
ef416fc2 3 *
4 * Banner routines for the Common UNIX Printing System (CUPS).
5 *
fa73b229 6 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 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 *
ef416fc2 26 * cupsdFindBanner() - Find a named banner.
27 * cupsdLoadBanners() - Load all available banner files...
e1d6a774 28 * add_banner() - Add a banner to the array.
fa73b229 29 * compare_banners() - Compare two banners.
e1d6a774 30 * free_banners() - Free all banners.
ef416fc2 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
e1d6a774 45static void add_banner(const char *name, const char *filename);
fa73b229 46static int compare_banners(const cupsd_banner_t *b0,
47 const cupsd_banner_t *b1);
e1d6a774 48static void free_banners(void);
ef416fc2 49
50
51/*
52 * 'cupsdFindBanner()' - Find a named banner.
53 */
54
55cupsd_banner_t * /* O - Pointer to banner or NULL */
56cupsdFindBanner(const char *name) /* I - Name of banner */
57{
58 cupsd_banner_t key; /* Search key */
59
60
bd7854cb 61 key.name = (char *)name;
ef416fc2 62
fa73b229 63 return ((cupsd_banner_t *)cupsArrayFind(Banners, &key));
64}
65
66
ef416fc2 67/*
68 * 'cupsdLoadBanners()' - Load all available banner files...
69 */
70
71void
72cupsdLoadBanners(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
e1d6a774 84 free_banners();
ef416fc2 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
fa73b229 101 Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL);
102
ef416fc2 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
bd7854cb 115 if (dent->filename[0] == '~' ||
116 dent->filename[strlen(dent->filename) - 1] == '~')
ef416fc2 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
e1d6a774 129 add_banner(dent->filename, filename);
ef416fc2 130 }
131
132 /*
fa73b229 133 * Close the directory...
ef416fc2 134 */
135
136 cupsDirClose(dir);
ef416fc2 137}
138
139
e1d6a774 140/*
141 * 'add_banner()' - Add a banner to the array.
142 */
143
144static void
145add_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
ef416fc2 181/*
fa73b229 182 * 'compare_banners()' - Compare two banners.
ef416fc2 183 */
184
185static int /* O - -1 if name0 < name1, etc. */
fa73b229 186compare_banners(
187 const cupsd_banner_t *b0, /* I - First banner */
188 const cupsd_banner_t *b1) /* I - Second banner */
ef416fc2 189{
190 return (strcasecmp(b0->name, b1->name));
191}
192
193
194/*
e1d6a774 195 * 'free_banners()' - Free all banners.
196 */
197
07725fee 198static void
e1d6a774 199free_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/*
07725fee 218 * End of "$Id: banners.c 5948 2006-09-12 13:58:39Z mike $".
ef416fc2 219 */