]> 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 6156a8cc2bd2bd9a43f313af1db96743ca1a9abf..0ef5a59c29f8f552ebf47d0005448416694a388d 100644 (file)
  *
  */
 
-/*
- * Convert a string to btime_t (64 bit seconds)
- * Returns 0: if error
-          1: if OK, and value stored in value
- */
-int string_to_btime(char *str, btime_t *value)
+/* Return true of buffer has all zero bytes */
+int is_buf_zero(char *buf, int len)
 {
-   int i, ch, len;
-   double val;
-   static int  mod[] = {'*', 's', 'n', 'h', 'd', 'w', 'm', 'q', 'y', 0};
-   static int mult[] = {1,    1,  60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 
-                 60*60*24*91, 60*60*24*365};
-
-   /* Look for modifier */
-   len = strlen(str);
-   ch = str[len - 1];
-   i = 0;
-   if (ISALPHA(ch)) {
-      if (ISUPPER(ch)) {
-        ch = tolower(ch);
-      }
-      while (mod[++i] != 0) {
-        if (ch == mod[i]) {
-           len--;
-           str[len] = 0; /* strip modifier */
-           break;
-        }
-      }
-   }
-   if (mod[i] == 0 || !is_a_number(str)) {
-      return 0;
-   }
-   val = strtod(str, NULL);
-   if (errno != 0 || val < 0) {
-      return 0;
-   }
-   *value = (btime_t)(val * mult[i]);
-   return 1;
-
-}
+   uint64_t *ip = (uint64_t *)buf;
+   char *p;
+   int i, len64, done, rem;
 
-char *edit_btime(btime_t val, char *buf)
-{
-   char mybuf[30];
-   static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
-   static char *mod[]  = {"year",  "month",  "day", "hour", "min"};
-   int i;
-   uint32_t times;
-
-   *buf = 0;
-   for (i=0; i<5; i++) {
-      times = val / mult[i];
-      if (times > 0) {
-        val = val - (btime_t)times * mult[i];
-         sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
-        strcat(buf, mybuf);
+   /* Optimize by checking uint64_t for zero */
+   len64 = len >> sizeof(uint64_t);
+   for (i=0; i < len64; i++) {
+      if (ip[i] != 0) {
+        return 0;
       }
    }
-   if (val == 0 && strlen(buf) == 0) {    
-      strcat(buf, "0 secs");
-   } else if (val != 0) {
-      sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
-      strcat(buf, mybuf);
-   }
-   return buf;
-}
-
-/*
- * Check if specified string is a number or not.
- *  Taken from SQLite, cool, thanks.
- */
-int is_a_number(const char *n)
-{
-   int digit_seen = 0;
-
-   if( *n == '-' || *n == '+' ) {
-      n++;
-   }
-   while (ISDIGIT(*n)) {
-      digit_seen = 1;
-      n++;
-   }
-   if (digit_seen && *n == '.') {
-      n++;
-      while (ISDIGIT(*n)) { n++; }
-   }
-   if (digit_seen && (*n == 'e' || *n == 'E')
-       && (ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && ISDIGIT(n[2])))) {
-      n += 2;                        /* skip e- or e+ or e digit */
-      while (ISDIGIT(*n)) { n++; }
-   }
-   return digit_seen && *n==0;
-}
-
-
-/*
- * Edit an integer number with commas, the supplied buffer
- * must be at least 27 bytes long.  The incoming number
- * is always widened to 64 bits.
- */
-char *edit_uint64_with_commas(uint64_t val, char *buf)
-{
-   sprintf(buf, "%" lld, val);
-   return add_commas(buf, buf);
-}
-
-/*
- * Edit an integer number, the supplied buffer
- * must be at least 27 bytes long.  The incoming number
- * is always widened to 64 bits.
- */
-char *edit_uint64(uint64_t val, char *buf)
-{
-   sprintf(buf, "%" lld, val);
-   return buf;
-}
-
-
-/*
- * Add commas to a string, which is presumably
- * a number.  
- */
-char *add_commas(char *val, char *buf)
-{
-   int len, nc;
-   char *p, *q;
-   int i;
-
-   if (val != buf) {
-      strcpy(buf, val);
-   }
-   len = strlen(buf);
-   if (len < 1) {
-      len = 1;
-   }
-   nc = (len - 1) / 3;
-   p = buf+len;
-   q = p + nc;
-   *q-- = *p--;
-   for ( ; nc; nc--) {
-      for (i=0; i < 3; i++) {
-         *q-- = *p--;
+   done = len64 << sizeof(uint64_t);  /* bytes already checked */
+   p = buf + done;
+   rem = len - done;
+   for (i = 0; i < rem; i++) {
+      if (p[i] != 0) {
+        return 0;
       }
-      *q-- = ',';
-   }   
-   return buf;
+   }
+   return 1;
 }
 
 
@@ -188,7 +65,7 @@ char *add_commas(char *val, char *buf)
 void lcase(char *str)
 {
    while (*str) {
-      if (ISUPPER(*str))
+      if (B_ISUPPER(*str))
         *str = tolower((int)(*str));
        str++;
    }
@@ -218,101 +95,11 @@ 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 (ISUPPER(c1 = *a)) {
-        c1 = tolower((int)c1);
-      }
-      if (ISUPPER(c2 = *b)) {
-        c2 = tolower((int)c2);
-      }
-      if (c1 != c2) {
-        return 0;
-      }
-      a++;
-      b++;
-   }
-   return 1;
-}
-
 
 char *encode_time(time_t time, char *buf)
 {
    struct tm tm;
-   int n;
+   int n = 0;
 
    if (localtime_r(&time, &tm)) {
       n = sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
@@ -323,22 +110,27 @@ char *encode_time(time_t time, char *buf)
 }
 
 /*
- * Concatenate a string (str) onto a poolmem message (msg)
- *  return new message pointer. The base of the pool memory
- *  is base.
+ * Concatenate a string (str) onto a pool memory buffer pm
  */
-void add_str_to_pool_mem(POOLMEM **base, char **msg, char *str)
+void pm_strcat(POOLMEM **pm, char *str)
 {
+   int pmlen = strlen(*pm);
    int len = strlen(str) + 1;
-   char *b, *m;
 
-   b = *base;
-   *base = check_pool_memory_size(*base, len);
-   m = *base - b + *msg;
-   while (*str) {
-      *m++ = *str++;
-   }
-   *msg = m;
+   *pm = check_pool_memory_size(*pm, pmlen + len);
+   memcpy(*pm+pmlen, str, len);
+}
+
+
+/*
+ * Copy a string (str) into a pool memory buffer pm
+ */
+void pm_strcpy(POOLMEM **pm, char *str)
+{
+   int len = strlen(str) + 1;
+
+   *pm = check_pool_memory_size(*pm, len);
+   memcpy(*pm, str, len);
 }
 
 
@@ -360,8 +152,8 @@ void jobstatus_to_ascii(int JobStatus, char *msg, int maxlen)
      case JS_Error:
          termstat = _("Non-fatal error");
         break;
-     case JS_Cancelled:
-         termstat = _("Cancelled");
+     case JS_Canceled:
+         termstat = _("Canceled");
         break;
      case JS_Differences:
          termstat = _("Verify differences");
@@ -372,8 +164,7 @@ void jobstatus_to_ascii(int JobStatus, char *msg, int maxlen)
         termstat = jstat;
         break;
    }
-   strncpy(msg, termstat, maxlen);
-   msg[maxlen-1] = 0;
+   bstrncpy(msg, termstat, maxlen);
 }
 
 /*
@@ -385,23 +176,23 @@ char *job_status_to_str(int stat)
 
    switch (stat) {
    case JS_Terminated:
-      str = "OK";
+      str = _("OK");
       break;
    case JS_ErrorTerminated:
    case JS_Error:
-      str = "Error";
+      str = _("Error");
       break;
    case JS_FatalError:
-      str = "Fatal Error";
+      str = _("Fatal Error");
       break;
-   case JS_Cancelled:
-      str = "Cancelled";
+   case JS_Canceled:
+      str = _("Canceled");
       break;
    case JS_Differences:
-      str = "Differences";
+      str = _("Differences");
       break;
    default:
-      str = "Unknown term code";
+      str = _("Unknown term code");
       break;
    }
    return str;
@@ -417,18 +208,19 @@ char *job_type_to_str(int type)
 
    switch (type) {
    case JT_BACKUP:
-      str = "Backup";
+      str = _("Backup");
       break;
    case JT_VERIFY:
-      str = "Verify";
+      str = _("Verify");
       break;
    case JT_RESTORE:
-      str = "Restore";
+      str = _("Restore");
       break;
    case JT_ADMIN:
-      str = "Admin";
+      str = _("Admin");
+      break;
    default:
-      str = "Unknown Job Type";
+      str = _("Unknown Type");
       break;
    }
    return str;
@@ -442,35 +234,34 @@ char *job_level_to_str(int level)
    char *str;
 
    switch (level) {
+   case L_BASE:
+      str = _("Base");
    case L_FULL:
-      str = "full";
+      str = _("Full");
       break;
    case L_INCREMENTAL:
-      str = "incremental";
+      str = _("Incremental");
       break;
    case L_DIFFERENTIAL:
-      str = "differential";
-      break;
-   case L_LEVEL:
-      str = "level";
+      str = _("Differential");
       break;
    case L_SINCE:
-      str = "since";
+      str = _("Since");
       break;
    case L_VERIFY_CATALOG:
-      str = "verify catalog";
+      str = _("Verify Catalog");
       break;
    case L_VERIFY_INIT:
-      str = "verify init";
+      str = _("Verify Init Catalog");
       break;
    case L_VERIFY_VOLUME_TO_CATALOG:
-      str = "verify volume to catalog";
+      str = _("Verify Volume to Catalog");
       break;
    case L_VERIFY_DATA:
-      str = "verify data";
+      str = _("Verify Data");
       break;
    default:
-      str = "Unknown Job level";
+      str = _("Unknown Job Level");
       break;
    }
    return str;
@@ -485,8 +276,8 @@ char *encode_mode(mode_t mode, char *buf)
 {
   char *cp = buf;  
 
-  *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' :
-          S_ISLNK(mode) ? 'l' : '-';
+  *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode)  ? 'b' : S_ISCHR(mode)  ? 'c' :
+          S_ISLNK(mode) ? 'l' : S_ISFIFO(mode) ? 'f' : S_ISSOCK(mode) ? 's' : '-';
   *cp++ = mode & S_IRUSR ? 'r' : '-';
   *cp++ = mode & S_IWUSR ? 'w' : '-';
   *cp++ = (mode & S_ISUID
@@ -506,44 +297,8 @@ char *encode_mode(mode_t mode, char *buf)
   return cp;
 }
 
-#ifdef WORKING
-extern char *getuser(uid_t uid);
-extern char *getgroup(gid_t gid);
-
-void print_ls_output(char *fname, char *lname, int type, struct stat *statp)
-{
-   char buf[1000]; 
-   char *p, *f;
-   int n;
-
-   p = encode_mode(statp->st_mode, buf);
-   n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
-   p += n;
-   n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
-   p += n;
-   n = sprintf(p, "%8ld  ", statp->st_size);
-   p += n;
-   p = encode_time(statp->st_ctime, p);
-   *p++ = ' ';
-   *p++ = ' ';
-   for (f=fname; *f; )
-      *p++ = *f++;
-   if (type == FT_LNK) {
-      *p++ = ' ';
-      *p++ = '-';
-      *p++ = '>';
-      *p++ = ' ';
-      /* Copy link name */
-      for (f=lname; *f; )
-        *p++ = *f++;
-   }
-   *p++ = '\n';
-   *p = 0;
-   fputs(buf, stdout);
-}
-#endif
 
-int do_shell_expansion(char *name)
+int do_shell_expansion(char *name, int name_len)
 {
 /*  ****FIXME***** this should work for Win32 too */
 #define UNIX
@@ -555,7 +310,6 @@ int do_shell_expansion(char *name)
    int pid, wpid, stat;
    int waitstatus;
    char *shellcmd;
-   void (*istat)(int), (*qstat)(int);
    int i;
    char echout[PATH_MAX + 256];
    int pfd[2];
@@ -593,34 +347,40 @@ int do_shell_expansion(char *name)
 
        case 0:                           /* child */
          /* look for shell */
-          if ((shellcmd = getenv("SHELL")) == NULL)
+          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 */
-         strcat(echout, name);
+         bstrncat(echout, name, sizeof(echout));
           execl(shellcmd, shellcmd, "-c", echout, NULL); /* give to shell */
           exit(127);                      /* shouldn't get here */
 
        default:                          /* parent */
          /* read output from child */
-         i = read(pfd[0], echout, sizeof echout);
-         echout[--i] = 0;                /* set end of string */
-         /* look for first word or first line. */
-         while (--i >= 0) {
-             if (echout[i] == ' ' || echout[i] == '\n')
-               echout[i] = 0;            /* keep only first one */
+         echout[0] = 0;
+         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. */
+            while (--i >= 0) {
+                if (echout[i] == '\n') {
+                  echout[i] = 0;            /* keep only first one */
+               }
+            }
          }
-         istat = signal(SIGINT, SIG_IGN);
-         qstat = signal(SIGQUIT, SIG_IGN);
          /* wait for child to exit */
          while ((wpid = wait(&waitstatus)) != pid && wpid != -1)
             { ; }
-         signal(SIGINT, istat);
-         signal(SIGQUIT, qstat);
-         strcpy(name, echout);
+         strip_trailing_junk(echout);
+         if (strlen(echout) > 0) {
+            bstrncpy(name, echout, name_len);
+         }
          stat = 1;
          break;
        }
@@ -649,146 +409,172 @@ int do_shell_expansion(char *name)
 
 }
 
-#define MAX_ARGV 100
-static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_arg);
 
-/*
- * Run an external program. Optionally wait a specified number
- *   of seconds. Program killed if wait exceeded. Optionally
- *   return the output from the program (normally a single line).
- */
-int run_program(char *prog, int wait, POOLMEM *results)
-{
-   int stat = ETIME;
-   int chldstatus = 0;
-   pid_t pid1, pid2;
-   int pfd[2];
-   char *bargv[MAX_ARGV];
-   int bargc;
+/*  MAKESESSIONKEY  -- Generate session key with optional start
+                       key.  If mode is TRUE, the key will be
+                       translated to a string, otherwise it is
+                       returned as 16 binary bytes.
 
-   
-   build_argc_argv(prog, &bargc, bargv, MAX_ARGV);
-#ifdef xxxxxxxxxx
-   printf("argc=%d\n", bargc);
-   int i;
-   for (i=0; i<bargc; i++) {
-      printf("argc=%d argv=%s\n", i, bargv[i]);
-   }
+    from SpeakFreely by John Walker */
+
+void make_session_key(char *key, char *seed, int mode)
+{
+     int j, k;
+     struct MD5Context md5c;
+     unsigned char md5key[16], md5key1[16];
+     char s[1024];
+
+     s[0] = 0;
+     if (seed != NULL) {
+       strcat(s, seed);
+     }
+
+     /* The following creates a seed for the session key generator
+       based on a collection of volatile and environment-specific
+       information unlikely to be vulnerable (as a whole) to an
+        exhaustive search attack.  If one of these items isn't
+       available on your machine, replace it with something
+       equivalent or, if you like, just delete it. */
+
+     sprintf(s + strlen(s), "%lu", (unsigned long) getpid());
+     sprintf(s + strlen(s), "%lu", (unsigned long) getppid());
+     getcwd(s + strlen(s), 256);
+     sprintf(s + strlen(s), "%lu", (unsigned long) clock());
+     sprintf(s + strlen(s), "%lu", (unsigned long) time(NULL));
+#ifdef Solaris
+     sysinfo(SI_HW_SERIAL,s + strlen(s), 12);
 #endif
+#ifdef HAVE_GETHOSTID
+     sprintf(s + strlen(s), "%lu", (unsigned long) gethostid());
+#endif
+#ifdef HAVE_GETDOMAINNAME
+     getdomainname(s + strlen(s), 256);
+#endif
+     gethostname(s + strlen(s), 256);
+     sprintf(s + strlen(s), "%u", (unsigned)getuid());
+     sprintf(s + strlen(s), "%u", (unsigned)getgid());
+     MD5Init(&md5c);
+     MD5Update(&md5c, (unsigned char *)s, strlen(s));
+     MD5Final(md5key, &md5c);
+     sprintf(s + strlen(s), "%lu", (unsigned long) ((time(NULL) + 65121) ^ 0x375F));
+     MD5Init(&md5c);
+     MD5Update(&md5c, (unsigned char *)s, strlen(s));
+     MD5Final(md5key1, &md5c);
+#define nextrand    (md5key[j] ^ md5key1[j])
+     if (mode) {
+       for (j = k = 0; j < 16; j++) {
+           unsigned char rb = nextrand;
+
+#define Rad16(x) ((x) + 'A')
+           key[k++] = Rad16((rb >> 4) & 0xF);
+           key[k++] = Rad16(rb & 0xF);
+#undef Rad16
+           if (j & 1) {
+                 key[k++] = '-';
+           }
+       }
+       key[--k] = 0;
+     } else {
+       for (j = 0; j < 16; j++) {
+           key[j] = nextrand;
+       }
+     }
+}
+#undef nextrand
 
-   if (results && pipe(pfd) == -1) {
-      return errno;
-   }
-   /* Start worker process */
-   switch (pid1 = fork()) {
-   case -1:
-      break;
 
-   case 0:                           /* child */
-//    printf("execl of %s\n", prog);
-      if (results) {
-        close(1); dup(pfd[1]);       /* attach pipes to stdin and stdout */
-        close(2); dup(pfd[1]);
-      }
-      execvp(bargv[0], bargv);
-      exit(errno);                   /* shouldn't get here */
-
-   default:                          /* parent */
-      /* start timer process */
-      if (wait > 0) {
-        switch (pid2=fork()) {
-        case -1:
+
+/*
+ * Edit job codes into main command line
+ *  %% = %
+ *  %j = Job name
+ *  %t = Job type (Backup, ...)
+ *  %e = Job Exit code
+ *  %i = JobId
+ *  %l = job level
+ *  %c = Client's name
+ *  %r = Recipients
+ *  %d = Director's name
+ *
+ *  omsg = edited output message
+ *  imsg = input string containing edit codes (%x)
+ *  to = recepients list 
+ *
+ */
+POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)   
+{
+   char *p, *str;
+   char add[20];
+
+   *omsg = 0;
+   Dmsg1(200, "edit_job_codes: %s\n", imsg);
+   for (p=imsg; *p; p++) {
+      if (*p == '%') {
+        switch (*++p) {
+         case '%':
+            str = "%";
            break;
-        case 0:                         /* child 2 */
-           /* Time the worker process */  
-           sleep(wait);
-           if (kill(pid1, SIGTERM) == 0) { /* time expired kill it */
-              exit(0);
+         case 'c':
+           str = jcr->client_name;
+           if (!str) {
+               str = "";
            }
-           sleep(3);
-           kill(pid1, SIGKILL);
-           exit(0);
-        default:                        /* parent */
            break;
-        }
-      }
-
-      /* Parent continues here */
-      int i;
-      if (results) {
-        i = read(pfd[0], results, sizeof_pool_memory(results) - 1);
-        if (--i < 0) {
-           i = 0;
-        }
-        results[i] = 0;                /* set end of string */
-      }
-      /* wait for worker child to exit */
-      for ( ;; ) {
-        pid_t wpid;
-        wpid = waitpid(pid1, &chldstatus, 0);         
-        if (wpid == pid1 || (errno != EINTR)) {
+         case 'd':
+            str = my_name;            /* Director's name */
+           break;
+         case 'e':
+           str = job_status_to_str(jcr->JobStatus); 
+           break;
+         case 'i':
+            bsnprintf(add, sizeof(add), "%d", jcr->JobId);
+           str = add;
+           break;
+         case 'j':                    /* Job name */
+           str = jcr->Job;
+           break;
+         case 'l':
+           str = job_level_to_str(jcr->JobLevel);
+           break;
+         case 'r':
+           str = to;
+           break;
+         case 't':
+           str = job_type_to_str(jcr->JobType);
+           break;
+        default:
+            add[0] = '%';
+           add[1] = *p;
+           add[2] = 0;
+           str = add;
            break;
         }
+      } else {
+        add[0] = *p;
+        add[1] = 0;
+        str = add;
       }
-      if (WIFEXITED(chldstatus))
-        stat = WEXITSTATUS(chldstatus);
-
-      if (wait > 0) {
-        kill(pid2, SIGKILL);           /* kill off timer process */
-        waitpid(pid2, &chldstatus, 0); /* reap timer process */
-      }
-      if (results) { 
-        close(pfd[0]);              /* close pipe */
-        close(pfd[1]);
-      }
-      break;
+      Dmsg1(1200, "add_str %s\n", str);
+      pm_strcat(&omsg, str);
+      Dmsg1(1200, "omsg=%s\n", omsg);
    }
-   return stat;
+   return omsg;
 }
 
-/*
- * Build argc and argv from a string
- */
-static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
+void set_working_directory(char *wd)
 {
-   int i, quote;
-   char *p, *q;
-   int argc = 0;
-
-   argc = 0;
-   for (i=0; i<max_argv; i++)
-      bargv[i] = NULL;
-
-   p = cmd;
-   quote = 0;
-   while  (*p && (*p == ' ' || *p == '\t'))
-      p++;
-   if (*p == '\"') {
-      quote = 1;
-      p++;
+   struct stat stat_buf; 
+
+   if (wd == NULL) {
+      Emsg0(M_ERROR_TERM, 0, _("Working directory not defined. Cannot continue.\n"));
    }
-   if (*p) {
-      while (*p && argc < MAX_ARGV) {
-        q = p;
-        if (quote) {
-            while (*q && *q != '\"')
-           q++;
-           quote = 0;
-        } else {
-            while (*q && *q != ' ')
-           q++;
-        }
-        if (*q)
-            *(q++) = '\0';
-        bargv[argc++] = p;
-        p = q;
-         while (*p && (*p == ' ' || *p == '\t'))
-           p++;
-         if (*p == '\"') {
-           quote = 1;
-           p++;
-        }
-      }
+   if (stat(wd, &stat_buf) != 0) {
+      Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" not found. Cannot continue.\n"),
+        wd);
+   }
+   if (!S_ISDIR(stat_buf.st_mode)) {
+      Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" is not a directory. Cannot continue.\n"),
+        wd);
    }
-   *bargc = argc;
+   working_directory = wd;           /* set global */
 }