Instead of dealing with port@host in many places, do it where it is needed.
It reduces code and complexity and at the same time allows as to use symbolic ports instead of just numeric everywhere.
This commit is contained in:
parent
a8243f1c4c
commit
c681304808
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: common.c,v 1.32 2006/01/18 19:11:25 garbled Exp $ */
|
||||
/* $NetBSD: common.c,v 1.33 2006/01/20 17:30:00 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
|
@ -39,7 +39,7 @@
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: common.c,v 1.32 2006/01/18 19:11:25 garbled Exp $");
|
||||
__RCSID("$NetBSD: common.c,v 1.33 2006/01/20 17:30:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
|
@ -116,34 +116,47 @@ extern uid_t uid, euid;
|
|||
|
||||
static int compar(const void *, const void *);
|
||||
|
||||
const char *
|
||||
gethost(const char *hname)
|
||||
{
|
||||
const char *p = strchr(hname, '@');
|
||||
return p ? ++p : hname;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a TCP connection to host "rhost" at port "rport".
|
||||
* If rport == 0, then use the printer service port.
|
||||
* Most of this code comes from rcmd.c.
|
||||
* Create a TCP connection to host "rhost". If "rhost" is of the
|
||||
* form port@host, use the specified port. Otherwise use the
|
||||
* default printer port. Most of this code comes from rcmd.c.
|
||||
*/
|
||||
int
|
||||
getport(const char *rhost, int rport)
|
||||
getport(const char *rhost)
|
||||
{
|
||||
struct addrinfo hints, *res, *r;
|
||||
u_int timo = 1;
|
||||
int s, lport = IPPORT_RESERVED - 1;
|
||||
int error;
|
||||
int refuse, trial;
|
||||
char pbuf[NI_MAXSERV];
|
||||
char hbuf[NI_MAXSERV], *ptr;
|
||||
const char *port = "printer";
|
||||
const char *hostname = rhost;
|
||||
|
||||
/*
|
||||
* Get the host address and port number to connect to.
|
||||
*/
|
||||
if (rhost == NULL)
|
||||
fatal("no remote host to connect to");
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
(void)strlcpy(hbuf, rhost, sizeof(hbuf));
|
||||
for (ptr = hbuf; *ptr; ptr++)
|
||||
if (*ptr == '@') {
|
||||
*ptr++ = '\0';
|
||||
port = hbuf;
|
||||
hostname = ptr;
|
||||
break;
|
||||
}
|
||||
(void)memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = PF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
if (rport)
|
||||
snprintf(pbuf, sizeof(pbuf), "%d", rport);
|
||||
else
|
||||
snprintf(pbuf, sizeof(pbuf), "printer");
|
||||
error = getaddrinfo(rhost, pbuf, &hints, &res);
|
||||
error = getaddrinfo(hostname, port, &hints, &res);
|
||||
if (error)
|
||||
fatal("printer/tcp: %s", gai_strerror(error));
|
||||
|
||||
|
@ -308,7 +321,6 @@ const char *
|
|||
checkremote(void)
|
||||
{
|
||||
char lname[NI_MAXHOST], rname[NI_MAXHOST];
|
||||
const char *rmhost;
|
||||
struct addrinfo hints, *res, *res0;
|
||||
static char errbuf[128];
|
||||
int error;
|
||||
|
@ -338,11 +350,7 @@ checkremote(void)
|
|||
hints.ai_family = PF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
res = NULL;
|
||||
if ((rmhost = strchr(RM, '@')))
|
||||
rmhost++;
|
||||
else
|
||||
rmhost = RM;
|
||||
error = getaddrinfo(rmhost, NULL, &hints, &res0);
|
||||
error = getaddrinfo(gethost(RM), NULL, &hints, &res0);
|
||||
if (error) {
|
||||
(void)snprintf(errbuf, sizeof(errbuf),
|
||||
"unable to resolve remote machine %s: %s",
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: displayq.c,v 1.29 2006/01/04 15:32:50 garbled Exp $ */
|
||||
/* $NetBSD: displayq.c,v 1.30 2006/01/20 17:30:00 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
|
@ -34,7 +34,7 @@
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: displayq.c,v 1.29 2006/01/04 15:32:50 garbled Exp $");
|
||||
__RCSID("$NetBSD: displayq.c,v 1.30 2006/01/20 17:30:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
|
@ -95,7 +95,7 @@ displayq(int format)
|
|||
{
|
||||
struct queue *q;
|
||||
int i, nitems, fd, ret;
|
||||
char *cp, *ecp, *rmhost;
|
||||
char *cp, *ecp;
|
||||
struct queue **queue;
|
||||
struct stat statb;
|
||||
FILE *fp;
|
||||
|
@ -235,10 +235,7 @@ displayq(int format)
|
|||
(void)strlcpy(cp, user[i], ecp - cp);
|
||||
}
|
||||
(void)strlcat(line, "\n", sizeof(line));
|
||||
if ((rmhost = strchr(RM, '@')))
|
||||
fd = getport(rmhost+1, atoi(RM));
|
||||
else
|
||||
fd = getport(RM, 0);
|
||||
fd = getport(RM);
|
||||
if (fd < 0) {
|
||||
if (from != host)
|
||||
printf("%s: ", host);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: lp.h,v 1.20 2006/01/17 19:11:12 garbled Exp $ */
|
||||
/* $NetBSD: lp.h,v 1.21 2006/01/20 17:30:00 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
|
@ -108,7 +108,8 @@ void dump(const char *, const char *, int);
|
|||
void fatal(const char *, ...)
|
||||
__attribute__((__format__(__printf__, 1, 2)));
|
||||
int getline(FILE *);
|
||||
int getport(const char *, int);
|
||||
const char *gethost(const char *);
|
||||
int getport(const char *);
|
||||
int getq(struct queue *(*[]));
|
||||
void header(void);
|
||||
void inform(const char *);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: rmjob.c,v 1.22 2006/01/04 15:32:50 garbled Exp $ */
|
||||
/* $NetBSD: rmjob.c,v 1.23 2006/01/20 17:30:00 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
|
@ -34,7 +34,7 @@
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)rmjob.c 8.2 (Berkeley) 4/28/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: rmjob.c,v 1.22 2006/01/04 15:32:50 garbled Exp $");
|
||||
__RCSID("$NetBSD: rmjob.c,v 1.23 2006/01/20 17:30:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
|
@ -297,7 +297,7 @@ isowner(const char *owner, const char *file)
|
|||
void
|
||||
rmremote(void)
|
||||
{
|
||||
char *cp, *s, *rmhost;
|
||||
char *cp, *s;
|
||||
int i, rem;
|
||||
size_t len;
|
||||
|
||||
|
@ -339,10 +339,7 @@ rmremote(void)
|
|||
cp[0] = '\n';
|
||||
cp[1] = '\0';
|
||||
|
||||
if ((rmhost = strchr(RM, '@')))
|
||||
rem = getport(rmhost+1, atoi(RM));
|
||||
else
|
||||
rem = getport(RM, 0);
|
||||
rem = getport(RM);
|
||||
if (rem < 0) {
|
||||
if (from != host)
|
||||
printf("%s: ", host);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: printjob.c,v 1.47 2006/01/19 19:17:59 garbled Exp $ */
|
||||
/* $NetBSD: printjob.c,v 1.48 2006/01/20 17:30:00 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
|
@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: printjob.c,v 1.47 2006/01/19 19:17:59 garbled Exp $");
|
||||
__RCSID("$NetBSD: printjob.c,v 1.48 2006/01/20 17:30:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
|
@ -125,7 +125,7 @@ static void init(void);
|
|||
static void setup_ofilter(int);
|
||||
static void close_ofilter(void);
|
||||
static void openpr(void);
|
||||
static void opennet(char *);
|
||||
static void opennet(void);
|
||||
static void opentty(void);
|
||||
static void openrem(void);
|
||||
static int print(int, char *);
|
||||
|
@ -1359,11 +1359,9 @@ close_ofilter(void)
|
|||
static void
|
||||
openpr(void)
|
||||
{
|
||||
char *cp;
|
||||
|
||||
if (!remote && *LP) {
|
||||
if ((cp = strchr(LP, '@')))
|
||||
opennet(cp);
|
||||
if (strchr(LP, '@') != NULL)
|
||||
opennet();
|
||||
else
|
||||
opentty();
|
||||
} else if (remote) {
|
||||
|
@ -1385,24 +1383,14 @@ openpr(void)
|
|||
* or to a terminal server on the net
|
||||
*/
|
||||
static void
|
||||
opennet(char *cp)
|
||||
opennet(void)
|
||||
{
|
||||
int i;
|
||||
int resp, port;
|
||||
char save_ch;
|
||||
|
||||
save_ch = *cp;
|
||||
*cp = '\0';
|
||||
port = atoi(LP);
|
||||
if (port <= 0) {
|
||||
syslog(LOG_ERR, "%s: bad port number: %s", printer, LP);
|
||||
exit(1);
|
||||
}
|
||||
*cp++ = save_ch;
|
||||
int resp;
|
||||
|
||||
for (i = 1; ; i = i < 256 ? i << 1 : i) {
|
||||
resp = -1;
|
||||
pfd = getport(cp, port);
|
||||
pfd = getport(LP);
|
||||
if (pfd < 0 && errno == ECONNREFUSED)
|
||||
resp = 1;
|
||||
else if (pfd >= 0) {
|
||||
|
@ -1422,7 +1410,7 @@ opennet(char *cp)
|
|||
}
|
||||
sleep(i);
|
||||
}
|
||||
pstatus("sending to %s port %d", cp, port);
|
||||
pstatus("sending to %s", LP);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1461,14 +1449,10 @@ openrem(void)
|
|||
{
|
||||
int i, n;
|
||||
int resp;
|
||||
char *rmhost;
|
||||
|
||||
for (i = 1; ; i = i < 256 ? i << 1 : i) {
|
||||
resp = -1;
|
||||
if ((rmhost = strchr(RM, '@')))
|
||||
pfd = getport(rmhost+1, atoi(RM));
|
||||
else
|
||||
pfd = getport(RM, 0);
|
||||
pfd = getport(RM);
|
||||
if (pfd >= 0) {
|
||||
n = snprintf(line, sizeof(line), "\2%s\n", RP);
|
||||
if (write(pfd, line, n) == n &&
|
||||
|
|
Loading…
Reference in New Issue