Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- //de facut o konsola lol si prin fork si exec coamanda va fi executata
- //nu trebuie executat top kkt de asta
- //trebuie cat pentru std
- // toti parametri vor fi separati de spatiu
- //execvp
- //cd de implementat
- //exit
- //http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/exec.html
- char currentPath[300];
- DIR *currentDirectory;
- void ls(DIR *dir)
- {
- //ls
- struct dirent *end;
- if ( dir != NULL) {
- end= readdir (dir);
- while (end != NULL) {
- printf("%s\n",end->d_name);
- end = readdir(dir);
- }
- closedir (dir);
- }
- else {
- perror ("");
- return ;
- }
- }
- void SchimbaDirectorCurent(const char *directorDorit)
- {
- if(chdir(directorDorit) < 0)
- {
- printf("Meh\n");
- }
- else {
- if(directorDorit[0] == '/')
- strcpy(currentPath,directorDorit);
- else strcat(currentPath,directorDorit);
- }
- // if(strcmp(directorDorit,"..") == 0)
- // {
- // strcat(currentPath,"..");
- // currentDirectory = opendir(currentPath);
- // }
- // else if(directorDorit != NULL){
- // if(opendir(directorDorit) );
- // }
- }
- void Executa(char *cmd,char **arg)
- {
- pid_t pid;
- int status;
- if(strcmp(arg[0],"cd") == 0)
- {
- SchimbaDirectorCurent(arg[1]);
- return;
- }
- if((pid=fork()) < 0)
- {
- perror("eroare la fork\n");
- }
- else if(pid == 0)
- {
- if(execvp(cmd,arg) < 0 )
- {
- printf("Eroare la execl\n");
- exit(1);
- }
- }
- else {
- wait(&status);
- // printf("Am terminat de asteptat si am primit %d\n",status);
- }
- }
- int ParsareInput(char *input,char *argv[])
- {
- char *p;
- int len=0;
- p=strtok(input," ");
- //printf("Parsare baetiii : \n")\;
- while(p)
- {
- argv[len] = malloc(sizeof(char) * 25);
- strcpy(argv[len],p);
- // printf("%s\n",argv[len]);
- len++;
- p=strtok(NULL," ");
- }
- argv[len]=NULL;
- argv[len-1][strlen(argv[len-1])-1]=0;
- return len;
- }
- void EliminaSpatiiInput(char input[])
- {
- // printf("am intrat |%c| \n",input[strlen(input)-1]);
- for(int i=strlen(input)-1;input[i]==' ';i--)
- input[i]=0;//,printf("am gasit spatiu\n");
- }
- void Dealocare(char *argv[],int len)
- {
- for(int i=0;i<len;i++)
- free(argv[i]);
- }
- void PathInitial()
- {
- strcpy(currentPath,".");
- printf("Dati un path de inceput\n");
- char *linie;
- fgets(linie,256,stdin);
- if(!opendir(linie))
- {
- strcpy(currentPath,linie);
- currentPath[strlen(currentPath)-1]=0;
- }
- else {
- return;
- }
- }
- int main(int argc,char *argv[])
- {
- char machine[300];
- strcpy(machine,"[lucian@l440");
- PathInitial();
- chdir(currentPath);
- while(1)
- {
- printf("%s %s]:",machine,currentPath);
- currentDirectory=opendir(currentPath);
- char *args[64];
- char linie[256];
- fgets(linie , 256, stdin);
- EliminaSpatiiInput(linie);
- if(strcmp(linie,"exit\n") == 0 )
- return 1;
- int lg=ParsareInput(linie,args);
- Exec int lg=ParsareInput(linie,args)uta(args[0],args);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement