`ls`: unbreak things heavily.

master
Drake 2 years ago
parent 6617f684b8
commit 182090801f

@ -55,7 +55,7 @@ Available arguments:
}*/
static int cmp(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
return strcmp(*(char * const *)a, *(char * const *)b);
}
static int listDirs(char *thatpath, bool specpath, bool colour, bool showdot,
@ -80,23 +80,29 @@ static int listDirs(char *thatpath, bool specpath, bool colour, bool showdot,
printf("couldn't open '%s'. perhaps the path doesn't exist?\n", wd);
return 1;
}
int relen = sizeof(char);
char **words = malloc(relen);
int n=0;
while ((dp = readdir(dirp)) != NULL) {
if (!startsWithChar(dp->d_name, '.') || showdot == true) {
n++;
}
}
printf("%i\n",n);
rewinddir(dirp);
char *words[n];
int count = 0;
int length = 0;
while ((dp = readdir(dirp)) != NULL) {
char *dirname = dp->d_name;
if (!startsWithChar(dirname, '.') || showdot == true) {
relen = relen + strlen(dirname) +
3; // 3 is so fucking arbitrary, but it works
words = realloc(words, relen);
words[count] = malloc(strlen(dirname) + 3);
words[count] = dirname;
count++;
words[count] = (char*)malloc(strlen(dirname)+1);
strncpy(words[count],dirname,strlen(dirname));
count++;
length+=strlen(dirname)+1;
}
}
free(dirp);
free(dp);
qsort(words, count - 1, sizeof(words), cmp);
qsort(words, n, sizeof(char*), cmp);
char oldwd[PATH_MAX];
strcpy(oldwd, wd);
int currLen = 0;
@ -132,7 +138,6 @@ static int listDirs(char *thatpath, bool specpath, bool colour, bool showdot,
}
}
}
free(words);
printf("\n");
return 0;
}

Loading…
Cancel
Save