]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/util.c
ab82295e9217bb97759e668082ced9e16b229d3e
[bacula/bacula] / bacula / src / lib / util.c
1 /*
2  *   util.c  miscellaneous utility subroutines for Bacula
3  * 
4  *    Kern Sibbald, MM
5  */
6
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "findlib/find.h"
29
30 /*
31  * Various Bacula Utility subroutines
32  *
33  */
34
35 /*
36  * Edit a number with commas, the supplied buffer
37  * must be at least 27 bytes long.
38  */
39 char *edit_uint_with_commas(uint64_t val, char *buf)
40 {
41    sprintf(buf, "%" lld, val);
42    return add_commas(buf, buf);
43 }
44
45 char *add_commas(char *val, char *buf)
46 {
47    int len, nc;
48    char *p, *q;
49    int i;
50
51    if (val != buf) {
52       strcpy(buf, val);
53    }
54    len = strlen(buf);
55    if (len < 1) {
56       len = 1;
57    }
58    nc = (len - 1) / 3;
59    p = buf+len;
60    q = p + nc;
61    *q-- = *p--;
62    for ( ; nc; nc--) {
63       for (i=0; i < 3; i++) {
64           *q-- = *p--;
65       }
66       *q-- = ',';
67    }   
68    return buf;
69 }
70
71
72 /* Convert a string in place to lower case */
73 void 
74 lcase(char *str)
75 {
76    while (*str) {
77       if (ISUPPER(*str))
78          *str = tolower((int)(*str));
79        str++;
80    }
81 }
82
83 /* Convert spaces to non-space character. 
84  * This makes scanf of fields containing spaces easier.
85  */
86 void
87 bash_spaces(char *str)
88 {
89    while (*str) {
90       if (*str == ' ')
91          *str = 0x1;
92       str++;
93    }
94 }
95
96 /* Convert non-space characters (0x1) back into spaces */
97 void
98 unbash_spaces(char *str)
99 {
100    while (*str) {
101      if (*str == 0x1)
102         *str = ' ';
103      str++;
104    }
105 }
106
107 /* Strip any trailing junk from the command */
108 void strip_trailing_junk(char *cmd)
109 {
110    char *p;
111    p = cmd + strlen(cmd) - 1;
112
113    /* strip trailing junk from command */
114    while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
115       *p-- = 0;
116 }
117
118 /* Strip any trailing slashes from a directory path */
119 void strip_trailing_slashes(char *dir)
120 {
121    char *p;
122    p = dir + strlen(dir) - 1;
123
124    /* strip trailing slashes */
125    while ((p >= dir) && (*p == '/'))
126       *p-- = 0;
127 }
128
129 /*
130  * Skip spaces
131  *  Returns: 0 on failure (EOF)             
132  *           1 on success
133  *           new address in passed parameter 
134  */
135 int skip_spaces(char **msg)
136 {
137    char *p = *msg;
138    if (!p) {
139       return 0;
140    }
141    while (*p && *p == ' ') {
142       p++;
143    }
144    *msg = p;
145    return *p ? 1 : 0;
146 }
147
148 /*
149  * Skip nonspaces
150  *  Returns: 0 on failure (EOF)             
151  *           1 on success
152  *           new address in passed parameter 
153  */
154 int skip_nonspaces(char **msg)
155 {
156    char *p = *msg;
157
158    if (!p) {
159       return 0;
160    }
161    while (*p && *p != ' ') {
162       p++;
163    }
164    *msg = p;
165    return *p ? 1 : 0;
166 }
167
168 /* folded search for string - case insensitive */
169 int
170 fstrsch(char *a, char *b)   /* folded case search */
171 {
172    register char *s1,*s2;
173    register char c1, c2;
174
175    s1=a;
176    s2=b;
177    while (*s1) {                      /* do it the fast way */
178       if ((*s1++ | 0x20) != (*s2++ | 0x20))
179          return 0;                    /* failed */
180    }
181    while (*a) {                       /* do it over the correct slow way */
182       if (ISUPPER(c1 = *a)) {
183          c1 = tolower((int)c1);
184       }
185       if (ISUPPER(c2 = *b)) {
186          c2 = tolower((int)c2);
187       }
188       if (c1 != c2) {
189          return 0;
190       }
191       a++;
192       b++;
193    }
194    return 1;
195 }
196
197
198 char *encode_time(time_t time, char *buf)
199 {
200    struct tm tm;
201    int n;
202
203    if (localtime_r(&time, &tm)) {
204       n = sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
205                    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
206                    tm.tm_hour, tm.tm_min, tm.tm_sec);
207    }
208    return buf+n;
209 }
210
211 /***********************************************************************
212  * Encode the mode bits into a 10 character string like LS does
213  ***********************************************************************/
214
215 char *encode_mode(mode_t mode, char *buf)
216 {
217   char *cp = buf;  
218
219   *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' :
220           S_ISLNK(mode) ? 'l' : '-';
221   *cp++ = mode & S_IRUSR ? 'r' : '-';
222   *cp++ = mode & S_IWUSR ? 'w' : '-';
223   *cp++ = (mode & S_ISUID
224                ? (mode & S_IXUSR ? 's' : 'S')
225                : (mode & S_IXUSR ? 'x' : '-'));
226   *cp++ = mode & S_IRGRP ? 'r' : '-';
227   *cp++ = mode & S_IWGRP ? 'w' : '-';
228   *cp++ = (mode & S_ISGID
229                ? (mode & S_IXGRP ? 's' : 'S')
230                : (mode & S_IXGRP ? 'x' : '-'));
231   *cp++ = mode & S_IROTH ? 'r' : '-';
232   *cp++ = mode & S_IWOTH ? 'w' : '-';
233   *cp++ = (mode & S_ISVTX
234                ? (mode & S_IXOTH ? 't' : 'T')
235                : (mode & S_IXOTH ? 'x' : '-'));
236   *cp = '\0';
237   return cp;
238 }
239
240 #ifdef WORKING
241 extern char *getuser(uid_t uid);
242 extern char *getgroup(gid_t gid);
243
244 void print_ls_output(char *fname, char *lname, int type, struct stat *statp)
245 {
246    char buf[1000]; 
247    char *p, *f;
248    int n;
249
250    p = encode_mode(statp->st_mode, buf);
251    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
252    p += n;
253    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
254    p += n;
255    n = sprintf(p, "%8ld  ", statp->st_size);
256    p += n;
257    p = encode_time(statp->st_ctime, p);
258    *p++ = ' ';
259    *p++ = ' ';
260    for (f=fname; *f; )
261       *p++ = *f++;
262    if (type == FT_LNK) {
263       *p++ = ' ';
264       *p++ = '-';
265       *p++ = '>';
266       *p++ = ' ';
267       /* Copy link name */
268       for (f=lname; *f; )
269          *p++ = *f++;
270    }
271    *p++ = '\n';
272    *p = 0;
273    fputs(buf, stdout);
274 }
275 #endif
276
277 int do_shell_expansion(char *name)
278 {
279 /*  ****FIXME***** this should work for Win32 too */
280 #define UNIX
281 #ifdef UNIX
282 #ifndef PATH_MAX
283 #define PATH_MAX 512
284 #endif
285
286    int pid, wpid, stat;
287    int waitstatus;
288    char *shellcmd;
289    void (*istat)(int), (*qstat)(int);
290    int i;
291    char echout[PATH_MAX + 256];
292    int pfd[2];
293    static char meta[] = "~\\$[]*?`'<>\"";
294    int found = FALSE;
295    int len;
296
297    /* Check if any meta characters are present */
298    len = strlen(meta);
299    for (i = 0; i < len; i++) {
300       if (strchr(name, meta[i])) {
301          found = TRUE;
302          break;
303       }
304    }
305    stat = 0;
306    if (found) {
307 #ifdef nt
308        /* If the filename appears to be a DOS filename,
309           convert all backward slashes \ to Unix path
310           separators / and insert a \ infront of spaces. */
311        len = strlen(name);
312        if (len >= 3 && name[1] == ':' && name[2] == '\\') {
313           for (i=2; i<len; i++)
314              if (name[i] == '\\')
315                 name[i] = '/';
316        }
317 #else
318        /* Pass string off to the shell for interpretation */
319        if (pipe(pfd) == -1)
320           return 0;
321        switch(pid = fork()) {
322        case -1:
323           break;
324
325        case 0:                            /* child */
326           /* look for shell */
327           if ((shellcmd = getenv("SHELL")) == NULL)
328              shellcmd = "/bin/sh";
329           close(1); dup(pfd[1]);          /* attach pipes to stdin and stdout */
330           close(2); dup(pfd[1]);
331           for (i = 3; i < 32; i++)        /* close everything else */
332              close(i);
333           strcpy(echout, "echo ");        /* form echo command */
334           strcat(echout, name);
335           execl(shellcmd, shellcmd, "-c", echout, NULL); /* give to shell */
336           exit(127);                      /* shouldn't get here */
337
338        default:                           /* parent */
339           /* read output from child */
340           i = read(pfd[0], echout, sizeof echout);
341           echout[--i] = 0;                /* set end of string */
342           /* look for first word or first line. */
343           while (--i >= 0) {
344              if (echout[i] == ' ' || echout[i] == '\n')
345                 echout[i] = 0;            /* keep only first one */
346           }
347           istat = signal(SIGINT, SIG_IGN);
348           qstat = signal(SIGQUIT, SIG_IGN);
349           /* wait for child to exit */
350           while ((wpid = wait(&waitstatus)) != pid && wpid != -1)
351              { ; }
352           signal(SIGINT, istat);
353           signal(SIGQUIT, qstat);
354           strcpy(name, echout);
355           stat = 1;
356           break;
357        }
358        close(pfd[0]);                     /* close pipe */
359        close(pfd[1]);
360 #endif /* nt */
361    }
362    return stat;
363
364 #endif /* UNIX */
365
366 #if  MSC | MSDOS | __WATCOMC__
367
368    char prefix[100], *env, *getenv();
369
370    /* Home directory reference? */
371    if (*name == '~' && (env=getenv("HOME"))) {
372       strcpy(prefix, env);            /* copy HOME directory name */
373       name++;                         /* skip over ~ in name */
374       strcat(prefix, name);
375       name--;                         /* get back to beginning */
376       strcpy(name, prefix);           /* move back into name */
377    }
378    return 1;
379 #endif
380
381 }