some improvements

This commit is contained in:
unknown 2019-02-23 15:42:05 +01:00
parent 519b265164
commit 7739a2a6d6

View File

@ -84,36 +84,35 @@ for(int i=0;i<=lenght-1; i++){
} }
void cd(char command[BUFFERSIZE]){ void cd(char command[BUFFERSIZE]){
char *tok; char *temp;
tok = strchr(command,' '); temp = strchr(command,' ');
char *tempTok = tok + 1; temp = temp+1;
tok = tempTok; char *locationOfNewLine = strchr(temp, '\n');
char *locationOfNewLine = strchr(tok, '\n');
if(locationOfNewLine) { if(locationOfNewLine) {
*locationOfNewLine = '\0'; *locationOfNewLine = '\0';
} }
chdir(tok); chdir(temp);
} }
void list_dir(char *name) { void ls(char *name) {
DIR *dir; DIR *dir;
struct dirent *dp; struct dirent *dp;
struct stat statbuf; struct stat statbuf;
if ((dir = opendir(name)) == NULL) { if ((dir = opendir(name)) == NULL) {
perror("Blad"); perror("Blad");
} }
while ((dp = readdir(dir)) != NULL) { while ((dp = readdir(dir)) != NULL) {
if (stat(dp->d_name, &statbuf) == -1) continue; if (stat(dp->d_name, &statbuf) == -1) continue;
if (dp->d_name[0] == '.') continue; if (dp->d_name[0] == '.') continue;
printf( "%s ", dp->d_name); printf( "%s ", dp->d_name);
} }
closedir(dir); closedir(dir);
} }
int main(int argc, char **argv) { int main() {
char command[BUFFERSIZE]; char command[BUFFERSIZE];
@ -124,9 +123,8 @@ int main(int argc, char **argv) {
if (strcmp(command, "help\n") == 0){ help(); } if (strcmp(command, "help\n") == 0){ help(); }
else if (strcmp(command, "ls\n") == 0){ else if (strcmp(command, "ls\n") == 0){
chdir(*(++argv)); ls(".");
list_dir("."); printf("\n");
printf("\n");
} }
else if (strcmp(command, "pwd\n") == 0){ else if (strcmp(command, "pwd\n") == 0){
pwd(); pwd();