Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <dirent.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <sys/wait.h>
  10. //de facut o konsola lol si prin fork si exec coamanda va fi executata
  11. //nu trebuie executat top kkt de asta
  12. //trebuie cat pentru std
  13. // toti parametri vor fi separati de spatiu
  14. //execvp
  15. //cd de implementat
  16. //exit
  17.  
  18. //http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/exec.html
  19.  
  20. char currentPath[300];
  21. DIR *currentDirectory;
  22.  
  23. void ls(DIR *dir)
  24. {
  25. //ls
  26. struct dirent *end;
  27. if ( dir != NULL) {
  28. end= readdir (dir);
  29. while (end != NULL) {
  30. printf("%s\n",end->d_name);
  31. end = readdir(dir);
  32. }
  33. closedir (dir);
  34. }
  35. else {
  36. perror ("");
  37. return ;
  38. }
  39. }
  40.  
  41. void SchimbaDirectorCurent(const char *directorDorit)
  42. {
  43. if(chdir(directorDorit) < 0)
  44. {
  45. printf("Meh\n");
  46. }
  47. else {
  48. if(directorDorit[0] == '/')
  49. strcpy(currentPath,directorDorit);
  50. else strcat(currentPath,directorDorit);
  51. }
  52. // if(strcmp(directorDorit,"..") == 0)
  53. // {
  54. // strcat(currentPath,"..");
  55. // currentDirectory = opendir(currentPath);
  56. // }
  57. // else if(directorDorit != NULL){
  58. // if(opendir(directorDorit) );
  59.  
  60. // }
  61. }
  62.  
  63. void Executa(char *cmd,char **arg)
  64. {
  65. pid_t pid;
  66. int status;
  67.  
  68. if(strcmp(arg[0],"cd") == 0)
  69. {
  70. SchimbaDirectorCurent(arg[1]);
  71. return;
  72. }
  73.  
  74. if((pid=fork()) < 0)
  75. {
  76. perror("eroare la fork\n");
  77. }
  78. else if(pid == 0)
  79. {
  80. if(execvp(cmd,arg) < 0 )
  81. {
  82. printf("Eroare la execl\n");
  83. exit(1);
  84. }
  85. }
  86. else {
  87. wait(&status);
  88. // printf("Am terminat de asteptat si am primit %d\n",status);
  89. }
  90.  
  91. }
  92.  
  93. int ParsareInput(char *input,char *argv[])
  94. {
  95. char *p;
  96. int len=0;
  97.  
  98. p=strtok(input," ");
  99. //printf("Parsare baetiii : \n")\;
  100. while(p)
  101. {
  102. argv[len] = malloc(sizeof(char) * 25);
  103. strcpy(argv[len],p);
  104.  
  105. // printf("%s\n",argv[len]);
  106. len++;
  107. p=strtok(NULL," ");
  108. }
  109. argv[len]=NULL;
  110.  
  111. argv[len-1][strlen(argv[len-1])-1]=0;
  112. return len;
  113. }
  114.  
  115. void EliminaSpatiiInput(char input[])
  116. {
  117. // printf("am intrat |%c| \n",input[strlen(input)-1]);
  118. for(int i=strlen(input)-1;input[i]==' ';i--)
  119. input[i]=0;//,printf("am gasit spatiu\n");
  120. }
  121.  
  122. void Dealocare(char *argv[],int len)
  123. {
  124. for(int i=0;i<len;i++)
  125. free(argv[i]);
  126. }
  127.  
  128. void PathInitial()
  129. {
  130. strcpy(currentPath,".");
  131. printf("Dati un path de inceput\n");
  132. char *linie;
  133. fgets(linie,256,stdin);
  134. if(!opendir(linie))
  135. {
  136. strcpy(currentPath,linie);
  137. currentPath[strlen(currentPath)-1]=0;
  138. }
  139. else {
  140. return;
  141. }
  142. }
  143.  
  144. int main(int argc,char *argv[])
  145. {
  146.  
  147. char machine[300];
  148. strcpy(machine,"[lucian@l440");
  149.  
  150. PathInitial();
  151.  
  152. chdir(currentPath);
  153.  
  154.  
  155. while(1)
  156. {
  157.  
  158. printf("%s %s]:",machine,currentPath);
  159.  
  160. currentDirectory=opendir(currentPath);
  161.  
  162. char *args[64];
  163.  
  164. char linie[256];
  165.  
  166. fgets(linie , 256, stdin);
  167.  
  168. EliminaSpatiiInput(linie);
  169.  
  170. if(strcmp(linie,"exit\n") == 0 )
  171. return 1;
  172.  
  173. int lg=ParsareInput(linie,args);
  174.  
  175. Exec int lg=ParsareInput(linie,args)uta(args[0],args);
  176.  
  177. }
  178.  
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement