root / cat.c @ master
Historique | Voir | Annoter | Télécharger (1005 octet)
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 <fcntl.h> |
8 | 132932cd | Romuald DELAVERGNE | #include <io.h> |
9 | 132932cd | Romuald DELAVERGNE | #include <dir.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 | int handle, n, done;
|
21 | 132932cd | Romuald DELAVERGNE | struct ffblk ffblk;
|
22 | 132932cd | Romuald DELAVERGNE | char *prog = argv[0]; |
23 | 132932cd | Romuald DELAVERGNE | |
24 | 132932cd | Romuald DELAVERGNE | buf = malloc(BUFSIZ); |
25 | 132932cd | Romuald DELAVERGNE | |
26 | 132932cd | Romuald DELAVERGNE | if (argc == 1) { |
27 | 132932cd | Romuald DELAVERGNE | printf("%s: too few parameters !\n", prog);
|
28 | 132932cd | Romuald DELAVERGNE | printf("concatenate files and print on the standard output\n");
|
29 | 132932cd | Romuald DELAVERGNE | printf("usage: %s FILE [FILE...]\n", prog);
|
30 | 132932cd | Romuald DELAVERGNE | exit(0);
|
31 | 132932cd | Romuald DELAVERGNE | } |
32 | 132932cd | Romuald DELAVERGNE | |
33 | 132932cd | Romuald DELAVERGNE | while (--argc > 0) { |
34 | 132932cd | Romuald DELAVERGNE | done = findfirst(*++argv, &ffblk, 0);
|
35 | 132932cd | Romuald DELAVERGNE | while (!done) {
|
36 | 132932cd | Romuald DELAVERGNE | if ((handle = _open(ffblk.ff_name, O_RDONLY|O_BINARY)) == -1) exitOnError(prog); |
37 | 132932cd | Romuald DELAVERGNE | while (!eof(handle))
|
38 | 132932cd | Romuald DELAVERGNE | if ((n = _read(handle, buf, BUFSIZ)) == -1 || _write(1, buf, n) == -1) |
39 | 132932cd | Romuald DELAVERGNE | exitOnError(prog); |
40 | 132932cd | Romuald DELAVERGNE | _close(handle); |
41 | 132932cd | Romuald DELAVERGNE | done = findnext(&ffblk); |
42 | 132932cd | Romuald DELAVERGNE | } |
43 | 132932cd | Romuald DELAVERGNE | } |
44 | 132932cd | Romuald DELAVERGNE | return 0; |
45 | 132932cd | Romuald DELAVERGNE | } |