summaryrefslogtreecommitdiff
path: root/navit/support/libc/open.c
blob: 21028170676c76c9d91eb5835343e6a7f7ac1627 (plain)
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
/*
 * open.c: open, _open and_wopen implementations for WinCE.
 *
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is a part of the mingw-runtime package.
 * No warranty is given; refer to the file DISCLAIMER within the package.
 *
 * Written by Pedro Alves <pedro_alves@portugalmail.pt> Feb 2007
 *
 */

#include <windows.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

static int
vwopen (const wchar_t *wpath, int oflag, va_list ap)
{
  DWORD fileaccess;
  DWORD fileshare;
  DWORD filecreate;
  DWORD fileattrib;
  HANDLE hnd;

  switch (oflag & (O_RDONLY | O_WRONLY | O_RDWR))
    {
    case O_RDONLY:
      fileaccess = GENERIC_READ;
      break;
    case O_WRONLY:
      fileaccess = GENERIC_WRITE;
      break;
    case O_RDWR:
      fileaccess = GENERIC_READ | GENERIC_WRITE;
      break;
    default:
      return -1;
    }

  switch (oflag & (O_CREAT | O_EXCL | O_TRUNC))
    {
    case 0:
    case O_EXCL:		/* ignore EXCL w/o CREAT */
      filecreate = OPEN_EXISTING;
      break;
    case O_CREAT:
      filecreate = OPEN_ALWAYS;
      break;
    case O_CREAT | O_EXCL:
    case O_CREAT | O_TRUNC | O_EXCL:
      filecreate = CREATE_NEW;
      break;

    case O_TRUNC:
    case O_TRUNC | O_EXCL:	/* ignore EXCL w/o CREAT */
      filecreate = TRUNCATE_EXISTING;
      break;
    case O_CREAT | O_TRUNC:
      filecreate = CREATE_ALWAYS;
      break;
    default:
      /* this can't happen ... all cases are covered */
      return -1;
    }

  fileshare = 0;
  if (oflag & O_CREAT)
    {
      int pmode = va_arg (ap, int);
      if ((pmode & S_IREAD) == S_IREAD)
	fileshare |= FILE_SHARE_READ;
      if ((pmode & S_IWRITE) == S_IWRITE)
	fileshare |= FILE_SHARE_WRITE;
    }

  fileattrib = FILE_ATTRIBUTE_NORMAL;

  hnd = CreateFileW (wpath, fileaccess, fileshare, NULL, filecreate,
		     fileattrib, NULL);
  if (hnd == INVALID_HANDLE_VALUE)
    return -1;

  if (oflag & O_APPEND)
    SetFilePointer (hnd, 0, NULL, FILE_END);

  return (int) hnd;
}

static int
vopen (const char *path, int oflag, va_list ap)
{
  wchar_t wpath[MAX_PATH];

  size_t path_len = strlen (path);
  if (path_len >= MAX_PATH)
    return -1;

  mbstowcs (wpath, path, path_len + 1);

  return vwopen (wpath, oflag, ap);
}

int
_open (const char *path, int oflag, ...)
{
  va_list ap;
  int ret;
  va_start (ap, oflag);
  ret = vopen (path, oflag, ap);
  va_end (ap);
  return ret;
}

int
open (const char *path, int oflag, ...)
{
  va_list ap;
  int ret;
  va_start (ap, oflag);
  ret = vopen (path, oflag, ap);
  va_end (ap);
  return ret;
}

int
_wopen (const wchar_t *path, int oflag, ...)
{
  va_list ap;
  int ret;
  va_start (ap, oflag);
  ret = vwopen (path, oflag, ap);
  va_end (ap);
  return ret;
}