]> gitweb.hhaalo.de Git - discspan.git/commitdiff
sortiert Dateien nach Groesse
authorBastian Dehn <hhaalo@arcor.de>
Sun, 19 Jul 2015 10:34:55 +0000 (12:34 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 19 Jul 2015 10:34:55 +0000 (12:34 +0200)
discspan.c

index 1b1abe18213981e2e4e7c8102f2498bc3f520238..7fb0b4df96cb3dcc0d7facb15ceb5c46b0b76301 100644 (file)
@@ -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++);