2 * Banner routines for the CUPS scheduler.
4 * Copyright 2007-2011 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
22 static void add_banner(const char *name, const char *filename);
23 static int compare_banners(const cupsd_banner_t *b0,
24 const cupsd_banner_t *b1);
25 static void free_banners(void);
29 * 'cupsdFindBanner()' - Find a named banner.
32 cupsd_banner_t * /* O - Pointer to banner or NULL */
33 cupsdFindBanner(const char *name) /* I - Name of banner */
35 cupsd_banner_t key; /* Search key */
38 key.name = (char *)name;
40 return ((cupsd_banner_t *)cupsArrayFind(Banners, &key));
45 * 'cupsdLoadBanners()' - Load all available banner files...
49 cupsdLoadBanners(const char *d) /* I - Directory to search */
51 cups_dir_t *dir; /* Directory pointer */
52 cups_dentry_t *dent; /* Directory entry */
53 char filename[1024], /* Name of banner */
54 *ext; /* Pointer to extension */
58 * Free old banner info...
64 * Try opening the banner directory...
67 if ((dir = cupsDirOpen(d)) == NULL)
69 cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdLoadBanners: Unable to open banner directory \"%s\": %s",
75 * Read entries, skipping directories and backup files.
78 Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL);
80 while ((dent = cupsDirRead(dir)) != NULL)
83 * Check the file to make sure it isn't a directory or a backup
84 * file of some sort...
87 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
89 if (S_ISDIR(dent->fileinfo.st_mode))
92 if (dent->filename[0] == '~' ||
93 dent->filename[strlen(dent->filename) - 1] == '~')
96 if ((ext = strrchr(dent->filename, '.')) != NULL)
97 if (!strcmp(ext, ".bck") ||
98 !strcmp(ext, ".bak") ||
103 * Must be a valid file; add it!
106 add_banner(dent->filename, filename);
110 * Close the directory...
118 * 'add_banner()' - Add a banner to the array.
122 add_banner(const char *name, /* I - Name of banner */
123 const char *filename) /* I - Filename for banner */
125 mime_type_t *filetype; /* Filetype */
126 cupsd_banner_t *temp; /* New banner data */
130 * See what the filetype is...
133 if ((filetype = mimeFileType(MimeDatabase, filename, NULL, NULL)) == NULL)
135 cupsdLogMessage(CUPSD_LOG_WARN,
136 "add_banner: Banner \"%s\" (\"%s\") is of an unknown file "
137 "type - skipping!", name, filename);
145 if ((temp = calloc(1, sizeof(cupsd_banner_t))) == NULL)
147 cupsdLogMessage(CUPSD_LOG_WARN,
148 "add_banner: Unable to allocate memory for banner \"%s\" - "
154 * Copy the new banner data over...
157 if ((temp->name = strdup(name)) == NULL)
159 cupsdLogMessage(CUPSD_LOG_WARN,
160 "add_banner: Unable to allocate memory for banner \"%s\" - "
166 temp->filetype = filetype;
168 cupsArrayAdd(Banners, temp);
173 * 'compare_banners()' - Compare two banners.
176 static int /* O - -1 if name0 < name1, etc. */
178 const cupsd_banner_t *b0, /* I - First banner */
179 const cupsd_banner_t *b1) /* I - Second banner */
181 return (_cups_strcasecmp(b0->name, b1->name));
186 * 'free_banners()' - Free all banners.
192 cupsd_banner_t *temp; /* Current banner */
195 for (temp = (cupsd_banner_t *)cupsArrayFirst(Banners);
197 temp = (cupsd_banner_t *)cupsArrayNext(Banners))
203 cupsArrayDelete(Banners);