nano

nano with my custom patches
git clone git://bsandro.tech/nano
Log | Files | Refs | README | LICENSE

commit 8173b1bfbe8956959df98bd0074e9290ce0112a3
parent 93400ec8a19a868442a0f60ae555759d3bb80600
Author: Mike Frysinger <vapier@gentoo.org>
Date:   Sat, 26 Feb 2022 02:15:46 -0500

general: fix building for Windows

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

Diffstat:
Mconfigure.ac | 3+++
Msrc/files.c | 25++++++++++++++++++++++---
Msrc/nano.c | 13+++++++++----
Msrc/text.c | 6++++++
4 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/configure.ac b/configure.ac @@ -353,6 +353,9 @@ 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 funlockfile + fork fsync geteuid pipe wait waitpid) + dnl Checks for available flags. AX_CHECK_COMPILE_FLAG([-Wall], [CFLAGS="$CFLAGS -Wall"], [], []) diff --git 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); @@ -675,9 +679,13 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) block_sigwinch(TRUE); #endif +#ifdef HAVE_FLOCKFILE /* Lock the file before starting to read it, to avoid the overhead * of locking it for each single byte that we read from it. */ flockfile(f); +#else +# define getc_unlocked getc +#endif control_C_was_pressed = FALSE; @@ -741,8 +749,10 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) errornumber = errno; +#ifdef HAVE_FUNLOCKFILE /* We are done with the file, unlock it. */ 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 */ @@ -1653,6 +1669,7 @@ bool make_backup_of(char *realname) if (backup_file == NULL) goto problem; +#ifdef HAVE_FCHOWN /* Try to change owner and group to those of the original file; * ignore permission errors, as a normal user cannot change the owner. */ if (fchown(descriptor, openfile->statinfo->st_uid, @@ -1660,7 +1677,8 @@ bool make_backup_of(char *realname) fclose(backup_file); goto problem; } - +#endif +#ifdef HAVE_FCHMOD /* 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. */ @@ -1668,6 +1686,7 @@ bool make_backup_of(char *realname) fclose(backup_file); goto problem; } +#endif original = fopen(realname, "rb"); diff --git 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) +#ifdef __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) { @@ -894,10 +894,11 @@ void signal_init(void) sigaction(SIGTERM, &deed, NULL); #ifndef NANO_TINY +#ifdef SIGWINCH /* Trap SIGWINCH because we want to handle window resizes. */ deed.sa_handler = handle_sigwinch; sigaction(SIGWINCH, &deed, NULL); - +#endif #ifdef SIGTSTP /* Prevent the suspend handler from getting interrupted. */ sigfillset(&deed.sa_mask); @@ -998,11 +999,13 @@ void continue_nano(int signal) /* Block or unblock the SIGWINCH signal, depending on the blockit parameter. */ void block_sigwinch(bool blockit) { +#ifdef SIGWINCH sigset_t winch; sigemptyset(&winch); sigaddset(&winch, SIGWINCH); sigprocmask(blockit ? SIG_BLOCK : SIG_UNBLOCK, &winch, NULL); +#endif #ifndef NANO_TINY if (the_window_resized) @@ -1786,10 +1789,12 @@ int main(int argc, char **argv) /* Back up the terminal settings so that they can be restored. */ tcgetattr(STDIN_FILENO, &original_state); +#if defined(F_GETFL) && defined(F_SETFL) /* Get the state of standard input and ensure it uses blocking mode. */ 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 +2077,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 @@ -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. */