Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <wait.h>
  6. #include <signal.h>
  7. #include <sys/types.h>
  8.  
  9. #include "data.h"
  10.  
  11.  
  12. void childSignalHandler(int signum) {
  13. int status;
  14. pid_t pid;
  15.  
  16. pid = waitpid(-1, &status, WNOHANG);
  17. }
  18.  
  19. int main(int argc, char **argv) {
  20. char bBuffer[BUFSIZ], *pArgs[10], *aPtr = NULL, *sPtr;
  21. bool background;
  22. ssize_t rBytes;
  23. int aCount;
  24. pid_t pid;
  25.  
  26. signal(SIGCHLD, childSignalHandler);
  27.  
  28. while(1) {
  29. write(1, "\e[1;31mmyBash \e[1;32m# \e[0m", 27);
  30. rBytes = read(0, bBuffer, BUFSIZ-1);
  31.  
  32. if(rBytes == -1) {
  33. perror("read");
  34. exit(1);
  35. }
  36.  
  37. bBuffer[rBytes-1] = '\0';
  38.  
  39. if(!strcasecmp(bBuffer, "exit")) {
  40. exit(0);
  41. }
  42.  
  43. sPtr = bBuffer;
  44. aCount = 0;
  45.  
  46. do {
  47. aPtr = strsep(&sPtr, " ");
  48. pArgs[aCount++] = aPtr;
  49. } while(aPtr);
  50.  
  51. background = FALSE;
  52.  
  53. if(!strcmp(pArgs[aCount-2], "&")) {
  54. pArgs[aCount-2] = NULL;
  55. background = TRUE;
  56. }
  57.  
  58. if(strlen(pArgs[0]) > 1) {
  59. pid = fork();
  60.  
  61. if(pid == -1) {
  62. perror("fork");
  63. exit(1);
  64. }
  65.  
  66. if(pid == 0) {
  67. execvp(pArgs[0], pArgs);
  68. exit(0);
  69. }
  70.  
  71. if(!background) {
  72. wait(NULL);
  73. }
  74. }
  75. }
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement