First try at extension tolerant /proc parser. Only used by route so far.
authorKlaas Freitag <freitag@suse.de>
Tue, 12 May 1998 22:59:07 +0000 (22:59 +0000)
committerKlaas Freitag <freitag@suse.de>
Tue, 12 May 1998 22:59:07 +0000 (22:59 +0000)
lib/Makefile
lib/inet_gr.c
lib/proc.c [new file with mode: 0644]
lib/proc.h [new file with mode: 0644]

index a36f989..ebfabe5 100644 (file)
@@ -34,7 +34,7 @@ AFOBJS         = unix.o inet.o inet6.o ax25.o ipx.o ddp.o ipx.o netrom.o af.o rose.o ec
 AFGROBJS = inet_gr.o inet6_gr.o ipx_gr.o ddp_gr.o netrom_gr.o ax25_gr.o rose_gr.o getroute.o
 AFSROBJS = inet_sr.o inet6_sr.o netrom_sr.o ipx_sr.o setroute.o
 ACTOBJS  = slip_ac.o ppp_ac.o activate.o
-VARIA   = getargs.o masq_info.o
+VARIA   = getargs.o masq_info.o proc.o
 NLSMISC  = net-string.o
 
 OBJS   = $(NLSMISC) $(VARIA) $(AFOBJS) $(HWOBJS) \
@@ -43,7 +43,7 @@ OBJS  = $(NLSMISC) $(VARIA) $(AFOBJS) $(HWOBJS) \
 
 # This can be overwritten by the TOPLEVEL Makefile
 TOPDIR=..
-COPTS = -O2 -Wall -fomit-frame-pointer #-DDEBUG
+COPTS = -O2 -Wall #-DDEBUG
 LOPTS = -s
 
 CFLAGS  = $(COPTS) -I. -I$(TOPDIR) -I$(TOPDIR)/include
index de6e6dd..108bef5 100644 (file)
@@ -22,6 +22,7 @@
 #include "net-locale.h"
 
 #include "net-features.h"
+#include "proc.h"
 
 extern     struct aftype   inet_aftype;
 
@@ -34,6 +35,7 @@ int rprint_fib(int ext, int numeric)
   struct sockaddr snet, sgate, smask;
   int num, iflags, metric, refcnt, use, mss, window, irtt;
   FILE *fp=fopen(_PATH_PROCNET_ROUTE, "r");
+  char *fmt;   
 
   if (!fp) {
        ESYSNOT("getroute","INET FIB");
@@ -59,9 +61,28 @@ int rprint_fib(int ext, int numeric)
   irtt=0;
   window=0;
   mss=0;
+
+  fmt = proc_gen_fmt(_PATH_PROCNET_ROUTE, fp,
+                                        "Iface", "%16s",
+                                        "Destination", "%128s",
+                                        "Gateway", "%128s",
+                                        "Flags", "%X",
+                                        "RefCnt", "%d", 
+                                        "Use",  "%d", 
+                                        "Metric", "%d", 
+                                        "Mask", "%128s",
+                                        "MTU", "%d", 
+                                        "Window", "%d",
+                                        "IRTT", "%d", 
+                                        NULL); 
+  /* "%16s %128s %128s %X %d %d %d %128s %d %d %d\n" */ 
+
+  if (!fmt) 
+         return 1; 
+
   while (fgets(buff, 1023, fp))
   {
-       num = sscanf(buff, "%16s %128s %128s %X %d %d %d %128s %d %d %d\n",
+       num = sscanf(buff, fmt, 
                iface, net_addr, gate_addr,
                &iflags, &refcnt, &use, &metric, mask_addr,
                &mss,&window,&irtt);
diff --git a/lib/proc.c b/lib/proc.c
new file mode 100644 (file)
index 0000000..9e439f3
--- /dev/null
@@ -0,0 +1,39 @@
+/* Tolerant /proc file parser. Copyright 1998 Andi Kleen */ 
+
+#include <string.h> 
+#include <stdarg.h>
+#include <stdio.h>
+
+/* Caller must free return string. */ 
+char * 
+proc_gen_fmt(char *name, FILE *fh, ...)
+{
+       char buf[512], format[512] = ""; 
+       char *title, *head; 
+       va_list ap;
+
+       if (!fgets(buf, sizeof buf, fh)) 
+               return NULL; 
+
+       va_start(ap,fh); 
+       head = strtok(buf, " \t"); 
+       title = va_arg(ap, char *); 
+       while (title && head) { 
+               if (!strcmp(title, head)) { 
+                       strcat(format, va_arg(ap, char *)); 
+                       title = va_arg(ap, char *); 
+               } else {
+                       strcat(format, "%*[^ \t]"); 
+               }
+               strcat(format, " "); 
+               head = strtok(NULL, " \t"); 
+       }
+       va_end(ap); 
+
+       if (title) { 
+               fprintf(stderr, "warning: %s does not contain required field %s\n",
+                                               name, title); 
+               return NULL; 
+       }
+       return strdup(format); 
+}
diff --git a/lib/proc.h b/lib/proc.h
new file mode 100644 (file)
index 0000000..795765a
--- /dev/null
@@ -0,0 +1,4 @@
+
+
+/* Generate a suitable scanf format for a column title line */ 
+char *proc_gen_fmt(char *name, FILE *fh, ...);