]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bsys.c
- Add user supplied patch to add inet_aton() of old Solaris
[bacula/bacula] / bacula / src / lib / bsys.c
index bc5ae5834bd5d900a07e0b84eb20705cc3313700..088faad840b893ee94646517b671ce1f21977193 100644 (file)
@@ -45,6 +45,16 @@ char *bstrncpy(char *dest, const char *src, int maxlen)
    return dest;
 }
 
+/*
+ * Guarantee that the string is properly terminated */
+char *bstrncpy(char *dest, POOL_MEM &src, int maxlen)
+{
+   strncpy(dest, src.c_str(), maxlen-1);
+   dest[maxlen-1] = 0;
+   return dest;
+}
+
+
 char *bstrncat(char *dest, const char *src, int maxlen)
 {
    strncat(dest, src, maxlen-1);
@@ -52,6 +62,14 @@ char *bstrncat(char *dest, const char *src, int maxlen)
    return dest;
 }
 
+char *bstrncat(char *dest, POOL_MEM &src, int maxlen)
+{
+   strncat(dest, src.c_str(), maxlen-1);
+   dest[maxlen-1] = 0;
+   return dest;
+}
+
+
 
 #ifndef DEBUG
 void *bmalloc(size_t size)
@@ -132,15 +150,16 @@ int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap)
 
 #else
 
-   int len;
+   int len, buflen;
    char *buf;
-   buf = get_memory(BIG_BUF);
+   buflen = size > BIG_BUF ? size : BIG_BUF;
+   buf = get_memory(buflen);
    len = vsprintf(buf, format, ap);
-   if (len >= BIG_BUF) {
+   if (len >= buflen) {
       Emsg0(M_ABORT, 0, _("Buffer overflow.\n"));
    }
-   memcpy(str, buf, size);
-   str[size-1] = 0;
+   memcpy(str, buf, len);
+   str[len] = 0;               /* len excludes the null */
    free_memory(buf);
    return len;
 #endif
@@ -151,14 +170,16 @@ int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap)
 struct tm *localtime_r(const time_t *timep, struct tm *tm)
 {
     static pthread_mutex_t mutex;
-    static int first = 1;
-    struct tm *ltm;
+    static bool first = true;
+    struct tm *ltm
 
     if (first) {
        pthread_mutex_init(&mutex, NULL);
-       first = 0;
+       first = false;
     }
+
     P(mutex);
+
     ltm = localtime(timep);
     if (ltm) {
        memcpy(tm, ltm, sizeof(struct tm));
@@ -291,10 +312,10 @@ void create_pid_file(char *dir, const char *progname, int port)
    struct stat statp;
 
    Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
-   if (stat(mp_chr(fname), &statp) == 0) {
+   if (stat(fname, &statp) == 0) {
       /* File exists, see what we have */
       *pidbuf = 0;
-      if ((pidfd = open(mp_chr(fname), O_RDONLY|O_BINARY, 0)) < 0 || 
+      if ((pidfd = open(fname, O_RDONLY|O_BINARY, 0)) < 0 || 
           read(pidfd, &pidbuf, sizeof(pidbuf)) < 0 ||
            sscanf(pidbuf, "%d", &oldpid) != 1) {
          Emsg2(M_ERROR_TERM, 0, _("Cannot open pid file. %s ERR=%s\n"), fname, strerror(errno));
@@ -305,10 +326,10 @@ void create_pid_file(char *dir, const char *progname, int port)
               progname, oldpid, fname);
       }
       /* He is not alive, so take over file ownership */
-      unlink(mp_chr(fname));                 /* remove stale pid file */
+      unlink(fname);                 /* remove stale pid file */
    }
    /* Create new pid file */
-   if ((pidfd = open(mp_chr(fname), O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0640)) >= 0) {
+   if ((pidfd = open(fname, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0640)) >= 0) {
       len = sprintf(pidbuf, "%d\n", (int)getpid());
       write(pidfd, pidbuf, len);
       close(pidfd);
@@ -335,7 +356,7 @@ int delete_pid_file(char *dir, const char *progname, int port)
    }
    del_pid_file_ok = FALSE;
    Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
-   unlink(mp_chr(fname));
+   unlink(fname);
    free_pool_memory(fname);
 #endif
    return 1;
@@ -368,7 +389,7 @@ void read_state_file(char *dir, const char *progname, int port)
    Mmsg(&fname, "%s/%s.%d.state", dir, progname, port);
    /* If file exists, see what we have */
 // Dmsg1(10, "O_BINARY=%d\n", O_BINARY);
-   if ((sfd = open(mp_chr(fname), O_RDONLY|O_BINARY, 0)) < 0) {
+   if ((sfd = open(fname, O_RDONLY|O_BINARY, 0)) < 0) {
       Dmsg3(010, "Could not open state file. sfd=%d size=%d: ERR=%s\n", 
                    sfd, sizeof(hdr), strerror(errno));
           goto bail_out;
@@ -406,7 +427,7 @@ void write_state_file(char *dir, const char *progname, int port)
 
    Mmsg(&fname, "%s/%s.%d.state", dir, progname, port);
    /* Create new state file */
-   if ((sfd = open(mp_chr(fname), O_CREAT|O_WRONLY|O_BINARY, 0640)) < 0) {
+   if ((sfd = open(fname, O_CREAT|O_WRONLY|O_BINARY, 0640)) < 0) {
       Dmsg2(000, _("Could not create state file. %s ERR=%s\n"), fname, strerror(errno));
       Emsg2(M_ERROR, 0, _("Could not create state file. %s ERR=%s\n"), fname, strerror(errno));
       goto bail_out;
@@ -513,8 +534,9 @@ int bmicrosleep(time_t sec, long usec)
    P(timer_mutex);
    stat = pthread_cond_timedwait(&timer, &timer_mutex, &timeout);
    if (stat != 0) {
+      berrno be;
       Dmsg2(200, "pthread_cond_timedwait stat=%d ERR=%s\n", stat,
-        strerror(stat));
+        be.strerror(stat));
    }
    V(timer_mutex);
    return stat;