funkcja cut

This commit is contained in:
Jakub Adamski 2019-01-10 13:13:37 +01:00
parent 3073884260
commit 86cd2fe0c0
1 changed files with 41 additions and 56 deletions

View File

@ -5,64 +5,49 @@
const int max = 1000; const int max = 1000;
int cut(char **argv, char *command){ void cut(char **argv, char *command){
int i=0; int i = 0, w = 0, x = 0;
return i; char argument[100];
while (1){
if(command[i] == 10){
argv[w] = NULL;
break;
}
else if (command[i] == 32){
i+=1;
x=0;
continue;
}
else{
while(command[i] != 32 && command[i]!=10){
argument[x]=command[i];
x+=1;
i+=1;
}
argument[x] = '\0';
argv[w]=malloc(x+1 * sizeof(char));
strcpy(argv[w], argument);
w+=1;
}
}
} }
int main (){ int main (){
while (1){ char* argv[100];
char komenda[max], directory[max]; while (1){
if (getcwd(directory, sizeof(directory)) == NULL){ char komenda[max], directory[max];
perror("getcwd() error"); if (getcwd(directory, sizeof(directory)) == NULL){
return 1; perror("getcwd() error");
} return 1;
printf("[%s]$ ", directory); }
fgets(komenda, max, stdin); printf("[%s]$ ", directory);
if (!strcmp(komenda, "exit\n")) fgets(komenda, max, stdin);
break;
else{ if (!strcmp(komenda, "exit\n"))
int i = 0, z, w = 0, x = 0; break;
char polecenie[100], argument[100]; else
char* argv[100]; cut(argv, komenda);
while (1){ execvp(argv[0], argv);
if(komenda[i] == 10){ }
argv[w] = NULL; return 0;
execvp(polecenie, argv);
break;
}
else if (komenda[i] == 32){
i+=1;
x=0;
continue;
}
else if(w==0){
w=1;
while(komenda[i] != 32 && komenda[i]!=10){
polecenie[x]=komenda[i];
i+=1;
x+=1;
}
polecenie[x] = '\0';
printf("%s\n", polecenie);
argv[w-1]=malloc(x+1 * sizeof(char));
strcpy(argv[w-1], polecenie);
continue;
}
else{
while(komenda[i] != 32 && komenda[i]!=10){
argument[x]=komenda[i];
x+=1;
i+=1;
}
argument[x] = '\0';
argv[w]=malloc(x+1 * sizeof(char));
strcpy(argv[w], argument);
w+=1;
}
}
}
}
return 0;
} }