#include <sys/stat.h>
#include <ctype.h>
#include <string.h>
+#include <dirent.h> /* for cmd_rdir to read contents of the directory */
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
#include <limits.h>
#include <errno.h>
#include "citadel.h"
+#include <libcitadel.h>
#include "server.h"
#include "database.h"
#include "config.h"
#include "msgbase.h"
#include "citserver.h"
#include "control.h"
-#include "tools.h"
#include "citadel_dirs.h"
struct floor *floorcache[MAXFLOORS];
struct cdbdata *cdbqr;
char lowercase_name[ROOMNAMELEN];
char personal_lowercase_name[ROOMNAMELEN];
- int a;
+ char *dptr, *sptr, *eptr;
- for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
- lowercase_name[a] = tolower(room_name[a]);
+ dptr = lowercase_name;
+ sptr = room_name;
+ eptr = (dptr + (sizeof lowercase_name - 1));
+ while (!IsEmptyStr(sptr) && (dptr < eptr)){
+ *dptr = tolower(*sptr);
+ sptr++; dptr++;
}
- lowercase_name[a] = 0;
+ *dptr = '\0';
memset(qrbuf, 0, sizeof(struct ctdlroom));
char lowercase_name[ROOMNAMELEN];
char *aptr, *bptr;
long len;
- int a;
aptr = room_name;
bptr = lowercase_name;
/* and yank any nulls */
while ((numitems > 0) && (listptrs[0] == 0L)) {
- memcpy(&listptrs[0], &listptrs[1],
+ memmove(&listptrs[0], &listptrs[1],
(sizeof(long) * (numitems - 1)));
--numitems;
}
void cmd_rdir(void)
{
char buf[256];
- char flnm[256];
char comment[256];
- FILE *ls, *fd;
+ FILE *fd;
struct stat statbuf;
- char tempfilename[PATH_MAX];
-
+ DIR *filedir = NULL;
+ struct dirent *filedir_entry;
+ int d_namelen;
+ char buf2[SIZ];
+
if (CtdlAccessCheck(ac_logged_in)) return;
- CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
getroom(&CC->room, CC->room.QRname);
getuser(&CC->user, CC->curr_user);
cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
return;
}
- cprintf("%d %s|%s/%s\n",
- LISTING_FOLLOWS,
- config.c_fqdn,
- ctdl_file_dir,
- CC->room.QRdirname);
-
- snprintf(buf, sizeof buf,
- "ls %s/%s >%s 2>/dev/null",
- ctdl_file_dir,
- CC->room.QRdirname,
- tempfilename);
- system(buf);
-
- snprintf(buf, sizeof buf,
- "%s/%s/filedir",
- ctdl_file_dir,
- CC->room.QRdirname);
- fd = fopen(buf, "r");
- if (fd == NULL)
- fd = fopen("/dev/null", "r");
-
- ls = fopen(tempfilename, "r");
- while (fgets(flnm, sizeof flnm, ls) != NULL) {
- flnm[strlen(flnm) - 1] = 0;
- if (strcasecmp(flnm, "filedir")) {
- snprintf(buf, sizeof buf,
- "%s/%s/%s",
- ctdl_file_dir,
- CC->room.QRdirname,
- flnm);
- stat(buf, &statbuf);
- safestrncpy(comment, "", sizeof comment);
- fseek(fd, 0L, 0);
- while ((fgets(buf, sizeof buf, fd) != NULL)
- && (IsEmptyStr(comment))) {
- buf[strlen(buf) - 1] = 0;
- if ((!strncasecmp(buf, flnm, strlen(flnm)))
- && (buf[strlen(flnm)] == ' '))
- safestrncpy(comment,
- &buf[strlen(flnm) + 1],
- sizeof comment);
+ cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
+
+ snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
+ filedir = opendir (buf);
+ if (filedir)
+ {
+ snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
+ fd = fopen(buf, "r");
+ if (fd == NULL)
+ fd = fopen("/dev/null", "r");
+ while ((filedir_entry = readdir(filedir)))
+ {
+ if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
+ {
+#ifdef _DIRENT_HAVE_D_NAMELEN
+ d_namelen = filedir_entry->d_namelen;
+#else
+ d_namelen = strlen(filedir_entry->d_name);
+#endif
+ snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
+ stat(buf, &statbuf); /* stat the file */
+ if (!(statbuf.st_mode & S_IFREG))
+ {
+ snprintf(buf2, sizeof buf2, "Command RDIR found something that is not a useable file. It should be cleaned up.\n RDIR found this non regular file:\n%s\n", buf);
+ aide_message(buf2, "RDIR found bad file");
+ continue; /* not a useable file type so don't show it */
+ }
+ safestrncpy(comment, "", sizeof comment);
+ fseek(fd, 0L, 0); /* rewind descriptions file */
+ /* Get the description from the descriptions file */
+ while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment)))
+ {
+ buf[strlen(buf) - 1] = 0;
+ if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
+ safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
+ }
+ cprintf("%s|%ld|%s\n", filedir_entry->d_name, (long)statbuf.st_size, comment);
}
- cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
}
+ fclose(fd);
+ closedir(filedir);
}
- fclose(ls);
- fclose(fd);
- unlink(tempfilename);
-
cprintf("000\n");
}
if (CtdlAccessCheck(ac_logged_in)) return;
- if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
+ if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
cprintf("%d You need higher access to create rooms.\n",
ERROR + HIGHER_ACCESS_REQUIRED);
return;