/*
- * "$Id: banners.c,v 1.5.2.8 2004/06/29 13:15:10 mike Exp $"
+ * "$Id$"
*
- * Banner routines for the Common UNIX Printing System (CUPS).
+ * Banner routines for the CUPS scheduler.
*
- * Copyright 1997-2004 by Easy Software Products.
+ * Copyright 2007-2011 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636-3142 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
* Contents:
*
- * AddBanner() - Add a banner to the array.
- * FindBanner() - Find a named banner.
- * LoadBanners() - Load all available banner files...
- * compare() - Compare two banners.
+ * cupsdFindBanner() - Find a named banner.
+ * cupsdLoadBanners() - Load all available banner files...
+ * add_banner() - Add a banner to the array.
+ * compare_banners() - Compare two banners.
+ * free_banners() - Free all banners.
*/
/*
*/
#include "cupsd.h"
+#include <cups/dir.h>
/*
* Local functions...
*/
-static int compare(const banner_t *b0, const banner_t *b1);
+static void add_banner(const char *name, const char *filename);
+static int compare_banners(const cupsd_banner_t *b0,
+ const cupsd_banner_t *b1);
+static void free_banners(void);
/*
- * 'AddBanner()' - Add a banner to the array.
+ * 'cupsdFindBanner()' - Find a named banner.
+ */
+
+cupsd_banner_t * /* O - Pointer to banner or NULL */
+cupsdFindBanner(const char *name) /* I - Name of banner */
+{
+ cupsd_banner_t key; /* Search key */
+
+
+ key.name = (char *)name;
+
+ return ((cupsd_banner_t *)cupsArrayFind(Banners, &key));
+}
+
+
+/*
+ * 'cupsdLoadBanners()' - Load all available banner files...
*/
void
-AddBanner(const char *name, /* I - Name of banner */
- const char *filename) /* I - Filename for banner */
+cupsdLoadBanners(const char *d) /* I - Directory to search */
{
- mime_type_t *filetype; /* Filetype */
- banner_t *temp; /* New banner data */
+ cups_dir_t *dir; /* Directory pointer */
+ cups_dentry_t *dent; /* Directory entry */
+ char filename[1024], /* Name of banner */
+ *ext; /* Pointer to extension */
/*
- * See what the filetype is...
+ * Free old banner info...
*/
- if ((filetype = mimeFileType(MimeDatabase, filename, NULL)) == NULL)
- {
- LogMessage(L_WARN, "AddBanner: Banner \"%s\" is of an unknown file type - skipping!",
- name);
- return;
- }
+ free_banners();
/*
- * Allocate memory...
+ * Try opening the banner directory...
*/
- if (NumBanners == 0)
- temp = malloc(sizeof(banner_t));
- else
- temp = realloc(Banners, sizeof(banner_t) * (NumBanners + 1));
-
- if (temp == NULL)
+ if ((dir = cupsDirOpen(d)) == NULL)
{
- LogMessage(L_ERROR, "AddBanner: Ran out of memory adding a banner!");
+ cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdLoadBanners: Unable to open banner directory \"%s\": %s",
+ d, strerror(errno));
return;
}
/*
- * Copy the new banner data over...
+ * Read entries, skipping directories and backup files.
*/
- Banners = temp;
- temp += NumBanners;
- NumBanners ++;
+ Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL);
- memset(temp, 0, sizeof(banner_t));
- strlcpy(temp->name, name, sizeof(temp->name));
- temp->filetype = filetype;
-}
+ while ((dent = cupsDirRead(dir)) != NULL)
+ {
+ /*
+ * Check the file to make sure it isn't a directory or a backup
+ * file of some sort...
+ */
+ snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
-/*
- * 'FindBanner()' - Find a named banner.
- */
+ if (S_ISDIR(dent->fileinfo.st_mode))
+ continue;
-banner_t * /* O - Pointer to banner or NULL */
-FindBanner(const char *name) /* I - Name of banner */
-{
- banner_t key; /* Search key */
+ if (dent->filename[0] == '~' ||
+ dent->filename[strlen(dent->filename) - 1] == '~')
+ continue;
+
+ if ((ext = strrchr(dent->filename, '.')) != NULL)
+ if (!strcmp(ext, ".bck") ||
+ !strcmp(ext, ".bak") ||
+ !strcmp(ext, ".sav"))
+ continue;
+ /*
+ * Must be a valid file; add it!
+ */
+
+ add_banner(dent->filename, filename);
+ }
- strlcpy(key.name, name, sizeof(key.name));
+ /*
+ * Close the directory...
+ */
- return ((banner_t *)bsearch(&key, Banners, NumBanners, sizeof(banner_t),
- (int (*)(const void *, const void *))compare));
+ cupsDirClose(dir);
}
/*
- * 'LoadBanners()' - Load all available banner files...
+ * 'add_banner()' - Add a banner to the array.
*/
-void
-LoadBanners(const char *d) /* I - Directory to search */
+static void
+add_banner(const char *name, /* I - Name of banner */
+ const char *filename) /* I - Filename for banner */
{
- DIR *dir; /* Directory pointer */
- DIRENT *dent; /* Directory entry */
- char filename[1024], /* Name of banner */
- *ext; /* Pointer to extension */
- struct stat fileinfo; /* File information */
+ mime_type_t *filetype; /* Filetype */
+ cupsd_banner_t *temp; /* New banner data */
/*
- * Free old banner info...
+ * See what the filetype is...
*/
- if (NumBanners)
+ if ((filetype = mimeFileType(MimeDatabase, filename, NULL, NULL)) == NULL)
{
- free(Banners);
- NumBanners = 0;
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "add_banner: Banner \"%s\" (\"%s\") is of an unknown file "
+ "type - skipping!", name, filename);
+ return;
}
/*
- * Try opening the banner directory...
+ * Allocate memory...
*/
- if ((dir = opendir(d)) == NULL)
+ if ((temp = calloc(1, sizeof(cupsd_banner_t))) == NULL)
{
- LogMessage(L_ERROR, "LoadBanners: Unable to open banner directory \"%s\": %s",
- d, strerror(errno));
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "add_banner: Unable to allocate memory for banner \"%s\" - "
+ "skipping!", name);
return;
}
/*
- * Read entries, skipping directories and backup files.
+ * Copy the new banner data over...
*/
- while ((dent = readdir(dir)) != NULL)
+ if ((temp->name = strdup(name)) == NULL)
{
- /*
- * Check the file to make sure it isn't a directory or a backup
- * file of some sort...
- */
-
- snprintf(filename, sizeof(filename), "%s/%s", d, dent->d_name);
-
- if (stat(filename, &fileinfo))
- {
- LogMessage(L_WARN, "LoadBanners: Unable to stat \"%s\" banner: %s",
- dent->d_name, strerror(errno));
- continue;
- }
-
- if (S_ISDIR(fileinfo.st_mode))
- continue;
-
- if (dent->d_name[0] == '~')
- continue;
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "add_banner: Unable to allocate memory for banner \"%s\" - "
+ "skipping!", name);
+ free(temp);
+ return;
+ }
- if ((ext = strrchr(dent->d_name, '.')) != NULL)
- if (strcmp(ext, ".bck") == 0 ||
- strcmp(ext, ".bak") == 0 ||
- strcmp(ext, ".sav") == 0)
- continue;
+ temp->filetype = filetype;
- /*
- * Must be a valid file; add it!
- */
+ cupsArrayAdd(Banners, temp);
+}
- AddBanner(dent->d_name, filename);
- }
- /*
- * Close the directory and sort as needed...
- */
-
- closedir(dir);
+/*
+ * 'compare_banners()' - Compare two banners.
+ */
- if (NumBanners > 1)
- qsort(Banners, NumBanners, sizeof(banner_t),
- (int (*)(const void *, const void *))compare);
+static int /* O - -1 if name0 < name1, etc. */
+compare_banners(
+ const cupsd_banner_t *b0, /* I - First banner */
+ const cupsd_banner_t *b1) /* I - Second banner */
+{
+ return (_cups_strcasecmp(b0->name, b1->name));
}
/*
- * 'compare()' - Compare two banners.
+ * 'free_banners()' - Free all banners.
*/
-static int /* O - -1 if name0 < name1, etc. */
-compare(const banner_t *b0, /* I - First banner */
- const banner_t *b1) /* I - Second banner */
+static void
+free_banners(void)
{
- return (strcasecmp(b0->name, b1->name));
+ cupsd_banner_t *temp; /* Current banner */
+
+
+ for (temp = (cupsd_banner_t *)cupsArrayFirst(Banners);
+ temp;
+ temp = (cupsd_banner_t *)cupsArrayNext(Banners))
+ {
+ free(temp->name);
+ free(temp);
+ }
+
+ cupsArrayDelete(Banners);
+ Banners = NULL;
}
/*
- * End of "$Id: banners.c,v 1.5.2.8 2004/06/29 13:15:10 mike Exp $".
+ * End of "$Id$".
*/