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

Loading…
Cancel
Save