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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_71/] [or1ksim/] [peripheral/] [channels/] [file.c] - Blame information for rev 1070

Go to most recent revision | 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
#define _GNU_SOURCE     /* for strndup */
23
 
24
#include <sys/types.h>  /* open() */
25
#include <sys/stat.h>   /* open() */
26
#include <fcntl.h>      /* open() */
27
#include <malloc.h>     /* calloc(), free() */
28
#include <string.h>     /* strndup(), strchr() */
29
#include <errno.h>      /* errno */
30
#include <unistd.h>     /* close() */
31
 
32
#include "channel.h"    /* struct channel_ops */
33
#include "fd.h"         /* struct fd_channel, fd_read(), fd_write() */
34
 
35
struct file_channel
36
{
37
        struct fd_channel fds;
38
        char * namein;
39
        char * nameout;
40
};
41
 
42
static void * file_init(const char * args)
43
{
44
        struct file_channel * retval;
45
        char * nameout;
46
 
47
        if(!args)
48
        {
49
                errno = EINVAL;
50
                return NULL;
51
        }
52
 
53
        retval = (struct file_channel*)calloc(1, sizeof(struct file_channel));
54
 
55
        if(!retval)
56
        {
57
                return NULL;
58
        }
59
 
60
        retval->fds.fdin  = -1;
61
        retval->fds.fdout = -1;
62
 
63
        nameout = strchr(args, ',');
64
 
65
        if(nameout)
66
        {
67
                retval->namein  = strndup(args, nameout - args);
68
                retval->nameout = strdup(nameout+1);
69
        }
70
        else
71
        {
72
                retval->nameout = retval->namein  = strdup(args);
73
        }
74
 
75
        return (void*)retval;
76
}
77
 
78
static int file_open(void * data)
79
{
80
        struct file_channel * files = (struct file_channel *)data;
81
 
82
        if(!files)
83
        {
84
                errno = ENODEV;
85
                return -1;
86
        }
87
 
88
        if(files->namein == files->nameout)
89
        {
90
                /* if we have the same name in and out
91
                 * it cannot (logically) be a regular files.
92
                 * so we wont create one
93
                 */
94
                files->fds.fdin = files->fds.fdout = open(files->namein, O_RDWR);
95
 
96
                return files->fds.fdin < 0 ? -1 : 0;
97
        }
98
 
99
 
100
        files->fds.fdin = open(files->namein, O_RDONLY | O_CREAT, 0664);
101
 
102
        if(files->fds.fdin < 0)
103
                return -1;
104
 
105
        files->fds.fdout = open(files->nameout, O_WRONLY | O_CREAT, 0664);
106
 
107
        if(files->fds.fdout < 0)
108
        {
109
                close(files->fds.fdout);
110
                files->fds.fdout = -1;
111
                return -1;
112
        }
113
 
114
        return 0;
115
}
116
 
117
static void file_close(void * data)
118
{
119
        struct file_channel * files = (struct file_channel *)data;
120
 
121
        if(files->fds.fdin != files->fds.fdout)
122
                close(files->fds.fdin);
123
 
124
        close(files->fds.fdout);
125
 
126
        files->fds.fdin  = -1;
127
        files->fds.fdout = -1;
128
}
129
 
130
static void file_free(void * data)
131
{
132
        struct file_channel * files = (struct file_channel *)data;
133
 
134
        if(files->namein != files->nameout)
135
                free(files->namein);
136
 
137
        free(files->nameout);
138
 
139
        free(files);
140
}
141
 
142
struct channel_ops file_channel_ops =
143
{
144
        init:   file_init,
145
        open:   file_open,
146
        close:  file_close,
147
        read:   fd_read,
148
        write:  fd_write,
149
        free:   file_free,
150
};
151
 
152
/*
153
 * Local variables:
154
 * c-file-style: "linux"
155
 * End:
156
 */

powered by: WebSVN 2.1.0

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