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