SCCS-vsn: usr.sbin/lpr/common_source/common.c 4.5
-/* common.c 4.4 83/05/27 */
+/* common.c 4.5 83/06/02 */
/*
* Routines and data common to all the line printer functions.
*/
/*
* Routines and data common to all the line printer functions.
*/
struct servent *sp;
struct sockaddr_in sin;
int s, timo = 1, lport = IPPORT_RESERVED - 1;
struct servent *sp;
struct sockaddr_in sin;
int s, timo = 1, lport = IPPORT_RESERVED - 1;
/*
* Get the host address and port number to connect to.
/*
* Get the host address and port number to connect to.
if (s < 0)
return(-1);
if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
if (s < 0)
return(-1);
if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
+ err = errno;
+ (void) close(s);
+ errno = err;
if (errno == EADDRINUSE) {
if (errno == EADDRINUSE) {
lport--;
goto retry;
}
if (errno == ECONNREFUSED && timo <= 16) {
lport--;
goto retry;
}
if (errno == ECONNREFUSED && timo <= 16) {
sleep(timo);
timo *= 2;
goto retry;
sleep(timo);
timo *= 2;
goto retry;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
return(-1);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
return(-1);
+ for (; *alport > IPPORT_RESERVED/2; (*alport)--) {
sin.sin_port = htons((u_short) *alport);
if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0)
return(s);
if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
sin.sin_port = htons((u_short) *alport);
if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0)
return(s);
if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
- return(-1);
- (*alport)--;
- if (*alport == IPPORT_RESERVED/2)
- return(-1);
+ (void) close(s);
+ return(-1);
if ((dirp = opendir(".")) == NULL)
return(-1);
if (fstat(dirp->dd_fd, &stbuf) < 0)
if ((dirp = opendir(".")) == NULL)
return(-1);
if (fstat(dirp->dd_fd, &stbuf) < 0)
/*
* Estimate the array size by taking the size of the directory file
/*
* Estimate the array size by taking the size of the directory file
arraysz = (stbuf.st_size / 24);
queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
if (queue == NULL)
arraysz = (stbuf.st_size / 24);
queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
if (queue == NULL)
nitems = 0;
while ((d = readdir(dirp)) != NULL) {
nitems = 0;
while ((d = readdir(dirp)) != NULL) {
continue; /* Doesn't exist */
q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
if (q == NULL)
continue; /* Doesn't exist */
q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
if (q == NULL)
q->q_time = stbuf.st_mtime;
strcpy(q->q_name, d->d_name);
/*
q->q_time = stbuf.st_mtime;
strcpy(q->q_name, d->d_name);
/*
queue = (struct queue **)realloc((char *)queue,
(stbuf.st_size/12) * sizeof(struct queue *));
if (queue == NULL)
queue = (struct queue **)realloc((char *)queue,
(stbuf.st_size/12) * sizeof(struct queue *));
if (queue == NULL)
qsort(queue, nitems, sizeof(struct queue *), compar);
*namelist = queue;
return(nitems);
qsort(queue, nitems, sizeof(struct queue *), compar);
*namelist = queue;
return(nitems);
+
+errdone:
+ closedir(dirp);
+ return(-1);