]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/banners.c
Cleanup.
[thirdparty/cups.git] / scheduler / banners.c
CommitLineData
02d91449 1/*
c9d3f842 2 * "$Id$"
02d91449 3 *
4 * Banner routines for the Common UNIX Printing System (CUPS).
5 *
c9d3f842 6 * Copyright 1997-2005 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
8650fcf2 18 * Hollywood, Maryland 20636 USA
02d91449 19 *
edfd3c3d 20 * Voice: (301) 373-9600
02d91449 21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
589eb420 26 * cupsdAddBanner() - Add a banner to the array.
27 * cupsdFindBanner() - Find a named banner.
28 * cupsdLoadBanners() - Load all available banner files...
f3e786fc 29 * compare() - Compare two banners.
02d91449 30 */
31
32/*
33 * Include necessary headers...
34 */
35
36#include "cupsd.h"
08379093 37#include <cups/dir.h>
02d91449 38
39
40/*
41 * Local functions...
42 */
43
589eb420 44static int compare(const cupsd_banner_t *b0, const cupsd_banner_t *b1);
02d91449 45
46
47/*
589eb420 48 * 'cupsdAddBanner()' - Add a banner to the array.
02d91449 49 */
50
51void
589eb420 52cupsdAddBanner(const char *name, /* I - Name of banner */
f3e786fc 53 const char *filename) /* I - Filename for banner */
02d91449 54{
f3e786fc 55 mime_type_t *filetype; /* Filetype */
589eb420 56 cupsd_banner_t *temp; /* New banner data */
02d91449 57
58
59 /*
60 * See what the filetype is...
61 */
62
d59a189c 63 if ((filetype = mimeFileType(MimeDatabase, filename, NULL)) == NULL)
02d91449 64 {
f3e786fc 65 cupsdLogMessage(CUPSD_LOG_WARN,
66 "cupsdAddBanner: Banner \"%s\" (\"%s\") is of an unknown file type - skipping!",
67 name, filename);
02d91449 68 return;
69 }
70
71 /*
72 * Allocate memory...
73 */
74
75 if (NumBanners == 0)
589eb420 76 temp = malloc(sizeof(cupsd_banner_t));
02d91449 77 else
589eb420 78 temp = realloc(Banners, sizeof(cupsd_banner_t) * (NumBanners + 1));
02d91449 79
80 if (temp == NULL)
81 {
f3e786fc 82 cupsdLogMessage(CUPSD_LOG_ERROR,
83 "cupsdAddBanner: Ran out of memory adding a banner!");
02d91449 84 return;
85 }
86
87 /*
88 * Copy the new banner data over...
89 */
90
91 Banners = temp;
92 temp += NumBanners;
93 NumBanners ++;
94
589eb420 95 memset(temp, 0, sizeof(cupsd_banner_t));
def978d5 96 strlcpy(temp->name, name, sizeof(temp->name));
02d91449 97 temp->filetype = filetype;
98}
99
100
101/*
589eb420 102 * 'cupsdFindBanner()' - Find a named banner.
02d91449 103 */
104
589eb420 105cupsd_banner_t * /* O - Pointer to banner or NULL */
106cupsdFindBanner(const char *name) /* I - Name of banner */
02d91449 107{
589eb420 108 cupsd_banner_t key; /* Search key */
02d91449 109
110
def978d5 111 strlcpy(key.name, name, sizeof(key.name));
02d91449 112
589eb420 113 return ((cupsd_banner_t *)bsearch(&key, Banners, NumBanners, sizeof(cupsd_banner_t),
02d91449 114 (int (*)(const void *, const void *))compare));
115}
116
117
118/*
589eb420 119 * 'cupsdLoadBanners()' - Load all available banner files...
02d91449 120 */
121
122void
589eb420 123cupsdLoadBanners(const char *d) /* I - Directory to search */
02d91449 124{
08379093 125 cups_dir_t *dir; /* Directory pointer */
126 cups_dentry_t *dent; /* Directory entry */
127 char filename[1024], /* Name of banner */
128 *ext; /* Pointer to extension */
02d91449 129
130
131 /*
132 * Free old banner info...
133 */
134
135 if (NumBanners)
136 {
137 free(Banners);
138 NumBanners = 0;
139 }
140
141 /*
142 * Try opening the banner directory...
143 */
144
08379093 145 if ((dir = cupsDirOpen(d)) == NULL)
02d91449 146 {
f3e786fc 147 cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdLoadBanners: Unable to open banner directory \"%s\": %s",
02d91449 148 d, strerror(errno));
149 return;
150 }
151
152 /*
153 * Read entries, skipping directories and backup files.
154 */
155
08379093 156 while ((dent = cupsDirRead(dir)) != NULL)
02d91449 157 {
158 /*
159 * Check the file to make sure it isn't a directory or a backup
160 * file of some sort...
161 */
162
08379093 163 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
02d91449 164
08379093 165 if (S_ISDIR(dent->fileinfo.st_mode))
02d91449 166 continue;
02d91449 167
08379093 168 if (dent->filename[0] == '~')
02d91449 169 continue;
170
08379093 171 if ((ext = strrchr(dent->filename, '.')) != NULL)
172 if (!strcmp(ext, ".bck") ||
173 !strcmp(ext, ".bak") ||
174 !strcmp(ext, ".sav"))
02d91449 175 continue;
176
177 /*
178 * Must be a valid file; add it!
179 */
180
589eb420 181 cupsdAddBanner(dent->filename, filename);
02d91449 182 }
183
184 /*
185 * Close the directory and sort as needed...
186 */
187
08379093 188 cupsDirClose(dir);
02d91449 189
190 if (NumBanners > 1)
589eb420 191 qsort(Banners, NumBanners, sizeof(cupsd_banner_t),
02d91449 192 (int (*)(const void *, const void *))compare);
193}
194
195
196/*
197 * 'compare()' - Compare two banners.
198 */
199
f3e786fc 200static int /* O - -1 if name0 < name1, etc. */
589eb420 201compare(const cupsd_banner_t *b0, /* I - First banner */
202 const cupsd_banner_t *b1) /* I - Second banner */
02d91449 203{
204 return (strcasecmp(b0->name, b1->name));
205}
206
207
02d91449 208/*
c9d3f842 209 * End of "$Id$".
02d91449 210 */