]> andersk Git - moira.git/blobdiff - server/mr_util.c
Fix another freeing-memory-we-didn't-malloc bug in AddMachine. This
[moira.git] / server / mr_util.c
index e4348d2f6ddbf1c629e6d56eb7754729ca8fc1e4..af7e266f24b6a474ab560166762189e25a6ba218 100644 (file)
@@ -9,15 +9,15 @@
  */
 
 #ifndef lint
-static char *rcsid_sms_util_c = "$Header$";
+static char *rcsid_mr_util_c = "$Header$";
 #endif lint
 
 #include <mit-copyright.h>
-#include "sms_server.h"
-
+#include "mr_server.h"
+#include <com_err.h>
 #include <ctype.h>
-#include <strings.h>
 #include <sys/types.h>
+#include <string.h>
 
 extern char *whoami;
 
@@ -30,26 +30,21 @@ requote(buf, cp, len)
        register unsigned char c;
        if (len <= 2) return buf;
        *buf++ = '"'; count++; len--;
-       for(; (count < 40) && (len > 1) && (c = *cp);
+       for(; (count < 258) && (len > 1) && (c = *cp);
            cp++, --len, ++count) {
                if (c == '\\' || c == '"') *buf++ = '\\';
                if (isprint(c)) *buf++ = c;
                else {
                        sprintf(buf, "\\%03o", c);
-                       buf = index(buf, '\0');
+                       buf = strchr(buf, '\0');
                }
        }
        if (len > 1) { *buf++ = '"'; count++; len--; }
-       if (len > 3 && count >= 40) {
-               *buf++ = '.'; count++; len--;
-               *buf++ = '.'; count++; len--;
-               *buf++ = '.'; count++; len--;
-       }
        if (len > 1) *buf = '\0';
        return buf;
 }
 
-log_args(tag, version, argc, argv)
+void log_args(tag, version, argc, argv)
        char *tag;
        int version;
        int argc;
@@ -63,24 +58,23 @@ log_args(tag, version, argc, argv)
        sprintf(buf, "%s[%d]: ", tag, version);
        for (bp = buf; *bp; bp++);
        
-       for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) {
+       for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++) {
                if (i != 0) {
                        *bp++ = ',';
                        *bp++ = ' '; 
                }
-               bp = requote(bp, argv[i], (buf - bp) + 1024);
+               bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
        }
        *bp = '\0';
        com_err(whoami, 0, "%s", buf);
 }
        
-void sms_com_err(whoami, code, fmt, pvar)
+void mr_com_err(whoami, code, fmt, pvar)
        char *whoami;
        int code;
        char *fmt;
        caddr_t pvar;
 {
-       extern char *error_message();
        extern client *cur_client;
        
        if (whoami) {
@@ -98,21 +92,32 @@ void sms_com_err(whoami, code, fmt, pvar)
 }
 
 
-/* sms_trim_args: passed an argument vector, it will trim any trailing
- * spaces on the args by writing a null into the string.
+/* mr_trim_args: passed an argument vector, it will trim any trailing
+ * spaces on the args by writing a null into the string.  If an argument
+ * appears to be binary instead of ASCII, it will not be trimmed.
  */
 
-int sms_trim_args(argc, argv)
+int mr_trim_args(argc, argv)
 int argc;
 char **argv;
 {
     register char **arg;
-    register char *p, *lastch;
+    register unsigned char *p, *lastch;
 
     for (arg = argv; argc--; arg++) {
-       for (lastch = p = *arg; *p; p++)
-         if (!isspace(*p))
-           lastch = p;
+       for (lastch = p = (unsigned char *) *arg; *p; p++) {
+           /* If any byte in the string has the high bit set, assume
+            * that it is binary and we do not want to trim it.
+            * Setting p = lastch will cause us not to trim the string
+            * when we break out of this inner loop.
+            */
+           if (*p >= 0x80) {
+               p = lastch;
+               break;
+           }
+           if (!isspace(*p))
+             lastch = p;
+       }
        if (p != lastch) {
            if (isspace(*lastch))
              *lastch = 0;
@@ -124,18 +129,19 @@ char **argv;
 }
 
 
-trim(s)
-register char *s;
+/* returns a copy of the argv and all of it's strings */
+
+char **mr_copy_args(argv, argc)
+char **argv;
+int argc;
 {
-    register char *p;
+    char **a;
+    int i;
 
-    for (p = s; *s; s++)
-      if (*s != ' ')
-       p = s;
-    if (p != s) {
-       if (*p == ' ')
-         *p = 0;
-       else
-         p[1] = 0;
-    }
+    a = (char **) malloc(argc * sizeof(char *));
+    if (a == 0)
+      return(a);
+    for (i = 0; i < argc; i++)
+      a[i] = strsave(argv[i]);
+    return(a);
 }
This page took 0.337352 seconds and 4 git commands to generate.