- char flags[64];
- unsigned long int d, g, m;
-
- char sdest[16], sgw[16];
-
- FILE *fp = bb_xfopen("/proc/net/route", "r");
-
- if (noresolve)
- noresolve = 0x0fff;
-
- printf("Kernel IP routing table\n");
- printf
- ("Destination Gateway Genmask Flags %s Iface\n",
- netstatfmt ? " MSS Window irtt" : "Metric Ref Use");
-
- while (fgets(buff, sizeof(buff), fp) != NULL) {
- if (nl) {
- int ifl = 0;
- int numeric;
- struct sockaddr_in s_addr;
-
- while (buff[ifl] != ' ' && buff[ifl] != '\t' && buff[ifl] != '\0')
- ifl++;
- buff[ifl] = 0; /* interface */
- if (sscanf(buff + ifl + 1, "%lx%lx%X%d%d%d%lx%d%d%d",
- &d, &g, &flgs, &ref, &use, &metric, &m, &mtu, &win,
- &ir) != 10) {
- bb_error_msg_and_die("Unsuported kernel route format\n");
- }
- ifl = 0; /* parse flags */
- if (flgs & RTF_UP) {
- if (flgs & RTF_REJECT)
- flags[ifl++] = '!';
- else
- flags[ifl++] = 'U';
- if (flgs & RTF_GATEWAY)
- flags[ifl++] = 'G';
- if (flgs & RTF_HOST)
- flags[ifl++] = 'H';
- if (flgs & RTF_REINSTATE)
- flags[ifl++] = 'R';
- if (flgs & RTF_DYNAMIC)
- flags[ifl++] = 'D';
- if (flgs & RTF_MODIFIED)
- flags[ifl++] = 'M';
- flags[ifl] = 0;
- dest.s_addr = d;
- gw.s_addr = g;
- mask.s_addr = m;
- memset(&s_addr, 0, sizeof(struct sockaddr_in));
- s_addr.sin_family = AF_INET;
- s_addr.sin_addr = dest;
- numeric = noresolve | 0x8000; /* default instead of * */
- INET_rresolve(sdest, sizeof(sdest), &s_addr, numeric, m);
- numeric = noresolve | 0x4000; /* host instead of net */
- s_addr.sin_addr = gw;
- INET_rresolve(sgw, sizeof(sgw), &s_addr, numeric, m);
-
- printf("%-16s%-16s%-16s%-6s", sdest, sgw, inet_ntoa(mask),
- flags);
- if (netstatfmt)
- printf("%5d %-5d %6d %s\n", mtu, win, ir, buff);
- else
- printf("%-6d %-2d %7d %s\n", metric, ref, use, buff);
+ struct sockaddr_in s_addr;
+ struct in_addr mask;
+
+ FILE *fp = xfopen_for_read("/proc/net/route");
+
+ printf("Kernel IP routing table\n"
+ "Destination Gateway Genmask Flags %s Iface\n",
+ netstatfmt ? " MSS Window irtt" : "Metric Ref Use");
+
+ if (fscanf(fp, "%*[^\n]\n") < 0) { /* Skip the first line. */
+ goto ERROR; /* Empty or missing line, or read error. */
+ }
+ while (1) {
+ int r;
+ r = fscanf(fp, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n",
+ devname, &d, &g, &flgs, &ref, &use, &metric, &m,
+ &mtu, &win, &ir);
+ if (r != 11) {
+ if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */
+ break;