aboutsummaryrefslogtreecommitdiff
path: root/libgm2/libm2pim/CFileSysOp.cc
blob: fc2538c3cb9cd5ade85df726aa2b0368ebf017a4 (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
136
137
138
139
140
141
142
143
144
145


/* A C++ implementation for CFileSysOp.def.  This file will use
   autoconf to test for the presence of operating system facilities.  */

#include <config.h>
#include <m2rts.h>

#define EXPORT(FUNC) m2pim ## _CFileSysOp_ ## FUNC
#define M2EXPORT(FUNC) m2pim ## _M2_CFileSysOp_ ## FUNC
#define M2LIBNAME "m2pim"

#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#endif

#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif

#if defined(HAVE_SYS_STAT_H)
#include <sys/stat.h>
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#if defined(HAVE_SYS_TYPES_H)
#include <sys/types.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

/* Define a generic NULL if one hasn't already been defined.  */

#if !defined(NULL)
#define NULL 0
#endif

#define A_FAIL (1<<5)


extern "C" int
EXPORT(Unlink) (char *filename)
{
#if defined(HAVE_UNLINK)
  return unlink (filename);
#else
  return -1;
#endif
}


/* Access test access to a path or file.  The behavior is
   the same as defined in access(2).  Except that A_FAIL
   is only used during the return result indicating the
   underlying C access has returned -1 (and errno can be
   checked).  */

extern "C" int
EXPORT(Access) (char *pathname, int mode)
{
#if defined(HAVE_ACCESS)
  int result = access (pathname, mode);

  if (result == -1)
    return A_FAIL;
  return result;
#else
  return A_FAIL;
#endif
}


/* Exists return true if pathname exists.  */

extern "C" bool
EXPORT(Exists) (char *pathname)
{
#if defined(HAVE_ACCESS)
  return (access (pathname, F_OK) == 0);
#else
  return false;
#endif
}

/* IsDir return true if filename is a regular directory.  */

extern "C" bool
EXPORT(IsDir) (char *dirname)
{
#if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
  struct stat dir_stat;
  int res = stat (dirname, (struct stat *)&dir_stat);
  if (res == 0)
    return (dir_stat.st_mode & S_IFMT) == S_IFDIR;
  return false;
#else
  return false;
#endif
}

/* IsFile return true if filename is a regular file.  */

extern "C" bool
EXPORT(IsFile) (char *filename)
{
#if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT)
  struct stat file_stat;
  int res = stat (filename, (struct stat *)&file_stat);
  if (res == 0)
    return (file_stat.st_mode & S_IFMT) == S_IFREG;
  return false;
#else
  return false;
#endif
}

/* GNU Modula-2 linking hooks.  */

extern "C" void
M2EXPORT(init) (int, char **, char **)
{
}

extern "C" void
M2EXPORT(fini) (int, char **, char **)
{
}

extern "C" void
M2EXPORT(dep) (void)
{
}

extern "C" void __attribute__((__constructor__))
M2EXPORT(ctor) (void)
{
  m2pim_M2RTS_RegisterModule ("CfileSysOp", M2LIBNAME,
			      M2EXPORT(init), M2EXPORT(fini),
			      M2EXPORT(dep));
}