yeah, this is arse backwards, I'm willing to commit the oposite though, i.e. get rid of the void casts for printf
On Tue, Jun 13, 2017 at 08:12:47PM +0200, Klemens Nanni wrote: > printf's return value is ignored allmost all the times so do the same > for the rest of them as well for consistency. > > Subsequent calls got merged where I think is appropiate and readability > improves. > > The usage format string now looks like it's actual put. > > Index: ping.c > =================================================================== > RCS file: /cvs/src/sbin/ping/ping.c,v > retrieving revision 1.218 > diff -u -p -r1.218 ping.c > --- ping.c 22 Feb 2017 13:43:35 -0000 1.218 > +++ ping.c 13 Jun 2017 18:08:42 -0000 > @@ -756,10 +756,10 @@ main(int argc, char *argv[]) > arc4random_buf(&tv64_offset, sizeof(tv64_offset)); > arc4random_buf(&mac_key, sizeof(mac_key)); > > - printf("PING %s (", hostname); > + (void)printf("PING %s (", hostname); > if (options & F_VERBOSE) > - printf("%s --> ", pr_addr(from, from->sa_len)); > - printf("%s): %d data bytes\n", pr_addr(dst, dst->sa_len), datalen); > + (void)printf("%s --> ", pr_addr(from, from->sa_len)); > + (void)printf("%s): %d data bytes\n", pr_addr(dst, dst->sa_len), > datalen); > > smsghdr.msg_name = dst; > smsghdr.msg_namelen = dst->sa_len; > @@ -862,7 +862,7 @@ main(int argc, char *argv[]) > */ > if ((mtu = get_pathmtu(&m, &dst6)) > 0) { > if ((options & F_VERBOSE) != 0) { > - printf("new path MTU (%d) is " > + (void)printf("new path MTU (%d) is " > "notified\n", mtu); > } > } > @@ -926,28 +926,28 @@ fill(char *bp, char *patp) > void > summary(void) > { > - printf("\n--- %s ping statistics ---\n", hostname); > - printf("%lld packets transmitted, ", ntransmitted); > - printf("%lld packets received, ", nreceived); > + (void)printf("\n--- %s ping statistics ---\n" > + "%lld packets transmitted, %lld packets received, ", > + hostname, ntransmitted, nreceived); > > if (nrepeats) > - printf("%lld duplicates, ", nrepeats); > + (void)printf("%lld duplicates, ", nrepeats); > if (ntransmitted) { > if (nreceived > ntransmitted) > - printf("-- somebody's duplicating packets!"); > + (void)printf("-- somebody's duplicating packets!"); > else > - printf("%.1f%% packet loss", > - ((((double)ntransmitted - nreceived) * 100) / > - ntransmitted)); > + (void)printf("%.1f%% packet loss", > + (double)(ntransmitted - nreceived) > + * 100 / ntransmitted); > } > - printf("\n"); > + (void)printf("\n"); > if (timinginfo) { > /* Only display average to microseconds */ > double num = nreceived + nrepeats; > double avg = tsum / num; > double dev = sqrt(fmax(0, tsumsq / num - avg * avg)); > - printf("round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f > ms\n", > - tmin, avg, tmax, dev); > + (void)printf("round-trip min/avg/max/std-dev = " > + "%.3f/%.3f/%.3f/%.3f ms\n", tmin, avg, tmax, dev); > } > } > > @@ -1092,7 +1092,7 @@ pinger(int s) > if (i < 0 || i != cc) { > if (i < 0) > warn("sendmsg"); > - printf("ping: wrote %s %d chars, ret=%d\n", hostname, cc, i); > + (void)printf("ping: wrote %s %d chars, ret=%d\n", hostname, cc, > i); > } > if (!(options & F_QUIET) && options & F_FLOOD) > (void)write(STDOUT_FILENO, &DOT, 1); > @@ -1278,11 +1278,9 @@ pr_pack(u_char *buf, int cc, struct msgh > cp = (u_char *) > &icp->icmp_data[0]; > for (i = ECHOLEN; i < cc && i < datalen; > - ++i, ++cp) { > - if ((i % 32) == 8) > - (void)printf("\n\t"); > - (void)printf("%x ", *cp); > - } > + ++i, ++cp) > + (void)printf(i % 32 == 8 ? > + "\n\t%x " : "%x ", *cp); > break; > } > } > @@ -1560,8 +1558,7 @@ pr_icmph(struct icmp *icp) > (void)printf("Redirect, Unknown Code: %d", > icp->icmp_code); > break; > } > - (void)printf("(New addr: %s)\n", > - inet_ntoa(icp->icmp_gwaddr)); > + (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); > pr_retip((struct ip *)icp->icmp_data); > break; > case ICMP_ECHO: > @@ -1570,8 +1567,8 @@ pr_icmph(struct icmp *icp) > break; > case ICMP_ROUTERADVERT: > /* RFC1256 */ > - (void)printf("Router Discovery Advertisement\n"); > - (void)printf("(%d entries, lifetime %d seconds)\n", > + (void)printf("Router Discovery Advertisement\n" > + "(%d entries, lifetime %d seconds)\n", > icp->icmp_num_addrs, ntohs(icp->icmp_lifetime)); > break; > case ICMP_ROUTERSOLICIT: > @@ -1648,14 +1645,13 @@ pr_iph(struct ip *ip) > hlen = ip->ip_hl << 2; > cp = (u_char *)ip + 20; /* point to options */ > > - (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src > Dst Data\n"); > - (void)printf(" %1x %1x %02x %04x %04x", > - ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id); > - (void)printf(" %1x %04x", ((ip->ip_off) & 0xe000) >> 13, > - (ip->ip_off) & 0x1fff); > - (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, ip->ip_sum); > - (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); > - (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); > + (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src > Dst Data\n" > + " %1x %1x %02x %04x %04x %1x %04x %02x %02x %04x %s %s ", > + ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id, > + ((ip->ip_off) & 0xe000) >> 13, (ip->ip_off) & 0x1fff, > + ip->ip_ttl, ip->ip_p, ip->ip_sum, > + inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr), > + inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); > /* dump and option bytes */ > while (hlen-- > 20) { > (void)printf("%02x", *cp++); > @@ -1747,16 +1743,16 @@ pr_exthdrs(struct msghdr *mhdr) > > switch (cm->cmsg_type) { > case IPV6_HOPOPTS: > - printf(" HbH Options: "); > + (void)printf(" HbH Options: "); > pr_ip6opt(CMSG_DATA(cm)); > break; > case IPV6_DSTOPTS: > case IPV6_RTHDRDSTOPTS: > - printf(" Dst Options: "); > + (void)printf(" Dst Options: "); > pr_ip6opt(CMSG_DATA(cm)); > break; > case IPV6_RTHDR: > - printf(" Routing: "); > + (void)printf(" Routing: "); > pr_rthdr(CMSG_DATA(cm)); > break; > } > @@ -1777,7 +1773,7 @@ pr_ip6opt(void *extbuf) > > ext = (struct ip6_hbh *)extbuf; > extlen = (ext->ip6h_len + 1) * 8; > - printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt, > + (void)printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt, > (unsigned int)ext->ip6h_len, (unsigned long)extlen); > > currentlen = 0; > @@ -1793,16 +1789,16 @@ pr_ip6opt(void *extbuf) > */ > case IP6OPT_JUMBO: > inet6_opt_get_val(databuf, 0, &value4, sizeof(value4)); > - printf(" Jumbo Payload Opt: Length %u\n", > + (void)printf(" Jumbo Payload Opt: Length %u\n", > (u_int32_t)ntohl(value4)); > break; > case IP6OPT_ROUTER_ALERT: > inet6_opt_get_val(databuf, 0, &value2, sizeof(value2)); > - printf(" Router Alert Opt: Type %u\n", > + (void)printf(" Router Alert Opt: Type %u\n", > ntohs(value2)); > break; > default: > - printf(" Received Opt %u len %lu\n", > + (void)printf(" Received Opt %u len %lu\n", > type, (unsigned long)len); > break; > } > @@ -1819,23 +1815,23 @@ pr_rthdr(void *extbuf) > int i, segments; > > /* print fixed part of the header */ > - printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt, > + (void)printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt, > rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type); > if ((segments = inet6_rth_segments(extbuf)) >= 0) > - printf("%d segments, ", segments); > + (void)printf("%d segments, ", segments); > else > - printf("segments unknown, "); > - printf("%d left\n", rh->ip6r_segleft); > + (void)printf("segments unknown, "); > + (void)printf("%d left\n", rh->ip6r_segleft); > > for (i = 0; i < segments; i++) { > in6 = inet6_rth_getaddr(extbuf, i); > if (in6 == NULL) > - printf(" [%d]<NULL>\n", i); > + (void)printf(" [%d]<NULL>\n", i); > else { > if (!inet_ntop(AF_INET6, in6, ntopbuf, > sizeof(ntopbuf))) > strncpy(ntopbuf, "?", sizeof(ntopbuf)); > - printf(" [%d]%s\n", i, ntopbuf); > + (void)printf(" [%d]%s\n", i, ntopbuf); > } > } > > @@ -1893,8 +1889,8 @@ get_pathmtu(struct msghdr *mhdr, struct > > mtuctl->ip6m_addr.sin6_scope_id != > dst->sin6_scope_id)) { > if ((options & F_VERBOSE) != 0) { > - printf("path MTU for %s is notified. " > - "(ignored)\n", > + (void)printf("path MTU for %s is " > + "notified. (ignored)\n", > pr_addr((struct sockaddr *) > &mtuctl->ip6m_addr, > sizeof(mtuctl->ip6m_addr))); > @@ -2054,16 +2050,16 @@ pr_iph6(struct ip6_hdr *ip6) > tc = (tc >> 4) & 0x0f; > tc |= (ip6->ip6_vfc << 4); > > - printf("Vr TC Flow Plen Nxt Hlim\n"); > - printf(" %1x %02x %05x %04x %02x %02x\n", > + (void)printf("Vr TC Flow Plen Nxt Hlim\n" > + " %1x %02x %05x %04x %02x %02x\n", > (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow), > ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim); > if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf))) > strncpy(ntop_buf, "?", sizeof(ntop_buf)); > - printf("%s->", ntop_buf); > + (void)printf("%s->", ntop_buf); > if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf))) > strncpy(ntop_buf, "?", sizeof(ntop_buf)); > - printf("%s\n", ntop_buf); > + (void)printf("%s\n", ntop_buf); > } > > /* > @@ -2077,7 +2073,7 @@ pr_retip6(struct ip6_hdr *ip6, u_char *e > int hlen; > > if (end - (u_char *)ip6 < sizeof(*ip6)) { > - printf("IP6"); > + (void)printf("IP6"); > goto trunc; > } > pr_iph6(ip6); > @@ -2088,49 +2084,49 @@ pr_retip6(struct ip6_hdr *ip6, u_char *e > while (end - cp >= 8) { > switch (nh) { > case IPPROTO_HOPOPTS: > - printf("HBH "); > + (void)printf("HBH "); > hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3; > nh = ((struct ip6_hbh *)cp)->ip6h_nxt; > break; > case IPPROTO_DSTOPTS: > - printf("DSTOPT "); > + (void)printf("DSTOPT "); > hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3; > nh = ((struct ip6_dest *)cp)->ip6d_nxt; > break; > case IPPROTO_FRAGMENT: > - printf("FRAG "); > + (void)printf("FRAG "); > hlen = sizeof(struct ip6_frag); > nh = ((struct ip6_frag *)cp)->ip6f_nxt; > break; > case IPPROTO_ROUTING: > - printf("RTHDR "); > + (void)printf("RTHDR "); > hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3; > nh = ((struct ip6_rthdr *)cp)->ip6r_nxt; > break; > case IPPROTO_AH: > - printf("AH "); > + (void)printf("AH "); > hlen = (((struct ah *)cp)->ah_hl+2) << 2; > nh = ((struct ah *)cp)->ah_nh; > break; > case IPPROTO_ICMPV6: > - printf("ICMP6: type = %d, code = %d\n", > + (void)printf("ICMP6: type = %d, code = %d\n", > *cp, *(cp + 1)); > return; > case IPPROTO_ESP: > - printf("ESP\n"); > + (void)printf("ESP\n"); > return; > case IPPROTO_TCP: > - printf("TCP: from port %u, to port %u (decimal)\n", > + (void)printf("TCP: from port %u, to port %u > (decimal)\n", > (*cp * 256 + *(cp + 1)), > (*(cp + 2) * 256 + *(cp + 3))); > return; > case IPPROTO_UDP: > - printf("UDP: from port %u, to port %u (decimal)\n", > + (void)printf("UDP: from port %u, to port %u > (decimal)\n", > (*cp * 256 + *(cp + 1)), > (*(cp + 2) * 256 + *(cp + 3))); > return; > default: > - printf("Unknown Header(%d)\n", nh); > + (void)printf("Unknown Header(%d)\n", nh); > return; > } > > @@ -2140,30 +2136,26 @@ pr_retip6(struct ip6_hdr *ip6, u_char *e > if (end - cp < 8) > goto trunc; > > - putchar('\n'); > + (void)putchar('\n'); > return; > > trunc: > - printf("...\n"); > + (void)printf("...\n"); > return; > } > > __dead void > usage(void) > { > - if (v6flag) { > - (void)fprintf(stderr, > - "usage: ping6 [-dEefHLmnqv] [-c count] [-h hoplimit] " > - "[-I sourceaddr]\n\t[-i wait] [-l preload] [-p pattern] " > - "[-s packetsize] [-V rtable]\n\t[-w maxwait] host\n"); > - } else { > - (void)fprintf(stderr, > - "usage: ping [-DdEefHLnqRv] [-c count] [-I ifaddr]" > - " [-i wait]\n\t[-l preload] [-p pattern] [-s packetsize]" > + (void)fprintf(stderr, v6flag ? > + "usage: ping6 [-dEefHLmnqv] [-c count] [-h hoplimit] [-I > sourceaddr]" > + "\n\t[-i wait] [-l preload] [-p pattern] [-s packetsize] [-V > rtable]" > + "\n\t[-w maxwait] host\n" : > + "usage: ping [-DdEefHLnqRv] [-c count] [-I ifaddr] [-i wait]" > + "\n\t[-l preload] [-p pattern] [-s packetsize]" > #ifndef SMALL > - " [-T toskeyword]" > + " [-T toskeyword]" > #endif /* SMALL */ > - "\n\t[-t ttl] [-V rtable] [-w maxwait] host\n"); > - } > + "\n\t[-t ttl] [-V rtable] [-w maxwait] host\n"); > exit(1); > } > -- I'm not entirely sure you are real.