]>
Commit | Line | Data |
---|---|---|
02d91449 | 1 | /* |
17438bf4 | 2 | * "$Id: banners.c,v 1.8 2002/05/16 13:44:58 mike Exp $" |
02d91449 | 3 | * |
4 | * Banner routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
efb2f309 | 6 | * Copyright 1997-2002 by Easy Software Products. |
02d91449 | 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)); | |
17438bf4 | 93 | strlcpy(temp->name, name, sizeof(temp->name)); |
02d91449 | 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 | ||
17438bf4 | 108 | strlcpy(key.name, name, sizeof(key.name)); |
02d91449 | 109 | |
110 | return ((banner_t *)bsearch(&key, Banners, NumBanners, sizeof(banner_t), | |
111 | (int (*)(const void *, const void *))compare)); | |
112 | } | |
113 | ||
114 | ||
115 | /* | |
116 | * 'LoadBanners()' - Load all available banner files... | |
117 | */ | |
118 | ||
119 | void | |
120 | LoadBanners(const char *d) /* I - Directory to search */ | |
121 | { | |
122 | DIR *dir; /* Directory pointer */ | |
123 | DIRENT *dent; /* Directory entry */ | |
124 | char filename[1024], /* Name of banner */ | |
125 | *ext; /* Pointer to extension */ | |
126 | struct stat fileinfo; /* File information */ | |
127 | ||
128 | ||
129 | /* | |
130 | * Free old banner info... | |
131 | */ | |
132 | ||
133 | if (NumBanners) | |
134 | { | |
135 | free(Banners); | |
136 | NumBanners = 0; | |
137 | } | |
138 | ||
139 | /* | |
140 | * Try opening the banner directory... | |
141 | */ | |
142 | ||
143 | if ((dir = opendir(d)) == NULL) | |
144 | { | |
145 | LogMessage(L_ERROR, "LoadBanners: Unable to open banner directory \"%s\": %s", | |
146 | d, strerror(errno)); | |
147 | return; | |
148 | } | |
149 | ||
150 | /* | |
151 | * Read entries, skipping directories and backup files. | |
152 | */ | |
153 | ||
154 | while ((dent = readdir(dir)) != NULL) | |
155 | { | |
156 | /* | |
157 | * Check the file to make sure it isn't a directory or a backup | |
158 | * file of some sort... | |
159 | */ | |
160 | ||
04de52f8 | 161 | snprintf(filename, sizeof(filename), "%s/%s", d, dent->d_name); |
02d91449 | 162 | |
163 | if (stat(filename, &fileinfo)) | |
164 | { | |
165 | LogMessage(L_WARN, "LoadBanners: Unable to state \"%s\" banner: %s", | |
166 | dent->d_name, strerror(errno)); | |
167 | continue; | |
168 | } | |
169 | ||
170 | if (S_ISDIR(fileinfo.st_mode)) | |
171 | continue; | |
172 | ||
173 | if (dent->d_name[0] == '~') | |
174 | continue; | |
175 | ||
176 | if ((ext = strrchr(dent->d_name, '.')) != NULL) | |
177 | if (strcmp(ext, ".bck") == 0 || | |
178 | strcmp(ext, ".bak") == 0 || | |
179 | strcmp(ext, ".sav") == 0) | |
180 | continue; | |
181 | ||
182 | /* | |
183 | * Must be a valid file; add it! | |
184 | */ | |
185 | ||
186 | AddBanner(dent->d_name, filename); | |
187 | } | |
188 | ||
189 | /* | |
190 | * Close the directory and sort as needed... | |
191 | */ | |
192 | ||
193 | closedir(dir); | |
194 | ||
195 | if (NumBanners > 1) | |
196 | qsort(Banners, NumBanners, sizeof(banner_t), | |
197 | (int (*)(const void *, const void *))compare); | |
198 | } | |
199 | ||
200 | ||
201 | /* | |
202 | * 'compare()' - Compare two banners. | |
203 | */ | |
204 | ||
205 | static int /* O - -1 if name0 < name1, etc. */ | |
206 | compare(const banner_t *b0, /* I - First banner */ | |
207 | const banner_t *b1) /* I - Second banner */ | |
208 | { | |
209 | return (strcasecmp(b0->name, b1->name)); | |
210 | } | |
211 | ||
212 | ||
02d91449 | 213 | /* |
17438bf4 | 214 | * End of "$Id: banners.c,v 1.8 2002/05/16 13:44:58 mike Exp $". |
02d91449 | 215 | */ |