Advertisement
Guest User

Untitled

a guest
Aug 29th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.55 KB | None | 0 0
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h> //linux
  8. #include <fcntl.h>
  9. #include <time.h>
  10. #include <string.h>
  11.  
  12. #define HST ".historia"
  13.  
  14.  
  15. int cmd2int(char * cmd) { //zamienia string cmd na inta do funkcji switch.
  16.     if(strcmp(cmd,"cd")==0) return 1;
  17.     else if(strcmp(cmd,"run")==0) return 2;
  18.     else if(strcmp(cmd,"history")==0) return 3;
  19.     else if(strcmp(cmd,"quit")==0) return 0;
  20.     else return 999; //każdy ciąg znaków nie uwzględniony wyżej z powrotem wejdzie do pętli
  21.  
  22. }
  23.  
  24. void history(char command[], char * startdir) { //funkcja odpowiadająca za zapisanie polecenia do historii
  25.     int prvdir= get_current_dir_name();
  26.     chdir(startdir);
  27.     FILE * hfile;
  28.     hfile=fopen(HST, "a"); //otwarcie/stworzenie pliku, prawa oznaczone jako "a" pozwalają na dopisanie nowych danych na końcu pliku, bez nadpisywania starych
  29.     if(!hfile) {
  30.         printf ("Nie mogę otworzyć pliku .historia do zapisu!\n");
  31.         exit(1);
  32.     }
  33.     fprintf (hfile,"%s\n", command); //wpisywanie komendy w plik
  34.     fclose (hfile); //zamknięcie pliku
  35.     chdir(prvdir);
  36. }
  37.  
  38. void showhistory(char * startdir) { //funkcja wyświetlająca historie
  39.     int prvdir= get_current_dir_name();
  40.     chdir(startdir);
  41.     FILE * hfile;
  42.     char bufor[256];
  43.     hfile=fopen(HST, "r"); //otworzenie pliku tylko do odczytu
  44.     if(!hfile) {
  45.         printf("Nie mogę otworzyć pliku .historia do odczytu lub plik jeszcze nie istnieje!\n");
  46.         exit(-1);
  47.     }
  48.     while(fgets(bufor,256,hfile)) { //szczytywanie danych z pliku
  49.         printf("%s", bufor);
  50.     }
  51.     fclose(hfile); //zamknięcie pliku
  52.     chdir(prvdir);
  53. }
  54. //rodzina funkcji do uruchomienia zewnetrznych polecen
  55. void run1(char * arg1) {
  56.     pid_t proc;
  57.     proc=fork();
  58.     if(proc>0) {
  59.     printf("Wykonuje polecenie %s\n",arg1);
  60.  
  61.     pid_t waiting;
  62.     waiting=wait(&proc);
  63.     if(waiting>0) printf("Wykonano proces\n");
  64.     }
  65.     else {
  66.     execlp(arg1,NULL);
  67.     }
  68. }
  69. void run2(char * arg1, char * arg2) {
  70.     pid_t proc;
  71.     proc=fork();
  72.     if(proc>0) {
  73.     printf("Wykonuje polecenie %s %s\n",arg1,arg2);
  74.  
  75.     pid_t waiting;
  76.     waiting=wait(&proc);
  77.     if(waiting>0) printf("Wykonano proces\n");
  78.     }
  79.     else {
  80.     execlp(arg1,arg2,NULL);
  81.     }
  82. }
  83. void run3(char * arg1, char * arg2, char * arg3) {
  84.     pid_t proc;
  85.     proc=fork();
  86.     if(proc>0) {
  87.     printf("Wykonuje polecenie %s %s %s\n",arg1,arg2,arg3);
  88.  
  89.     pid_t waiting;
  90.     waiting=wait(&proc);
  91.     if(waiting>0) printf("Wykonano proces\n");
  92.     }
  93.     else {
  94.     execlp(arg1,arg2,arg3,NULL);}
  95. }
  96.  
  97.  
  98. int cpk(char command[]) {
  99.     int i=0;
  100.     int state=0;
  101.     for(i=0;i<strlen(command);i++)
  102.     {
  103.     if(command[i]=='\"')
  104.     state=1;
  105.     }
  106.     return state; }
  107.  
  108. int main() {
  109.     char * startdir = get_current_dir_name();
  110.     const MAX_SIZE=1024; //max rozmiar
  111.     char command[MAX_SIZE]; //command przechowuje wpisana komende
  112.     int icmd=1; //icmd to wartosc liczbowa komendy zwrocona z funkcji
  113.     char * cmd; //glowna komenda
  114.     char * arg1; //argument 1
  115.     char * arg2; //argument 2
  116.     char * arg3; //argument 3
  117.     int cpk1=0;
  118.  
  119.     while (icmd != 0) { //główna pęta do wczytywania komendy;
  120.         printf("%s", get_current_dir_name()); //aktualny katalog
  121.         printf("$ "); //znak zachęty - jeszcze nie działa
  122.         fgets(command, MAX_SIZE, stdin);; //pobieranie polecenia wraz ze spacjami
  123.         history(command,startdir); //zapisuje string komendy do historii
  124.         cpk1=cpk(command);
  125.         printf("%d\n", cpk1);
  126.         if(cpk1==0)
  127.         {
  128.         cmd=strtok(command," ,-\n");
  129.         printf("Komenda: %s\n",cmd);
  130.         arg1=strtok(NULL," ,-\n");
  131.         printf("Argument 1: %s\n",arg1);
  132.         arg2=strtok(NULL," ,-\n");
  133.         printf("Argument 2: %s\n",arg2);
  134.         arg3=strtok(NULL," ,-\n");
  135.         printf("Argument 3: %s\n",arg3);
  136.         }
  137.         else
  138.         {
  139.         cmd=strtok(command," ,-\n");
  140.         printf("Komenda: %s\n",cmd);
  141.         arg1=strtok(NULL,",-\n");
  142.         printf("Argument 1: %s\n",arg1);
  143.         arg2=strtok(NULL," ,-\n");
  144.         printf("Argument 2: %s\n",arg2);
  145.         arg3=strtok(NULL," ,-\n");
  146.         printf("Argument 3: %s\n",arg3);
  147.         }
  148.         icmd=cmd2int(cmd);  //icmd = wersja int polecenia
  149.  
  150.         if (arg1 != NULL)
  151.         {
  152.             if(strcmp(arg1,">")==0) {
  153.                 FILE * hfile;
  154.                 hfile=fopen(arg2,"w");
  155.                 if(!hfile) {
  156.                     printf("Nie mogę stworzyć pliku!\n");
  157.                     exit(1);
  158.                 }
  159.                 fprintf(hfile,"%s\n",cmd);
  160.                 fclose(hfile);
  161.             }
  162.             else if(strcmp(arg1,">>")==0) {
  163.                 FILE * hfile;
  164.                 hfile=fopen(arg2,"a");
  165.                 if(!hfile) {
  166.                     printf("Nie mogę otworzyć pliku!\n");
  167.                     exit(1);
  168.                 }
  169.                 fprintf(hfile,"%s\n",cmd);
  170.                 fclose(hfile);
  171.             }
  172.         }
  173.  
  174.         if (arg2 != NULL)
  175.         {
  176.             if(strcmp(arg2,">")==0) {
  177.                 FILE * hfile;
  178.                 hfile=fopen(arg3,"w");
  179.                 if(!hfile) {
  180.                     printf("Nie mogę stworzyć pliku!\n");
  181.                     exit(1);
  182.                 }
  183.                 fprintf(hfile,"%s\n",cmd);
  184.                 fclose(hfile);
  185.             }
  186.             else if(strcmp(arg2,">>")==0) {
  187.                 FILE * hfile;
  188.                 hfile=fopen(arg3,"a");
  189.                 if(!hfile) {
  190.                     printf("Nie mogę otworzyć pliku!\n");
  191.                     exit(1);
  192.                 }
  193.                 fprintf(hfile,"%s\n",cmd);
  194.                 fclose(hfile);
  195.             }
  196.         }
  197.  
  198.         switch(icmd) {  //obsługa polecenia
  199.             case 1: //cd
  200.                 chdir(arg1);
  201.                 break;
  202.             case 2: //run
  203.                 if (arg2==NULL)
  204.                     run1(arg1); //execl dziala tylko na linuxie - do sprawdzenia
  205.                 else if (arg3==NULL)
  206.                     run2(arg1,arg2);
  207.                 else
  208.                     run3(arg1,arg2,arg3);
  209.                 break;
  210.             case 3: //history
  211.                 showhistory(startdir);
  212.                 break;
  213.             case 4: //
  214.                 break;
  215.             case 0:
  216.                 icmd=0; //zamyka program
  217.                 break;
  218.             default:
  219.                 printf("Take polecenie nie istnieje\n");
  220.                 break;
  221.             }
  222.         printf("\n");
  223.  
  224.     }
  225.  
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement