[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  SMAUG coding
. . -> [Subject]  List files in a dir?
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

List files in a dir?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Zeno   USA  (2,871 posts)  [Biography] bio
Date Tue 22 Mar 2005 02:43 AM (UTC)
Message
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)

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #1 on Tue 22 Mar 2005 05:11 AM (UTC)

Amended on Tue 22 Mar 2005 05:14 AM (UTC) by Nick Cash

Message
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.

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.

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #2 on Tue 22 Mar 2005 05:16 AM (UTC)
Message
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.

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #3 on Tue 22 Mar 2005 06:11 AM (UTC)
Message
Here is something that I had lying around, its not tested very well but it should do the job
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.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] bio
Date Reply #4 on Tue 22 Mar 2005 03:42 PM (UTC)

Amended on Tue 22 Mar 2005 07:08 PM (UTC) by Zeno

Message
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...
ch_printf(ch, "&G  %s, %s\n\r", dentry->d_name, dentry->date);

As an example.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
[Go to top] top

Posted by Raz   (32 posts)  [Biography] bio
Date Reply #5 on Tue 22 Mar 2005 10:26 PM (UTC)
Message
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);


No standardized variable. Check your system's documentation to see if the implementation includes such a variable in the structure.

-Raz
C++ Wiki: http://danday.homelinux.org/dan/cppwiki/index.php
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] bio
Date Reply #6 on Wed 23 Mar 2005 01:36 AM (UTC)
Message
Yes I know that. It was an example. I have no idea what function would display it.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #7 on Wed 23 Mar 2005 01:40 AM (UTC)
Message
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.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


4,942 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]