]> andersk Git - moira.git/blob - server/mr_util.c
Diane Delgado's changes for a fixed table-locking order
[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 < 258) && (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 > 1) *buf = '\0';
43         return buf;
44 }
45
46 log_args(tag, version, argc, argv)
47         char *tag;
48         int version;
49         int argc;
50         char **argv;
51 {
52         char buf[BUFSIZ];
53         register int i;
54         register char *bp;
55         
56         i = strlen(tag);
57         sprintf(buf, "%s[%d]: ", tag, version);
58         for (bp = buf; *bp; bp++);
59        
60         for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++) {
61                 if (i != 0) {
62                         *bp++ = ',';
63                         *bp++ = ' '; 
64                 }
65                 bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
66         }
67         *bp = '\0';
68         com_err(whoami, 0, "%s", buf);
69 }
70         
71 void mr_com_err(whoami, code, fmt, pvar)
72         char *whoami;
73         int code;
74         char *fmt;
75         caddr_t pvar;
76 {
77         extern char *error_message();
78         extern client *cur_client;
79         
80         if (whoami) {
81                 fputs(whoami, stderr);
82                 if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
83                 fputs(": ", stderr);
84         }
85         if (code) {
86                 fputs(error_message(code), stderr);
87         }
88         if (fmt) {
89                 _doprnt(fmt, pvar, stderr);
90         }
91         putc('\n', stderr);
92 }
93
94
95 /* mr_trim_args: passed an argument vector, it will trim any trailing
96  * spaces on the args by writing a null into the string.  If an argument
97  * appears to be binary instead of ASCII, it will not be trimmed.
98  */
99
100 int mr_trim_args(argc, argv)
101 int argc;
102 char **argv;
103 {
104     register char **arg;
105     register unsigned char *p, *lastch;
106
107     for (arg = argv; argc--; arg++) {
108         for (lastch = p = (unsigned char *) *arg; *p; p++) {
109             /* If any byte in the string has the high bit set, assume
110              * that it is binary and we do not want to trim it.
111              * Setting p = lastch will cause us not to trim the string
112              * when we break out of this inner loop.
113              */
114             if (*p >= 0x80) {
115                 p = lastch;
116                 break;
117             }
118             if (!isspace(*p))
119               lastch = p;
120         }
121         if (p != lastch) {
122             if (isspace(*lastch))
123               *lastch = 0;
124             else
125               *(++lastch) = 0;
126         }
127     }
128     return(0);
129 }
130
131
132 /* returns a copy of the argv and all of it's strings */
133
134 char **mr_copy_args(argv, argc)
135 char **argv;
136 int argc;
137 {
138     char **a;
139     int i;
140
141     a = (char **) malloc(argc * sizeof(char *));
142     if (a == 0)
143       return(a);
144     for (i = 0; i < argc; i++)
145       a[i] = strsave(argv[i]);
146     return(a);
147 }
This page took 0.21893 seconds and 5 git commands to generate.