diff options
| author | manuel <manuel@mausz.at> | 2012-03-27 11:51:08 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2012-03-27 11:51:08 +0200 |
| commit | 4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b (patch) | |
| tree | 868c52e06f207b5ec8a3cc141f4b8b2bdfcc165c /examples/cp.c | |
| parent | eae0bd57f0a26314a94785061888d193d186944a (diff) | |
| download | progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.gz progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.bz2 progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.zip | |
reorganize file structure to match the upstream requirements
Diffstat (limited to 'examples/cp.c')
| -rw-r--r-- | examples/cp.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/cp.c b/examples/cp.c new file mode 100644 index 0000000..86a5cd7 --- /dev/null +++ b/examples/cp.c | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | /* cat.c | ||
| 2 | |||
| 3 | Copies one file to another. */ | ||
| 4 | |||
| 5 | #include <stdio.h> | ||
| 6 | #include <syscall.h> | ||
| 7 | |||
| 8 | int | ||
| 9 | main (int argc, char *argv[]) | ||
| 10 | { | ||
| 11 | int in_fd, out_fd; | ||
| 12 | |||
| 13 | if (argc != 3) | ||
| 14 | { | ||
| 15 | printf ("usage: cp OLD NEW\n"); | ||
| 16 | return EXIT_FAILURE; | ||
| 17 | } | ||
| 18 | |||
| 19 | /* Open input file. */ | ||
| 20 | in_fd = open (argv[1]); | ||
| 21 | if (in_fd < 0) | ||
| 22 | { | ||
| 23 | printf ("%s: open failed\n", argv[1]); | ||
| 24 | return EXIT_FAILURE; | ||
| 25 | } | ||
| 26 | |||
| 27 | /* Create and open output file. */ | ||
| 28 | if (!create (argv[2], filesize (in_fd))) | ||
| 29 | { | ||
| 30 | printf ("%s: create failed\n", argv[2]); | ||
| 31 | return EXIT_FAILURE; | ||
| 32 | } | ||
| 33 | out_fd = open (argv[2]); | ||
| 34 | if (out_fd < 0) | ||
| 35 | { | ||
| 36 | printf ("%s: open failed\n", argv[2]); | ||
| 37 | return EXIT_FAILURE; | ||
| 38 | } | ||
| 39 | |||
| 40 | /* Copy data. */ | ||
| 41 | for (;;) | ||
| 42 | { | ||
| 43 | char buffer[1024]; | ||
| 44 | int bytes_read = read (in_fd, buffer, sizeof buffer); | ||
| 45 | if (bytes_read == 0) | ||
| 46 | break; | ||
| 47 | if (write (out_fd, buffer, bytes_read) != bytes_read) | ||
| 48 | { | ||
| 49 | printf ("%s: write failed\n", argv[2]); | ||
| 50 | return EXIT_FAILURE; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | return EXIT_SUCCESS; | ||
| 55 | } | ||
