aboutsummaryrefslogtreecommitdiff
path: root/src/lib/cfuns-c.c
diff options
context:
space:
mode:
authordos-reis <gdr@axiomatics.org>2013-06-01 07:35:18 +0000
committerdos-reis <gdr@axiomatics.org>2013-06-01 07:35:18 +0000
commit64aeafac79d72f440b6546bae91583e6efd6b674 (patch)
treede419861e4625d20bc0e1c68f5db1590a50708b9 /src/lib/cfuns-c.c
parentde3a19c35df30298d323c5882e39931f329ea29e (diff)
downloadopen-axiom-64aeafac79d72f440b6546bae91583e6efd6b674.tar.gz
Support --output in compiler, for bootstrapping stage.
Diffstat (limited to 'src/lib/cfuns-c.c')
-rw-r--r--src/lib/cfuns-c.c67
1 files changed, 47 insertions, 20 deletions
diff --git a/src/lib/cfuns-c.c b/src/lib/cfuns-c.c
index e95abbee..cac76b6c 100644
--- a/src/lib/cfuns-c.c
+++ b/src/lib/cfuns-c.c
@@ -2,7 +2,7 @@
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
- Copyright (C) 2007-2011, Gabriel Dos Reis.
+ Copyright (C) 2007-2013, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -37,6 +37,7 @@
#include "openaxiom-c-macros.h"
#include <errno.h>
+#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
@@ -47,7 +48,7 @@
#include <unistd.h>
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
# include <windows.h>
#else
# include <dirent.h>
@@ -103,7 +104,7 @@ addtopath(char *dir)
static inline int
openaxiom_is_path_separator(char c)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return c == '\\' || c == '/';
#else
return c == '/';
@@ -256,7 +257,7 @@ writeablep(const char *path)
the MinGW/MSYS port appears to use MS' StrDup as the real
worker. Consequently, the guarantee that the the string can
free'd no longer holds. We have to use MS's LocalFree. */
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
LocalFree(dir);
#else
free(dir);
@@ -338,7 +339,7 @@ OPENAXIOM_C_EXPORT int
std_stream_is_terminal(int fd)
{
assert(fd > -1 && fd < 3);
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
DWORD handle;
switch (fd) {
case 0: handle = STD_INPUT_HANDLE; break;
@@ -369,11 +370,11 @@ std_stream_is_terminal(int fd)
OPENAXIOM_C_EXPORT int
oa_chdir(const char* path)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return SetCurrentDirectory(path) ? 0 : -1;
#else
return chdir(path);
-#endif /* __WIN32__ */
+#endif /* OPENAXIOM_MS_WINDOWS_HOST */
}
@@ -394,7 +395,7 @@ oa_unlink(const char* path)
{
const char* curdir;
int status = -1;
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
WIN32_FIND_DATA findData;
HANDLE walkHandle;
@@ -478,7 +479,7 @@ oa_unlink(const char* path)
status = -1;
else
status = 0;
-#endif /* __WIN32__ */
+#endif /* OPENAXIOM_MS_WINDOWS_HOST */
sortie:
oa_chdir(curdir);
@@ -486,11 +487,37 @@ oa_unlink(const char* path)
return status;
}
+OPENAXIOM_C_EXPORT const char*
+oa_acquire_temporary_pathname() {
+#if OPENAXIOM_MS_WINDOWS_HOST
+ char buf[MAX_PATH];
+ const char* tmpdir = oa_get_tmpdir();
+ auto n = GetTempFileName(tmpdir, "oa-", rand() % SHORT_MAX, buf);
+ free(tmpdir);
+ if (n == 0) {
+ perror("oa_acquire_temporary_pathname");
+ exit(1);
+ }
+ return strdup(buf);
+#elif HAVE_DECL_TEMPNAM
+ return tempnam(oa_get_tmpdir(), "oa-");
+#else
+ std::string s = "oa-" + std::to_string(rand());
+ return strdup(s.c_str());
+#endif
+}
+
+OPENAXIOM_C_EXPORT void
+oa_release_temporary_pathname(const char* s)
+{
+ free(const_cast<char*>(s)); // yuck!
+}
+
/* Rename a file or directory. */
OPENAXIOM_C_EXPORT int
oa_rename(const char* old_path, const char* new_path)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return MoveFile(old_path, new_path) ? 0 : -1;
#else
return rename(old_path, new_path);
@@ -502,7 +529,7 @@ oa_rename(const char* old_path, const char* new_path)
OPENAXIOM_C_EXPORT int
oa_mkdir(const char* path)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return CreateDirectory(path, NULL) ? 0 : -1;
#else
# define DIRECTORY_PERM ((S_IRWXU|S_IRWXG|S_IRWXO) & ~(S_IWGRP|S_IWOTH))
@@ -521,7 +548,7 @@ oa_system(const char* cmd)
OPENAXIOM_C_EXPORT int
oa_getpid(void)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return GetCurrentProcessId();
#else
return getpid();
@@ -566,7 +593,7 @@ oa_strcat(const char* left, const char* right)
OPENAXIOM_C_EXPORT char*
oa_getenv(const char* var)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
#define BUFSIZE 128
char* buf = (char*) malloc(BUFSIZE);
int len = GetEnvironmentVariable(var, buf, BUFSIZE);
@@ -593,7 +620,7 @@ oa_getenv(const char* var)
OPENAXIOM_C_EXPORT int
oa_setenv(const char* var, const char* val)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return SetEnvironmentVariable(var, val);
#elif HAVE_DECL_SETENV
return !setenv(var, val, true);
@@ -615,7 +642,7 @@ oa_getcwd(void)
{
size_t bufsz = 256;
char* buf = (char*) malloc(bufsz);
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
DWORD n = GetCurrentDirectory(bufsz, buf);
if (n == 0) {
perror("oa_getcwd");
@@ -629,7 +656,7 @@ oa_getcwd(void)
}
}
return buf;
-#else /* __WIN32__ */
+#else /* OPENAXIOM_MS_WINDOWS_HOST */
errno = 0;
while (getcwd(buf,bufsz) == 0) {
if (errno == ERANGE) {
@@ -649,7 +676,7 @@ oa_getcwd(void)
OPENAXIOM_C_EXPORT int
oa_access_file_for_read(const char* path)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES ? -1 : 1;
#else
return access(path, R_OK);
@@ -660,7 +687,7 @@ oa_access_file_for_read(const char* path)
OPENAXIOM_C_EXPORT const char*
oa_get_tmpdir(void)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
char* buf;
/* First, probe. */
int bufsz = GetTempPath(0, NULL);
@@ -689,7 +716,7 @@ oa_get_tmpdir(void)
OPENAXIOM_C_EXPORT int
oa_copy_file(const char* src, const char* dst)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
return CopyFile(src,dst, /* bFailIfExists = */ 0) ? 0 : -1;
#else
#define OA_BUFSZ 512
@@ -767,7 +794,7 @@ oa_allocate_process_argv(Process* proc, int argc)
OPENAXIOM_C_EXPORT int
oa_spawn(Process* proc, SpawnFlags flags)
{
-#ifdef __WIN32__
+#ifdef OPENAXIOM_MS_WINDOWS_HOST
const char* path = NULL;
char* cmd_line = NULL;
int curpos = strlen(proc->argv[0]);