Statistiques
| Branche: | Révision :

root / split.c @ master

Historique | Voir | Annoter | Télécharger (1,88 ko)

1 132932cd Romuald DELAVERGNE
/*
2 132932cd Romuald DELAVERGNE
 * Copyright (c) 2005 by Romuald DELAVERGNE
3 132932cd Romuald DELAVERGNE
 * It can be redistributed under the terms of the GNU General Public License.
4 132932cd Romuald DELAVERGNE
*/
5 132932cd Romuald DELAVERGNE
#include <stdlib.h>
6 132932cd Romuald DELAVERGNE
#include <stdio.h>
7 132932cd Romuald DELAVERGNE
#include <string.h>
8 132932cd Romuald DELAVERGNE
#include <sys/stat.h>
9 132932cd Romuald DELAVERGNE
#include <io.h>
10 132932cd Romuald DELAVERGNE
11 132932cd Romuald DELAVERGNE
void
12 132932cd Romuald DELAVERGNE
exitOnError(char *msg) {
13 132932cd Romuald DELAVERGNE
        perror(msg);
14 132932cd Romuald DELAVERGNE
        exit(1);
15 132932cd Romuald DELAVERGNE
}
16 132932cd Romuald DELAVERGNE
17 132932cd Romuald DELAVERGNE
int
18 132932cd Romuald DELAVERGNE
main(int argc, char *argv[]) {
19 132932cd Romuald DELAVERGNE
        void *buf;
20 132932cd Romuald DELAVERGNE
        struct stat statFileIn;
21 132932cd Romuald DELAVERGNE
        FILE *fileIn, *fileOut;
22 132932cd Romuald DELAVERGNE
        char
23 132932cd Romuald DELAVERGNE
                fileName[255],
24 132932cd Romuald DELAVERGNE
                *prog = argv[0],
25 132932cd Romuald DELAVERGNE
                *prefix = argv[3];
26 132932cd Romuald DELAVERGNE
        unsigned long
27 132932cd Romuald DELAVERGNE
                nbFiles, lastSize, maxSize, size, nb, nbWrited, nbRest;
28 132932cd Romuald DELAVERGNE
        int i;
29 132932cd Romuald DELAVERGNE
30 132932cd Romuald DELAVERGNE
        buf = malloc(BUFSIZ);
31 132932cd Romuald DELAVERGNE
32 132932cd Romuald DELAVERGNE
        if (argc < 4) {
33 132932cd Romuald DELAVERGNE
                printf("%s: too few parameters !\n", prog);
34 132932cd Romuald DELAVERGNE
                printf("split a file into pieces\n");
35 132932cd Romuald DELAVERGNE
                printf("usage: %s FILE SIZE PREFIX\n", prog);
36 132932cd Romuald DELAVERGNE
                printf("  FILE  : big file to split\n");
37 132932cd Romuald DELAVERGNE
                printf("  SIZE  : size in bytes\n");
38 132932cd Romuald DELAVERGNE
                printf("  PREFIX: result files without suffix\n");
39 132932cd Romuald DELAVERGNE
                exit(0);
40 132932cd Romuald DELAVERGNE
        }
41 132932cd Romuald DELAVERGNE
42 132932cd Romuald DELAVERGNE
        if ((fileIn = fopen(argv[1], "rb")) == NULL) exitOnError(argv[1]);
43 132932cd Romuald DELAVERGNE
        if (fstat(fileIn->fd, &statFileIn) == -1) exitOnError(argv[1]);
44 132932cd Romuald DELAVERGNE
        if (sscanf(argv[2], "%lu", &maxSize) == 0) {
45 132932cd Romuald DELAVERGNE
                fprintf(stderr, "%s: invalid size !\n", argv[2]);
46 132932cd Romuald DELAVERGNE
                exit(1);
47 132932cd Romuald DELAVERGNE
        }
48 132932cd Romuald DELAVERGNE
        nbFiles = statFileIn.st_size/maxSize;
49 132932cd Romuald DELAVERGNE
        lastSize = statFileIn.st_size%maxSize;
50 132932cd Romuald DELAVERGNE
        if (lastSize > 0) nbFiles++; else lastSize = maxSize;
51 132932cd Romuald DELAVERGNE
        i = 0;
52 132932cd Romuald DELAVERGNE
        while (i < nbFiles) {
53 132932cd Romuald DELAVERGNE
                i++;
54 132932cd Romuald DELAVERGNE
                size = (i == nbFiles) ? lastSize: maxSize;
55 132932cd Romuald DELAVERGNE
                sprintf(fileName, "%s.%03d", prefix, i);
56 132932cd Romuald DELAVERGNE
                if (access(fileName, 0) == 0) {
57 132932cd Romuald DELAVERGNE
                        fprintf(stderr, "%s: file already exists !\n", fileName);
58 132932cd Romuald DELAVERGNE
                        exit(1);
59 132932cd Romuald DELAVERGNE
                }
60 132932cd Romuald DELAVERGNE
                if ((fileOut = fopen(fileName, "wb")) == NULL) exitOnError(fileName);
61 132932cd Romuald DELAVERGNE
                nbWrited = 0;
62 132932cd Romuald DELAVERGNE
                while (nbWrited < size) {
63 132932cd Romuald DELAVERGNE
                        nbRest = size - nbWrited;
64 132932cd Romuald DELAVERGNE
                        nb = (nbRest < BUFSIZ) ? nbRest: BUFSIZ;
65 132932cd Romuald DELAVERGNE
                        if (fread(buf, nb, 1, fileIn) != 1) exitOnError(prog);
66 132932cd Romuald DELAVERGNE
                        if (fwrite(buf, nb, 1, fileOut) != 1) exitOnError(prog);
67 132932cd Romuald DELAVERGNE
                        nbWrited += nb;
68 132932cd Romuald DELAVERGNE
                }
69 132932cd Romuald DELAVERGNE
                fclose(fileOut);
70 132932cd Romuald DELAVERGNE
        }
71 132932cd Romuald DELAVERGNE
        fclose(fileIn);
72 132932cd Romuald DELAVERGNE
73 132932cd Romuald DELAVERGNE
        return 0;
74 132932cd Romuald DELAVERGNE
}