From: tb Date: Sun, 18 Dec 2022 12:48:28 +0000 (+0000) Subject: nc: clean up and simplify connection_infO() X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=65e1b8d27dca941f51931d87bcd0d64380ff13eb;p=openbsd nc: clean up and simplify connection_infO() ok mpf as part of a larger diff --- diff --git a/usr.bin/nc/netcat.c b/usr.bin/nc/netcat.c index c013d2328a5..e945ba6e8f0 100644 --- a/usr.bin/nc/netcat.c +++ b/usr.bin/nc/netcat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: netcat.c,v 1.221 2022/12/18 12:47:31 tb Exp $ */ +/* $OpenBSD: netcat.c,v 1.222 2022/12/18 12:48:28 tb Exp $ */ /* * Copyright (c) 2001 Eric Jackson * Copyright (c) 2015 Bob Beck. All rights reserved. @@ -1524,12 +1524,13 @@ connection_info(const char *host, const char *port, const char *proto, const char *ipaddr) { struct servent *sv; + char *service = "*"; - /* Don't look up port if -n. */ - if (nflag) - sv = NULL; - else { + /* Look up service name unless -n. */ + if (!nflag) { sv = getservbyport(ntohs(atoi(port)), proto); + if (sv != NULL) + service = sv->s_name; } fprintf(stderr, "Connection to %s", host); @@ -1541,8 +1542,7 @@ connection_info(const char *host, const char *port, const char *proto, if (!nflag && !xflag && strcmp(host, ipaddr) != 0) fprintf(stderr, " (%s)", ipaddr); - fprintf(stderr, " %s port [%s/%s] succeeded!\n", port, proto, - sv ? sv->s_name : "*"); + fprintf(stderr, " %s port [%s/%s] succeeded!\n", port, proto, service); } void