How would I go about listing all the files a in certain dir? I want to list all the log files in the "log" dir, but am not sure how to. I didn't even see any code to append lines to the log files, only checking log files in the startup script. I made a command where Imms can view the contents of the log files at any line, but it's hard to do without knowing what log files exist. (Like if 1050.log is there, etc)
List files in a dir?
Posted by Zeno on Tue 22 Mar 2005 02:43 AM — 8 posts, 30,859 views.
For that I would just make a function that checked to see if the particular file was there at all when the user tried using the command, rather than listing them all.
Here is a function I found in a snippet a while ago, I forget who/where/what it was, but kudos to them for this.
Small and nice, this function has found much use amongst my code.
In your example you would then take the string the imm supplied containing the name of the log file (lets just call it buf) and do:
Please note that is supposing you added ../log/ to the string they supplied, as it is not likely they would supply it them selves.
Here is a function I found in a snippet a while ago, I forget who/where/what it was, but kudos to them for this.
bool exists_file( char *filen )
{
struct stat fst;
if ( stat( filen, &fst) == -1 )
return FALSE;
else
return TRUE;
return TRUE;
}
Small and nice, this function has found much use amongst my code.
In your example you would then take the string the imm supplied containing the name of the log file (lets just call it buf) and do:
if ( (exists_file(buf)) == FALSE )
{
send_to_char( "Sorry, but thats not a log file.\n\r", ch );
return;
}
Please note that is supposing you added ../log/ to the string they supplied, as it is not likely they would supply it them selves.
Thinking about this a bit more, it might be nice to make a function to list all of the log files for them in that case. You could even use the same function and just do a loop. Don't want your imm's going around guessing which log file, and you could have it mark the current one as well.
Here is something that I had lying around, its not tested very well but it should do the job
If you just want your log directory, specify that and remove the option for an argument, as that can be a security risk. Or leave a argument option to a high level imm.
void do_listdir(CHAR_DATA * ch, char *argument)
{
char buf[MSL];
DIR *dp;
struct dirent *dentry;
int count = 0, dircount = 0;
snprintf(buf, MSL, "../%s", argument);
if ( (dp = opendir( buf )) == NULL )
{
send_to_char("That directory does not exist\n\r", ch);
return;
}
dentry = readdir( dp );
ch_printf(ch, "&RDirectory listing for: %s\n\r", buf);
for ( dentry = readdir(dp) ; dentry ; dentry = readdir(dp), count++ )
{
if ( dentry->d_name[0] == '.' )
{
dircount++;
continue;
}
ch_printf(ch, "&G %s\n\r", dentry->d_name);
}
ch_printf(ch, "&pTotal number of files: %d\n\r", count - dircount );
closedir( dp );
}If you just want your log directory, specify that and remove the option for an argument, as that can be a security risk. Or leave a argument option to a high level imm.
Yeah I already have the function check if the file exists, obviously.
Thanks, I'll try that out Greven.
[EDIT] It worked well. How would I also display the last modified date? (shown with ls -l). Like...
As an example.
Thanks, I'll try that out Greven.
[EDIT] It worked well. How would I also display the last modified date? (shown with ls -l). Like...
ch_printf(ch, "&G %s, %s\n\r", dentry->d_name, dentry->date);As an example.
Quote:
It worked well. How would I also display the last modified date? (shown with ls -l). Like...
ch_printf(ch, "&G %s, %s\n\r", dentry->d_name, dentry->date);
It worked well. How would I also display the last modified date? (shown with ls -l). Like...
ch_printf(ch, "&G %s, %s\n\r", dentry->d_name, dentry->date);
No standardized variable. Check your system's documentation to see if the implementation includes such a variable in the structure.
Yes I know that. It was an example. I have no idea what function would display it.
There is a way to check the last modifed time, as to how I'm not certain as I'm at work, but if you look at Samson's site at the pfile pruning code, its in there. fstat, I think, check the man page for that.