]> andersk Git - moira.git/blob - server/mr_util.c
Added support for optional debugging malloc. No code changes if you are
[moira.git] / server / mr_util.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  */
10
11 #ifndef lint
12 static char *rcsid_mr_util_c = "$Header$";
13 #endif lint
14
15 #include <mit-copyright.h>
16 #include "mr_server.h"
17
18 #include <ctype.h>
19 #include <sys/types.h>
20
21 extern char *whoami;
22
23 char *
24 requote(buf, cp, len)
25         char *buf;
26         register char *cp;
27 {
28         register int count = 0;
29         register unsigned char c;
30         if (len <= 2) return buf;
31         *buf++ = '"'; count++; len--;
32         for(; (count < 40) && (len > 1) && (c = *cp);
33             cp++, --len, ++count) {
34                 if (c == '\\' || c == '"') *buf++ = '\\';
35                 if (isprint(c)) *buf++ = c;
36                 else {
37                         sprintf(buf, "\\%03o", c);
38                         buf = index(buf, '\0');
39                 }
40         }
41         if (len > 1) { *buf++ = '"'; count++; len--; }
42         if (len > 3 && count >= 40) {
43                 *buf++ = '.'; count++; len--;
44                 *buf++ = '.'; count++; len--;
45                 *buf++ = '.'; count++; len--;
46         }
47         if (len > 1) *buf = '\0';
48         return buf;
49 }
50
51 log_args(tag, version, argc, argv)
52         char *tag;
53         int version;
54         int argc;
55         char **argv;
56 {
57         char buf[BUFSIZ];
58         register int i;
59         register char *bp;
60         
61         i = strlen(tag);
62         sprintf(buf, "%s[%d]: ", tag, version);
63         for (bp = buf; *bp; bp++);
64        
65         for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) {
66                 if (i != 0) {
67                         *bp++ = ',';
68                         *bp++ = ' '; 
69                 }
70                 bp = requote(bp, argv[i], (buf - bp) + 1024);
71         }
72         *bp = '\0';
73         com_err(whoami, 0, "%s", buf);
74 }
75         
76 void mr_com_err(whoami, code, fmt, pvar)
77         char *whoami;
78         int code;
79         char *fmt;
80         caddr_t pvar;
81 {
82         extern char *error_message();
83         extern client *cur_client;
84         
85         if (whoami) {
86                 fputs(whoami, stderr);
87                 if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
88                 fputs(": ", stderr);
89         }
90         if (code) {
91                 fputs(error_message(code), stderr);
92         }
93         if (fmt) {
94                 _doprnt(fmt, pvar, stderr);
95         }
96         putc('\n', stderr);
97 }
98
99
100 /* mr_trim_args: passed an argument vector, it will trim any trailing
101  * spaces on the args by writing a null into the string.  If an argument
102  * appears to be binary instead of ASCII, it will not be trimmed.
103  */
104
105 int mr_trim_args(argc, argv)
106 int argc;
107 char **argv;
108 {
109     register char **arg;
110     register unsigned char *p, *lastch;
111
112     for (arg = argv; argc--; arg++) {
113         for (lastch = p = (unsigned char *) *arg; *p; p++) {
114             /* If any byte in the string has the high bit set, assume
115              * that it is binary and we do not want to trim it.
116              * Setting p = lastch will cause us not to trim the string
117              * when we break out of this inner loop.
118              */
119             if (*p >= 0x80) {
120                 p = lastch;
121                 break;
122             }
123             if (!isspace(*p))
124               lastch = p;
125         }
126         if (p != lastch) {
127             if (isspace(*lastch))
128               *lastch = 0;
129             else
130               *(++lastch) = 0;
131         }
132     }
133     return(0);
134 }
135
136
137 /* returns a copy of the argv and all of it's strings */
138
139 char **mr_copy_args(argv, argc)
140 char **argv;
141 int argc;
142 {
143     char **a;
144     int i;
145
146     a = (char **) malloc(argc * sizeof(char *));
147     if (a == 0)
148       return(a);
149     for (i = 0; i < argc; i++)
150       a[i] = strsave(argv[i]);
151     return(a);
152 }
This page took 0.054572 seconds and 5 git commands to generate.