]> gitweb.hhaalo.de Git - discspan.git/commitdiff
erste Version
authorBastian Dehn <hhaalo@arcor.de>
Mon, 13 Jul 2015 13:01:58 +0000 (15:01 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Mon, 13 Jul 2015 13:01:58 +0000 (15:01 +0200)
Makefile [new file with mode: 0644]
batch.sh [new file with mode: 0755]
discspan.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..34b5958
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,15 @@
+CC=/usr/bin/gcc
+CFLAGS=-Wall
+
+BINPATH=/usr/bin
+
+all: discspan
+discspan: discspan.c
+       $(CC) $(CFLAGS) -o discspan discspan.c
+install:
+       chmod 755 discspan
+       cp discspan $(BINPATH)/discspan
+uninstall:
+       rm $(BINPATH)/discspan
+clean:
+       rm discspan
diff --git a/batch.sh b/batch.sh
new file mode 100755 (executable)
index 0000000..d2a1662
--- /dev/null
+++ b/batch.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+cp testfile work
+touch rest
+
+i=0
+while [ -f rest ]
+do
+       rm rest
+       i=$((i + 1))
+       ./discspan work medium$i rest -9
+       if [ -f rest ]
+       then
+               cp rest work
+       fi
+done
+rm work
diff --git a/discspan.c b/discspan.c
new file mode 100644 (file)
index 0000000..bcb5e81
--- /dev/null
@@ -0,0 +1,132 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int exists(const char *fname)
+{
+       struct stat buffer;
+       if (stat(fname, &buffer) == 0)
+               return 0;
+       else
+               return 1;
+}
+
+void splitter(const char *input, const char *output, const char *rest, unsigned long long splitgroesse)
+{
+       struct stat st;
+       char pfad[255];
+       unsigned long long filesize;
+       unsigned long long humanread;
+       char einheit = 'B';
+       char i = 0;
+
+       FILE *in; FILE *out; FILE *re;
+
+       in = fopen(input, "r");
+       out = fopen(output, "w");
+
+       while (fscanf(in, "%[^\n]\n", pfad) == 1)
+       {
+               stat(pfad, &st);
+               filesize = st.st_size;
+               if (splitgroesse >= filesize)
+               {
+                       splitgroesse -= filesize;
+                       fprintf(out, "%s\n", pfad);
+               }
+               else
+               {
+                       if (exists(rest) == 1)
+                               re = fopen(rest, "w");
+
+                       fprintf(re, "%s\n", pfad);
+               }
+       }
+
+       fclose(in); fclose(out);
+       if(exists(rest) == 0)
+               fclose(re);
+       
+       humanread = splitgroesse;
+       while (humanread >= 1024)
+       {
+               i++;
+               humanread /= 1024;
+               switch (i)
+               {
+                       case 1:
+                               einheit = 'K';
+                       break;
+                       case 2:
+                               einheit = 'M';
+                       break;
+                       case 3:
+                               einheit = 'G';
+                       break;
+               }
+       }
+
+       printf("Restlicher Platz in Bytes(%s): %lld (%lld %c)\n", output, splitgroesse, humanread, einheit);
+}
+
+void usage()
+{      
+       printf("\nUsage: discspan <input> <output> <rest> <option>\n\n");
+       printf(" <input>\tTextdatei enthält alle Dateipfade, die auf den Datenträger sollen\n");
+       printf(" <output>\tTextdatei enthält alle Dateipfade, die auf den Datenträger passen\n");
+       printf(" <rest>\t\tTextdatei enthält alle Dateipfade, die nicht auf den Datenträger gepasst haben\n\n");
+       printf(" option:\n");
+       printf("   -9\t\tEntspricht der Größe einer Double Layer DVD: 8.500.000.000 Bytes\n");
+       printf("   -5\t\tEntspricht der Größe einer Single Layer DVD: 4.700.000.000 Bytes\n");
+       printf("   -c\t\tEntspricht der Größe einer CD: 734.003.200 Bytes\n"); 
+       printf("   -f <Größe>\tOption zur Größenangabe in Bytes\n\n");
+}
+
+int main(int argc, char *argv[])
+{
+       const char *input = argv[1];
+       const char *output = argv[2];
+       const char *rest = argv[3];
+       unsigned long long split;
+       int c;
+       extern char *optarg;
+       // Konstanten aus dat.c bzw. dat.h
+       const unsigned long long dvd9 = 8500000000;
+       const unsigned long long dvd5 = 4700000000;
+       const unsigned long long cd = 734003200;
+
+       if(argc <= 4)
+       {
+               usage();
+               return 1;
+       }
+
+       while ((c = getopt(argc, argv, ":59bdcf:")) != -1)
+       {
+               switch(c)
+               {
+                       case '5':
+                               split = dvd5;
+                       break;
+                       case '9':
+                               split = dvd9;
+                       break;
+                       case 'c':
+                               split = cd;
+                       break;
+                       case 'f':
+                               split = atoll(optarg);
+                       break;
+                       case '?':
+                               usage();
+                               return 1;
+                       break;
+               }
+       }
+
+       splitter(input, output, rest, split);
+
+       return 0;
+}