File: cygfile.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (63 lines) | stat: -rw-r--r-- 1,308 bytes parent folder | download | duplicates (3)
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
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#if defined(__CYGWIN__) || defined(__MSYS__)

#include <windows.h>
#include <pobl/bl_debug.h>
#include <pobl/bl_path.h> /* bl_conv_to_win32_path */

/* --- global functions --- */

HANDLE
cygfopen(const char *path, const char *mode) {
  char winpath[MAX_PATH];
  HANDLE file;

  if (bl_conv_to_win32_path(path, winpath, sizeof(winpath)) < 0) {
    return NULL;
  }

  if (*mode == 'a') {
    file =
        CreateFileA(winpath, FILE_APPEND_DATA, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  } else /* if( *mode == 'w') */
  {
    file = CreateFileA(winpath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  }

  if (file == INVALID_HANDLE_VALUE) {
    return NULL;
  } else {
    return file;
  }
}

int cygfclose(HANDLE file) {
  CloseHandle(file);

  return 0;
}

size_t cygfwrite(const void *ptr, size_t size, size_t nmemb, HANDLE file) {
  DWORD written;

  if (WriteFile(file, ptr, size * nmemb, &written, NULL)) {
    return written;
  } else {
    return 0;
  }
}

#ifdef __DEBUG
void main(void) {
  HANDLE file = cygfopen("test.log", "w");
  cygfwrite("aaa\n", 1, 4, file);
  cygfclose(file);

  file = cygfopen("test.log", "a");
  cygfwrite("bbb\n", 1, 4, file);
  cygfclose(file);
}
#endif

#endif