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