]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bd7854cb | 2 | * "$Id: banners.c 5062 2006-02-03 16:36:24Z 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 | * | |
26 | * cupsdAddBanner() - Add a banner to the array. | |
27 | * cupsdFindBanner() - Find a named banner. | |
28 | * cupsdLoadBanners() - Load all available banner files... | |
fa73b229 | 29 | * compare_banners() - Compare two banners. |
ef416fc2 | 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 | ||
fa73b229 | 44 | static int compare_banners(const cupsd_banner_t *b0, |
45 | const cupsd_banner_t *b1); | |
ef416fc2 | 46 | |
47 | ||
48 | /* | |
49 | * 'cupsdAddBanner()' - Add a banner to the array. | |
50 | */ | |
51 | ||
52 | void | |
53 | cupsdAddBanner(const char *name, /* I - Name of banner */ | |
54 | const char *filename) /* I - Filename for banner */ | |
55 | { | |
56 | mime_type_t *filetype; /* Filetype */ | |
57 | cupsd_banner_t *temp; /* New banner data */ | |
58 | ||
59 | ||
60 | /* | |
61 | * See what the filetype is... | |
62 | */ | |
63 | ||
4400e98d | 64 | if ((filetype = mimeFileType(MimeDatabase, filename, NULL, NULL)) == NULL) |
ef416fc2 | 65 | { |
66 | cupsdLogMessage(CUPSD_LOG_WARN, | |
67 | "cupsdAddBanner: Banner \"%s\" (\"%s\") is of an unknown file type - skipping!", | |
68 | name, filename); | |
69 | return; | |
70 | } | |
71 | ||
72 | /* | |
73 | * Allocate memory... | |
74 | */ | |
75 | ||
fa73b229 | 76 | temp = calloc(1, sizeof(cupsd_banner_t)); |
ef416fc2 | 77 | |
78 | /* | |
79 | * Copy the new banner data over... | |
80 | */ | |
81 | ||
bd7854cb | 82 | temp->name = strdup(name); |
ef416fc2 | 83 | temp->filetype = filetype; |
fa73b229 | 84 | |
85 | cupsArrayAdd(Banners, temp); | |
ef416fc2 | 86 | } |
87 | ||
88 | ||
89 | /* | |
90 | * 'cupsdFindBanner()' - Find a named banner. | |
91 | */ | |
92 | ||
93 | cupsd_banner_t * /* O - Pointer to banner or NULL */ | |
94 | cupsdFindBanner(const char *name) /* I - Name of banner */ | |
95 | { | |
96 | cupsd_banner_t key; /* Search key */ | |
97 | ||
98 | ||
bd7854cb | 99 | key.name = (char *)name; |
ef416fc2 | 100 | |
fa73b229 | 101 | return ((cupsd_banner_t *)cupsArrayFind(Banners, &key)); |
102 | } | |
103 | ||
104 | ||
105 | /* | |
106 | * 'cupsdFreeBanners()' - Free all banners. | |
107 | */ | |
108 | ||
109 | void | |
110 | cupsdFreeBanners(void) | |
111 | { | |
112 | cupsd_banner_t *temp; /* Current banner */ | |
113 | ||
114 | ||
115 | for (temp = (cupsd_banner_t *)cupsArrayFirst(Banners); | |
116 | temp; | |
117 | temp = (cupsd_banner_t *)cupsArrayNext(Banners)) | |
bd7854cb | 118 | { |
119 | free(temp->name); | |
fa73b229 | 120 | free(temp); |
bd7854cb | 121 | } |
fa73b229 | 122 | |
123 | cupsArrayDelete(Banners); | |
124 | Banners = NULL; | |
ef416fc2 | 125 | } |
126 | ||
127 | ||
128 | /* | |
129 | * 'cupsdLoadBanners()' - Load all available banner files... | |
130 | */ | |
131 | ||
132 | void | |
133 | cupsdLoadBanners(const char *d) /* I - Directory to search */ | |
134 | { | |
135 | cups_dir_t *dir; /* Directory pointer */ | |
136 | cups_dentry_t *dent; /* Directory entry */ | |
137 | char filename[1024], /* Name of banner */ | |
138 | *ext; /* Pointer to extension */ | |
139 | ||
140 | ||
141 | /* | |
142 | * Free old banner info... | |
143 | */ | |
144 | ||
fa73b229 | 145 | cupsdFreeBanners(); |
ef416fc2 | 146 | |
147 | /* | |
148 | * Try opening the banner directory... | |
149 | */ | |
150 | ||
151 | if ((dir = cupsDirOpen(d)) == NULL) | |
152 | { | |
153 | cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdLoadBanners: Unable to open banner directory \"%s\": %s", | |
154 | d, strerror(errno)); | |
155 | return; | |
156 | } | |
157 | ||
158 | /* | |
159 | * Read entries, skipping directories and backup files. | |
160 | */ | |
161 | ||
fa73b229 | 162 | Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL); |
163 | ||
ef416fc2 | 164 | while ((dent = cupsDirRead(dir)) != NULL) |
165 | { | |
166 | /* | |
167 | * Check the file to make sure it isn't a directory or a backup | |
168 | * file of some sort... | |
169 | */ | |
170 | ||
171 | snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename); | |
172 | ||
173 | if (S_ISDIR(dent->fileinfo.st_mode)) | |
174 | continue; | |
175 | ||
bd7854cb | 176 | if (dent->filename[0] == '~' || |
177 | dent->filename[strlen(dent->filename) - 1] == '~') | |
ef416fc2 | 178 | continue; |
179 | ||
180 | if ((ext = strrchr(dent->filename, '.')) != NULL) | |
181 | if (!strcmp(ext, ".bck") || | |
182 | !strcmp(ext, ".bak") || | |
183 | !strcmp(ext, ".sav")) | |
184 | continue; | |
185 | ||
186 | /* | |
187 | * Must be a valid file; add it! | |
188 | */ | |
189 | ||
190 | cupsdAddBanner(dent->filename, filename); | |
191 | } | |
192 | ||
193 | /* | |
fa73b229 | 194 | * Close the directory... |
ef416fc2 | 195 | */ |
196 | ||
197 | cupsDirClose(dir); | |
ef416fc2 | 198 | } |
199 | ||
200 | ||
201 | /* | |
fa73b229 | 202 | * 'compare_banners()' - Compare two banners. |
ef416fc2 | 203 | */ |
204 | ||
205 | static int /* O - -1 if name0 < name1, etc. */ | |
fa73b229 | 206 | compare_banners( |
207 | const cupsd_banner_t *b0, /* I - First banner */ | |
208 | const cupsd_banner_t *b1) /* I - Second banner */ | |
ef416fc2 | 209 | { |
210 | return (strcasecmp(b0->name, b1->name)); | |
211 | } | |
212 | ||
213 | ||
214 | /* | |
bd7854cb | 215 | * End of "$Id: banners.c 5062 2006-02-03 16:36:24Z mike $". |
ef416fc2 | 216 | */ |