2 * util.c miscellaneous utility subroutines for Bacula
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 #include "findlib/find.h"
33 * Various Bacula Utility subroutines
38 * Convert a string to btime_t (64 bit seconds)
40 1: if OK, and value stored in value
42 int string_to_btime(char *str, btime_t *value)
46 static int mod[] = {'*', 's', 'm', 'h', 'd', 'w', 'o', 'q', 'y', 0};
47 static int mult[] = {1, 1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30,
48 60*60*24*91, 60*60*24*365};
50 /* Look for modifier */
58 while (mod[++i] != 0) {
61 str[len] = 0; /* strip modifier */
66 if (mod[i] == 0 || !is_a_number(str)) {
69 val = (btime_t)strtod(str, NULL);
70 if (errno != 0 || val < 0) {
73 *value = val * mult[i];
78 char *edit_btime(btime_t val, char *buf)
81 static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
82 static char *mod[] = {"year", "month", "day", "hour", "min"};
88 times = val / mult[i];
90 val = val - (btime_t)times * mult[i];
91 sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
95 if (val == 0 && strlen(buf) == 0) {
96 strcat(buf, "0 secs");
97 } else if (val != 0) {
98 sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
105 * Check if specified string is a number or not.
106 * Taken from SQLite, cool, thanks.
108 int is_a_number(const char *n)
112 if( *n == '-' || *n == '+' ) {
115 while (ISDIGIT(*n)) {
119 if (digit_seen && *n == '.') {
121 while (ISDIGIT(*n)) { n++; }
123 if (digit_seen && (*n == 'e' || *n == 'E')
124 && (ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && ISDIGIT(n[2])))) {
125 n += 2; /* skip e- or e+ or e digit */
126 while (ISDIGIT(*n)) { n++; }
128 return digit_seen && *n==0;
133 * Edit an integer number with commas, the supplied buffer
134 * must be at least 27 bytes long. The incoming number
135 * is always widened to 64 bits.
137 char *edit_uint64_with_commas(uint64_t val, char *buf)
139 sprintf(buf, "%" lld, val);
140 return add_commas(buf, buf);
144 * Edit an integer number, the supplied buffer
145 * must be at least 27 bytes long. The incoming number
146 * is always widened to 64 bits.
148 char *edit_uint64(uint64_t val, char *buf)
150 sprintf(buf, "%" lld, val);
156 * Add commas to a string, which is presumably
159 char *add_commas(char *val, char *buf)
177 for (i=0; i < 3; i++) {
186 /* Convert a string in place to lower case */
187 void lcase(char *str)
191 *str = tolower((int)(*str));
196 /* Convert spaces to non-space character.
197 * This makes scanf of fields containing spaces easier.
200 bash_spaces(char *str)
209 /* Convert non-space characters (0x1) back into spaces */
211 unbash_spaces(char *str)
220 /* Strip any trailing junk from the command */
221 void strip_trailing_junk(char *cmd)
224 p = cmd + strlen(cmd) - 1;
226 /* strip trailing junk from command */
227 while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
231 /* Strip any trailing slashes from a directory path */
232 void strip_trailing_slashes(char *dir)
235 p = dir + strlen(dir) - 1;
237 /* strip trailing slashes */
238 while ((p >= dir) && (*p == '/'))
244 * Returns: 0 on failure (EOF)
246 * new address in passed parameter
248 int skip_spaces(char **msg)
254 while (*p && *p == ' ') {
263 * Returns: 0 on failure (EOF)
265 * new address in passed parameter
267 int skip_nonspaces(char **msg)
274 while (*p && *p != ' ') {
281 /* folded search for string - case insensitive */
283 fstrsch(char *a, char *b) /* folded case search */
285 register char *s1,*s2;
286 register char c1, c2;
290 while (*s1) { /* do it the fast way */
291 if ((*s1++ | 0x20) != (*s2++ | 0x20))
292 return 0; /* failed */
294 while (*a) { /* do it over the correct slow way */
295 if (ISUPPER(c1 = *a)) {
296 c1 = tolower((int)c1);
298 if (ISUPPER(c2 = *b)) {
299 c2 = tolower((int)c2);
311 char *encode_time(time_t time, char *buf)
316 if (localtime_r(&time, &tm)) {
317 n = sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
318 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
319 tm.tm_hour, tm.tm_min, tm.tm_sec);
324 /***********************************************************************
325 * Encode the mode bits into a 10 character string like LS does
326 ***********************************************************************/
328 char *encode_mode(mode_t mode, char *buf)
332 *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' :
333 S_ISLNK(mode) ? 'l' : '-';
334 *cp++ = mode & S_IRUSR ? 'r' : '-';
335 *cp++ = mode & S_IWUSR ? 'w' : '-';
336 *cp++ = (mode & S_ISUID
337 ? (mode & S_IXUSR ? 's' : 'S')
338 : (mode & S_IXUSR ? 'x' : '-'));
339 *cp++ = mode & S_IRGRP ? 'r' : '-';
340 *cp++ = mode & S_IWGRP ? 'w' : '-';
341 *cp++ = (mode & S_ISGID
342 ? (mode & S_IXGRP ? 's' : 'S')
343 : (mode & S_IXGRP ? 'x' : '-'));
344 *cp++ = mode & S_IROTH ? 'r' : '-';
345 *cp++ = mode & S_IWOTH ? 'w' : '-';
346 *cp++ = (mode & S_ISVTX
347 ? (mode & S_IXOTH ? 't' : 'T')
348 : (mode & S_IXOTH ? 'x' : '-'));
354 extern char *getuser(uid_t uid);
355 extern char *getgroup(gid_t gid);
357 void print_ls_output(char *fname, char *lname, int type, struct stat *statp)
363 p = encode_mode(statp->st_mode, buf);
364 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
366 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
368 n = sprintf(p, "%8ld ", statp->st_size);
370 p = encode_time(statp->st_ctime, p);
375 if (type == FT_LNK) {
390 int do_shell_expansion(char *name)
392 /* ****FIXME***** this should work for Win32 too */
402 void (*istat)(int), (*qstat)(int);
404 char echout[PATH_MAX + 256];
406 static char meta[] = "~\\$[]*?`'<>\"";
410 /* Check if any meta characters are present */
412 for (i = 0; i < len; i++) {
413 if (strchr(name, meta[i])) {
421 /* If the filename appears to be a DOS filename,
422 convert all backward slashes \ to Unix path
423 separators / and insert a \ infront of spaces. */
425 if (len >= 3 && name[1] == ':' && name[2] == '\\') {
426 for (i=2; i<len; i++)
431 /* Pass string off to the shell for interpretation */
434 switch(pid = fork()) {
440 if ((shellcmd = getenv("SHELL")) == NULL)
441 shellcmd = "/bin/sh";
442 close(1); dup(pfd[1]); /* attach pipes to stdin and stdout */
443 close(2); dup(pfd[1]);
444 for (i = 3; i < 32; i++) /* close everything else */
446 strcpy(echout, "echo "); /* form echo command */
447 strcat(echout, name);
448 execl(shellcmd, shellcmd, "-c", echout, NULL); /* give to shell */
449 exit(127); /* shouldn't get here */
451 default: /* parent */
452 /* read output from child */
453 i = read(pfd[0], echout, sizeof echout);
454 echout[--i] = 0; /* set end of string */
455 /* look for first word or first line. */
457 if (echout[i] == ' ' || echout[i] == '\n')
458 echout[i] = 0; /* keep only first one */
460 istat = signal(SIGINT, SIG_IGN);
461 qstat = signal(SIGQUIT, SIG_IGN);
462 /* wait for child to exit */
463 while ((wpid = wait(&waitstatus)) != pid && wpid != -1)
465 signal(SIGINT, istat);
466 signal(SIGQUIT, qstat);
467 strcpy(name, echout);
471 close(pfd[0]); /* close pipe */
479 #if MSC | MSDOS | __WATCOMC__
481 char prefix[100], *env, *getenv();
483 /* Home directory reference? */
484 if (*name == '~' && (env=getenv("HOME"))) {
485 strcpy(prefix, env); /* copy HOME directory name */
486 name++; /* skip over ~ in name */
487 strcat(prefix, name);
488 name--; /* get back to beginning */
489 strcpy(name, prefix); /* move back into name */