1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 27 unchanged lines hidden (view full) --- 36 * SUCH DAMAGE. 37 */ 38 39#ifndef lint 40/* 41static char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95"; 42*/ 43static const char rcsid[] = |
44 "$FreeBSD: head/usr.sbin/lpr/common_source/common.c 68401 2000-11-06 19:36:38Z gad $"; |
45#endif /* not lint */ 46 47#include <sys/param.h> 48#include <sys/stat.h> 49#include <sys/time.h> 50#include <sys/types.h> 51 52#include <dirent.h> --- 50 unchanged lines hidden (view full) --- 103/* 104 * Scan the current directory and make a list of daemon files sorted by 105 * creation time. 106 * Return the number of entries and a pointer to the list. 107 */ 108int 109getq(pp, namelist) 110 const struct printer *pp; |
111 struct jobqueue *(*namelist[]); |
112{ 113 register struct dirent *d; |
114 register struct jobqueue *q, **queue; |
115 register int nitems; 116 struct stat stbuf; 117 DIR *dirp; 118 int arraysz; 119 120 seteuid(euid); 121 if ((dirp = opendir(pp->spool_dir)) == NULL) 122 return(-1); 123 if (fstat(dirp->dd_fd, &stbuf) < 0) 124 goto errdone; 125 seteuid(uid); 126 127 /* 128 * Estimate the array size by taking the size of the directory file 129 * and dividing it by a multiple of the minimum size entry. 130 */ 131 arraysz = (stbuf.st_size / 24); |
132 queue = (struct jobqueue **)malloc(arraysz * sizeof(struct jobqueue *)); |
133 if (queue == NULL) 134 goto errdone; 135 136 nitems = 0; 137 while ((d = readdir(dirp)) != NULL) { 138 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 139 continue; /* daemon control files only */ 140 seteuid(euid); 141 if (stat(d->d_name, &stbuf) < 0) 142 continue; /* Doesn't exist */ 143 seteuid(uid); |
144 q = (struct jobqueue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); |
145 if (q == NULL) 146 goto errdone; |
147 q->job_time = stbuf.st_mtime; 148 strcpy(q->job_cfname, d->d_name); |
149 /* 150 * Check to make sure the array has space left and 151 * realloc the maximum size. 152 */ 153 if (++nitems > arraysz) { 154 arraysz *= 2; |
155 queue = (struct jobqueue **)realloc((char *)queue, 156 arraysz * sizeof(struct jobqueue *)); |
157 if (queue == NULL) 158 goto errdone; 159 } 160 queue[nitems-1] = q; 161 } 162 closedir(dirp); 163 if (nitems) |
164 qsort(queue, nitems, sizeof(struct jobqueue *), compar); |
165 *namelist = queue; 166 return(nitems); 167 168errdone: 169 closedir(dirp); 170 return(-1); 171} 172 173/* 174 * Compare modification times. 175 */ 176static int 177compar(p1, p2) 178 const void *p1, *p2; 179{ |
180 const struct jobqueue *qe1, *qe2; 181 qe1 = *(const struct jobqueue **)p1; 182 qe2 = *(const struct jobqueue **)p2; |
183 |
184 if (qe1->job_time < qe2->job_time) |
185 return (-1); |
186 if (qe1->job_time > qe2->job_time) |
187 return (1); 188 /* 189 * At this point, the two files have the same last-modification time. 190 * return a result based on filenames, so that 'cfA001some.host' will 191 * come before 'cfA002some.host'. Since the jobid ('001') will wrap 192 * around when it gets to '999', we also assume that '9xx' jobs are 193 * older than '0xx' jobs. 194 */ |
195 if ((qe1->job_cfname[3] == '9') && (qe2->job_cfname[3] == '0')) |
196 return (-1); |
197 if ((qe1->job_cfname[3] == '0') && (qe2->job_cfname[3] == '9')) |
198 return (1); |
199 return (strcmp(qe1->job_cfname, qe2->job_cfname)); |
200} 201 202/* sleep n milliseconds */ 203void 204delay(n) 205 int n; 206{ 207 struct timeval tdelay; --- 385 unchanged lines hidden --- |