Пример #1
0
//exit with winner
void w_exit(int winner)
{
	if(winner == 1)
		addlog("You win!", 3, 3);
	else if(winner == -1)
		addlog("Opponent disconnected",3, 3);
	else
		addlog("You lose", 3, 3);

	sleep(5);
	s_exit(0);
}
Пример #2
0
/*

    Function:   vUtlSignalsFatalHandler()

    Purpose:    To handle fatal signals. Currently we handle the 
                following signals:

                    SIGQUIT
                    SIGILL
                    SIGTRAP
                    SIGABRT
                    SIGEMT
                    SIGFPE
                    SIGBUS
                    SIGSEGV
                    SIGSYS
                    SIGXCPU
                    SIGXFSZ

    Parameters: iSignal     signal number

    Globals:    none

    Returns:    void

*/
void vUtlSignalsFatalHandler
(
    int iSignal
)
{

    int     iStatus = 0;
    pid_t   zPid = 0;


    /* Pass on the signal to any kids */
    vUtlSignalsPassToChildProcesses(iSignal);


    /* Log the event */
    switch ( iSignal ) {
#if defined(SIGQUIT)
        case SIGQUIT:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received a quit from keyboard signal");
#endif    /* defined(SIGQUIT) */

#if defined(SIGILL)
        case SIGILL:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received an illegal instruction signal");
#endif    /* defined(SIGILL) */

#if defined(SIGTRAP)
        case SIGTRAP:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received a trace/breakpoint trap signal");
#endif    /* defined(SIGTRAP) */

#if defined(SIGABRT)
        case SIGABRT:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received an abort signal");
#endif    /* defined(SIGABRT) */

#if defined(SIGEMT)
        case SIGEMT:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received an emulation trap signal");
#endif    /* defined(SIGEMT) */

#if defined(SIGFPE)
        case SIGFPE:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received a floating point exception signal");
#endif    /* defined(SIGFPE) */

#if defined(SIGBUS)
        case SIGBUS:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received a bus error signal");
#endif    /* defined(SIGBUS) */

#if defined(SIGSEGV)
        case SIGSEGV:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received an invalid memory reference signal");
#endif    /* defined(SIGSEGV) */

#if defined(SIGSYS)
        case SIGSYS:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received a bad system call signal");
#endif    /* defined(SIGSYS) */

#if defined(SIGXCPU)
        case SIGXCPU:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received a CPU time limit exceeded signal");
#endif    /* defined(SIGXCPU) */

#if defined(SIGXFSZ)
        case SIGXFSZ:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received a file size limit exceeded signal");
#endif    /* defined(SIGXFSZ) */

        default:
            iUtlLogPanic(UTL_LOG_CONTEXT, "Received an unknown signal: %d", iSignal);
    }


    /* Make sure we bail */
    s_exit(EXIT_FAILURE);

}
Пример #3
0
/*

    Function:   vUtlSignalsNonFatalHandler()

    Purpose:    To handle non-fatal signals. Currently we handle the 
                following signals:

                    SIGINT
                    SIGKILL
                    SIGPIPE
                    SIGALRM
                    SIGTERM
                    SIGSTOP

    Parameters: iSignal     signal number

    Globals:    none

    Returns:    void

*/
void vUtlSignalsNonFatalHandler
(
    int iSignal
)
{

    int     iStatus = 0;
    pid_t   zPid = 0;


    /* Pass on the signal to any kids */
    vUtlSignalsPassToChildProcesses(iSignal);


    /*  Log the event */
    switch ( iSignal ) {
#if defined(SIGINT)
        case SIGINT:
            iUtlLogWarn(UTL_LOG_CONTEXT, "Received an interrupt from keyboard signal.");
            break;
#endif    /* defined(SIGINT) */

#if defined(SIGKILL)
        case SIGKILL:
            iUtlLogWarn(UTL_LOG_CONTEXT, "Received a kill signal.");
            break;
#endif    /* defined(SIGKILL) */

#if defined(SIGPIPE)
        case SIGPIPE:
            iUtlLogWarn(UTL_LOG_CONTEXT, "Received broken pipe signal.");
            break;
#endif    /* defined(SIGPIPE) */

#if defined(SIGALRM)
        case SIGALRM:
            iUtlLogWarn(UTL_LOG_CONTEXT, "Received an alarm signal.");
            break;
#endif    /* defined(SIGALRM) */

#if defined(SIGTERM)
        case SIGTERM:
            iUtlLogWarn(UTL_LOG_CONTEXT, "Received a termination signal.");
            break;
#endif    /* defined(SIGTERM) */

#if defined(SIGSTOP)
        case SIGSTOP:
            iUtlLogWarn(UTL_LOG_CONTEXT, "Received a stop signal.");
            break;
#endif    /* defined(SIGSTOP) */

        default:
            iUtlLogWarn(UTL_LOG_CONTEXT, "Received an unknown signal: %d.", iSignal);
    }


    /* Make sure we bail */
    s_exit(EXIT_SUCCESS);

}
Пример #4
0
void parsecommand(char* s){
	int clength = 0;
	int error = 0;
	int hasParameter = 0;
	getCommand(s);
	clength = strlen(shellbuff.command);
	if(shellbuff.buffer[clength]==' '){
		hasParameter = 1;
		getParameter(shellbuff.buffer, clength);
	}
	// Execute the requested command 
	if(!strcmp(shellbuff.command,"test")){
		error = s_test(hasParameter, shellbuff.parameter);
	}
	if(!strcmp(shellbuff.command,"help")){
		error = s_help(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command,"bios")){
		error = s_checkBIOS(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command, "exit")){
		error = s_exit(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command, "reboot")){
		error = s_k_reboot(hasParameter, shellbuff.parameter); 
	}
	else if(!strcmp(shellbuff.command,"clear")){
		error = s_clear(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command,"about")){
		error = s_about(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command,"timestyle")){
		error = s_timestyle(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command,"bkg")){
		error = s_bkg(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command,"logout")){
		error = s_logout(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command,"cpu")){
		error = s_cpu(hasParameter, shellbuff.parameter);
	}
	else if(!strcmp(shellbuff.command,"mouse")){
		error = s_mouse(hasParameter, shellbuff.parameter);
	}
	else {
		s_printError("COMMAND NOT FOUND\n");
		s_beep();
	}
	// Print error messages if needed
	switch(error){
		case 1:
			s_printAlert("Too many arguments.\n");
			break;
		case 2:
			s_printAlert("Too few arguments.\n");
			break;
		case 3:
			s_printAlert("Invalid argument.\n");
			break;
		case 4:
			s_printAlert("This command doesn't receive parameters.\n");
			break;
		default:
			break;
	}
	return;
}
Пример #5
0
input ()
{
    int     infile;
    int     n;
    long    dist;
    int     nfd;
    int     stat, slen;
    char    otherbuf[S_BSIZE];
    char    tempbuf[24];
    char    *fromptr, *toptr, *ccptr, *subjptr, *bccptr, *otherptr;
    sigtype     (*old1) (),
	    (*old2) (),
	    (*old3) ();
    register int    i;

    printf ("SND  (%s)\n", verdate);

	bccflag = 0;
	dropen(DRBEGIN);

/*	Set up headers to be written into the draft file	*/
	fromptr = from;
	toptr = to;
	ccptr = cc;
	subjptr = subject;
	bccptr = bcc;
	otherptr = otherbuf;

	strcpy(from, signature);
	strcpy(otherptr, fromname);
	strcat(otherptr, fromptr);
	strcpy(fromptr, otherptr);

	strcpy(otherptr, toname);
	strcat(otherptr, toptr);
	strcpy(toptr, otherptr);

	strcpy(otherptr, ccname);
	strcat(otherptr, ccptr);
	strcpy(ccptr, otherptr);

	slen = strlen( subjname ); 
	strcpy(otherptr, subjname);
	strcat(otherptr, subjptr);
	strcpy(subjptr, otherptr);
	
/*	Write the header info to the draft file		*/

	if(write(drffd, fromptr, strlen(fromptr)) < 0)
	{
		printf("Cannot write draft %s\n", drffile);
		s_exit(-1);
	}
	write(drffd, "\n", 1);
	write(drffd, toptr, strlen(toptr));
	write(drffd, "\n", 1);
	write(drffd, ccptr, strlen(ccptr));
	write(drffd, "\n", 1);

/*	If there are blind carbon copies write the necessary header b.s. */
	if(bcc[0] != '\0')
	{
		strcpy(otherptr, bccname);
		strcat(otherptr, bccptr);
		strcpy(bccptr, otherptr);
		write(drffd, bccptr, strlen(subjptr));
		write(drffd, "\n", 1);


		bccflag = 1;
	}
	if( strlen( subject ) > slen )
	{
		char newsubj[S_BSIZE], *p, *pnt;
		char *index();
		int plc;

		newsubj[0] = '\0';
		pnt = subjptr;
		while( (p = index(pnt,'\n')) != 0 )
		{
			*p++ = '\0';
			plc = strlen(newsubj);
			sprintf( &newsubj[plc],"%s\n%s",pnt," ");
			pnt = p;
		}
		strcat( newsubj, pnt );
		strcpy( subjptr, newsubj );
	}
	write(drffd, subjptr, strlen(subjptr));
	write(drffd, "\n\n", 2);

/*	If there is a forwarded message		*/
	if (inclfile[0] != '\0')
	{                             /* tack file to end of message        */
		body = TRUE;
		strcpy (bigbuf, inclfile);
		infile = open (bigbuf, 0);
		if (inclfile[0] != '\0')
			inclfile[0] = '\0';
		if (infile < 0)
			printf ("can't open: '%s'\n", bigbuf);
		dropen (DREND);
		body = TRUE;
		while ((i = read (infile, bigbuf, BBSIZE - 1)) > 0)
			write (drffd, bigbuf, i);
		(void) close (infile);
	}

edit:

	fflush (stdout);

        drclose ();

        old1 =  signal (SIGHUP, SIG_IGN); /* ignore signals intended for edit */
        old2 =  signal (SIGINT, SIG_IGN);
        old3 =  signal (SIGQUIT, SIG_IGN);

/*	Crank up the editor now that the draft file is set-up	*/
        if (fork () == 0)
        {
	    signal (SIGHUP, old1);
	    signal (SIGINT, orig);
	    signal (SIGQUIT, old3);

            execlp (editor, editor, drffile, (char *)0);

	    fprintf (stderr,"can't execute\n");
	    s_exit (-1);
        }
        wait (&stat);
        body = TRUE;	/* Don't know but assume he created a draft */
        lastsend = 0;
        signal (SIGHUP, old1);     /* restore signals */
        signal (SIGINT, old2);
        signal (SIGQUIT, old3);

        fflush (stdout);
        signal (SIGINT, SIG_IGN);
        signal (SIGQUIT, SIG_IGN);

	dropen(DRBEGIN);
	
/*	Rip the header information from the draft file and
**	take care of any aliases
**	Algorithm:
**	Read each header line (function grabaddr), keeping track of the 
**	number of characters read.  Handle the aliases (function doalias).
**	Open a new file descriptor which points to the beginning of the
**	draft file.  Read the draft file from the point where the header
**	information leaves off while writing to the draft from the beginning.
**	This effectively reads the header info, then shifts the body of the
**	message up to the top of the draft, overwriting the header.
*/

	dist = gethdr( fromptr, toptr, ccptr, bccptr, subjptr );

	doalias( toptr );
	doalias( ccptr );
	doalias( bccptr );

	if(chmod(drffile, 0600) < 0)
	{
		fprintf(stderr,"Unable to make draft writable %s.\n", drffile);
		s_exit(-1);
	}
	if((nfd = open(drffile, 2, 600)) < 0)
	{
		fprintf(stderr,"Could not open draft %s\n", drffile);
		s_exit(-1);
	}
	if(lseek(nfd, 0L, 0) < 0)
	{
		fprintf(stderr,"Lseek failed on draft %s\n", drffile);
		s_exit(-1);
	}
	if(lseek(drffd, dist-1, 0) < 0)
	{
		fprintf(stderr,"Lseek failed on draft %s\n", drffile);
		s_exit(-1);
	}

	dist = 0L;
	while((i = read(drffd, bigbuf, BBSIZE -1)) > 0)
		dist += write(nfd, bigbuf, i);

	if(truncate(drffile, dist) < 0)
	{
		fprintf(stderr,"Truncate failed on draft %s\n", drffile);
		s_exit(-1);
	}

	if(chmod(drffile, 0400) < 0)
		fprintf(stderr,"Unable to make draft %s writable.\n",drffile);
	
	(void) close( nfd );

    setjmp (savej);
    nsent = 0;
/*
**	MAIN COMMAND INTERPRETER LOOP
*/
    for (;;)
    {
    	char *cp;

	signal (SIGINT, onint3);
	printf ("Command or ?: ");
	fflush (stdout);
	aborted = FALSE;
	if (fgets (bigbuf, BBSIZE, stdin) == NULL)
	    goto byebye;
    	if (cp = index(bigbuf, '\n'))
    	    *cp = '\0';

/*	"Set" option	*/
	if( strncmp( "set", bigbuf, 3) == 0){
		char *av[NARGS];

		i = sstr2arg(bigbuf, 20, av, " \t");
		if( i == 1 ) {	/* tell him what the current options are */
			printf(" Copyfile  - '%s'\n", copyfile);
			printf(" Signature - '%s'\n", signature);
			printf(" Aliases   - '%s'\n", aliasfilename);
			printf(" Editor    - '%s'\n", editor);
			printf(" Checker   - '%s'\n", checker);
			printf(" Subargs   - '%s'\n", subargs);

			if(qflag == 0 && cflag == 1)
				printf(" File copy option is on\n");
			if(qflag == 1 && cflag == 0)
				printf(" File copy on confirmation only\n");
			continue;
		}
		if( (i = picko( av[1] ) ) < 0) {
			fprintf(stderr,"Option '%s' is invalid.\n", av[1]);
			continue;
		}
		select_o( i, &av[1] );
		continue;
	}
	compress (bigbuf, bigbuf);
				  /* so prefix works on multi-words     */
	if (bigbuf[0] == '\0')
	    continue;             /* just hit newline                   */

	signal (SIGINT, onint);

	for (i = 0; !isnull (bigbuf[i]); i++)
	    bigbuf[i] = uptolow (bigbuf[i]);

	if (prefix ("bcc", bigbuf))
	{
	    getaddr (bccname, bcc, YES, locname);
	    bccflag = 1;	
	    continue;
	}

/*	"Edit" option (grandfathered to also accept "v" for visual edit) */

	if (prefix ("edit body", bigbuf) || prefix("visual edit body", bigbuf))
	{
		dropen( DRBEGIN );
    		if((tmpfd = open(tmpdrffile, 2, 0600)) < 0)
    		{
    			fprintf(stderr,"Cannot open temporary draft file %s\n", tmpdrffile);
    			s_exit(-1);
    		}

    		if(lseek(tmpfd, 0L, 0) < 0)
    		{
    			fprintf(stderr,"Lseek failed on temporary draft file %s\n", tmpdrffile);
    			s_exit(-1);
    		}

    		if(chmod(drffile, 0600) < 0)
    			fprintf(stderr,"Could not make draft %s writable\n", drffile);

		/* 	Write the header info to the temporary draft	*/
		dist = 0L;
		dist += write(tmpfd, fromname, strlen( fromname ));
    		dist += write(tmpfd, from, strlen( from ));
    		dist += write(tmpfd, "\n", 1);
    		dist += write(tmpfd, toname, strlen( toname ));
    		dist += write(tmpfd, to, strlen( to ));
    		dist += write(tmpfd, "\n", 1);
    		dist += write(tmpfd, ccname, strlen( ccname ));
    		dist += write(tmpfd, cc, strlen( cc ));
    		dist += write(tmpfd, "\n", 1);
    		if(bccflag)
    		{
    			dist += write(tmpfd, bccname, strlen( bccname ));
    			dist += write(tmpfd, bcc, strlen( bcc ));
    			dist += write(tmpfd, "\n", 1);
    		}
    		dist += write(tmpfd, subjname, strlen( subjname ));
    		dist += write(tmpfd, subject, strlen( subject ));
    		dist += write(tmpfd, "\n", 1);

		if(lseek(drffd, 0L, 0) < 0)
		{
		 	fprintf(stderr,"Lseek failed on draft %s\n", drffile);
		 	s_exit(-1);
		}
    		if(lseek(tmpfd, 0L, 2) < 0)
    		{
    			fprintf(stderr,"Lseek failed on temporary draft\n");
    			s_exit(-1);
    		}

		/*	Write the body into the temp. draft	*/
    		while((i = read(drffd, bigbuf, BBSIZE-1)) > 0)
    			dist += write(tmpfd, bigbuf, i);

    		if(truncate( tmpdrffile, dist) < 0)
    		{
    			fprintf(stderr,"Could not truncate temporary draft\n");
    			s_exit(-1);
    		}

    		if(lseek(tmpfd, 0L, 0) < 0)
    			fprintf(stderr,"Lseek failed on temporary draft\n");
    		if(lseek(drffd, 0L, 0) < 0)
    			fprintf(stderr,"Lseek failed on draft %s\n", drffile);
    		
    		dist = 0L;
		/*	Copy temp. draft which contains headers and
		**	body back into draft file
		*/
    		while((i = read(tmpfd, bigbuf, BBSIZE-1)) > 0)
    			dist += write(drffd, bigbuf, i);

    		if(truncate( drffile, dist ) < 0)
    			fprintf(stderr,"Could not truncate draft %s\n", drffile);
    		if(truncate( tmpdrffile, 0 ) < 0)
    			fprintf(stderr,"Could not truncate temporary draft\n");
    		if(close( tmpfd ) < 0)
    			fprintf(stderr,"Could not close temporary draft\n");
    		if(lseek(drffd, 0L, 0) < 0)
    			fprintf(stderr,"Lseek failed on draft %s\n", drffile);
    		if(chmod(drffile, 0400) < 0)
    			fprintf(stderr,"Could not protect draft %s\n", drffile);
		/*	Fire-up the editor	*/
		goto edit;
	}
/*	"File include" option	*/
	if (prefix ("file include", bigbuf))
	{
	    printf ("File: ");
	    gather (bigbuf, BBSIZE - 1);
	    if (bigbuf[0] == '\0')
		continue;

	    infile = open (bigbuf, 0);
	    if (inclfile[0] != '\0')
	    {                     /* and include file                   */
		inclfile[0] = '\0';
	    }
	    if (infile < 0)
	    {
		fprintf(stderr,"can't open: '%s'\n", bigbuf);
		continue;
	    }
	    dropen (DREND);
	    body = TRUE;
	    while ((i = read (infile, bigbuf, BBSIZE - 1)) > 0)
		write (drffd, bigbuf, i);
	    (void) close (infile);
	    printf (" ...included\n");
	    continue;
	}
/*	"Program run" option	*/
	if (prefix ("program run", bigbuf))
	{
	    printf ("Program: ");
	    fflush (stdout);
	    if (gets (tempbuf) == NULL)
		continue;
	    drclose ();

	    /* ignore signals intended for subprocess */
	    old1 = signal (SIGHUP, SIG_IGN);
	    old2 = signal (SIGINT, SIG_IGN);
	    old3 = signal (SIGQUIT, SIG_IGN);

	    if (fork () == 0)
	    {
		signal (SIGHUP, old1);
		signal (SIGINT, orig);
		signal (SIGQUIT, old3);
		execlp ("sh", "send-shell", "-c", tempbuf, (char *)0);
		fprintf(stderr,"can't execute\n");
		s_exit (-1);
	    }
	    wait (&stat);
	    signal (SIGHUP, old1);     /* restore signals */
	    signal (SIGINT, old2);
	    signal (SIGQUIT, old3);
	    dropen (DREND);
	    continue;
	}
/*	"quit" option	*/
	if (prefix ("quit", bigbuf) ||
		prefix ("bye", bigbuf))
	{
    byebye: 
	    if ( body && !nsent)
	    {
		printf ("Without sending draft");
		if (confirm ())
		    return;
		else
		    continue;
	    }
	    drclose ();
	    return;
	}
/*	"review message" option	*/
	if (prefix ("review message", bigbuf))
	{
		dropen( DRBEGIN );
		/*	Put header info into temp. draft	*/
		if((nfd = open(tmpdrffile, 2, 0600)) < 0)
		{
			fprintf(stderr,"Can't open temporary file\n");
			s_exit(-1);
		}
		if(lseek(nfd, 0L, 0) < 0)
		{
			fprintf(stderr,"Lseek failed on temporary file\n");
			s_exit(-1);
		}
		if(write(nfd, fromname, strlen( fromname )) < 0)
		{
			fprintf(stderr,"Cannot write temporary file\n");
			s_exit(-1);
		}
		write(nfd, from, strlen( from ));
		write(nfd, "\n", 1);
		if(to[0] != '\0')
		{
			write(nfd, toname, strlen( toname ));
			write(nfd, to, strlen( to ));
			write(nfd, "\n", 1);
		}
		else if(bccflag)
		{
			write(nfd, toname, strlen( toname ));
			write(nfd, "list: ;", 7);
			write(nfd, "\n", 1);
		}
		if(cc[0] != '\0')
		{
			write(nfd, ccname, strlen( ccname ));
			write(nfd, cc, strlen( cc ));
			write(nfd, "\n", 1);
		}
		if(bccflag)
		{
			write(nfd, bccname, strlen( bccname ));
			write(nfd, bcc, strlen( bcc ));
			write(nfd, "\n", 1);
		}			
		if(subject[0] != '\0')
		{
			write(nfd, subjname, strlen( subjname ));
			write(nfd, subject, strlen( subject ));
			write(nfd, "\n", 1);
		}

		if(lseek(drffd, 0L, 0) < 0)
			fprintf(stderr,"Lseek failed on draft %s\n", drffile);
	/*	Write the body into the temp. file	*/
		while((i = read(drffd, bigbuf, BBSIZE - 1)) > 0)
			write(nfd, bigbuf, i);
		drclose();
	   if(pflag)
	   {
		(void) close( nfd );
			
		    /* ignore signals intended for subprocess */
		    old1 = signal (SIGHUP, SIG_IGN);
		    old2 = signal (SIGINT, SIG_IGN);
		    old3 = signal (SIGQUIT, SIG_IGN);
		/*	Start-up "more" program on the temp. draft	*/
		    if (fork () == 0)
		    {
			signal (SIGHUP, old1);
			signal (SIGINT, orig);
			signal (SIGQUIT, old3);
			execlp ("more", "-c", tmpdrffile, 0);
			fprintf(stderr,"can't execute\n");
			s_exit (-1);
		    }
		    wait (&stat);
		    signal (SIGHUP, old1);     /* restore signals */
		    signal (SIGINT, old2);
		    signal (SIGQUIT, old3);

	   }
	   else	/*	If the paging option is off, just show it */
	   {
		if( lseek(nfd, 0L, 0) < 0 )
			fprintf(stderr, "Lseek failed on temporary draft.\n");

		while((i = read(nfd, bigbuf, BBSIZE - 1)) > 0)
			write( 1, bigbuf, i);

		(void) close( nfd );
	   }
	   if(truncate(tmpdrffile, 0) < 0)
		fprintf(stderr,"truncate failed on temp. draft\n");

	   fflush( stdout );
	   continue;
	}
/*	"Check spelling" option	*/
	if (prefix ("check spelling", bigbuf))
	{
	    drclose ();

	    old1 =  signal (SIGHUP, SIG_IGN); /* ignore signals intended for cheker */
	    old2 =  signal (SIGINT, SIG_IGN);
	    old3 =  signal (SIGQUIT, SIG_IGN);

	    if (fork () == 0)
	    {
		signal (SIGHUP, old1);
		signal (SIGINT, orig);
		signal (SIGQUIT, old3);
		execlp (checker, checker, drffile, (char *)0);
		fprintf(stderr,"can't execute\n");
		s_exit (-1);
	    }
	    wait (&stat);
	    signal (SIGHUP, old1);     /* restore signals */
	    signal (SIGINT, old2);
	    signal (SIGQUIT, old3);
	    dropen (DREND);
	    continue;
	}
/*	"Post message" option	*/
	if (prefix ("post message", bigbuf) ||
		prefix ("send message", bigbuf))
	{
	    if (lastsend && lastsend == body)
	    {
		printf ("Without changing anything");
		if (!confirm ())
		    break;
	    }
	    signal (SIGINT, onint2);
	    if( qflag == 1 ){
		printf(" Do you want a file copy of this message ?  ");
		fflush(stdout);
		fgets(tempbuf, sizeof(tempbuf), stdin );
		if( tempbuf[0] == 'y' || tempbuf[0] == 'Y' || tempbuf[0] == '\n')
			cflag = 1;
	    	else
	    		cflag = 0;
	    }
	    post ();
	    if ( qflag == 1 )
		cflag = 0;

/* *** auto-exit, upon successfull send, since draft is saved *** */

	    goto byebye;
	}
/*	help	*/
	if (prefix ("?", bigbuf))
	{
	    printf ("bcc\n");
	    printf ("bye\n");
	    printf ("check spelling\n");
	    printf ("edit body\n");
	    printf ("file include\n");
	    printf ("program run\n");
	    printf ("quit\n");
	    printf ("review message\n");
	    printf ("send message\n");
	    printf ("set [option] [option value]\n");
	    continue;
	}

	printf (" unknown command (type ? for help)\n");
    }				  /* end of loop */
}
Пример #6
0
int main(int argc, char *argv[])
{
	//usage error check
	if(argc < 2) { printf("usage : ./server <port number>\n"); exit(1); }
	//make port number
	int server_port = atoi(argv[1]);
	
	//socket variables
	int chat_sd;
	int game_sd;
	int listen_sd;
	struct sockaddr_in addr;
	struct timeval timeout;
	fd_set select_result;
	int on=1;
	
	//pthread variables
	pthread_t thr;

	//etc variables
	int i, j, buf_len, rc;
	int x, y, winner;
	char buffer[4096];
	char smbuf[5];

	//server init
	listen_sd = socket(AF_INET, SOCK_STREAM, 0);
	if(listen_sd<0){printf("socket error\n"); exit(2);}
	rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
	if(rc<0){printf("setsockopt error\n"); exit(2);}

	memset(&addr, 0, sizeof(addr));
	addr.sin_family 		= AF_INET;
	addr.sin_addr.s_addr 	= htonl(INADDR_ANY);
	addr.sin_port			= htons(server_port);

	rc = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr));
	if(rc < 0){printf("bind error\n"); exit(3);} 

	rc = listen(listen_sd, 32);
	if(rc < 0){printf("listen error\n"); exit(4);}

	init_set();
	add_to_set(listen_sd);

	//timeval init
	timeout.tv_sec = 3*60;
	timeout.tv_usec = 0;

	//Waiting client
	printf("Waiting client\n");
	chat_sd = accept(listen_sd, (struct sockaddr *)NULL, NULL);
	add_to_set(chat_sd);
	printf("accept chat_sd\n");

	game_sd = accept(listen_sd, (struct sockaddr *)NULL, NULL);
	add_to_set(game_sd);
	printf("accept game_sd\n");
	
	//ncurses init
	window=initscr();
	keypad(stdscr,TRUE);
	mousemask(BUTTON1_RELEASED, NULL);


	//game init
	initlog();
	init_board();
	while(1)
	{
		//your turn
		
		addlog(" < YOUR TURN > ", 3, 1);

		//make thread to check mouse
		pipe(pipefd);
		add_to_set(pipefd[0]);
		pthread_create(&thr, NULL, thr_mouse, NULL);
		while(1)
		{
			printall(1);
			memcpy(&select_result, &master_set, sizeof(master_set));
			rc = select(max_sd+1,&select_result, NULL,NULL, &timeout);
			if(rc<0) s_exit(5);
			if(rc==0) s_exit(6); //timecheck error
			if(FD_ISSET(game_sd, &select_result)) w_exit(-1);
			else if(FD_ISSET(pipefd[0],&select_result))
			{
				read(pipefd[0],smbuf,2);
				pthread_cancel(thr);
				y=smbuf[0];
				x=smbuf[1];
				board[y][x] = 1;
				send(game_sd,smbuf,2,0);
				if(winner = finish_board())
					w_exit(winner);
				break;

			}
			else if(FD_ISSET(chat_sd, &select_result))
			{
				rc = recv(chat_sd, buffer, 4096, 0);
				if(rc<0) s_exit(7);	//recv error
				if(rc == 0) w_exit(-1); //disconnected
				buffer[rc]='\0';
				addlog(buffer, 2, 1);
			}
		}
		//opp turn
		
		while(1)
		{
			printall(2);
			memcpy(&select_result, &master_set, sizeof(master_set));
			rc = select(max_sd+1, &select_result, NULL,NULL,&timeout);
			if(rc<0) s_exit(5);
			if(rc==0) s_exit(6); //timeout;
			if(FD_ISSET(game_sd, &select_result))
			{
				rc = recv(game_sd, smbuf,2 ,0);
				if(rc<0) s_exit(7); //recv error
				if(rc==0) w_exit(-1); //disconnected
				if(smbuf[0] == 't')
				{
					addlog("Opponent timeout", 3, 2);
					break;
				}
				else
				{
					y = smbuf[0];
					x = smbuf[1];
					board[y][x] = 2;
					if(winner = finish_board())
						w_exit(winner);
					break;
				}
			}
			else if(FD_ISSET(chat_sd, &select_result))
			{
				rc = recv(chat_sd, buffer, 4096,0);
				if(rc<0) s_exit(7);
				if(rc == 0) w_exit(-1);
				buffer[rc]= '\0';
				addlog(buffer, 2, 2);
			}
		}
	}
	
	s_exit(0);
}