2 * "$Id: quotas.c 4729 2005-09-30 17:46:19Z mike $"
4 * Quota routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddQuota() - Add a quota record for this printer and user.
27 * cupsdFindQuota() - Find a quota record.
28 * cupsdFreeQuotas() - Free quotas for a printer.
29 * cupsdUpdateQuota() - Update quota data for the specified printer and user.
30 * compare() - Compare two quota records...
34 * Include necessary headers...
44 static int compare(const cupsd_quota_t *q1, const cupsd_quota_t *q2);
48 * 'cupsdAddQuota()' - Add a quota record for this printer and user.
51 cupsd_quota_t * /* O - Quota data */
52 cupsdAddQuota(cupsd_printer_t *p, /* I - Printer */
53 const char *username)/* I - User */
55 cupsd_quota_t *q; /* New quota data */
61 if (p->num_quotas == 0)
62 q = malloc(sizeof(cupsd_quota_t));
64 q = realloc(p->quotas, sizeof(cupsd_quota_t) * (p->num_quotas + 1));
73 memset(q, 0, sizeof(cupsd_quota_t));
74 strlcpy(q->username, username, sizeof(q->username));
76 if (p->num_quotas > 1)
77 qsort(p->quotas, p->num_quotas, sizeof(cupsd_quota_t),
78 (int (*)(const void *, const void *))compare);
80 return (cupsdFindQuota(p, username));
85 * 'cupsdFindQuota()' - Find a quota record.
88 cupsd_quota_t * /* O - Quota data */
90 cupsd_printer_t *p, /* I - Printer */
91 const char *username) /* I - User */
93 cupsd_quota_t *q, /* Quota data pointer */
94 match; /* Search data */
100 if (p->num_quotas == 0)
104 strlcpy(match.username, username, sizeof(match.username));
106 q = bsearch(&match, p->quotas, p->num_quotas, sizeof(cupsd_quota_t),
107 (int(*)(const void *, const void *))compare);
113 return (cupsdAddQuota(p, username));
118 * 'cupsdFreeQuotas()' - Free quotas for a printer.
122 cupsdFreeQuotas(cupsd_printer_t *p) /* I - Printer */
136 * 'cupsdUpdateQuota()' - Update quota data for the specified printer and user.
139 cupsd_quota_t * /* O - Quota data */
141 cupsd_printer_t *p, /* I - Printer */
142 const char *username, /* I - User */
143 int pages, /* I - Number of pages */
144 int k) /* I - Number of kilobytes */
146 cupsd_quota_t *q; /* Quota data */
147 cupsd_job_t *job; /* Current job */
148 time_t curtime; /* Current time */
149 ipp_attribute_t *attr; /* Job attribute */
155 if (!p->k_limit && !p->page_limit)
158 if ((q = cupsdFindQuota(p, username)) == NULL)
161 cupsdLogMessage(CUPSD_LOG_DEBUG,
162 "cupsdUpdateQuota: p=%s username=%s pages=%d k=%d",
163 p->name, username, pages, k);
165 curtime = time(NULL);
167 if (curtime < q->next_update)
169 q->page_count += pages;
176 curtime -= p->quota_period;
184 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
186 job = (cupsd_job_t *)cupsArrayNext(Jobs))
188 if (strcasecmp(job->dest, p->name) != 0 ||
189 strcasecmp(job->username, q->username) != 0)
192 if ((attr = ippFindAttribute(job->attrs, "time-at-completion",
193 IPP_TAG_INTEGER)) == NULL)
194 if ((attr = ippFindAttribute(job->attrs, "time-at-processing",
195 IPP_TAG_INTEGER)) == NULL)
196 attr = ippFindAttribute(job->attrs, "time-at-creation",
202 if (attr->values[0].integer < curtime)
205 cupsdCancelJob(job, 1);
210 if (q->next_update == 0)
211 q->next_update = attr->values[0].integer + p->quota_period;
213 if ((attr = ippFindAttribute(job->attrs, "job-media-sheets-completed",
214 IPP_TAG_INTEGER)) != NULL)
215 q->page_count += attr->values[0].integer;
217 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
218 IPP_TAG_INTEGER)) != NULL)
219 q->k_count += attr->values[0].integer;
227 * 'compare()' - Compare two quota records...
230 static int /* O - Result of comparison */
231 compare(const cupsd_quota_t *q1, /* I - First quota record */
232 const cupsd_quota_t *q2) /* I - Second quota record */
234 return (strcasecmp(q1->username, q2->username));
239 * End of "$Id: quotas.c 4729 2005-09-30 17:46:19Z mike $".