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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc1/] [or1ksim/] [peripheral/] [channels/] [file.c] - Blame information for rev 1749

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 1748 jeremybenn
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
6
   Copyright (C) 2008 Embecosm Limited
7 1070 rprescott
 
8 1748 jeremybenn
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
9 1070 rprescott
 
10 1748 jeremybenn
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
11 1070 rprescott
 
12 1748 jeremybenn
   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 1070 rprescott
 
17 1748 jeremybenn
   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 1070 rprescott
 
22 1748 jeremybenn
   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 1244 hpanther
 
25 1748 jeremybenn
/* This program is commented throughout in a fashion suitable for processing
26
   with Doxygen. */
27 1070 rprescott
 
28
 
29 1748 jeremybenn
/* Autoconf and/or portability configuration */
30
#include "config.h"
31
#include "port.h"
32 1070 rprescott
 
33 1748 jeremybenn
/* 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 1070 rprescott
struct file_channel
45
{
46 1748 jeremybenn
  struct fd_channel  fds;
47
  char              *namein;
48
  char              *nameout;
49 1070 rprescott
};
50
 
51 1748 jeremybenn
 
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 1070 rprescott
{
73 1748 jeremybenn
  struct file_channel *retval;
74
  char *nameout;
75 1070 rprescott
 
76 1748 jeremybenn
  if (!args)
77
    {
78
      errno = EINVAL;
79
      return NULL;
80
    }
81 1070 rprescott
 
82 1748 jeremybenn
  retval = (struct file_channel *) calloc (1, sizeof (struct file_channel));
83 1070 rprescott
 
84 1748 jeremybenn
  if (!retval)
85
    {
86
      return NULL;
87
    }
88 1070 rprescott
 
89 1748 jeremybenn
  retval->fds.fdin = -1;
90
  retval->fds.fdout = -1;
91 1070 rprescott
 
92 1748 jeremybenn
  nameout = strchr (args, ',');
93 1070 rprescott
 
94 1748 jeremybenn
  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 1070 rprescott
 
104 1748 jeremybenn
  return (void *) retval;
105 1070 rprescott
}
106
 
107 1748 jeremybenn
static int
108
file_open (void *data)
109 1070 rprescott
{
110 1748 jeremybenn
  struct file_channel *files = (struct file_channel *) data;
111 1070 rprescott
 
112 1748 jeremybenn
  if (!files)
113
    {
114
      errno = ENODEV;
115
      return -1;
116
    }
117 1070 rprescott
 
118 1748 jeremybenn
  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 1070 rprescott
 
126 1748 jeremybenn
      return files->fds.fdin < 0 ? -1 : 0;
127
    }
128 1070 rprescott
 
129
 
130 1748 jeremybenn
  files->fds.fdin = open (files->namein, O_RDONLY | O_CREAT, 0664);
131 1070 rprescott
 
132 1748 jeremybenn
  if (files->fds.fdin < 0)
133
    return -1;
134 1070 rprescott
 
135 1748 jeremybenn
  files->fds.fdout = open (files->nameout, O_WRONLY | O_CREAT, 0664);
136 1070 rprescott
 
137 1748 jeremybenn
  if (files->fds.fdout < 0)
138
    {
139
      close (files->fds.fdout);
140
      files->fds.fdout = -1;
141
      return -1;
142
    }
143 1070 rprescott
 
144 1748 jeremybenn
  return 0;
145 1070 rprescott
}
146
 
147 1748 jeremybenn
static void
148
file_close (void *data)
149 1070 rprescott
{
150 1748 jeremybenn
  struct file_channel *files = (struct file_channel *) data;
151 1070 rprescott
 
152 1748 jeremybenn
  if (files->fds.fdin != files->fds.fdout)
153
    close (files->fds.fdin);
154 1070 rprescott
 
155 1748 jeremybenn
  close (files->fds.fdout);
156 1070 rprescott
 
157 1748 jeremybenn
  files->fds.fdin = -1;
158
  files->fds.fdout = -1;
159 1070 rprescott
}
160
 
161 1748 jeremybenn
static void
162
file_free (void *data)
163 1070 rprescott
{
164 1748 jeremybenn
  struct file_channel *files = (struct file_channel *) data;
165 1070 rprescott
 
166 1748 jeremybenn
  if (files->namein != files->nameout)
167
    free (files->namein);
168 1070 rprescott
 
169 1748 jeremybenn
  free (files->nameout);
170 1070 rprescott
 
171 1748 jeremybenn
  free (files);
172 1070 rprescott
}

powered by: WebSVN 2.1.0

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