Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <errno.h>
- #define CMDLEN 80
- int main()
- {
- int pid, clientId = 0, clientNum = 0;
- int status, fd;
- char command [ CMDLEN ];
- for (;;)
- {
- printf ( "CONSOLE: Please enter a command: \n" );
- //fflush ( stdout);
- fgets ( command, CMDLEN, stdin );
- command [ strlen ( command ) - 1 ] = '\0';
- if ( strcmp ( command, "quit" ) == 0 )
- break;
- pid = fork ( );
- if ( pid != 0 )
- {
- waitpid ( pid, &status, 0 );
- dup2 ( fd, STDOUT_FILENO );
- printf ( "CONSOLE\n" );
- }
- else
- {
- if ( ( fd = open ( "clients.log", O_WRONLY | O_APPEND )) < 0 )
- printf ( "ERROR %s", strerror ( errno ) );
- else
- dup2 ( fd, STDOUT_FILENO );
- printf ( "Client %d: %s\n", clientId, command );
- execlp ( command, command, NULL );
- fflush ( stdout );
- _exit ( 1 );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement