File: dcompat.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (295 lines) | stat: -rw-r--r-- 8,265 bytes parent folder | download | duplicates (2)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 *
 *  Copyright (C) 1994-2024, OFFIS e.V.
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation were partly developed by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *  For further copyrights, see the following paragraphs.
 *
 */

/*
**  Copyright (C) 1993/1994, OFFIS, Oldenburg University and CERIUM
**
**  This software and supporting documentation were
**  developed by
**
**    Institut OFFIS
**    Bereich Kommunikationssysteme
**    Westerstr. 10-12
**    26121 Oldenburg, Germany
**
**    Fachbereich Informatik
**    Abteilung Prozessinformatik
**    Carl von Ossietzky Universitaet Oldenburg
**    Ammerlaender Heerstr. 114-118
**    26111 Oldenburg, Germany
**
**    CERIUM
**    Laboratoire SIM
**    Faculte de Medecine
**    2 Avenue du Pr. Leon Bernard
**    35043 Rennes Cedex, France
**
**  for CEN/TC251/WG4 as a contribution to the Radiological
**  Society of North America (RSNA) 1993 Digital Imaging and
**  Communications in Medicine (DICOM) Demonstration.
**
**  THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND NEITHER OFFIS,
**  OLDENBURG UNIVERSITY NOR CERIUM MAKE ANY WARRANTY REGARDING
**  THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR
**  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER
**  DISEASES OR ITS CONFORMITY TO ANY SPECIFICATION.  THE
**  ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF THE SOFTWARE
**  IS WITH THE USER.
**
**  Copyright of the software and supporting documentation
**  is, unless otherwise stated, jointly owned by OFFIS,
**  Oldenburg University and CERIUM and free access is hereby
**  granted as a license to use this software, copy this
**  software and prepare derivative works based upon this
**  software. However, any distribution of this software
**  source code or supporting documentation or derivative
**  works (source code and supporting documentation) must
**  include the three paragraphs of this copyright notice.
**
*/

/*
**
** Author: Andrew Hewett                Created: 11-08-93
**
** Module: dcompat.cc
**
** Purpose:
**      This is the place to declare compatibility routines
**      which can be missing on some systems.
**
** Base Reference System is SUNOS 4.1.3
**
** This include file is automatically included by dicom.h
**
** Module Prefix: none
**
*/

#include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */

#include "dcmtk/dcmnet/dcompat.h"
#include "dcmtk/dcmnet/dicom.h"
#include "dcmtk/ofstd/ofbmanip.h"
#include "dcmtk/ofstd/ofconsol.h"
#include "dcmtk/ofstd/ofstd.h"
#include "dcmtk/dcmnet/diutil.h"

BEGIN_EXTERN_C
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>  /* for access() on Win32 */
#endif
END_EXTERN_C

/*
 * On DEC alpha the linker moans if a library is empty.
 * So define a dummy variable.
 */
char dcompat_functionDefinedOnlyToStopLinkerMoaning;


#ifndef HAVE_FLOCK

#ifdef _WIN32

#ifndef USE__LOCKING

/* Note: this emulation of flock() for Win32 uses the function _get_osfhandle()
 * which takes a Unix-style file descriptor and derives the corresponding
 * Win32 API file handle (HANDLE). This function may not be available on all
 * compilers for Win32, sorry.
 * Since _locking() does not implement shared/exclusive locks, the only alternative
 * would be to re-write the complete file access to Win32 API functions. Have fun!
 */

int dcmtk_flock(int fd, int operation)
{
#ifdef __CYGWIN__
  HANDLE handle=(void *)get_osfhandle(fd);
#else
  HANDLE handle=(void *)_get_osfhandle(fd);
#endif
  OVERLAPPED overl;
  OFBitmanipTemplate<char>::zeroMem((char *)&overl, sizeof(overl));

  if (operation==LOCK_SH)
  {
    if (GetVersion() < 0x80000000) // Windows NT
    {
      // LockFileEx is only supported on Windows NT
      if (LockFileEx(handle,0,0,(DWORD)-1,(DWORD)-1,&overl) !=0) return 0; else return -1;
    } else {
      // using LockFile on Win32s and Win95. LOCKS ARE ALWAYS EXCLUSIVE!
      // make sure that we lock less than 2GB, to avoid errors on FAT16
      if (LockFile(handle, 0,0,(DWORD)0x7FFFFFFF, 0) !=0) return 0; else return -1;
    }
  }
  else if (operation==LOCK_EX)
  {
    if (GetVersion() < 0x80000000) // Windows NT
    {
      // LockFileEx is only supported on Windows NT
      if (LockFileEx(handle,LOCKFILE_EXCLUSIVE_LOCK,0,(DWORD)-1,(DWORD)-1,&overl) !=0) return 0; else return -1;
    } else {
      // using LockFile on Win32s and Win95.
      // make sure that we lock less than 2GB, to avoid errors on FAT16
      if (LockFile(handle, 0,0,(DWORD)0x7FFFFFFF, 0) !=0) return 0; else return -1;
    }
  }
  else if (operation==LOCK_UN)
  {
    if (GetVersion() < 0x80000000) // Windows NT
    {
      // UnlockFileEx is only supported on Windows NT
      if (UnlockFileEx(handle,0,(DWORD)-1,(DWORD)-1,&overl) !=0) return 0; else return -1;
    } else {
      // using UnlockFile on Win32s and Win95.
      if (UnlockFile(handle, 0, 0,(DWORD)0x7FFFFFFF, 0) !=0) return 0; else return -1;
    }
  }
  else return -1; /* unknown lock operation */
}

#else /* USE__LOCKING */

/* Note: this alternative emulation of flock() for Win32 uses _locking().
 * This version should only be used on compilers where _get_osfhandle()
 * is not available since it does not implement shared locks.
 */

int dcmtk_flock(int fd, int operation)
{
    long originalPosition = tell(fd);
    long fileSize = lseek(fd, 0L, SEEK_END);
    if (fileSize < 0) return fileSize;
    long maxSize = 0x7fffffff;

    /* seek to beginning of file */
    long pos = lseek(fd, 0L, SEEK_SET);
    if (pos < 0) return pos;
    int mode = 0;

    /* we only have exclusive lock using the windows _locking function */
    if (operation & LOCK_SH) mode = _LK_LOCK; /* shared lock */
    if (operation & LOCK_EX) mode = _LK_LOCK; /* exclusive lock */
    if (operation & LOCK_UN) mode = _LK_UNLCK; /* unlock */
    if (operation & LOCK_NB) mode = _LK_NBLCK; /* non-blocking */
    int status = _locking(fd, mode, maxSize);

    /* reset file point back to original position */
    pos = lseek(fd, originalPosition, SEEK_SET);
    if (pos < 0) return pos;
    return status;
}

#endif /* USE__LOCKING */
#else /* _WIN32 */

/*
 * Simulate the flock function calls (e.g. Solaris 2 does not have them)
 * using the facilities of fcntl(2)
 */

int dcmtk_flock(int fd, int operation)
{
    struct flock fl;
    int result;
    int cmd;

    fl.l_whence = 0;
    fl.l_start = 0;	/* from beginning of file */
    fl.l_len = 0;	/* until end of file */

    if (operation & LOCK_SH) {
	/* shared lock */
    	fl.l_type = F_RDLCK;
    }
    if (operation & LOCK_EX) {
        /* exclusive lock */
	fl.l_type = F_WRLCK;
    }
    if (operation & LOCK_UN) {
	/* unlock */
        fl.l_type = F_UNLCK;
    }
    if (operation & LOCK_NB) {
        /* non-blocking */
	cmd = F_SETLK;
    } else {
    	/* blocking */
	cmd = F_SETLKW;
    }

#if SIZEOF_VOID_P == SIZEOF_INT
  /* some systems, e.g. NeXTStep, need the third argument
   * for fcntl calls to be casted to int. Other systems,
   * e.g. OSF1-Alpha, won't accept this because int and struct flock *
   * have different sizes. The workaround used here is to use a typecast to int
   * if sizeof(void *) == sizeof(int) and leave it away otherwise.
   */
    result = fcntl(fd, cmd, (int)(&fl));
#else
    result = fcntl(fd, cmd, &fl);
#endif

    return result;
}

#endif /* _WIN32 */

#endif /* HAVE_FLOCK */

#ifndef HAVE_ACCESS

/*
** The access function is OS dependent.
*/

#if defined(macintosh) || defined(_WIN32)
int access(const char* path, int /* amode */)
{
    int rc;
    struct stat buf;

    rc = stat(path, &buf);

    /* WARNING
    ** on the macintosh if a file is there we can do anything with it except
    ** if it is locked or on a read only filesystem.  Trying to find out about
    ** that is too complicated at the moment.
    */
    return rc;
}
#endif

#endif /* HAVE_ACCESS */

DCMTK_DCMNET_EXPORT void dcmtk_plockerr(const char *s)
{
  DCMNET_ERROR(s << ": " << OFStandard::getLastSystemErrorCode().message());
}