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