Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include<signal.h>
  9.  
  10.  
  11. struct parseInfo {
  12. int type; // niewykorzystywany w programie, potrzebny przy jego rozbudowie
  13. int redirect; // przekierowanie >>
  14. int background; // proces w tle &
  15. int flagCount; // licznik flag
  16. int piped; // wskaźnik na |
  17. char* redirect_file;
  18. char* command;
  19. char* restCommand;
  20. char** flags;
  21. };
  22.  
  23. void parseInfoInit(struct parseInfo *parseInfo_s);
  24. struct parseInfo *parse(char *cmdline);
  25. struct parseInfo *parseCommand(char* command, struct parseInfo* cmd);
  26. char* readline();
  27. void history(char* line);
  28. void handlerSIGQUIT(int signum);
  29.  
  30.  
  31. int main()
  32. {
  33. signal(SIGQUIT, handlerSIGQUIT);
  34. struct parseInfo* cmd;
  35. while(1)
  36. {
  37. int childPid;
  38. char * cmdLine;
  39. cmdLine = readline(); // sczytywanie linii
  40. history(cmdLine); // dodawanie do historii
  41. cmd = parse(cmdLine); // rozdział linii
  42. free(cmdLine);
  43. if(cmd->type == 1)
  44. {
  45. }
  46. else
  47. {
  48. while(1)
  49. { // podział na procesy w zależności od wartości flag piped i background
  50. if(cmd->piped == 0)
  51. {
  52.  
  53. childPid = fork();
  54. if(childPid == 0)
  55. {
  56. char* command = cmd->command;
  57. char** argv = cmd->flags;
  58. int k;
  59. int fd = open("temp.txt", O_RDWR | O_CREAT | O_TRUNC, 0666);
  60.  
  61.  
  62. char l;
  63. if(cmd->redirect == 0)
  64. {
  65. if(k = execvp(command, argv) < 0)
  66. fprintf(stderr, "Command Error\n%d\n", k);
  67. }
  68. else
  69. {
  70. int fd = open(cmd->redirect_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  71. dup2(fd, 1);
  72. if(k = execvp(command, argv) < 0)
  73. fprintf(stderr, "Command Error\n%d\n", k);
  74. close(fd);
  75. }
  76. }
  77. break;
  78. }
  79. else
  80. {
  81. int fd = open("temp.txt", O_RDWR | O_CREAT | O_TRUNC, 0666);
  82. char* l;
  83. childPid = fork();
  84. if(childPid == 0)
  85. {
  86. char* command = cmd->command;
  87. char** argv = cmd->flags;
  88. int k;
  89.  
  90. close(1);
  91. dup(fd);
  92.  
  93. if(k = execvp(command, argv) < 0)
  94. fprintf(stderr, "Command Error\n%d\n", k);
  95.  
  96. }
  97. char* vol = cmd->restCommand;
  98. free(cmd);
  99. cmd = parse(cmd->restCommand);
  100. close(fd);
  101. }
  102.  
  103. if(cmd->background == 1);
  104. else
  105. {
  106. waitpid(childPid);
  107. }
  108.  
  109. }
  110.  
  111. }
  112.  
  113. }
  114.  
  115. return 0;
  116. }
  117.  
  118. void parseInfoInit(struct parseInfo* parseInfo_s) // inicjalizacja struktury
  119. {
  120. parseInfo_s->type = 0;
  121. parseInfo_s->redirect = 0;
  122. parseInfo_s->flagCount = 0;
  123. parseInfo_s->background = 0;
  124. parseInfo_s->piped = 0;
  125. parseInfo_s->redirect_file = (char*)NULL;
  126. parseInfo_s->command = (char*)NULL;
  127. parseInfo_s->restCommand = (char*)NULL;
  128. parseInfo_s->flags = (char**)NULL;
  129. }
  130.  
  131. struct parseInfo *parse(char *cmdline) // parser. Pobiera linie danych i je rozdziela na poszczególne elementy
  132. { // najwazniejszy element programu
  133. char* tab = (char*)NULL;
  134. char* help;
  135. int i = 0, j = 0;
  136. int size = 0;
  137. struct parseInfo* cmd = malloc(sizeof(struct parseInfo));
  138. parseInfoInit(cmd);
  139. while(cmdline[i] != '\0')
  140. {
  141. size = 0;
  142. tab = (char*) NULL;
  143. while(cmdline[i] != ' ' && cmdline[i] != '\0') // wczytuje dane do tablicy do póki nie trafi na spację lub koniec pliku
  144. {
  145. help = tab;
  146. tab = malloc(sizeof(char) * (++size));
  147. for(j = 0; j < size-1; ++j)
  148. {
  149. tab[j] = help[j];
  150. }
  151. free(help);
  152. tab[size-1] = cmdline[i];
  153. ++i;
  154. }
  155. if(cmdline[i] == ' ')
  156. {
  157. ++i;
  158. }
  159. if(cmdline[i] == '|') // trafia na potok. Zapisuje komende do zmiennej restCommand
  160. {
  161. i += 2; // bo spacja po pipe
  162. cmd->piped = 1;
  163. int j = 1;
  164. int k = 0;
  165. cmd->restCommand = (char*)NULL;
  166. char* help1;
  167. while(cmdline[i] != '\0')
  168. {
  169. help1 = cmd->restCommand;
  170. cmd->restCommand = malloc(sizeof(char)*(j+2));
  171. for(k = 0; k < (j-1); k++)
  172. {
  173. cmd->restCommand[k] = help1[k];
  174. }
  175. cmd->restCommand[j-1] = cmdline[i];
  176. cmd->restCommand[j] = '\0';
  177. free(help1);
  178. i++;
  179. j++;
  180. }
  181. help = cmd->restCommand;
  182. int temp = strlen(help);
  183. cmd->restCommand = malloc(sizeof(char)*temp+1);
  184. strcpy(cmd->restCommand, help);
  185. cmd->restCommand[temp] = '\0';
  186. free(help);
  187.  
  188. help = tab;
  189. tab = malloc(sizeof(char) *(++size));
  190. strcpy(tab, help);
  191. tab[size-1] = '\0';
  192. free(help);
  193.  
  194. cmd = parseCommand(tab, cmd);
  195. return cmd;
  196. }
  197. help = tab;
  198. tab = malloc(sizeof(char) *(++size));
  199. strcpy(tab, help);
  200. tab[size-1] = '\0';
  201. free(help);
  202. cmd = parseCommand(tab, cmd);
  203. }
  204. return cmd;
  205. }
  206.  
  207. struct parseInfo* parseCommand(char* command, struct parseInfo* cmd) // ustawnianie flag sczytywanych w main()
  208. {
  209. if(cmd->command == (char*)NULL)
  210. {
  211. cmd->command = command;
  212. }
  213. if(cmd->flags == (char**)NULL)
  214. {
  215. cmd->flagCount++;
  216. char **flag = malloc(sizeof(char*)*(cmd->flagCount+1));
  217. flag[0] = command;
  218. flag[1] = NULL;
  219. cmd->flags = flag;
  220. }
  221.  
  222. else if(!strcmp(command, "&"))
  223. {
  224. cmd->background = 1;
  225. }
  226. else if(!strcmp(command, ">>"))
  227. {
  228. cmd->redirect = 2;
  229. }
  230. else if(cmd->redirect == 2)
  231. {
  232. cmd->redirect_file = command;
  233. cmd->redirect = 1;
  234. }
  235. else
  236. {
  237. cmd->flagCount++;
  238. int i = 0;
  239. char **help = cmd->flags;
  240. char **flag = malloc(sizeof(char*)*(cmd->flagCount+1));
  241. for(i = 0; i < cmd->flagCount; ++i)
  242. {
  243. flag[i] = help[i];
  244. }
  245. free(help);
  246. flag[cmd->flagCount-1] = command;
  247. flag[cmd->flagCount] = NULL;
  248. cmd->flags = flag;
  249. }
  250.  
  251. return cmd;
  252. }
  253.  
  254. char* readline() // funkcja sczytująca linię ze standardowego wyjścia
  255. {
  256. int c, i;
  257. int size = 0;
  258. char* help;
  259. char* line = (char*)NULL;
  260. while((c = getchar()) != '\n')
  261. {
  262. help = line;
  263. line = (char*)malloc(sizeof(char) * (++size));
  264. for(i = 0; i < (size-1); i++)
  265. {
  266. line[i] = help[i];
  267. }
  268. free(help);
  269. line[size-1] = c;
  270. }
  271. help = line;
  272. line = (char*)malloc(sizeof(char) * (++size));
  273. for(i = 0; i < (size-1); i++)
  274. {
  275. line[i] = help[i];
  276. }
  277. line[size-1] = '\0';
  278. free(help);
  279. return line;
  280. }
  281.  
  282. void history(char* line) // zbiera historie do 20 komend
  283. {
  284. if(*line == '\0') // pusta linia, nie zapisywana do historii
  285. return;
  286. int fd = open ("history.txt", O_RDWR | O_CREAT, 0666);
  287. char l;
  288. int count = 0;
  289. off_t pos = lseek(fd, 0, SEEK_SET);
  290. while(1 == read(fd, &l, sizeof(l))) // licznik od pozycji 0
  291. {
  292. if(l == '\n')
  293. {
  294. count++;
  295. }
  296. }
  297. if(count > 19)
  298. {
  299. pos = lseek(fd, 0, SEEK_SET); //nadpisanie najstarszej wartości
  300. count = 0;
  301. while(1 == read(fd, &l, sizeof(l)))
  302. {
  303. count++;
  304. if(l == '\n')
  305. break;
  306. }
  307. pos = lseek(fd, 0, SEEK_SET);
  308. char* wr = malloc(sizeof(char)*count);
  309. memset(wr, '\0', count);
  310. write(fd, wr, count);
  311. }
  312.  
  313. pos = lseek(fd, 0, SEEK_END);
  314. size_t len = strlen(line);
  315. write(fd,line, len);
  316. write(fd, (char*)"\r\n", 2);
  317. close(fd);
  318. }
  319.  
  320. void handlerSIGQUIT(int signum) // wyświetlenie całej historii
  321. {
  322. int fd = open ("history.txt", O_RDWR | O_CREAT, 0666);
  323. char buffer[16];
  324. while(16 <= read(fd, buffer, sizeof(buffer)))
  325. {
  326. printf("%s", buffer);
  327. }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement