[PATCH] fix building for Windows

Mike Frysinger <[email protected]>
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
Fix building with x86_64-w64-mingw32 to cross-compile native Windows
programs.  Need to:
* add checks for missing functions
* don't use signals that are unavailable on the platform
* avoid useless non-Linux sys/ioctl.h include
* use putenv instead of setenv as the latter is unavailable
---
 configure.ac |  1 +
 src/files.c  | 24 ++++++++++++++++++++++--
 src/nano.c   | 12 +++++++++---
 src/text.c   |  6 ++++++
 4 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index 6f25728865e4..ad87344d7bbc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -353,6 +353,7 @@ dnl Checks for functions.
 if test "x$enable_utf8" != xno; then
 	AC_CHECK_FUNCS(iswalpha iswalnum iswpunct mbstowcs wctomb)
 fi
+AC_CHECK_FUNCS_ONCE(chmod chown fchmod fchown flockfile fork fsync funlockfile geteuid pipe wait waitpid)
 
 dnl Checks for available flags.
 
diff --git a/src/files.c b/src/files.c
index 62cc8f211954..3d27b593a12e 100644
--- a/src/files.c
+++ b/src/files.c
@@ -33,6 +33,10 @@
 
 #define RW_FOR_ALL  (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
 
+#ifndef HAVE_FSYNC
+# define fsync(...) 0
+#endif
+
 /* Add an item to the circular list of openfile structs. */
 void make_new_buffer(void)
 {
@@ -143,7 +147,7 @@ const char *locking_suffix = ".swp";
  * existing version of that file.  Return TRUE on success; FALSE otherwise. */
 bool write_lockfile(const char *lockfilename, const char *filename, bool modified)
 {
-#ifdef HAVE_PWD_H
+#if defined(HAVE_PWD_H) && defined(HAVE_GETEUID)
 	pid_t mypid = getpid();
 	uid_t myuid = geteuid();
 	struct passwd *mypwuid = getpwuid(myuid);
@@ -411,7 +415,7 @@ bool open_buffer(const char *filename, bool new_one)
 			free(realname);
 			return FALSE;
 		}
-#else
+#elif defined(HAVE_GETEUID)
 		if (new_one && !(fileinfo.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) &&
 						geteuid() == ROOT_UID)
 			statusline(ALERT, _("%s is meant to be read-only"), realname);
@@ -677,7 +681,11 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable)
 
 	/* Lock the file before starting to read it, to avoid the overhead
 	 * of locking it for each single byte that we read from it. */
+#ifdef HAVE_FLOCKFILE
 	flockfile(f);
+#else
+# define getc_unlocked getc
+#endif
 
 	control_C_was_pressed = FALSE;
 
@@ -742,7 +750,9 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable)
 	errornumber = errno;
 
 	/* We are done with the file, unlock it. */
+#ifdef HAVE_FUNLOCKFILE
 	funlockfile(f);
+#endif
 
 #ifndef NANO_TINY
 	block_sigwinch(FALSE);
@@ -950,7 +960,9 @@ static pid_t pid_of_command = -1;
 /* Send an unconditional kill signal to the running external command. */
 void cancel_the_command(int signal)
 {
+#ifdef SIGKILL
 	kill(pid_of_command, SIGKILL);
+#endif
 }
 
 /* Send the text that starts at the given line to file descriptor fd. */
@@ -973,6 +985,7 @@ void send_data(const linestruct *line, int fd)
 /* Execute the given command in a shell.  Return TRUE on success. */
 bool execute_command(const char *command)
 {
+#if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAIT)
 	int from_fd[2], to_fd[2];
 		/* The pipes through which text will be written and read. */
 	struct sigaction oldaction, newaction = {{0}};
@@ -1106,6 +1119,9 @@ bool execute_command(const char *command)
 	terminal_init();
 
 	return TRUE;
+#else
+	return FALSE;
+#endif
 }
 #endif /* NANO_TINY */
 
@@ -1655,19 +1671,23 @@ bool make_backup_of(char *realname)
 
 	/* Try to change owner and group to those of the original file;
 	 * ignore permission errors, as a normal user cannot change the owner. */
+#ifdef HAVE_FCHOWN
 	if (fchown(descriptor, openfile->statinfo->st_uid,
 							openfile->statinfo->st_gid) < 0 && errno != EPERM) {
 		fclose(backup_file);
 		goto problem;
 	}
+#endif
 
 	/* Set the backup's permissions to those of the original file.
 	 * It is not a security issue if this fails, as we have created
 	 * the file with just read and write permission for the owner. */
+#ifdef HAVE_FCHMOD
 	if (fchmod(descriptor, openfile->statinfo->st_mode) < 0 && errno != EPERM) {
 		fclose(backup_file);
 		goto problem;
 	}
+#endif
 
 	original = fopen(realname, "rb");
 
diff --git a/src/nano.c b/src/nano.c
index 04ecdbbf3492..d551c9e2a1ed 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -26,7 +26,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
-#if defined(__linux__) || !defined(NANO_TINY)
+#if defined(__linux__)
 #include <sys/ioctl.h>
 #endif
 #ifdef ENABLE_UTF8
@@ -339,7 +339,7 @@ void emergency_save(const char *filename)
 		fprintf(stderr, _("\nToo many .save files\n"));
 	else if (write_file(targetname, NULL, SPECIAL, OVERWRITE, NONOTES)) {
 		fprintf(stderr, _("\nBuffer written to %s\n"), targetname);
-#ifndef NANO_TINY
+#if !defined(NANO_TINY) && defined(HAVE_CHMOD) && defined(HAVE_CHOWN)
 		/* Try to chmod/chown the saved file to the values of the original file,
 		 * but ignore any failure as we are in a hurry to get out. */
 		if (openfile->statinfo) {
@@ -896,7 +896,9 @@ void signal_init(void)
 #ifndef NANO_TINY
 	/* Trap SIGWINCH because we want to handle window resizes. */
 	deed.sa_handler = handle_sigwinch;
+#ifdef SIGWINCH
 	sigaction(SIGWINCH, &deed, NULL);
+#endif
 
 #ifdef SIGTSTP
 	/* Prevent the suspend handler from getting interrupted. */
@@ -1001,7 +1003,9 @@ void block_sigwinch(bool blockit)
 	sigset_t winch;
 
 	sigemptyset(&winch);
+#ifdef SIGWINCH
 	sigaddset(&winch, SIGWINCH);
+#endif
 	sigprocmask(blockit ? SIG_BLOCK : SIG_UNBLOCK, &winch, NULL);
 
 #ifndef NANO_TINY
@@ -1787,9 +1791,11 @@ int main(int argc, char **argv)
 	tcgetattr(STDIN_FILENO, &original_state);
 
 	/* Get the state of standard input and ensure it uses blocking mode. */
+#if defined(F_GETFL) && defined(F_SETFL)
 	stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
 	if (stdin_flags != -1)
 		fcntl(STDIN_FILENO, F_SETFL, stdin_flags & ~O_NONBLOCK);
+#endif
 
 #ifdef ENABLE_UTF8
 	/* If setting the locale is successful and it uses UTF-8, we will
@@ -2072,7 +2078,7 @@ int main(int argc, char **argv)
 
 	/* Curses needs TERM; if it is unset, try falling back to a VT220. */
 	if (getenv("TERM") == NULL)
-		setenv("TERM", "vt220", 0);
+		putenv("TERM=vt220");
 
 	/* Enter into curses mode.  Abort if this fails. */
 	if (initscr() == NULL)
diff --git a/src/text.c b/src/text.c
index 0db4be1bfdd4..d8f51fb1b79a 100644
--- a/src/text.c
+++ b/src/text.c
@@ -2061,6 +2061,7 @@ bool replace_buffer(const char *filename, undo_type action, const char *operatio
 /* Execute the given program, with the given temp file as last argument. */
 void treat(char *tempfile_name, char *theprogram, bool spelling)
 {
+#if defined(HAVE_FORK) && defined(HAVE_WAIT)
 	ssize_t was_lineno = openfile->current->lineno;
 	size_t was_pww = openfile->placewewant;
 	size_t was_x = openfile->current_x;
@@ -2183,6 +2184,7 @@ void treat(char *tempfile_name, char *theprogram, bool spelling)
 		statusline(REMARK, _("Finished checking spelling"));
 	else
 		statusline(REMARK, _("Buffer has been processed"));
+#endif
 }
 #endif /* ENABLE_SPELLER || ENABLE_COLOR */
 
@@ -2295,6 +2297,7 @@ bool fix_spello(const char *word)
  * correction. */
 void do_int_speller(const char *tempfile_name)
 {
+#if defined(HAVE_FORK) && defined(HAVE_WAITPID)
 	char *misspellings, *pointer, *oneword;
 	long pipesize;
 	size_t buffersize, bytesread, totalread;
@@ -2468,6 +2471,7 @@ void do_int_speller(const char *tempfile_name)
 		statusline(ALERT, _("Error invoking \"spell\""));
 	else
 		statusline(REMARK, _("Finished checking spelling"));
+#endif
 }
 
 /* Spell check the current file.  If an alternate spell checker is
@@ -2533,6 +2537,7 @@ void do_spell(void)
 /* Run a linting program on the current buffer. */
 void do_linter(void)
 {
+#if defined(HAVE_FORK) && defined(HAVE_WAITPID)
 	char *lintings, *pointer, *onelint;
 	long pipesize;
 	size_t buffersize, bytesread, totalread;
@@ -2872,6 +2877,7 @@ void do_linter(void)
 	lastmessage = VACUUM;
 	currmenu = MMOST;
 	titlebar(NULL);
+#endif
 }
 
 /* Run a manipulation program on the contents of the buffer. */
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.