moved around

diff --git a/libasn1compiler/asn1c_compat.c b/libasn1compiler/asn1c_compat.c
index 44ba3e6..1a402bf 100644
--- a/libasn1compiler/asn1c_compat.c
+++ b/libasn1compiler/asn1c_compat.c
@@ -1,8 +1,6 @@
+#include "asn1c_internal.h"
 #include <asn1c_compat.h>
 
-#include <string.h>
-#include <errno.h>
-
 #ifdef	HAVE_SYS_PARAM_H
 #include <sys/param.h>	/* For MAXPATHLEN */
 #endif
@@ -11,6 +9,63 @@
 #define	MAXPATHLEN	1024
 #endif
 
+#ifndef	DEFFILEMODE	/* Normally in <sys/stat.h> */
+#define	DEFFILEMODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
+#endif
+
+FILE *
+asn1c_open_file(const char *name, const char *ext) {
+	int created = 1;
+	struct stat sb;
+	char *fname;
+	int len;
+	FILE *fp;
+	int fd;
+
+	/*
+	 * Compute filenames.
+	 */
+	len = strlen(name) + strlen(ext) + 1;
+	fname = alloca(len);
+	snprintf(fname, len, "%s%s", name, ext);
+
+	/*
+	 * Create files.
+	 */
+	fd = open(fname, O_CREAT | O_EXCL | O_WRONLY, DEFFILEMODE);
+	if(fd == -1 && errno == EEXIST) {
+		fd = open(fname, O_WRONLY, DEFFILEMODE);
+		created = 0;
+	}
+	if(fd == -1) {
+		perror(fname);
+		return NULL;
+	}
+
+	/*
+	 * Check sanity.
+	 */
+	if(fstat(fd, &sb) || !S_ISREG(sb.st_mode)) {
+		fprintf(stderr, "%s: Not a regular file\n", fname);
+		if(created) unlink(fname);
+		close(fd);
+		return NULL;
+	}
+
+	(void)ftruncate(fd, 0);
+
+	/*
+	 * Convert file descriptor into file pointer.
+	 */
+	fp = fdopen(fd, "w");
+	if(fp == NULL) {
+		if(created) unlink(fname);
+		close(fd);
+	}
+	return fp;
+}
+
+
 char *
 a1c_basename(const char *path) {
 	static char strbuf[MAXPATHLEN];
@@ -33,7 +88,7 @@
 
 	for(name = pend; name > path && name[-1] != '/'; name--);
 
-	if((pend - name) >= sizeof(strbuf) - 1) {
+	if((pend - name) >= (int)sizeof(strbuf) - 1) {
 		errno = ENAMETOOLONG;
 		return 0;
 	}
@@ -79,7 +134,7 @@
 		return strbuf;
 	}
 
-	if((last - path) >= sizeof(strbuf)) {
+	if((last - path) >= (int)sizeof(strbuf)) {
 		errno = ENAMETOOLONG;
 		return 0;
 	}