File: rfio_rfilefdt.c

package info (click to toggle)
lcgdm 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,532 kB
  • sloc: ansic: 226,887; sh: 13,562; perl: 11,575; python: 11,572; cpp: 5,716; sql: 1,824; makefile: 1,301; fortran: 113
file content (198 lines) | stat: -rw-r--r-- 3,893 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * $Id: rfio_rfilefdt.c,v 1.1 2005/03/31 13:13:03 baud Exp $
 */

/*
 * System remote file I/O
 */
#ifdef _WIN32
#include "Cmutex.h"
#endif
#define RFIO_KERNEL     1
#include <fcntl.h>
#if defined(_WIN32)
#define MAXHOSTNAMELEN 64
#else
#include <sys/param.h>          /* For MAXHOSTNAMELEN definition  */
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
#include <stdlib.h>
#include "rfio.h"
#include "rfio_rfilefdt.h"

extern RFILE *rfilefdt[MAXRFD] ;

RFILE dummyrfile;               /* Used to fill with a dummy value */

/*
 * Seach for a free index in the rfilefdt table
 */
int DLL_DECL rfio_rfilefdt_allocentry(s)
     int s;
{
#ifdef _WIN32
  int i;
  int rc;

  if (Cmutex_lock((void *) rfilefdt,-1) != 0) {
    return(-1);
  }
  /* Scan it */

  for (i = 0; i < MAXRFD; i++) {
    if (rfilefdt[i] == NULL) {
      rc = i;
      rfilefdt[i] = &dummyrfile;
      goto _rfio_rfilefdt_allocentry_return;
    }
  }

  serrno = ENOENT;
  rc = -1;

 _rfio_rfilefdt_allocentry_return:
  if (Cmutex_unlock((void *) rfilefdt) != 0) {
    return(-1);
  }
  return(rc);
#else /* _WIN32 */
  return(((s >= 0) && (s < MAXRFD)) ? s : -1);
#endif /* _WIN32 */
}

/*
 * Seach for a given index in the rfilefdt table
 * On UNIX, if scanflag is FINDRDIR_WITH_SCAN,
 * a scan of table content is performed, otherwise
 * only boundary and content within the boundary
 * is performed.
 */
int DLL_DECL rfio_rfilefdt_findentry(s,scanflag)
     int s;
     int scanflag;
{
  int i;
#ifdef _WIN32
  int rc;

  if (Cmutex_lock((void *) rfilefdt,-1) != 0) {
    return(-1);
  }
  /* Scan it */

  for (i = 0; i < MAXRFD; i++) {
    if (rfilefdt[i] != NULL) {
      if (rfilefdt[i]->s == s) {
        rc = i;
        goto _rfio_rfilefdt_findentry_return;
      }
    }
  }

  serrno = ENOENT;
  rc = -1;

 _rfio_rfilefdt_findentry_return:
  if (Cmutex_unlock((void *) rfilefdt) != 0) {
    return(-1);
  }
  return(rc);
#else /* _WIN32 */
  if (scanflag == FINDRFILE_WITH_SCAN) {
    for (i = 0; i < MAXRFD; i++) {
      if (rfilefdt[i] != NULL) {
        if (rfilefdt[i]->s == s) {
          return(i);
        }
      }
    }
    return(-1);
  } else {
    return(((s >= 0) && (s < MAXRFD) && (rfilefdt[s] != NULL)) ? s : -1);
  }
#endif /* _WIN32 */
}


/*
 * Seach for a given pointer in the rfilefdt table
 * On UNIX, if scanflag is FINDRDIR_WITH_SCAN,
 * a scan of table content is performed, otherwise
 * only boundary and content within the boundary
 * is performed.
 */
int DLL_DECL rfio_rfilefdt_findptr(ptr,scanflag)
     RFILE *ptr;
     int scanflag;
{
  int i;
#ifdef _WIN32
  int rc;

  if (Cmutex_lock((void *) rfilefdt,-1) != 0) {
    return(-1);
  }
  /* Scan it */

  for (i = 0; i < MAXRFD; i++) {
    if (rfilefdt[i] == ptr) {
      rc = i;
      goto _rfio_rfilefdt_findentry_return;
    }
  }

  serrno = ENOENT;
  rc = -1;

 _rfio_rfilefdt_findentry_return:
  if (Cmutex_unlock((void *) rfilefdt) != 0) {
    return(-1);
  }
  return(rc);
#else /* _WIN32 */
  if (scanflag == FINDRFILE_WITH_SCAN) {
    for (i = 0; i < MAXRFD; i++) {
      if (rfilefdt[i] == ptr) {
        return(i);
      }
    }
    return(-1);
  } else {
    /* This method works only in FINDRFILE_WITH_SCAN mode */
    serrno = EINVAL;
    return(-1);
  }
#endif /* _WIN32 */
}


/*
 * Free a given index in the rfilefdt table
 * Warning : the argument is REALLY an index
 */
int DLL_DECL rfio_rfilefdt_freeentry(s)
     int s;
{
#ifdef _WIN32
  if (Cmutex_lock((void *) rfilefdt,-1) != 0) {
    return(-1);
  }
  if (rfilefdt[s] != NULL) {
    if (rfilefdt[s] != &dummyrfile) free(rfilefdt[s]);
    rfilefdt[s] = NULL;
  }
  if (Cmutex_unlock((void *) rfilefdt) != 0) {
    return(-1);
  }
#else /* _WIN32 */
  if ((s >= 0) && (s < MAXRFD) && (rfilefdt[s] != NULL)) {
    if (rfilefdt[s] != &dummyrfile) free((char *)rfilefdt[s]);
    rfilefdt[s] = NULL;
  }
#endif /* _WIN32 */
  return(0);
}