LBIO: add parameter
authorWilfried Goesgens <dothebart@citadel.org>
Fri, 15 Jul 2011 15:01:54 +0000 (15:01 +0000)
committerWilfried Goesgens <dothebart@citadel.org>
Sun, 4 Sep 2011 21:36:15 +0000 (21:36 +0000)
  - if the user provided '1' just the UIDs are listed instead of the actual user name.
  - we now use dirent and friends instead of outputting the output of the unix ls command *cough*

citadel/modules/bio/serv_bio.c

index a3a9c6a61646de1d59a99da4f89bea89ca34e4fc..e98131361f70598fd3c889dfc9c8051af62e0d3c 100644 (file)
@@ -22,6 +22,9 @@
 
 #include "ctdl_module.h"
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
 
 
 /*
@@ -86,27 +89,49 @@ void cmd_rbio(char *cmdbuf)
 /*
  * list of users who have entered bios
  */
-void cmd_lbio(char *cmdbuf) {
-       char buf[256];
-       FILE *ls;
+void cmd_lbio(char *cmdbuf)
+{
+       DIR *filedir = NULL;
+       struct dirent *filedir_entry;
+       struct dirent *d;
+       int dont_resolve_uids;
+       size_t d_namelen;
        struct ctdluser usbuf;
-       char listbios[256];
 
-       snprintf(listbios, sizeof(listbios),"cd %s; ls",ctdl_bio_dir);
-       ls = popen(listbios, "r");
-       if (ls == NULL) {
+       d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
+       if (d == NULL) {
                cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
                return;
        }
 
-       cprintf("%d\n", LISTING_FOLLOWS);
-       while (fgets(buf, sizeof buf, ls)!=NULL)
-               if (CtdlGetUserByNumber(&usbuf,atol(buf))==0)
+       filedir = opendir (ctdl_bio_dir);
+       if (filedir == NULL) {
+               free(d);
+               cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
+               return;
+       }
+       dont_resolve_uids = *cmdbuf == '1';
+       while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
+              (filedir_entry != NULL))
+       {
+#ifdef _DIRENT_HAVE_D_NAMELEN
+               d_namelen = filedir_entry->d_namelen;
+#else
+               d_namelen = strlen(filedir_entry->d_name);
+#endif
+               if (dont_resolve_uids) {
+                       filedir_entry->d_name[d_namelen++] = '\n';
+                       filedir_entry->d_name[d_namelen] = '\0';
+                       client_write(filedir_entry->d_name, d_namelen);
+               }
+               else if (CtdlGetUserByNumber(&usbuf,atol(filedir_entry->d_name))==0)
                        cprintf("%s\n", usbuf.fullname);
-       pclose(ls);
+       }
+       free(d);
+       closedir(filedir);
        cprintf("000\n");
-}
 
+}