| 1 |
|
| 2 |
|
| 3 |
/* A C++ implementation for CFileSysOp.def. This file will use |
| 4 |
autoconf to test for the presence of operating system facilities. */ |
| 5 |
|
| 6 |
#include <config.h> |
| 7 |
#include <m2rts.h> |
| 8 |
|
| 9 |
#define EXPORT(FUNC) m2pim ## _CFileSysOp_ ## FUNC |
| 10 |
#define M2EXPORT(FUNC) m2pim ## _M2_CFileSysOp_ ## FUNC |
| 11 |
#define M2LIBNAME "m2pim" |
| 12 |
|
| 13 |
#if defined(HAVE_STDLIB_H) |
| 14 |
#include <stdlib.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#if defined(HAVE_UNISTD_H) |
| 18 |
#include <unistd.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
#if defined(HAVE_SYS_STAT_H) |
| 22 |
#include <sys/stat.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef HAVE_STDIO_H |
| 26 |
#include <stdio.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(HAVE_SYS_TYPES_H) |
| 30 |
#include <sys/types.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef HAVE_FCNTL_H |
| 34 |
#include <fcntl.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
/* Define a generic NULL if one hasn't already been defined. */ |
| 38 |
|
| 39 |
#if !defined(NULL) |
| 40 |
#define NULL 0 |
| 41 |
#endif |
| 42 |
|
| 43 |
#define A_FAIL (1<<5) |
| 44 |
|
| 45 |
|
| 46 |
extern "C" int |
| 47 |
EXPORT(Unlink) (char *filename) |
| 48 |
{ |
| 49 |
#if defined(HAVE_UNLINK) |
| 50 |
return unlink (filename); |
| 51 |
#else |
| 52 |
return -1; |
| 53 |
#endif |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/* Access test access to a path or file. The behavior is |
| 58 |
the same as defined in access(2). Except that A_FAIL |
| 59 |
is only used during the return result indicating the |
| 60 |
underlying C access has returned -1 (and errno can be |
| 61 |
checked). */ |
| 62 |
|
| 63 |
extern "C" int |
| 64 |
EXPORT(Access) (char *pathname, int mode) |
| 65 |
{ |
| 66 |
#if defined(HAVE_ACCESS) |
| 67 |
int result = access (pathname, mode); |
| 68 |
|
| 69 |
if (result == -1) |
| 70 |
return A_FAIL; |
| 71 |
return result; |
| 72 |
#else |
| 73 |
return A_FAIL; |
| 74 |
#endif |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/* Exists return true if pathname exists. */ |
| 79 |
|
| 80 |
extern "C" bool |
| 81 |
EXPORT(Exists) (char *pathname) |
| 82 |
{ |
| 83 |
#if defined(HAVE_ACCESS) |
| 84 |
return (access (pathname, F_OK) == 0); |
| 85 |
#else |
| 86 |
return false; |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
/* IsDir return true if filename is a regular directory. */ |
| 91 |
|
| 92 |
extern "C" bool |
| 93 |
EXPORT(IsDir) (char *dirname) |
| 94 |
{ |
| 95 |
#if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT) |
| 96 |
struct stat dir_stat; |
| 97 |
int res = stat (dirname, (struct stat *)&dir_stat); |
| 98 |
if (res == 0) |
| 99 |
return (dir_stat.st_mode & S_IFMT) == S_IFDIR; |
| 100 |
return false; |
| 101 |
#else |
| 102 |
return false; |
| 103 |
#endif |
| 104 |
} |
| 105 |
|
| 106 |
/* IsFile return true if filename is a regular file. */ |
| 107 |
|
| 108 |
extern "C" bool |
| 109 |
EXPORT(IsFile) (char *filename) |
| 110 |
{ |
| 111 |
#if defined(HAVE_SYS_STAT_H) && defined(HAVE_STRUCT_STAT) |
| 112 |
struct stat file_stat; |
| 113 |
int res = stat (filename, (struct stat *)&file_stat); |
| 114 |
if (res == 0) |
| 115 |
return (file_stat.st_mode & S_IFMT) == S_IFREG; |
| 116 |
return false; |
| 117 |
#else |
| 118 |
return false; |
| 119 |
#endif |
| 120 |
} |
| 121 |
|
| 122 |
/* GNU Modula-2 linking hooks. */ |
| 123 |
|
| 124 |
extern "C" void |
| 125 |
M2EXPORT(init) (int, char **, char **) |
| 126 |
{ |
| 127 |
} |
| 128 |
|
| 129 |
extern "C" void |
| 130 |
M2EXPORT(fini) (int, char **, char **) |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
extern "C" void |
| 135 |
M2EXPORT(dep) (void) |
| 136 |
{ |
| 137 |
} |
| 138 |
|
| 139 |
extern "C" void __attribute__((__constructor__)) |
| 140 |
M2EXPORT(ctor) (void) |
| 141 |
{ |
| 142 |
m2pim_M2RTS_RegisterModule ("CfileSysOp", M2LIBNAME, |
| 143 |
M2EXPORT(init), M2EXPORT(fini), |
| 144 |
M2EXPORT(dep)); |
| 145 |
} |