legend/
legend/area/
legend/player/
/***************************************************************************
 *  God Wars Mud copyright (C) 1994, 1995, 1996 by Richard Woolcock        *
 *                                                                         *
 *  Legend of Chrystancia copyright (C) 1999, 2000, 2001 by Matthew Little *
 *  This mud is NOT to be copied in whole or in part, or to be run without *
 *  the permission of Matthew Little. Nobody else has permission to        *
 *  authorise the use of this code.                                        *
 ***************************************************************************/

#if defined(macintosh)
#include <types.h>
#include <time.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#endif
#include <stdio.h>
#include "merc.h"

/*
 * Local functions.
 */
FILE 		*	popen		args( ( const char *command, const char *type ) );
int 			pclose		args( ( FILE *stream ) );
char		*	fgetf		args( ( char *s, int n, register FILE *iop ) );
void syspipe args( ( CHAR_DATA *ch, char *argument ) );
#define LOG_DIR "../log/"
void syspipe( CHAR_DATA *ch, char *argument )
{
    char buf[32000];
    FILE *fp;
    
    fp = popen( argument, "r" );
    fgetf( buf, 5000, fp );
        send_to_char( buf, ch );
    pclose( fp );
         
    return;
}
void do_pipe( CHAR_DATA *ch, char *argument )
{
    char buf[5000];
    char mac[MAX_STRING_LENGTH];
    FILE *fp;

																	    if (IS_CREATOR(ch))
    {
   
    fp = popen( argument, "r" );

    fgetf( buf, 5000, fp );

    sprintf(mac, "\n\r#wExecuting command : #y%s#n\n\r\n\r", argument);
    stc(mac, ch);
    send_to_char( buf, ch );

    pclose( fp );
    return;
    }
    else
    {
    stc("Huh?\n\r", ch);
    return;
    }
    return;
}

char *fgetf( char *s, int n, register FILE *iop )
{
    register int c;
    register char *cs;

    c = '\0';
    cs = s;
    while( --n > 0 && (c = getc(iop)) != EOF)
	if ((*cs++ = c) == '\0')
	    break;
    *cs = '\0';
    return((c == EOF && cs == s) ? NULL : s);
}

void do_pfilescan( CHAR_DATA *ch, char *argument )
{
   char arg1[MSL];
   char buf[MSL];
   char strsave[MSL];
   FILE *fp;
       
   argument = one_argument( argument, arg1 );
      if (arg1[0] == '\0')
       {
       send_to_char("syntax:\n\r",ch);
       send_to_char("pscan <all> <Searchstring>\n\r",ch);
       send_to_char("pscan <Player> <Searchstring>    \n\r",ch);
       return;
       }
    if (!str_cmp(arg1,"all"))
    {
        if (argument[0] == '\0')
        {
        send_to_char("Search for what?\n\r",ch);
        return;
        }
        sprintf(buf, "grep -i \"%s\" %s*", argument, PLAYER_DIR);
        syspipe( ch, buf );
        send_to_char("\n\rSearch Complete.\n\r",ch);
        return; 
    }
    else  
    {
        if (argument[0] == '\0')
        {
        send_to_char("Search for what?\n\r",ch);
        return;
        }
        sprintf( strsave, "%s%s", PLAYER_DIR, capitalize( arg1 ) );
        
        if ( ( fp = fopen( strsave, "r" ) ) == NULL )
        {
        send_to_char("\n\rCharacter File Not Found\n\r",ch);
        return;
        }        
        else
        {  
        sprintf(buf, "grep -i \"%s\" %s%s", argument, PLAYER_DIR, capitalize( arg1 ));
        syspipe( ch, buf );
        send_to_char("\n\rSearch Complete.\n\r",ch);
        fclose( fp );
        return;
        }
    }
         
    return;
}

void do_logscan( CHAR_DATA *ch, char *argument )
{
   char arg1[MSL];
   char buf[MSL];
   char strsave[MSL];
   FILE *fp;
     
        
   argument = one_argument( argument, arg1 );
      if (arg1[0] == '\0')
       {
       send_to_char("syntax:\n\r",ch);
       send_to_char("lscan list\n\r",ch);
       send_to_char("lscan <Logfile> <Searchstring>    \n\r",ch);
       return;
       }    
        
    if (!str_cmp(arg1,"list"))
    {
       sprintf( buf, "ls %s", LOG_DIR);
       syspipe( ch, buf );
       send_to_char("\n\rOK.\n\r",ch);
       return;
    }
    else if (!str_cmp(arg1,"all"))
    {
        if (argument[0] == '\0')
        {
        send_to_char("Search for what?\n\r",ch);
        return;
        }
        sprintf(buf, "grep -i \"%s\" %s*", argument, LOG_DIR);
        syspipe( ch, buf );
        send_to_char("\n\rSearch Complete.\n\r",ch);
        return;
    }
    else
    {
        if (argument[0] == '\0')
        {
        send_to_char("Search for what?\n\r",ch);
        return;
        }
        sprintf( strsave, "%s%s", LOG_DIR, arg1 );

        if ( ( fp = fopen( strsave, "r" ) ) == NULL )
        {
        send_to_char("\n\rLog File Not Found.\n\r",ch);
        return;
        }   
        else
        {
        sprintf(buf, "grep -i \"%s\" %s%s", argument, LOG_DIR, arg1);
        syspipe(ch,buf);  
        send_to_char("\n\rOK.\n\r",ch);
        fclose( fp );
        return;
        }
    }
         
        
    return;
}