File: banners.c

package info (click to toggle)
cups 1.4.4-7+squeeze10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 25,548 kB
  • ctags: 9,583
  • sloc: ansic: 138,214; sh: 37,926; cpp: 25,469; makefile: 3,285; perl: 190; python: 127; php: 28
file content (224 lines) | stat: -rw-r--r-- 4,808 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * "$Id: banners.c 7221 2008-01-16 22:20:08Z mike $"
 *
 *   Banner routines for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 2007-2008 by Apple Inc.
 *   Copyright 1997-2006 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   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:
 *
 *   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 necessary headers...
 */

#include "cupsd.h"
#include <cups/dir.h>


/*
 * Local functions...
 */

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);


/*
 * '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
cupsdLoadBanners(const char *d)		/* I - Directory to search */
{
  cups_dir_t	*dir;			/* Directory pointer */
  cups_dentry_t	*dent;			/* Directory entry */
  char		filename[1024],		/* Name of banner */
		*ext;			/* Pointer to extension */


 /*
  * Free old banner info...
  */

  free_banners();

 /*
  * Try opening the banner directory...
  */

  if ((dir = cupsDirOpen(d)) == NULL)
  {
    cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdLoadBanners: Unable to open banner directory \"%s\": %s",
               d, strerror(errno));
    return;
  }

 /*
  * Read entries, skipping directories and backup files.
  */

  Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL);

  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);

    if (S_ISDIR(dent->fileinfo.st_mode))
      continue;

    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);
  }

 /*
  * Close the directory...
  */

  cupsDirClose(dir);
}


/*
 * 'add_banner()' - Add a banner to the array.
 */

static void
add_banner(const char *name,		/* I - Name of banner */
           const char *filename)	/* I - Filename for banner */
{
  mime_type_t		*filetype;	/* Filetype */
  cupsd_banner_t	*temp;		/* New banner data */


 /*
  * See what the filetype is...
  */

  if ((filetype = mimeFileType(MimeDatabase, filename, NULL, NULL)) == NULL)
  {
    cupsdLogMessage(CUPSD_LOG_WARN,
                    "add_banner: Banner \"%s\" (\"%s\") is of an unknown file "
		    "type - skipping!", name, filename);
    return;
  }

 /*
  * Allocate memory...
  */

  if ((temp = calloc(1, sizeof(cupsd_banner_t))) == NULL)
  {
    cupsdLogMessage(CUPSD_LOG_WARN,
                    "add_banner: Unable to allocate memory for banner \"%s\" - "
		    "skipping!", name);
    return;
  }

 /*
  * Copy the new banner data over...
  */

  if ((temp->name = strdup(name)) == NULL)
  {
    cupsdLogMessage(CUPSD_LOG_WARN,
                    "add_banner: Unable to allocate memory for banner \"%s\" - "
		    "skipping!", name);
    free(temp);
    return;
  }

  temp->filetype = filetype;

  cupsArrayAdd(Banners, temp);
}


/*
 * 'compare_banners()' - Compare two banners.
 */

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 (strcasecmp(b0->name, b1->name));
}


/*
 * 'free_banners()' - Free all banners.
 */

static void
free_banners(void)
{
  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 7221 2008-01-16 22:20:08Z mike $".
 */