This commit was generated by cvs2svn to compensate for changes in r2587,
[iproute2.git] / lib / utils.c
index 73ce865..4bdda71 100644 (file)
@@ -27,6 +27,9 @@
 #include <resolv.h>
 #include <asm/types.h>
 #include <linux/pkt_sched.h>
+#include <time.h>
+#include <sys/time.h>
+
 
 #include "utils.h"
 
@@ -237,10 +240,11 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family)
                                dst->bitlen = 32;
                }
                if (slash) {
-                       if (get_integer(&plen, slash+1, 0) || plen > dst->bitlen) {
+                       if (get_unsigned(&plen, slash+1, 0) || plen > dst->bitlen) {
                                err = -1;
                                goto done;
                        }
+                       dst->flags |= PREFIXLEN_SPECIFIED;
                        dst->bitlen = plen;
                }
        }
@@ -500,9 +504,9 @@ const char *format_host(int af, int len, const void *addr,
 }
 
 
-__u8* hexstring_n2a(const __u8 *str, int len, __u8 *buf, int blen)
+char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen)
 {
-       __u8 *ptr = buf;
+       char *ptr = buf;
        int i;
 
        for (i=0; i<len; i++) {
@@ -519,7 +523,7 @@ __u8* hexstring_n2a(const __u8 *str, int len, __u8 *buf, int blen)
        return buf;
 }
 
-__u8* hexstring_a2n(const __u8 *str, __u8 *buf, int blen)
+__u8* hexstring_a2n(const char *str, __u8 *buf, int blen)
 {
        int cnt = 0;
 
@@ -556,3 +560,81 @@ __u8* hexstring_a2n(const __u8 *str, __u8 *buf, int blen)
                memset(buf+cnt, 0, blen-cnt);
        return buf;
 }
+
+int print_timestamp(FILE *fp)
+{
+       struct timeval tv;
+       char *tstr;
+
+       memset(&tv, 0, sizeof(tv));
+       gettimeofday(&tv, NULL);
+
+       tstr = asctime(localtime(&tv.tv_sec));
+       tstr[strlen(tstr)-1] = 0;
+       fprintf(fp, "Timestamp: %s %lu usec\n", tstr, tv.tv_usec);
+       return 0;
+}
+
+int cmdlineno;
+
+/* Like glibc getline but handle continuation lines and comments */
+size_t getcmdline(char **linep, size_t *lenp, FILE *in)
+{
+       size_t cc;
+       char *cp;
+               
+       if ((cc = getline(linep, lenp, in)) < 0)
+               return cc;      /* eof or error */
+       ++cmdlineno;
+
+       cp = strchr(*linep, '#');
+       if (cp) 
+               *cp = '\0';
+       
+       while ((cp = strstr(*linep, "\\\n")) != NULL) {
+               char *line1 = NULL;
+               size_t len1 = 0;
+               size_t cc1;
+
+               if ((cc1 = getline(&line1, &len1, in)) < 0) {
+                       fprintf(stderr, "Missing continuation line\n");
+                       return cc1;
+               }
+
+               ++cmdlineno;
+               *cp = 0;
+
+               cp = strchr(line1, '#');
+               if (cp) 
+                       *cp = '\0';
+
+               *linep = realloc(*linep, strlen(*linep) + strlen(line1) + 1);
+               if (!*linep) {
+                       fprintf(stderr, "Out of memory\n");
+                       return -1;
+               }
+               cc += cc1 - 2;
+               strcat(*linep, line1);
+               free(line1);
+       }
+       return cc;
+}
+
+/* split command line into argument vector */
+int makeargs(char *line, char *argv[], int maxargs)
+{
+       static const char ws[] = " \t\r\n";
+       char *cp;
+       int argc = 0;
+
+       for (cp = strtok(line, ws); cp; cp = strtok(NULL, ws)) {
+               if (argc >= (maxargs - 1)) {
+                       fprintf(stderr, "Too many arguments to command\n");
+                       exit(1);
+               }
+               argv[argc++] = cp;
+       }
+       argv[argc] = NULL;
+
+       return argc;
+}