]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/util.c
Add new hash table class
[bacula/bacula] / bacula / src / lib / util.c
index 78fc37c143a2cfe32c851f5e8b14a80fe3ceccba..0ef5a59c29f8f552ebf47d0005448416694a388d 100644 (file)
@@ -95,96 +95,6 @@ unbash_spaces(char *str)
    }
 }
 
-/* Strip any trailing junk from the command */
-void strip_trailing_junk(char *cmd)
-{
-   char *p;
-   p = cmd + strlen(cmd) - 1;
-
-   /* strip trailing junk from command */
-   while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
-      *p-- = 0;
-}
-
-/* Strip any trailing slashes from a directory path */
-void strip_trailing_slashes(char *dir)
-{
-   char *p;
-   p = dir + strlen(dir) - 1;
-
-   /* strip trailing slashes */
-   while ((p >= dir) && (*p == '/'))
-      *p-- = 0;
-}
-
-/*
- * Skip spaces
- *  Returns: 0 on failure (EOF)            
- *          1 on success
- *          new address in passed parameter 
- */
-int skip_spaces(char **msg)
-{
-   char *p = *msg;
-   if (!p) {
-      return 0;
-   }
-   while (*p && *p == ' ') {
-      p++;
-   }
-   *msg = p;
-   return *p ? 1 : 0;
-}
-
-/*
- * Skip nonspaces
- *  Returns: 0 on failure (EOF)            
- *          1 on success
- *          new address in passed parameter 
- */
-int skip_nonspaces(char **msg)
-{
-   char *p = *msg;
-
-   if (!p) {
-      return 0;
-   }
-   while (*p && *p != ' ') {
-      p++;
-   }
-   *msg = p;
-   return *p ? 1 : 0;
-}
-
-/* folded search for string - case insensitive */
-int
-fstrsch(char *a, char *b)   /* folded case search */
-{
-   register char *s1,*s2;
-   register char c1, c2;
-
-   s1=a;
-   s2=b;
-   while (*s1) {                     /* do it the fast way */
-      if ((*s1++ | 0x20) != (*s2++ | 0x20))
-        return 0;                    /* failed */
-   }
-   while (*a) {                      /* do it over the correct slow way */
-      if (B_ISUPPER(c1 = *a)) {
-        c1 = tolower((int)c1);
-      }
-      if (B_ISUPPER(c2 = *b)) {
-        c2 = tolower((int)c2);
-      }
-      if (c1 != c2) {
-        return 0;
-      }
-      a++;
-      b++;
-   }
-   return 1;
-}
-
 
 char *encode_time(time_t time, char *buf)
 {
@@ -324,6 +234,8 @@ char *job_level_to_str(int level)
    char *str;
 
    switch (level) {
+   case L_BASE:
+      str = _("Base");
    case L_FULL:
       str = _("Full");
       break;
@@ -386,7 +298,7 @@ char *encode_mode(mode_t mode, char *buf)
 }
 
 
-int do_shell_expansion(char *name)
+int do_shell_expansion(char *name, int name_len)
 {
 /*  ****FIXME***** this should work for Win32 too */
 #define UNIX
@@ -438,10 +350,9 @@ int do_shell_expansion(char *name)
           if ((shellcmd = getenv("SHELL")) == NULL) {
              shellcmd = "/bin/sh";
          }
-         close(1); dup(pfd[1]);          /* attach pipes to stdin and stdout */
-         close(2); dup(pfd[1]);
-         for (i = 3; i < 32; i++)        /* close everything else */
-            close(i);
+         close(pfd[0]);                  /* close stdin */
+         dup2(pfd[1], 1);                /* attach to stdout */
+         dup2(pfd[1], 2);                /* and stderr */
           strcpy(echout, "echo ");        /* form echo command */
          bstrncat(echout, name, sizeof(echout));
           execl(shellcmd, shellcmd, "-c", echout, NULL); /* give to shell */
@@ -450,7 +361,10 @@ int do_shell_expansion(char *name)
        default:                          /* parent */
          /* read output from child */
          echout[0] = 0;
-         i = read(pfd[0], echout, sizeof echout);
+         do {
+            i = read(pfd[0], echout, sizeof echout);
+         } while (i == -1 && errno == EINTR); 
+
          if (i > 0) {
             echout[--i] = 0;                /* set end of string */
             /* look for first line. */
@@ -463,7 +377,10 @@ int do_shell_expansion(char *name)
          /* wait for child to exit */
          while ((wpid = wait(&waitstatus)) != pid && wpid != -1)
             { ; }
-         strcpy(name, echout);
+         strip_trailing_junk(echout);
+         if (strlen(echout) > 0) {
+            bstrncpy(name, echout, name_len);
+         }
          stat = 1;
          break;
        }
@@ -500,7 +417,7 @@ int do_shell_expansion(char *name)
 
     from SpeakFreely by John Walker */
 
-void makeSessionKey(char *key, char *seed, int mode)
+void make_session_key(char *key, char *seed, int mode)
 {
      int j, k;
      struct MD5Context md5c;
@@ -584,11 +501,10 @@ void makeSessionKey(char *key, char *seed, int mode)
  *  to = recepients list 
  *
  */
-POOLMEM *edit_job_codes(void *mjcr, char *omsg, char *imsg, char *to)  
+POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)   
 {
    char *p, *str;
    char add[20];
-   JCR *jcr = (JCR *)mjcr;
 
    *omsg = 0;
    Dmsg1(200, "edit_job_codes: %s\n", imsg);
@@ -611,7 +527,7 @@ POOLMEM *edit_job_codes(void *mjcr, char *omsg, char *imsg, char *to)
            str = job_status_to_str(jcr->JobStatus); 
            break;
          case 'i':
-            sprintf(add, "%d", jcr->JobId);
+            bsnprintf(add, sizeof(add), "%d", jcr->JobId);
            str = add;
            break;
          case 'j':                    /* Job name */
@@ -645,119 +561,20 @@ POOLMEM *edit_job_codes(void *mjcr, char *omsg, char *imsg, char *to)
    return omsg;
 }
 
-/* 
- * Return next argument from command line.  Note, this
- * routine is destructive.
- */
-char *next_arg(char **s)
-{
-   char *p, *q, *n;
-   int in_quote = 0;
-
-   /* skip past spaces to next arg */
-   for (p=*s; *p && *p == ' '; ) {
-      p++;
-   }   
-   Dmsg1(400, "Next arg=%s\n", p);
-   for (n = q = p; *p ; ) {
-      if (*p == '\\') {
-        p++;
-        if (*p) {
-           *q++ = *p++;
-        } else {
-           *q++ = *p;
-        }
-        continue;
-      }
-      if (*p == '"') {                  /* start or end of quote */
-        if (in_quote) {
-           p++;                        /* skip quote */
-           in_quote = 0;
-           continue;
-        }
-        in_quote = 1;
-        p++;
-        continue;
-      }
-      if (!in_quote && *p == ' ') {     /* end of field */
-        p++;
-        break;
-      }
-      *q++ = *p++;
-   }
-   *q = 0;
-   *s = p;
-   Dmsg2(400, "End arg=%s next=%s\n", n, p);
-   return n;
-}   
-
-/*
- * This routine parses the input command line.
- * It makes a copy in args, then builds an
- *  argc, argv like list where
- *    
- *  argc = count of arguments
- *  argk[i] = argument keyword (part preceding =)
- *  argv[i] = argument value (part after =)
- *
- *  example:  arg1 arg2=abc arg3=
- *
- *  argc = c
- *  argk[0] = arg1
- *  argv[0] = NULL
- *  argk[1] = arg2
- *  argv[1] = abc
- *  argk[2] = arg3
- *  argv[2] = 
- */
-
-void parse_command_args(POOLMEM *cmd, POOLMEM *args, int *argc, 
-                       char **argk, char **argv) 
+void set_working_directory(char *wd)
 {
-   char *p, *q, *n;
-   int len;
+   struct stat stat_buf; 
 
-   len = strlen(cmd) + 1;
-   args = check_pool_memory_size(args, len);
-   bstrncpy(args, cmd, len);
-   strip_trailing_junk(args);
-   *argc = 0;
-   p = args;
-   /* Pick up all arguments */
-   while (*argc < MAX_CMD_ARGS) {
-      n = next_arg(&p);   
-      if (*n) {
-        argk[*argc] = n;
-        argv[(*argc)++] = NULL;
-      } else {
-        break;
-      }
+   if (wd == NULL) {
+      Emsg0(M_ERROR_TERM, 0, _("Working directory not defined. Cannot continue.\n"));
    }
-   /* Separate keyword and value */
-   for (int i=0; i < *argc; i++) {
-      p = strchr(argk[i], '=');
-      if (p) {
-        *p++ = 0;                    /* terminate keyword and point to value */
-        /* Unquote quoted values */
-         if (*p == '"') {
-            for (n = q = ++p; *p && *p != '"'; ) {
-               if (*p == '\\') {
-                 p++;
-              }
-              *q++ = *p++;
-           }
-           *q = 0;                   /* terminate string */
-           p = n;                    /* point to string */
-        }
-        if (strlen(p) > MAX_NAME_LENGTH-1) {
-           p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
-        }
-      }
-      argv[i] = p;                   /* save ptr to value or NULL */
+   if (stat(wd, &stat_buf) != 0) {
+      Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" not found. Cannot continue.\n"),
+        wd);
    }
-#ifdef xxxx
-   for (i=0; i<argc; i++) {
-      Dmsg3(000, "Arg %d: kw=%s val=%s\n", i, argk[i], argv[i]?argv[i]:"NULL");
+   if (!S_ISDIR(stat_buf.st_mode)) {
+      Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" is not a directory. Cannot continue.\n"),
+        wd);
    }
-#endif
+   working_directory = wd;           /* set global */
 }