Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h> //linux
- #include <fcntl.h>
- #include <time.h>
- #include <string.h>
- #define HST ".historia"
- int cmd2int(char * cmd) { //zamienia string cmd na inta do funkcji switch.
- if(strcmp(cmd,"cd")==0) return 1;
- else if(strcmp(cmd,"run")==0) return 2;
- else if(strcmp(cmd,"history")==0) return 3;
- else if(strcmp(cmd,"quit")==0) return 0;
- else return 999; //każdy ciąg znaków nie uwzględniony wyżej z powrotem wejdzie do pętli
- }
- void history(char command[], char * startdir) { //funkcja odpowiadająca za zapisanie polecenia do historii
- int prvdir= get_current_dir_name();
- chdir(startdir);
- FILE * hfile;
- hfile=fopen(HST, "a"); //otwarcie/stworzenie pliku, prawa oznaczone jako "a" pozwalają na dopisanie nowych danych na końcu pliku, bez nadpisywania starych
- if(!hfile) {
- printf ("Nie mogę otworzyć pliku .historia do zapisu!\n");
- exit(1);
- }
- fprintf (hfile,"%s\n", command); //wpisywanie komendy w plik
- fclose (hfile); //zamknięcie pliku
- chdir(prvdir);
- }
- void showhistory(char * startdir) { //funkcja wyświetlająca historie
- int prvdir= get_current_dir_name();
- chdir(startdir);
- FILE * hfile;
- char bufor[256];
- hfile=fopen(HST, "r"); //otworzenie pliku tylko do odczytu
- if(!hfile) {
- printf("Nie mogę otworzyć pliku .historia do odczytu lub plik jeszcze nie istnieje!\n");
- exit(-1);
- }
- while(fgets(bufor,256,hfile)) { //szczytywanie danych z pliku
- printf("%s", bufor);
- }
- fclose(hfile); //zamknięcie pliku
- chdir(prvdir);
- }
- //rodzina funkcji do uruchomienia zewnetrznych polecen
- void run1(char * arg1) {
- pid_t proc;
- proc=fork();
- if(proc>0) {
- printf("Wykonuje polecenie %s\n",arg1);
- pid_t waiting;
- waiting=wait(&proc);
- if(waiting>0) printf("Wykonano proces\n");
- }
- else {
- execlp(arg1,NULL);
- }
- }
- void run2(char * arg1, char * arg2) {
- pid_t proc;
- proc=fork();
- if(proc>0) {
- printf("Wykonuje polecenie %s %s\n",arg1,arg2);
- pid_t waiting;
- waiting=wait(&proc);
- if(waiting>0) printf("Wykonano proces\n");
- }
- else {
- execlp(arg1,arg2,NULL);
- }
- }
- void run3(char * arg1, char * arg2, char * arg3) {
- pid_t proc;
- proc=fork();
- if(proc>0) {
- printf("Wykonuje polecenie %s %s %s\n",arg1,arg2,arg3);
- pid_t waiting;
- waiting=wait(&proc);
- if(waiting>0) printf("Wykonano proces\n");
- }
- else {
- execlp(arg1,arg2,arg3,NULL);}
- }
- int cpk(char command[]) {
- int i=0;
- int state=0;
- for(i=0;i<strlen(command);i++)
- {
- if(command[i]=='\"')
- state=1;
- }
- return state; }
- int main() {
- char * startdir = get_current_dir_name();
- const MAX_SIZE=1024; //max rozmiar
- char command[MAX_SIZE]; //command przechowuje wpisana komende
- int icmd=1; //icmd to wartosc liczbowa komendy zwrocona z funkcji
- char * cmd; //glowna komenda
- char * arg1; //argument 1
- char * arg2; //argument 2
- char * arg3; //argument 3
- int cpk1=0;
- while (icmd != 0) { //główna pęta do wczytywania komendy;
- printf("%s", get_current_dir_name()); //aktualny katalog
- printf("$ "); //znak zachęty - jeszcze nie działa
- fgets(command, MAX_SIZE, stdin);; //pobieranie polecenia wraz ze spacjami
- history(command,startdir); //zapisuje string komendy do historii
- cpk1=cpk(command);
- printf("%d\n", cpk1);
- if(cpk1==0)
- {
- cmd=strtok(command," ,-\n");
- printf("Komenda: %s\n",cmd);
- arg1=strtok(NULL," ,-\n");
- printf("Argument 1: %s\n",arg1);
- arg2=strtok(NULL," ,-\n");
- printf("Argument 2: %s\n",arg2);
- arg3=strtok(NULL," ,-\n");
- printf("Argument 3: %s\n",arg3);
- }
- else
- {
- cmd=strtok(command," ,-\n");
- printf("Komenda: %s\n",cmd);
- arg1=strtok(NULL,",-\n");
- printf("Argument 1: %s\n",arg1);
- arg2=strtok(NULL," ,-\n");
- printf("Argument 2: %s\n",arg2);
- arg3=strtok(NULL," ,-\n");
- printf("Argument 3: %s\n",arg3);
- }
- icmd=cmd2int(cmd); //icmd = wersja int polecenia
- if (arg1 != NULL)
- {
- if(strcmp(arg1,">")==0) {
- FILE * hfile;
- hfile=fopen(arg2,"w");
- if(!hfile) {
- printf("Nie mogę stworzyć pliku!\n");
- exit(1);
- }
- fprintf(hfile,"%s\n",cmd);
- fclose(hfile);
- }
- else if(strcmp(arg1,">>")==0) {
- FILE * hfile;
- hfile=fopen(arg2,"a");
- if(!hfile) {
- printf("Nie mogę otworzyć pliku!\n");
- exit(1);
- }
- fprintf(hfile,"%s\n",cmd);
- fclose(hfile);
- }
- }
- if (arg2 != NULL)
- {
- if(strcmp(arg2,">")==0) {
- FILE * hfile;
- hfile=fopen(arg3,"w");
- if(!hfile) {
- printf("Nie mogę stworzyć pliku!\n");
- exit(1);
- }
- fprintf(hfile,"%s\n",cmd);
- fclose(hfile);
- }
- else if(strcmp(arg2,">>")==0) {
- FILE * hfile;
- hfile=fopen(arg3,"a");
- if(!hfile) {
- printf("Nie mogę otworzyć pliku!\n");
- exit(1);
- }
- fprintf(hfile,"%s\n",cmd);
- fclose(hfile);
- }
- }
- switch(icmd) { //obsługa polecenia
- case 1: //cd
- chdir(arg1);
- break;
- case 2: //run
- if (arg2==NULL)
- run1(arg1); //execl dziala tylko na linuxie - do sprawdzenia
- else if (arg3==NULL)
- run2(arg1,arg2);
- else
- run3(arg1,arg2,arg3);
- break;
- case 3: //history
- showhistory(startdir);
- break;
- case 4: //
- break;
- case 0:
- icmd=0; //zamyka program
- break;
- default:
- printf("Take polecenie nie istnieje\n");
- break;
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement