source: network-demos/tftpTest/test.c @ 0c9330e

4.11netdemos-4-5-branchnetwork-demos-4-10-branchnetwork-demos-4-6-branchnetwork-demos-4-7-branchnetwork-demos-4-8-branchnetwork-demos-4-9-branchrtems-4-5-branch
Last change on this file since 0c9330e was 0c9330e, checked in by Joel Sherrill <joel.sherrill@…>, on 08/21/98 at 13:22:35

KA9Q -> FreeBSD changes from Eric Norum

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * Test RTEMS/KA9Q TFTP device driver
3 *
4 * This program may be distributed and used for any purpose.
5 * I ask only that you:
6 *      1. Leave this author information intact.
7 *      2. Document any changes you make.
8 *
9 * W. Eric Norum
10 * Saskatchewan Accelerator Laboratory
11 * University of Saskatchewan
12 * Saskatoon, Saskatchewan, CANADA
13 * eric@skatter.usask.ca
14 */
15
16#include <stdio.h>
17#include <errno.h>
18#include <string.h>
19#include <rtems.h>
20#include <rtems/error.h>
21#include <sys/fcntl.h>
22#include <unistd.h>
23
24static char cbuf[1024];
25static char *fullname;
26static rtems_interval then, now;
27
28static void
29showRate (unsigned long totalRead)
30{
31        int elapsed;
32
33        printf ("Read %lu bytes", totalRead);
34        elapsed = now - then;
35        if (elapsed) {
36                rtems_interval ticksPerSecond;
37                rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond);
38                printf (" (%ld bytes/sec)",
39                                (long)(((long long)totalRead * ticksPerSecond)
40                                                                / elapsed));
41        }
42        printf (".\n");
43}
44
45static void
46testRawRead (void)
47{
48        int fd;
49        int nread;
50        unsigned long totalRead = 0;
51
52        fd = open (fullname, O_RDONLY);
53        if (fd < 0) {
54                printf ("Open failed: %s\n", strerror (errno));
55                return;
56        }
57
58        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
59        for (;;) {
60                nread = read (fd, cbuf, sizeof cbuf);
61                if (nread < 0) {
62                        printf ("Read failed: %s\n", strerror (errno));
63                        close (fd);
64                        return;
65                }
66                if (nread == 0)
67                        break;
68                totalRead += nread;
69        }
70        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
71        close (fd);
72        showRate (totalRead);
73}
74
75static void
76testFread (void)
77{
78        FILE *fp;
79        int nread;
80        unsigned long totalRead = 0;
81
82        fp = fopen (fullname, "r");
83        if (fp == NULL) {
84                printf ("Open failed: %s\n", strerror (errno));
85                return;
86        }
87
88        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
89        for (;;) {
90                nread = fread (cbuf, sizeof cbuf[0], sizeof cbuf, fp);
91                if (nread < 0) {
92                        printf ("Read failed: %s\n", strerror (errno));
93                        fclose (fp);
94                        return;
95                }
96                if (nread == 0)
97                        break;
98                totalRead += nread;
99        }
100        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
101        fclose (fp);
102        showRate (totalRead);
103}
104
105static int
106makeFullname (const char *hostname, const char *file)
107{
108        if (hostname == NULL)
109                hostname = "";
110        fullname = realloc (fullname, 8 + strlen (file) + strlen (hostname));
111        sprintf (fullname, "/TFTP/%s/%s", hostname, file);
112        printf ("Read `%s'.\n", fullname);
113        return 1;
114}
115
116void
117testTFTP (const char *hostname, const char *filename)
118{
119        /*
120         * Check that invalid file names are reported as such
121         */
122        if (makeFullname (hostname, "")) {
123                testRawRead ();
124                testFread ();
125        }
126
127        /*
128         * Check that non-existent files are reported as such
129         */
130        if (makeFullname (hostname, "BAD-FILE-NAME")) {
131                testRawRead ();
132                testFread ();
133        }
134
135        /*
136         * Check that read works
137         */
138        if (makeFullname (hostname, filename)) {
139                testRawRead ();
140                testFread ();
141        }
142}
Note: See TracBrowser for help on using the repository browser.