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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [peripheral/] [channels/] [file.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1070 rprescott
/* file.c -- Definition of functions and structures for
2
   peripheral to communicate with host through files
3
 
4
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
5
 
6
This file is part of OpenRISC 1000 Architectural Simulator.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
 
22 1244 hpanther
#if HAVE_CONFIG_H
23
#include <config.h>
24
#endif
25
 
26 1070 rprescott
#define _GNU_SOURCE     /* for strndup */
27
 
28
#include <sys/types.h>  /* open() */
29
#include <sys/stat.h>   /* open() */
30
#include <fcntl.h>      /* open() */
31 1244 hpanther
#if HAVE_MALLOC_H
32
#include <malloc.h>     /* calloc, free */
33
#endif
34 1070 rprescott
#include <string.h>     /* strndup(), strchr() */
35
#include <errno.h>      /* errno */
36
#include <unistd.h>     /* close() */
37
 
38
#include "channel.h"    /* struct channel_ops */
39
#include "fd.h"         /* struct fd_channel, fd_read(), fd_write() */
40
 
41
struct file_channel
42
{
43
        struct fd_channel fds;
44
        char * namein;
45
        char * nameout;
46
};
47
 
48
static void * file_init(const char * args)
49
{
50
        struct file_channel * retval;
51
        char * nameout;
52
 
53
        if(!args)
54
        {
55
                errno = EINVAL;
56
                return NULL;
57
        }
58
 
59
        retval = (struct file_channel*)calloc(1, sizeof(struct file_channel));
60
 
61
        if(!retval)
62
        {
63
                return NULL;
64
        }
65
 
66
        retval->fds.fdin  = -1;
67
        retval->fds.fdout = -1;
68
 
69
        nameout = strchr(args, ',');
70
 
71
        if(nameout)
72
        {
73
                retval->namein  = strndup(args, nameout - args);
74
                retval->nameout = strdup(nameout+1);
75
        }
76
        else
77
        {
78
                retval->nameout = retval->namein  = strdup(args);
79
        }
80
 
81
        return (void*)retval;
82
}
83
 
84
static int file_open(void * data)
85
{
86
        struct file_channel * files = (struct file_channel *)data;
87
 
88
        if(!files)
89
        {
90
                errno = ENODEV;
91
                return -1;
92
        }
93
 
94
        if(files->namein == files->nameout)
95
        {
96
                /* if we have the same name in and out
97
                 * it cannot (logically) be a regular files.
98
                 * so we wont create one
99
                 */
100
                files->fds.fdin = files->fds.fdout = open(files->namein, O_RDWR);
101
 
102
                return files->fds.fdin < 0 ? -1 : 0;
103
        }
104
 
105
 
106
        files->fds.fdin = open(files->namein, O_RDONLY | O_CREAT, 0664);
107
 
108
        if(files->fds.fdin < 0)
109
                return -1;
110
 
111
        files->fds.fdout = open(files->nameout, O_WRONLY | O_CREAT, 0664);
112
 
113
        if(files->fds.fdout < 0)
114
        {
115
                close(files->fds.fdout);
116
                files->fds.fdout = -1;
117
                return -1;
118
        }
119
 
120
        return 0;
121
}
122
 
123
static void file_close(void * data)
124
{
125
        struct file_channel * files = (struct file_channel *)data;
126
 
127
        if(files->fds.fdin != files->fds.fdout)
128
                close(files->fds.fdin);
129
 
130
        close(files->fds.fdout);
131
 
132
        files->fds.fdin  = -1;
133
        files->fds.fdout = -1;
134
}
135
 
136
static void file_free(void * data)
137
{
138
        struct file_channel * files = (struct file_channel *)data;
139
 
140
        if(files->namein != files->nameout)
141
                free(files->namein);
142
 
143
        free(files->nameout);
144
 
145
        free(files);
146
}
147
 
148
struct channel_ops file_channel_ops =
149
{
150
        init:   file_init,
151
        open:   file_open,
152
        close:  file_close,
153
        read:   fd_read,
154
        write:  fd_write,
155
        free:   file_free,
156
};
157
 
158
/*
159
 * Local variables:
160
 * c-file-style: "linux"
161
 * End:
162
 */

powered by: WebSVN 2.1.0

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