Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <sys/stat.h>
  8. #include <errno.h>
  9.  
  10. #define CMDLEN 80
  11.  
  12. int main()
  13. {
  14. int pid, clientId = 0, clientNum = 0;
  15. int status, fd;
  16. char command [ CMDLEN ];
  17.  
  18. for (;;)
  19. {
  20. printf ( "CONSOLE: Please enter a command: \n" );
  21. //fflush ( stdout);
  22.  
  23. fgets ( command, CMDLEN, stdin );
  24.  
  25. command [ strlen ( command ) - 1 ] = '\0';
  26.  
  27. if ( strcmp ( command, "quit" ) == 0 )
  28. break;
  29.  
  30. pid = fork ( );
  31.  
  32. if ( pid != 0 )
  33. {
  34. waitpid ( pid, &status, 0 );
  35.  
  36. dup2 ( fd, STDOUT_FILENO );
  37.  
  38. printf ( "CONSOLE\n" );
  39. }
  40. else
  41. {
  42. if ( ( fd = open ( "clients.log", O_WRONLY | O_APPEND )) < 0 )
  43. printf ( "ERROR %s", strerror ( errno ) );
  44. else
  45. dup2 ( fd, STDOUT_FILENO );
  46.  
  47. printf ( "Client %d: %s\n", clientId, command );
  48.  
  49. execlp ( command, command, NULL );
  50.  
  51. fflush ( stdout );
  52.  
  53. _exit ( 1 );
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement