I've updated alias.c and util.c to remove the fts_* functions
and put in a new get_du() function.  Also, I've implemented
the "scandir()" function and called it "bkscandir()".  I also
fixed an obvious memory leak in the alias functions that used scandir().

Going through the code it appears there's lots of memory leaks in there...

This should allow all POSIX compatible systems to run
the latest version.

Finally...  I can add the vlimits() stuff in.

Thanks,

Brian
Only in qmailadmin-1.0.8: Makefile
diff -c qmailadmin-1.0.8.orig/alias.c qmailadmin-1.0.8/alias.c
*** qmailadmin-1.0.8.orig/alias.c       Wed Jan 22 00:46:04 2003
--- qmailadmin-1.0.8/alias.c    Thu Jan 23 23:10:11 2003
***************
*** 31,36 ****
--- 31,40 ----
  #include "qmailadminx.h"
  
  char* dotqmail_alias_command(char* command);
+ int bkscandir(const char *dirname,
+               struct dirent ***namelist,
+             int (*select)(struct dirent *),
+             int (*compar)(const void *, const void *));
  
  int show_aliases(void)
  {
***************
*** 83,89 ****
      return(0);
    }
  
!   n = scandir(".", &namelist, 0, qa_sort);
    
    for (m=0; m<n; m++) {
      mydirent=namelist[m];
--- 87,93 ----
      return(0);
    }
  
!   n = bkscandir(".", &namelist, 0, qa_sort);
    
    for (m=0; m<n; m++) {
      mydirent=namelist[m];
***************
*** 172,181 ****
        fclose(fs);
        k++;
      }
-     free(namelist[m]);
    }
-     
    closedir(mydir);
    free(namelist);
  
    if (AdminType == DOMAIN_ADMIN) {
--- 176,186 ----
        fclose(fs);
        k++;
      }
    }
    closedir(mydir);
+   /* bk: fix memory leak */
+   for (m=0; m<n; m++)
+     free(namelist[m]);
    free(namelist);
  
    if (AdminType == DOMAIN_ADMIN) {
***************
*** 673,676 ****
--- 678,738 ----
      /* otherwise just report nothing */
      return(NULL);
    }
+ }
+ 
+ /*
+  * Brian Kolaci
+  * quick implementation of the scandir() BSD function
+  */
+ int bkscandir(const char *dirname,
+               struct dirent ***namelist,
+             int (*select)(struct dirent *),
+             int (*compar)(const void *, const void *))
+ {
+   int i;
+   int entries;
+   int esize;
+   struct dirent* dp;
+   struct dirent* dent;
+   DIR * dirp;
+ 
+   *namelist = NULL;
+   entries = esize = 0;
+ 
+   /* load the names */
+   if ((dirp = opendir(dirname)) == NULL)
+     return -1;
+ 
+   while ((dp = readdir(dirp)) != NULL) {
+     if (select == NULL || (*select)(dp)) {
+       if (entries >= esize) {
+         void* mem;
+         esize += 10;
+         if ((mem = realloc(*namelist, esize * sizeof(struct dirent*))) == NULL) {
+           for (i = 0; i < entries; i++)
+             free((*namelist)[i]);
+           free(*namelist);
+           closedir(dirp);
+           return -1;
+         }
+         *namelist = (struct dirent**)mem;
+       }
+       if ((dent = (struct dirent*)malloc(sizeof(struct dirent)+MAX_FILE_NAME)) == 
+NULL) {
+         for (i = 0; i < entries; i++)
+           free((*namelist)[i]);
+         free(*namelist);
+         closedir(dirp);
+         return -1;
+       }
+       memcpy(dent, dp, sizeof(*dp)+MAX_FILE_NAME);
+       (*namelist)[entries] = dent;
+       entries++;
+     }
+   }
+   closedir(dirp);
+ 
+   /* sort them */
+   if (compar)
+     qsort((void*)*namelist, entries, sizeof(struct dirent*), compar);
+   return entries;
  }
Common subdirectories: qmailadmin-1.0.8.orig/autom4te.cache and 
qmailadmin-1.0.8/autom4te.cache
diff -c qmailadmin-1.0.8.orig/cgi.c qmailadmin-1.0.8/cgi.c
*** qmailadmin-1.0.8.orig/cgi.c Tue Aug  6 18:04:59 2002
--- qmailadmin-1.0.8/cgi.c      Thu Jan 23 23:29:27 2003
***************
*** 27,33 ****
  #include "config.h"
  #include "qmailadmin.h"
  #include "qmailadminx.h"
- #include <syslog.h>
  
  get_cgi()
  {
--- 27,32 ----
Only in qmailadmin-1.0.8: config.h
Only in qmailadmin-1.0.8: config.log
Only in qmailadmin-1.0.8: config.status
Only in qmailadmin-1.0.8: doconfig
Common subdirectories: qmailadmin-1.0.8.orig/html and qmailadmin-1.0.8/html
Common subdirectories: qmailadmin-1.0.8.orig/images and qmailadmin-1.0.8/images
diff -c qmailadmin-1.0.8.orig/qmailadmin.h qmailadmin-1.0.8/qmailadmin.h
*** qmailadmin-1.0.8.orig/qmailadmin.h  Fri Oct 25 04:33:42 2002
--- qmailadmin-1.0.8/qmailadmin.h       Thu Jan 23 21:31:45 2003
***************
*** 51,54 ****
  char *get_quota_used(char*);           //jhopper prototype
  int quota_to_bytes(char[], char*);     //jhopper prototype
  int quota_to_megabytes(char[], char*); //jhopper prototype
! double get_du(char*);                  //jhopper prototype
--- 51,54 ----
  char *get_quota_used(char*);           //jhopper prototype
  int quota_to_bytes(char[], char*);     //jhopper prototype
  int quota_to_megabytes(char[], char*); //jhopper prototype
! off_t get_du(const char*);             //bk prototype
Only in qmailadmin-1.0.8: stamp-h1
diff -c qmailadmin-1.0.8.orig/user.c qmailadmin-1.0.8/user.c
*** qmailadmin-1.0.8.orig/user.c        Fri Jan 17 22:11:01 2003
--- qmailadmin-1.0.8/user.c     Thu Jan 23 23:29:34 2003
***************
*** 35,44 ****
  
  #define HOOKS 1
  
- #ifdef DEBUG
- #include <syslog.h>
- #endif
- 
  #ifdef HOOKS
  #define HOOK_ADDUSER 0
  #define HOOK_DELUSER 1
--- 35,40 ----
diff -c qmailadmin-1.0.8.orig/util.c qmailadmin-1.0.8/util.c
*** qmailadmin-1.0.8.orig/util.c        Fri Oct 25 04:33:42 2002
--- qmailadmin-1.0.8/util.c     Thu Jan 23 23:30:11 2003
***************
*** 24,30 ****
  #include <unistd.h>
  #include <pwd.h>
  #include <dirent.h>
- #include <fts.h>
  #include "config.h"
  #include "qmailadmin.h"
  #include "qmailadminx.h"
--- 24,29 ----
***************
*** 259,311 ****
      double size;
  
      size = get_du(dir);
!     if (size != 0) {
          size = size / 1048576;  // Convert to MBs.
      }
!     sprintf(tmpbuff, "%-2.2f", size);
      tmpstr = tmpbuff;
      return tmpstr;
  }
  int quota_to_bytes(char returnval[], char *quota) {
      char *tmpstr;
!     unsigned long tmpint;
  
      if (quota == NULL) { return 1; }
!     if (tmpint = atoi(quota)) { tmpint = tmpint * 1048576; }
      else { return 1; }
!     sprintf(returnval, "%i", tmpint);
      return 0;
  }
  int quota_to_megabytes(char returnval[], char *quota) {
      char *tmpstr;
!     unsigned long tmpint;
  
      if (quota == NULL) { return 1; }
!     if (tmpint = atoi(quota)) {
!          if (tmpint != 0) { tmpint = tmpint / 1048576; }
      }
      else { return 0; }
!     sprintf(returnval, "%i", tmpint);
      return 0;
  }
- double get_du(char *path) {
-     FTS *fileheir;
-     FTSENT *fsentry;
-     char **dirnames;
-     ssize_t size = 0;
  
!     dirnames = malloc(2 * sizeof(char*));
!     dirnames[0] = path;
!     dirnames[1] = NULL;
  
!     fileheir = fts_open(dirnames, FTS_PHYSICAL, NULL);
!     while ((fsentry = fts_read(fileheir)) != NULL) {
!         if (fsentry->fts_number == 0) {
!             size = size + fsentry->fts_statp->st_size;
!             fsentry->fts_number = 1;
!         }
      }
!     fts_close(fileheir);
!     free(dirnames);
!     return size;
  }
--- 258,325 ----
      double size;
  
      size = get_du(dir);
!     if (size > 0) {
          size = size / 1048576;  // Convert to MBs.
      }
!     sprintf(tmpbuff, "%-2.2lf", size);
      tmpstr = tmpbuff;
      return tmpstr;
  }
  int quota_to_bytes(char returnval[], char *quota) {
      char *tmpstr;
!     double tmp;
  
      if (quota == NULL) { return 1; }
!     if (tmp = atol(quota)) { tmp *= 1048576; }
      else { return 1; }
!     sprintf(returnval, "%-2.2lf", tmp);
      return 0;
  }
  int quota_to_megabytes(char returnval[], char *quota) {
      char *tmpstr;
!     double tmp;
  
      if (quota == NULL) { return 1; }
!     if (tmp = atol(quota)) {
!          if (tmp != 0) { tmp /= 1048576.0; }
      }
      else { return 0; }
!     sprintf(returnval, "%-2.2lf", tmp);
      return 0;
  }
  
! /*
!  * Brian Kolaci
!  * updated function that doesn't require fts_*
!  */
! off_t get_du(const char *dir_name)
! {
!   DIR *dirp;
!   struct dirent *dp;
!   struct stat statbuf;
!   off_t file_size = 0;
!   char *tmpstr;
  
!   if (dir_name == NULL)
!     return 0;
! 
!   if (chdir(dir_name) == -1)
!     return 0;
! 
!   if ((dirp = opendir(".")) == NULL)
!     return 0;
! 
!   while ((dp=readdir(dirp)) != NULL) {
!     if (!strcmp(dp->d_name, "..") || !strcmp(dp->d_name, "."))
!       continue;
!     if ((tmpstr=strstr(dp->d_name, ",S=")) != NULL) {
!       file_size += atol(tmpstr+3);
!     } else if (stat(dp->d_name,&statbuf)==0 && (statbuf.st_mode & S_IFDIR) ) {
!       file_size += get_du(dp->d_name);
      }
!   }
!   closedir(dirp);
!   if (dir_name != NULL && strcmp(dir_name, ".." ) && strcmp(dir_name, "." ))
!     chdir("..");
!   return(file_size);
  }

Reply via email to