1 //==========================================================================
3 // lib/tftp_dummy_file.c
5 // Dummy [in memory] file I/O functions
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
44 // Contributors: gthomas
50 //####DESCRIPTIONEND####
52 //==========================================================================
54 // TFTP file support - minimal "dummy" version
57 #include <tftp_support.h>
59 static int dummy_open(const char *, int);
60 static int dummy_close(int);
61 static int dummy_write(int, const void *, int);
62 static int dummy_read(int, void *, int);
64 struct tftpd_fileops dummy_fileops = {
65 dummy_open, dummy_close, dummy_write, dummy_read
70 unsigned char *pos, *eof;
73 struct _file_info *file;
75 #define FILE_OPEN 0x0001
78 static struct _file files[NUM_FILES];
87 static unsigned char _uu_data[] = "This is a test\n\
88 Four score and seven years ago,\n\
89 our forefathers brought forth a new nation,\n\
90 conceived in liberty and dedicated to the\n\
91 proposition that all men are created equal.\n\
92 Now we are engaged in a great civil war, testing\n\
93 whether that nation, or any nation so conceived\n\
94 and so dedicated, can long endure.\n\
95 1111111111111111111111111111111111\n\
96 2222222222222222222222222222222222\n\
97 3333333333333333333333333333333333\n\
98 4444444444444444444444444444444444\n\
99 5555555555555555555555555555555555\n\
100 6666666666666666666666666666666666\n\
103 static unsigned char _f0_data[1024*1024];
104 static unsigned char _f1_data[1024*1024];
105 static unsigned char _f2_data[1024*1024];
107 static char _name0[256] = "", _name1[256] = "", _name2[256] = "";
109 static struct _file_info file_list[] = {
110 { "uu", _uu_data, sizeof(_uu_data)-1, sizeof(_uu_data)-1},
111 { _name0, _f0_data, 0, sizeof(_f0_data)}, // Empty file
112 { _name1, _f1_data, 0, sizeof(_f1_data)}, // Empty file
113 { _name2, _f2_data, 0, sizeof(_f2_data)}, // Empty file
114 { 0, 0, 0} // End of list
117 static inline struct _file *
121 if ((fd < 0) || (fd >= NUM_FILES)) return (struct _file *)0;
123 if (!(fp->flags & FILE_OPEN)) return (struct _file *)0;
128 dummy_open(const char *fn, int flags)
133 struct _file_info *fi;
136 for (fd = 0; fd < NUM_FILES; fd++, fp++) {
137 if (!(fp->flags & FILE_OPEN)) break;
139 if (fd == NUM_FILES) {
140 return -1; // No free files
142 if (flags & O_RDONLY) {
143 // Search for an extant file
146 if (strcmp(fi->name, fn) == 0) {
149 fp->eof = fi->data + fi->length;
150 fp->flags = FILE_OPEN;
157 // Search for a non-existant file
160 if (fi->name[0] == '\0' || strcmp(fi->name, fn) == 0) {
163 strcpy(fi->name, fn);
165 fp->eof = fi->data + fi->size;
166 fp->file = fi; // So we can update file info later
168 fp->flags = FILE_OPEN;
181 struct _file *fp = dummy_fp(fd);
183 if (fp->mode & O_WRONLY) {
184 // Clean up - update file info for later
185 fp->file->length = fp->pos - fp->file->data;
187 fp->flags = 0; // No longer open
192 dummy_write(int fd, const void *buf, int len)
194 struct _file *fp = dummy_fp(fd);
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);
206 dummy_read(int fd, void *buf, int len)
208 struct _file *fp = dummy_fp(fd);
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);
220 // EOF tftp_dummy_file.c