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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [peripheral/] [channels/] [file.c] - Blame information for rev 127

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* 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
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
6
   Copyright (C) 2008 Embecosm Limited
7
 
8
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
9
 
10
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
11
 
12
   This program is free software; you can redistribute it and/or modify it
13
   under the terms of the GNU General Public License as published by the Free
14
   Software Foundation; either version 3 of the License, or (at your option)
15
   any later version.
16
 
17
   This program is distributed in the hope that it will be useful, but WITHOUT
18
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20
   more details.
21
 
22
   You should have received a copy of the GNU General Public License along
23
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
24
 
25
/* This program is commented throughout in a fashion suitable for processing
26
   with Doxygen. */
27
 
28
 
29
/* Autoconf and/or portability configuration */
30
#include "config.h"
31
#include "port.h"
32
 
33
/* System includes */
34
#include <stdlib.h>
35
#include <unistd.h>
36
#include <errno.h>
37
#include <fcntl.h>
38
 
39
/* Package includes */
40
#include "channel.h"
41
#include "fd.h"
42
 
43
/*! Data structure representing a channel to/from a file */
44
struct file_channel
45
{
46
  struct fd_channel  fds;
47
  char              *namein;
48
  char              *nameout;
49
};
50
 
51
 
52
/* Forward declarations of static routines */
53
static void *file_init (const char *args);
54
static int   file_open (void *data);
55
static void  file_close (void *data);
56
static void  file_free (void *data);
57
 
58
/*! Data structure with all the operations for communicating with a file
59
    channel */
60
struct channel_ops  file_channel_ops = {
61
        .init  = file_init,
62
        .open  = file_open,
63
        .close = file_close,
64
        .read  = fd_read,
65
        .write = fd_write,
66
        .free  = file_free,
67
};
68
 
69
 
70
static void *
71
file_init (const char *args)
72
{
73
  struct file_channel *retval;
74
  char *nameout;
75
 
76
  if (!args)
77
    {
78
      errno = EINVAL;
79
      return NULL;
80
    }
81
 
82
  retval = (struct file_channel *) calloc (1, sizeof (struct file_channel));
83
 
84
  if (!retval)
85
    {
86
      return NULL;
87
    }
88
 
89
  retval->fds.fdin = -1;
90
  retval->fds.fdout = -1;
91
 
92
  nameout = strchr (args, ',');
93
 
94
  if (nameout)
95
    {
96
      retval->namein = strndup (args, nameout - args);
97
      retval->nameout = strdup (nameout + 1);
98
    }
99
  else
100
    {
101
      retval->nameout = retval->namein = strdup (args);
102
    }
103
 
104
  return (void *) retval;
105
}
106
 
107
static int
108
file_open (void *data)
109
{
110
  struct file_channel *files = (struct file_channel *) data;
111
 
112
  if (!files)
113
    {
114
      errno = ENODEV;
115
      return -1;
116
    }
117
 
118
  if (files->namein == files->nameout)
119
    {
120
      /* if we have the same name in and out
121
       * it cannot (logically) be a regular files.
122
       * so we wont create one
123
       */
124
      files->fds.fdin = files->fds.fdout = open (files->namein, O_RDWR);
125
 
126
      return files->fds.fdin < 0 ? -1 : 0;
127
    }
128
 
129
 
130
  files->fds.fdin = open (files->namein, O_RDONLY | O_CREAT, 0664);
131
 
132
  if (files->fds.fdin < 0)
133
    return -1;
134
 
135
  files->fds.fdout = open (files->nameout, O_WRONLY | O_CREAT, 0664);
136
 
137
  if (files->fds.fdout < 0)
138
    {
139
      close (files->fds.fdout);
140
      files->fds.fdout = -1;
141
      return -1;
142
    }
143
 
144
  return 0;
145
}
146
 
147
static void
148
file_close (void *data)
149
{
150
  struct file_channel *files = (struct file_channel *) data;
151
 
152
  if (files->fds.fdin != files->fds.fdout)
153
    close (files->fds.fdin);
154
 
155
  close (files->fds.fdout);
156
 
157
  files->fds.fdin = -1;
158
  files->fds.fdout = -1;
159
}
160
 
161
static void
162
file_free (void *data)
163
{
164
  struct file_channel *files = (struct file_channel *) data;
165
 
166
  if (files->namein != files->nameout)
167
    free (files->namein);
168
 
169
  free (files->nameout);
170
 
171
  free (files);
172
}

powered by: WebSVN 2.1.0

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