]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/src/tftp_dummy_file.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / common / v2_0 / src / tftp_dummy_file.c
1 //==========================================================================
2 //
3 //      lib/tftp_dummy_file.c
4 //
5 //      Dummy [in memory] file I/O functions
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):    gthomas
44 // Contributors: gthomas
45 // Date:         2000-04-06
46 // Purpose:      
47 // Description:  
48 //              
49 //
50 //####DESCRIPTIONEND####
51 //
52 //==========================================================================
53
54 // TFTP file support - minimal "dummy" version
55
56 #include <network.h>
57 #include <tftp_support.h>
58
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);
63
64 struct tftpd_fileops dummy_fileops = {
65     dummy_open, dummy_close, dummy_write, dummy_read
66 };
67
68 struct _file_info;
69 struct _file {
70     unsigned char      *pos, *eof;
71     int                 flags;
72     int                 mode;
73     struct _file_info  *file;
74 };
75 #define FILE_OPEN 0x0001
76
77 #define NUM_FILES 8
78 static struct _file files[NUM_FILES];
79
80 struct _file_info {
81     char          *name;
82     unsigned char *data;
83     int            length, size;
84 };
85
86 // TEMP
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\
101 ";
102
103 static unsigned char _f0_data[1024*1024];
104 static unsigned char _f1_data[1024*1024];
105 static unsigned char _f2_data[1024*1024];
106
107 static char _name0[256] = "", _name1[256] = "", _name2[256] = "";
108
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
115 };
116
117 static inline struct _file *
118 dummy_fp(int fd)
119 {
120     struct _file *fp;
121     if ((fd < 0) || (fd >= NUM_FILES)) return (struct _file *)0;
122     fp = &files[fd];
123     if (!(fp->flags & FILE_OPEN)) return (struct _file *)0;
124     return fp;
125 }
126
127 static int
128 dummy_open(const char *fn, int flags)
129 {
130     int res = -1;
131     int fd;
132     struct _file *fp;
133     struct _file_info *fi;
134
135     fp = files;
136     for (fd = 0;  fd < NUM_FILES;  fd++, fp++) {
137         if (!(fp->flags & FILE_OPEN)) break;
138     }
139     if (fd == NUM_FILES) {
140         return -1;  // No free files
141     }
142     if (flags & O_RDONLY) {
143         // Search for an extant file
144         fi = file_list;
145         while (fi->name) {
146             if (strcmp(fi->name, fn) == 0) {
147                 // Found it!
148                 fp->pos = fi->data;
149                 fp->eof = fi->data + fi->length;
150                 fp->flags = FILE_OPEN;
151                 fp->mode = flags;
152                 return fd;
153             }
154             fi++;
155         }
156     } else {
157         // Search for a non-existant file
158         fi = file_list;
159         while (fi->name) {
160             if (fi->name[0] == '\0' || strcmp(fi->name, fn) == 0) {
161                 if ( !fi->name[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 }
218
219
220 // EOF tftp_dummy_file.c