Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include<readline/readline.h>
  9. #include<readline/history.h>
  10. #define max_letters 1000
  11. #define max_command 100
  12. #define max_pipes 10
  13. int ziomek=0;
  14. char* file;
  15. int przekierowac;
  16. int x;
  17. pid_t p1;
  18. int size;
  19. char*** command;
  20. int background;
  21. int czy_pipe;
  22. int pipe_command;
  23. char history[21][max_letters * max_command];
  24. int licznik=0;
  25. void handler(int sig)
  26. {
  27. int i;
  28. printf("\n-------------------------------HISTORIA------------------------------\n");
  29. for(i=size-1;i>-1;i--)
  30. printf("%s",history[i]);
  31. exit(sig);
  32. }
  33.  
  34. void add(char * tmp){
  35. strcpy(history[size],tmp);
  36. int x;
  37. size++;
  38. if(size==21){
  39. for(x=0;x<21;x++)
  40. strcpy(history[x],history[x+1]);
  41. size=20;
  42. }
  43.  
  44. int fd = open ("hist.txt", O_RDWR | O_CREAT |O_TRUNC , 0666);
  45. for(x=0;x<size;x++)
  46. write(fd, history[x], strlen(history[x]));
  47.  
  48. }
  49.  
  50. void przekierowanie(char* output)
  51. {
  52. int fileDescriptor;
  53. fileDescriptor = open(output, O_CREAT | O_TRUNC | O_WRONLY, 0600);
  54. dup2(fileDescriptor, STDOUT_FILENO);
  55. close(fileDescriptor);
  56. }
  57.  
  58. void free_d()
  59. {
  60. int i, j;
  61. for (i = 0; i < max_pipes; i++) {
  62. for (j = 0; j < max_command; j++)
  63. free(command[i][j]);
  64. free(command[i]);
  65. }
  66. free(command);
  67. }
  68.  
  69. void alloc() /* Allocate the array */
  70. {
  71. /* Check if allocation succeeded. (check for NULL pointer) */
  72. int i, j;
  73. command = malloc(max_pipes * sizeof(char**));
  74. for (i = 0; i < max_pipes; i++) {
  75. command[i] = malloc(max_command * sizeof(char*));
  76. for (j = 0; j < max_command; j++)
  77. command[i][j] = malloc(max_letters * sizeof(char));
  78. }
  79. }
  80.  
  81. int exe_process()
  82. {
  83. if (command[0][0][0] == 'c'&&command[0][0][1] == 'd') chdir(command[0][1]);
  84.  
  85.  
  86. p1 = fork();
  87.  
  88. if (p1 == -1) {
  89. return 0;
  90. }
  91. else if (p1 == 0) {
  92. if (przekierowac)
  93. przekierowanie(file);
  94. if (execvp(command[0][0], command[0]) < 0) {
  95. return 0;
  96. }
  97. exit(0);
  98. }
  99. else {
  100.  
  101. if (!background)
  102.  
  103. wait(NULL);
  104. return 1;
  105. }
  106. }
  107.  
  108. int exe_pipped_processes()
  109. {
  110. int status;
  111. int i = 0;
  112. pid_t pid;
  113.  
  114. int pipefds[2 * pipe_command];
  115.  
  116. for (i = 0; i < (pipe_command); i++) {
  117. if (pipe(pipefds + i * 2) < 0) {
  118. perror("couldn't pipe");
  119. exit(EXIT_FAILURE);
  120. }
  121. }
  122.  
  123. int j = 0;
  124. int c = 0;
  125. while (c <= pipe_command) {
  126. pid = fork();
  127. if (pid == 0) {
  128. if (przekierowac)
  129. przekierowanie(file);
  130. if (c < pipe_command) {
  131. if (dup2(pipefds[j + 1], 1) < 0) {
  132. perror("dup2");
  133. exit(EXIT_FAILURE);
  134. }
  135. }
  136.  
  137. //if not first command&& j!= 2*numPipes
  138. if (j != 0) {
  139. if (dup2(pipefds[j - 2], 0) < 0) {
  140. perror(" dup2"); ///j-2 0 j+1 1
  141. exit(EXIT_FAILURE);
  142. }
  143. }
  144.  
  145. for (i = 0; i < 2 * pipe_command; i++) {
  146. close(pipefds[i]);
  147. }
  148.  
  149. if (execvp(command[c][0], command[c]) < 0) {
  150. perror(command[c][0]);
  151. exit(EXIT_FAILURE);
  152. }
  153. }
  154. else if (pid < 0) {
  155. perror("error");
  156. exit(EXIT_FAILURE);
  157. }
  158.  
  159. c++;
  160. j += 2;
  161. }
  162. /**Parent closes the pipes and wait for children*/
  163.  
  164. for (i = 0; i < 2 * pipe_command; i++) {
  165. close(pipefds[i]);
  166. }
  167.  
  168. for (i = 0; i < pipe_command + 1; i++)
  169. wait(&status);
  170. }
  171.  
  172. char* path()
  173. {
  174. char* cwd = malloc(max_letters);
  175. char tmp[max_letters];
  176. getcwd(tmp, sizeof(tmp));
  177. strcpy(cwd, tmp);
  178. return cwd;
  179. }
  180.  
  181. void parse(char* line)
  182. {
  183. int x, y = 0, z = 0, arg = 0;
  184. pipe_command = 0;
  185. for (x = 0; x < strlen(line); x++) {
  186.  
  187. if (line[x] == ' ' || line[x + 1] == '\0') {
  188.  
  189. if (przekierowac)
  190. strcpy(file, strndup(line + z, y));
  191.  
  192. else
  193. strcpy(command[pipe_command][arg], strndup(line + z, y));
  194.  
  195. if (line[x + 1] == '|') {
  196. x += 2;
  197. czy_pipe = 1;
  198. command[pipe_command][++arg] = NULL;
  199. pipe_command++;
  200.  
  201. arg = -1;
  202. }
  203.  
  204. if (line[x + 1] == '>' && line[x + 2] == '>') {
  205. przekierowac = 1;
  206. x += 3;
  207. }
  208. else
  209.  
  210. arg++;
  211. z = x + 1;
  212. y = -1;
  213. }
  214. y++;
  215. }
  216. if (command[pipe_command][arg - 1][0] == 'c'&&command[pipe_command][arg - 1][1] == 'd') chdir(command[pipe_command][arg]);
  217.  
  218.  
  219. if (command[pipe_command][arg - 1][0] == '&') {
  220. background = 1;
  221. command[pipe_command][arg - 1] = NULL;
  222. }
  223. else
  224.  
  225. command[pipe_command][arg] = NULL;
  226. }
  227.  
  228. void exe(char* line)
  229. {
  230. alloc();
  231. add(line);
  232. parse(line);
  233.  
  234. if (!czy_pipe) {
  235. if (!exe_process())
  236. printf("Nie znaleziono polecenia \n");
  237. }
  238. else
  239. exe_pipped_processes();
  240.  
  241. free_d();
  242. }
  243.  
  244. int main()
  245. {
  246.  
  247. int script = 0;
  248. FILE* script_file;
  249. size = 0;
  250. signal(SIGQUIT, handler);
  251. int fd;
  252. int i=0;
  253. char* command_to_parse = (char*)malloc((max_letters * max_command) * sizeof(char));
  254. file = (char*)malloc((max_letters * max_command) * sizeof(char));
  255.  
  256. FILE *history_file;
  257. int fd_0 = open("hist.txt", O_RDONLY | O_CREAT, 0666);
  258. history_file = fdopen(fd_0, "r");
  259. char bufor[100];
  260. char * tmp;
  261.  
  262. while ((tmp = fgets(bufor, sizeof(bufor), history_file)) != NULL){
  263. strcpy(history[size],tmp); size++;}
  264.  
  265.  
  266.  
  267.  
  268. while (1) {
  269.  
  270. background = 0;
  271. przekierowac = 0;
  272. czy_pipe = 0;
  273.  
  274. printf("%s ", path());
  275.  
  276. fgets(command_to_parse, max_letters * max_command, stdin);
  277. if (command_to_parse[0] == '\n')
  278. continue;
  279.  
  280. if (command_to_parse[0] == '.' && command_to_parse[1] == '/') {
  281. script = 1;
  282. fd = open(strndup(command_to_parse + 2, strlen(command_to_parse) - 3), O_RDONLY, 0666);
  283. script_file = fdopen(fd, "r");
  284. if (script_file == NULL)
  285. continue;
  286. char bufor[max_letters];
  287. while ((command_to_parse = fgets(bufor, sizeof(bufor), script_file)) != NULL)
  288. exe(command_to_parse);
  289. }
  290. else
  291.  
  292. exe(command_to_parse);
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement