Rename samr_dissect_LOGON_HOURS() to dissect_ndr_nt_LOGON_HOURS() and
[obnox/wireshark/wip.git] / util.c
diff --git a/util.c b/util.c
index 6d3b5cf81eb6d2c49a31d00a0453d62bb3076a11..45b89ab59515cc9d2d81247b50c13bd22b728df1 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,12 +1,11 @@
 /* util.c
  * Utility routines
  *
- * $Id: util.c,v 1.26 2000/01/15 00:22:34 gram Exp $
+ * $Id: util.c,v 1.54 2001/11/09 07:44:48 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- *
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #endif
 
 #ifdef NEED_SNPRINTF_H
-# ifdef HAVE_STDARG_H
-#  include <stdarg.h>
-# else
-#  include <varargs.h>
-# endif
 # include "snprintf.h"
 #endif
 
 #include "mkstemp.h"
 #endif
 
-#include "util.h"
-
 #ifdef HAVE_IO_H
 #include <io.h>
 typedef int mode_t;    /* for win32 */
 #endif
 
+#include "util.h"
+
+/*
+ * Collect command-line arguments as a string consisting of the arguments,
+ * separated by spaces.
+ */
+char *
+get_args_as_string(int argc, char **argv, int optind)
+{
+       int len;
+       int i;
+       char *argstring;
+
+       /*
+        * Find out how long the string will be.
+        */
+       len = 0;
+       for (i = optind; i < argc; i++) {
+               len += strlen(argv[i]);
+               len++;  /* space, or '\0' if this is the last argument */
+       }
+
+       /*
+        * Allocate the buffer for the string.
+        */
+       argstring = g_malloc(len);
+
+       /*
+        * Now construct the string.
+        */
+       strcpy(argstring, "");
+       i = optind;
+       for (;;) {
+               strcat(argstring, argv[i]);
+               i++;
+               if (i == argc)
+                       break;
+               strcat(argstring, " ");
+       }
+       return argstring;
+}
+
 static char *
 setup_tmpdir(char *dir)
 {
@@ -276,3 +310,47 @@ EBCDIC_to_ASCII1(guint8 c)
 {
        return EBCDIC_translate_ASCII[c];
 }
+
+/* Compute the difference between two seconds/microseconds time stamps. */
+void
+compute_timestamp_diff(gint *diffsec, gint *diffusec,
+       guint32 sec1, guint32 usec1, guint32 sec2, guint32 usec2)
+{
+  if (sec1 == sec2) {
+    /* The seconds part of the first time is the same as the seconds
+       part of the second time, so if the microseconds part of the first
+       time is less than the microseconds part of the second time, the
+       first time is before the second time.  The microseconds part of
+       the delta should just be the difference between the microseconds
+       part of the first time and the microseconds part of the second
+       time; don't adjust the seconds part of the delta, as it's OK if
+       the microseconds part is negative. */
+
+    *diffsec = sec1 - sec2;
+    *diffusec = usec1 - usec2;
+  } else if (sec1 <= sec2) {
+    /* The seconds part of the first time is less than the seconds part
+       of the second time, so the first time is before the second time.
+
+       Both the "seconds" and "microseconds" value of the delta
+       should have the same sign, so if the difference between the
+       microseconds values would be *positive*, subtract 1,000,000
+       from it, and add one to the seconds value. */
+    *diffsec = sec1 - sec2;
+    if (usec2 >= usec1) {
+      *diffusec = usec1 - usec2;
+    } else {
+      *diffusec = (usec1 - 1000000) - usec2;
+      (*diffsec)++;
+    }
+  } else {
+    /* Oh, good, we're not caught in a chronosynclastic infindibulum. */
+    *diffsec = sec1 - sec2;
+    if (usec2 <= usec1) {
+      *diffusec = usec1 - usec2;
+    } else {
+      *diffusec = (usec1 + 1000000) - usec2;
+      (*diffsec)--;
+    }
+  }
+}