| 1 | #include <stdlib.h> | 
|---|
| 2 | #include <stdio.h> | 
|---|
| 3 | #include <string.h> | 
|---|
| 4 | #include <fcntl.h> | 
|---|
| 5 | #include <unistd.h> | 
|---|
| 6 | #include <errno.h> | 
|---|
| 7 |  | 
|---|
| 8 | #ifdef __OS2__ | 
|---|
| 9 | #include <io.h> | 
|---|
| 10 | #endif | 
|---|
| 11 |  | 
|---|
| 12 | #ifdef WIN32 | 
|---|
| 13 | #define sleep(sec) _sleep(sec*1000) | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | static int bail (const char *msg) | 
|---|
| 17 | { | 
|---|
| 18 | fprintf (stderr, "TEST ERROR: %s: %s (%d)\n", msg, strerror(errno), errno); | 
|---|
| 19 | return errno; | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | #define BUF_SIZE 4096 | 
|---|
| 23 |  | 
|---|
| 24 | int main(/*int argc, char *argv[]*/) | 
|---|
| 25 | { | 
|---|
| 26 | #if 1 | 
|---|
| 27 |  | 
|---|
| 28 | //sleep(3); | 
|---|
| 29 | int rc = write(fileno(stdout), "test-test", 9); | 
|---|
| 30 | fprintf(stderr, "rc=%d %s\n", rc, strerror(errno)); | 
|---|
| 31 | //sleep(3); | 
|---|
| 32 | rc = write(fileno(stdout), "\nend", 4); | 
|---|
| 33 | fprintf(stderr, "rc=%d %s\n", rc, strerror(errno)); | 
|---|
| 34 | //sleep(3); | 
|---|
| 35 | return 0; | 
|---|
| 36 |  | 
|---|
| 37 | #else | 
|---|
| 38 |  | 
|---|
| 39 | int rc; | 
|---|
| 40 |  | 
|---|
| 41 | setmode(fileno(stdout), O_BINARY); | 
|---|
| 42 |  | 
|---|
| 43 | const char *fname = "D:\\Users\\dmik\\fingerabdruck.mpg"; | 
|---|
| 44 |  | 
|---|
| 45 | int fd = open(fname, O_RDONLY | O_BINARY); | 
|---|
| 46 | if (fd == -1) | 
|---|
| 47 | return bail("open"); | 
|---|
| 48 |  | 
|---|
| 49 | timeval timeStart; | 
|---|
| 50 | gettimeofday(&timeStart, 0); | 
|---|
| 51 |  | 
|---|
| 52 | int size = 0; | 
|---|
| 53 | char buf[BUF_SIZE]; | 
|---|
| 54 | do { | 
|---|
| 55 | rc = read(fd, buf, sizeof(buf)); | 
|---|
| 56 | if (rc == -1) { | 
|---|
| 57 | bail("read"); | 
|---|
| 58 | break; | 
|---|
| 59 | } | 
|---|
| 60 | if (rc == 0) | 
|---|
| 61 | break; | 
|---|
| 62 | //      fprintf(stderr, "."); | 
|---|
| 63 | size += rc; | 
|---|
| 64 | rc = write(fileno(stdout), buf, rc); | 
|---|
| 65 | if (rc == -1) { | 
|---|
| 66 | bail("read"); | 
|---|
| 67 | break; | 
|---|
| 68 | } | 
|---|
| 69 | } while (true); | 
|---|
| 70 |  | 
|---|
| 71 | timeval timeEnd; | 
|---|
| 72 | gettimeofday(&timeEnd, 0); | 
|---|
| 73 |  | 
|---|
| 74 | close(fd); | 
|---|
| 75 |  | 
|---|
| 76 | //  fprintf(stderr, "\n"); | 
|---|
| 77 |  | 
|---|
| 78 | unsigned long long elapsed = timeEnd.tv_sec * 1000ll + timeEnd.tv_usec / 1000ll; | 
|---|
| 79 | elapsed -= (timeStart.tv_sec * 1000ll + timeStart.tv_usec / 1000ll); | 
|---|
| 80 |  | 
|---|
| 81 | unsigned long long speed = size * 1000ull; | 
|---|
| 82 | speed /= elapsed; | 
|---|
| 83 |  | 
|---|
| 84 | if (rc != -1) { | 
|---|
| 85 | fprintf(stderr, "Wrote %s\n", fname); | 
|---|
| 86 | fprintf(stderr, "Size %d bytes\n", size); | 
|---|
| 87 | fprintf(stderr, "Time %llu ms\n", elapsed); | 
|---|
| 88 | fprintf(stderr, "Speed %llu bytes/sec\n", speed); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | #endif | 
|---|
| 92 | } | 
|---|