OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [src/] [tftp_dummy_file.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
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 Free Software Foundation, 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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    gthomas
43
// Contributors: gthomas
44
// Date:         2000-04-06
45
// Purpose:      
46
// Description:  
47
//              
48
//
49
//####DESCRIPTIONEND####
50
//
51
//==========================================================================
52
 
53
// TFTP file support - minimal "dummy" version
54
 
55
#include <network.h>
56
#include <tftp_support.h>
57
 
58
static int dummy_open(const char *, int);
59
static int dummy_close(int);
60
static int dummy_write(int, const void *, int);
61
static int dummy_read(int, void *, int);
62
 
63
struct tftpd_fileops dummy_fileops = {
64
    dummy_open, dummy_close, dummy_write, dummy_read
65
};
66
 
67
struct _file_info;
68
struct _file {
69
    unsigned char      *pos, *eof;
70
    int                 flags;
71
    int                 mode;
72
    struct _file_info  *file;
73
};
74
#define FILE_OPEN 0x0001
75
 
76
#define NUM_FILES 8
77
static struct _file files[NUM_FILES];
78
 
79
struct _file_info {
80
    char          *name;
81
    unsigned char *data;
82
    int            length, size;
83
};
84
 
85
// TEMP
86
static unsigned char _uu_data[] = "This is a test\n\
87
Four score and seven years ago,\n\
88
our forefathers brought forth a new nation,\n\
89
conceived in liberty and dedicated to the\n\
90
proposition that all men are created equal.\n\
91
Now we are engaged in a great civil war, testing\n\
92
whether that nation, or any nation so conceived\n\
93
and so dedicated, can long endure.\n\
94
1111111111111111111111111111111111\n\
95
2222222222222222222222222222222222\n\
96
3333333333333333333333333333333333\n\
97
4444444444444444444444444444444444\n\
98
5555555555555555555555555555555555\n\
99
6666666666666666666666666666666666\n\
100
";
101
 
102
static unsigned char _f0_data[1024*1024];
103
static unsigned char _f1_data[1024*1024];
104
static unsigned char _f2_data[1024*1024];
105
 
106
static char _name0[256] = "", _name1[256] = "", _name2[256] = "";
107
 
108
static struct _file_info file_list[] = {
109
    { "uu", _uu_data, sizeof(_uu_data)-1, sizeof(_uu_data)-1},
110
    { _name0, _f0_data, 0, sizeof(_f0_data)}, // Empty file
111
    { _name1, _f1_data, 0, sizeof(_f1_data)}, // Empty file
112
    { _name2, _f2_data, 0, sizeof(_f2_data)}, // Empty file
113
    { 0, 0, 0}  // End of list
114
};
115
 
116
static inline struct _file *
117
dummy_fp(int fd)
118
{
119
    struct _file *fp;
120
    if ((fd < 0) || (fd >= NUM_FILES)) return (struct _file *)0;
121
    fp = &files[fd];
122
    if (!(fp->flags & FILE_OPEN)) return (struct _file *)0;
123
    return fp;
124
}
125
 
126
static int
127
dummy_open(const char *fn, int flags)
128
{
129
    int res = -1;
130
    int fd;
131
    struct _file *fp;
132
    struct _file_info *fi;
133
 
134
    fp = files;
135
    for (fd = 0;  fd < NUM_FILES;  fd++, fp++) {
136
        if (!(fp->flags & FILE_OPEN)) break;
137
    }
138
    if (fd == NUM_FILES) {
139
        return -1;  // No free files
140
    }
141
    if (flags & O_RDONLY) {
142
        // Search for an extant file
143
        fi = file_list;
144
        while (fi->name) {
145
            if (strcmp(fi->name, fn) == 0) {
146
                // Found it!
147
                fp->pos = fi->data;
148
                fp->eof = fi->data + fi->length;
149
                fp->flags = FILE_OPEN;
150
                fp->mode = flags;
151
                return fd;
152
            }
153
            fi++;
154
        }
155
    } else {
156
        // Search for a non-existant file
157
        fi = file_list;
158
        while (fi->name) {
159
            if (fi->name[0] == '\0' || strcmp(fi->name, fn) == 0) {
160
                if ( !fi->name[0] )
161
                    // Empty slot found
162
                    strcpy(fi->name, fn);
163
                fp->pos = fi->data;
164
                fp->eof = fi->data + fi->size;
165
                fp->file = fi;  // So we can update file info later
166
                fp->mode = flags;
167
                fp->flags = FILE_OPEN;
168
                return fd;
169
            }
170
            fi++;
171
        }
172
    }
173
    // No such file
174
    return res;
175
}
176
 
177
static int
178
dummy_close(int fd)
179
{
180
    struct _file *fp = dummy_fp(fd);
181
    if (!fp) return -1;
182
    if (fp->mode & O_WRONLY) {
183
        // Clean up - update file info for later
184
        fp->file->length = fp->pos - fp->file->data;
185
    }
186
    fp->flags = 0;  // No longer open
187
    return 0;
188
}
189
 
190
static int
191
dummy_write(int fd, const void *buf, int len)
192
{
193
    struct _file *fp = dummy_fp(fd);
194
    int res;
195
    if (!fp) return -1;
196
    res = fp->eof - fp->pos;  // Space left in file
197
    if (res <= 0) return 0;  // End of file
198
    if (res > len) res = len;
199
    bcopy(buf, fp->pos, res);
200
    fp->pos += res;
201
    return res;
202
}
203
 
204
static int
205
dummy_read(int fd, void *buf, int len)
206
{
207
    struct _file *fp = dummy_fp(fd);
208
    int res;
209
    if (!fp) return -1;
210
    res = fp->eof - fp->pos;  // Number of bytes left in "file"
211
    if (res > len) res = len;
212
    if (res <= 0) return 0;  // End of file
213
    bcopy(fp->pos, buf, res);
214
    fp->pos += res;
215
    return res;
216
}
217
 
218
 
219
// EOF tftp_dummy_file.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.