Mercurial > ecos-v2_0-branch
comparison packages/net/tcpip/current/src/lib/tftp_dummy_file.c @ 97:ced4577552cd ecos-sw-2000-06-06
Merge from eCos master repository on 2000-06-06-08:44:00-BST
| author | jlarmour |
|---|---|
| date | Tue, 06 Jun 2000 08:39:36 +0000 |
| parents | |
| children | 0c2b7be0d798 |
comparison
equal
deleted
inserted
replaced
| 96:b15c60e34c84 | 97:ced4577552cd |
|---|---|
| 1 //========================================================================== | |
| 2 // | |
| 3 // lib/tftp_dummy_file.c | |
| 4 // | |
| 5 // Dummy [in memory] file I/O functions | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Red Hat eCos Public License | |
| 12 // Version 1.1 (the "License"); you may not use this file except in | |
| 13 // compliance with the License. You may obtain a copy of the License at | |
| 14 // http://www.redhat.com/ | |
| 15 // | |
| 16 // Software distributed under the License is distributed on an "AS IS" | |
| 17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 18 // License for the specific language governing rights and limitations under | |
| 19 // the License. | |
| 20 // | |
| 21 // The Original Code is eCos - Embedded Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //####BSDCOPYRIGHTBEGIN#### | |
| 32 // | |
| 33 // ------------------------------------------- | |
| 34 // | |
| 35 // Portions of this software may have been derived from OpenBSD or other sources, | |
| 36 // and are covered by the appropriate copyright disclaimers included herein. | |
| 37 // | |
| 38 // ------------------------------------------- | |
| 39 // | |
| 40 //####BSDCOPYRIGHTEND#### | |
| 41 //========================================================================== | |
| 42 //#####DESCRIPTIONBEGIN#### | |
| 43 // | |
| 44 // Author(s): gthomas | |
| 45 // Contributors: gthomas | |
| 46 // Date: 2000-04-06 | |
| 47 // Purpose: | |
| 48 // Description: | |
| 49 // | |
| 50 // | |
| 51 //####DESCRIPTIONEND#### | |
| 52 // | |
| 53 //========================================================================== | |
| 54 | |
| 55 // TFTP file support - minimal "dummy" version | |
| 56 | |
| 57 #include <network.h> | |
| 58 #include <tftp_support.h> | |
| 59 | |
| 60 static int dummy_open(const char *, int); | |
| 61 static int dummy_close(int); | |
| 62 static int dummy_write(int, const void *, int); | |
| 63 static int dummy_read(int, void *, int); | |
| 64 | |
| 65 struct tftpd_fileops dummy_fileops = { | |
| 66 dummy_open, dummy_close, dummy_write, dummy_read | |
| 67 }; | |
| 68 | |
| 69 struct _file_info; | |
| 70 struct _file { | |
| 71 unsigned char *pos, *eof; | |
| 72 int flags; | |
| 73 int mode; | |
| 74 struct _file_info *file; | |
| 75 }; | |
| 76 #define FILE_OPEN 0x0001 | |
| 77 | |
| 78 #define NUM_FILES 8 | |
| 79 static struct _file files[NUM_FILES]; | |
| 80 | |
| 81 struct _file_info { | |
| 82 char *name; | |
| 83 unsigned char *data; | |
| 84 int length, size; | |
| 85 }; | |
| 86 | |
| 87 // TEMP | |
| 88 static char _uu_data[] = "This is a test | |
| 89 Four score and seven years ago, | |
| 90 our forefathers brought forth a new nation, | |
| 91 conceived in liberty and dedicated to the | |
| 92 proposition that all men are created equal. | |
| 93 Now we are engaged in a great civil war, testing | |
| 94 whether that nation, or any nation so conceived | |
| 95 and so dedicated, can long endure. | |
| 96 1111111111111111111111111111111111 | |
| 97 2222222222222222222222222222222222 | |
| 98 3333333333333333333333333333333333 | |
| 99 4444444444444444444444444444444444 | |
| 100 5555555555555555555555555555555555 | |
| 101 6666666666666666666666666666666666 | |
| 102 "; | |
| 103 | |
| 104 static char _f0_data[10*1024]; | |
| 105 static char _f1_data[10*1024]; | |
| 106 static char _f2_data[10*1024]; | |
| 107 | |
| 108 static char _name0[256] = "", _name1[256] = "", _name2[256] = ""; | |
| 109 | |
| 110 static struct _file_info file_list[] = { | |
| 111 { "uu", _uu_data, sizeof(_uu_data)-1, sizeof(_uu_data)-1}, | |
| 112 { _name0, _f0_data, 0, sizeof(_f0_data)}, // Empty file | |
| 113 { _name1, _f1_data, 0, sizeof(_f1_data)}, // Empty file | |
| 114 { _name2, _f2_data, 0, sizeof(_f2_data)}, // Empty file | |
| 115 { 0, 0, 0} // End of list | |
| 116 }; | |
| 117 | |
| 118 static struct _file * | |
| 119 dummy_fp(int fd) | |
| 120 { | |
| 121 struct _file *fp; | |
| 122 if ((fd < 0) || (fd >= NUM_FILES)) return (struct _file *)0; | |
| 123 fp = &files[fd]; | |
| 124 if (!(fp->flags & FILE_OPEN)) return (struct _file *)0; | |
| 125 return fp; | |
| 126 } | |
| 127 | |
| 128 static int | |
| 129 dummy_open(const char *fn, int flags) | |
| 130 { | |
| 131 int res = -1; | |
| 132 int fd; | |
| 133 struct _file *fp; | |
| 134 struct _file_info *fi; | |
| 135 | |
| 136 fp = files; | |
| 137 for (fd = 0; fd < NUM_FILES; fd++) { | |
| 138 if (!(fp->flags & FILE_OPEN)) break; | |
| 139 } | |
| 140 if (fd == NUM_FILES) { | |
| 141 return -1; // No free files | |
| 142 } | |
| 143 if (flags & O_RDONLY) { | |
| 144 // Search for an extant file | |
| 145 fi = file_list; | |
| 146 while (fi->name) { | |
| 147 if (strcmp(fi->name, fn) == 0) { | |
| 148 // Found it! | |
| 149 fp->pos = fi->data; | |
| 150 fp->eof = fi->data + fi->length; | |
| 151 fp->flags = FILE_OPEN; | |
| 152 fp->mode = flags; | |
| 153 return fd; | |
| 154 } | |
| 155 fi++; | |
| 156 } | |
| 157 } else { | |
| 158 // Search for a non-existant file | |
| 159 fi = file_list; | |
| 160 while (fi->name) { | |
| 161 if (fi->name[0] == '\0') { | |
| 162 // Empty slot found | |
| 163 strcpy(fi->name, fn); | |
| 164 fp->pos = fi->data; | |
| 165 fp->eof = fi->data + fi->size; | |
| 166 fp->file = fi; // So we can update file info later | |
| 167 fp->mode = flags; | |
| 168 fp->flags = FILE_OPEN; | |
| 169 return fd; | |
| 170 } | |
| 171 fi++; | |
| 172 } | |
| 173 } | |
| 174 // No such file | |
| 175 return res; | |
| 176 } | |
| 177 | |
| 178 static int | |
| 179 dummy_close(int fd) | |
| 180 { | |
| 181 struct _file *fp = dummy_fp(fd); | |
| 182 if (!fp) return -1; | |
| 183 if (fp->mode & O_WRONLY) { | |
| 184 // Clean up - update file info for later | |
| 185 fp->file->length = fp->pos - fp->file->data; | |
| 186 } | |
| 187 fp->flags = 0; // No longer open | |
| 188 return 0; | |
| 189 } | |
| 190 | |
| 191 static int | |
| 192 dummy_write(int fd, const void *buf, int len) | |
| 193 { | |
| 194 struct _file *fp = dummy_fp(fd); | |
| 195 int res; | |
| 196 if (!fp) return -1; | |
| 197 res = fp->eof - fp->pos; // Space left in file | |
| 198 if (res <= 0) return 0; // End of file | |
| 199 if (res > len) res = len; | |
| 200 bcopy(buf, fp->pos, res); | |
| 201 fp->pos += res; | |
| 202 return res; | |
| 203 } | |
| 204 | |
| 205 static int | |
| 206 dummy_read(int fd, void *buf, int len) | |
| 207 { | |
| 208 struct _file *fp = dummy_fp(fd); | |
| 209 int res; | |
| 210 if (!fp) return -1; | |
| 211 res = fp->eof - fp->pos; // Number of bytes left in "file" | |
| 212 if (res > len) res = len; | |
| 213 if (res <= 0) return 0; // End of file | |
| 214 bcopy(fp->pos, buf, res); | |
| 215 fp->pos += res; | |
| 216 return res; | |
| 217 } |
