From 00cf8e7fb86259b422efebdc732e27df9c4feb60 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sun, 19 Jul 2015 12:34:55 +0200 Subject: [PATCH] sortiert Dateien nach Groesse --- discspan.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/discspan.c b/discspan.c index 1b1abe1..7fb0b4d 100644 --- a/discspan.c +++ b/discspan.c @@ -86,17 +86,25 @@ void splitter(const char *input, char *output, const char *rest, unsigned long l printf("Restlicher Platz in Bytes(%s): %lld (%lld %c)\n", output, splitgroesse, humanread, einheit); } -int cmpfunc(const void * a, const void * b) +struct file_size { - if(*(long long unsigned int*)a < *(long long unsigned int*)b) + char name[255]; + long long unsigned int fsize; +}; + +int cmpfunc(const void * filea, const void * fileb) +{ + long long unsigned int a = ((const struct file_size*)filea)->fsize; + long long unsigned int b = ((const struct file_size*)fileb)->fsize; + if(a < b) return 1; - if(*(long long unsigned int*)a > *(long long unsigned int*)b) + if(a > b) return -1; - if(*(long long unsigned int*)a == *(long long unsigned int*)b) + if(a == b) return 0; } -long long unsigned int * filesize_sort(const char *input, const char *output) +void filesize_sort(const char *input, const char *output) { FILE *in; FILE *out; char pfad[255]; @@ -104,21 +112,29 @@ long long unsigned int * filesize_sort(const char *input, const char *output) int lines = 0; in = fopen(input, "r"); + out = fopen(output, "w"); while(fscanf(in, "%[^\n]\n", pfad) == 1) { lines++; } fseek(in, 0L, SEEK_SET); - long long unsigned int fsize[lines]; + struct file_size fs[lines -1]; lines = 0; while(fscanf(in, "%[^\n]\n", pfad) == 1) { stat(pfad, &st); - fsize[lines++] = st.st_size; + strcpy(fs[lines].name, pfad); + fs[lines++].fsize = st.st_size; + } + qsort(fs, lines, sizeof(struct file_size), cmpfunc); + int i; + for(i = 0; i < lines; i++) + { + fprintf(out, "%s\n", fs[i].name); } - qsort(fsize, lines, sizeof(unsigned long long), cmpfunc); - return fsize; + fclose(in); + fclose(out); } void usage() @@ -180,8 +196,7 @@ int main(int argc, char *argv[]) } } - long long unsigned int *fsort = filesize_sort(input, rest); - filecopy(input, rest); + filesize_sort(input, rest); while(exists(rest) == 0) { sprintf(outname, "%s%03d",output , num++); -- 2.39.5