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