Skip to content

Instantly share code, notes, and snippets.

@amuramatsu
Created January 29, 2019 11:41
Show Gist options
  • Save amuramatsu/d1686fd2feb0382c8bccc8789b6290e4 to your computer and use it in GitHub Desktop.
Save amuramatsu/d1686fd2feb0382c8bccc8789b6290e4 to your computer and use it in GitHub Desktop.
diff -ruN nethack-3.6.1.orig/include/global.h nethack-3.6.1/include/global.h
--- nethack-3.6.1.orig/include/global.h 2018-04-27 21:07:22.000000000 +0900
+++ nethack-3.6.1/include/global.h 2019-01-29 20:33:51.671000000 +0900
@@ -243,13 +243,14 @@
#undef SAFERHANGUP
#endif
-#define Sprintf (void) sprintf
+#include "my_vsprintf.h"
+#define Sprintf (void) __sprintf
#define Strcat (void) strcat
#define Strcpy (void) strcpy
#ifdef NEED_VARARGS
#define Vprintf (void) vprintf
#define Vfprintf (void) vfprintf
-#define Vsprintf (void) vsprintf
+#define Vsprintf (void) __vsprintf
#endif
/* primitive memory leak debugging; see alloc.c */
diff -ruN nethack-3.6.1.orig/include/my_vsprintf.h nethack-3.6.1/include/my_vsprintf.h
--- nethack-3.6.1.orig/include/my_vsprintf.h 1970-01-01 09:00:00.000000000 +0900
+++ nethack-3.6.1/include/my_vsprintf.h 2019-01-29 20:33:51.671000000 +0900
@@ -0,0 +1,11 @@
+#ifndef MY_VSPRINTF_H
+#define MY_VSPRINTF_H
+
+#include <stdarg.h>
+
+int __vsnprintf(char *buffer, int n, const char *format, va_list va);
+int __vsprintf(char *buffer, const char *format, va_list va);
+int __snprintf(char *buffer, int n, const char *format, ...);
+int __sprintf(char *buffer, const char *format, ...);
+
+#endif /* MY_VSPRINTF_H */
diff -ruN nethack-3.6.1.orig/src/my_vsprintf.c nethack-3.6.1/src/my_vsprintf.c
--- nethack-3.6.1.orig/src/my_vsprintf.c 1970-01-01 09:00:00.000000000 +0900
+++ nethack-3.6.1/src/my_vsprintf.c 2019-01-29 20:33:51.671000000 +0900
@@ -0,0 +1,282 @@
+/*
+ * integer vsnprintf, et. al. based on BDS C v1.6 library
+ */
+/*
+ STDLIB2.C -- for BDS C v1.6 -- 1/86
+ Copyright (c) 1982, 1986 by BD Software, Inc.
+
+ The files STDLIB1.C, STDLIB2.C and STDLIB3.C contain the source
+ listings for all functions present in the DEFF.CRL library object
+ file. (Note: DEFF2.CRL contains the .CSM-coded portions of the
+ library.)
+
+ STDLIB2.C contains mainly formatted text I/O functions:
+
+ printf fprintf sprintf lprintf _spr
+ scanf fscanf sscanf _scn
+ getline puts
+ putdec
+
+*/
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <ctype.h>
+#include <limits.h>
+
+typedef enum { OK, ERROR } _result;
+static int _spr(const char *format, va_list va, _result (*putcf)(char));
+
+static char *bufptr;
+static char *endbufptr;
+static _result
+_writer(char c)
+{
+ if (endbufptr && bufptr >= endbufptr)
+ return ERROR;
+ *bufptr++ = c;
+ return OK;
+}
+
+int
+__vsnprintf(char *buffer, int n, const char *format, va_list va)
+{
+ if (n == 0) return 0;
+ bufptr = buffer;
+ endbufptr = buffer + n;
+ _spr(format, va, &_writer);
+ _writer('\0');
+ buffer[n-1] = '\0';
+ return bufptr - buffer;
+}
+
+int
+__vsprintf(char *buffer, const char *format, va_list va)
+{
+ bufptr = buffer;
+ endbufptr = NULL;
+ _spr(format, va, &_writer);
+ _writer('\0');
+ return bufptr - buffer;
+}
+
+int
+__snprintf(char *buffer, int n, const char *format, ...)
+{
+ int s;
+ va_list va;
+ va_start(va, format);
+ s = __vsnprintf(buffer, n, format, va);
+ va_end(va);
+ return s;
+}
+
+int
+__sprintf(char *buffer, const char *format, ...)
+{
+ int s;
+ va_list va;
+ va_start(va, format);
+ s = __vsprintf(buffer, format, va);
+ va_end(va);
+ return s;
+}
+
+/*
+ Internal routine used by "_spr" to perform ascii-
+ to-decimal conversion and update an associated pointer:
+*/
+
+static int
+_gv2(const char **sptr)
+{
+ int n;
+ n = 0;
+ while (isdigit(**sptr))
+ n = 10*n + *(*sptr)++ - '0';
+ return n;
+}
+
+static char
+_uspr(char **string, unsigned n, unsigned base, int upperflag)
+{
+ int length;
+ if (n < base) {
+ *(*string)++ = (n < 10) ? n + '0'
+ : (upperflag ? (n + 'A' - 10) : (n + 'a' - 10));
+ return 1;
+ }
+ length = _uspr(string, n / base, base, upperflag);
+ _uspr(string, n % base, base, upperflag);
+ return length + 1;
+}
+
+static int
+_bc(char c, char b)
+{
+ if (isalpha(c = toupper(c)))
+ c -= 55;
+ else if (isdigit(c))
+ c -= 0x30;
+ else
+ return ERROR;
+
+ if (c > b-1)
+ return ERROR;
+ else
+ return c;
+}
+
+static int
+_spr(const char *format, va_list va, _result (*putcf)(char))
+{
+ char c, prefill, *wptr;
+ long value;
+ int ljflag, upperflag;
+ int base = 10;
+ char wbuf[128]; /* 128 is enough for all but %s */
+ int length, *args, width, precision;
+
+ while ((c = *format++)) {
+ if (c == '%') {
+ wptr = wbuf;
+ precision = INT_MAX;
+ width = ljflag = 0;
+ upperflag = 0;
+ prefill = ' ';
+
+ if (*format == '-') {
+ format++;
+ ljflag=1;
+ }
+ if (*format == '0') {
+ format++;
+ prefill = '0';
+ }
+ if (*format == '*') {
+ format++;
+ width = va_arg(va, int);
+ }
+ else if (isdigit(*format))
+ width = _gv2(&format);
+ if (*format == '.') {
+ format++;
+ if (*format == '*') {
+ format++;
+ precision = va_arg(va, int);
+ }
+ else {
+ precision = _gv2(&format);
+ }
+ }
+ else if (*format == 'l')
+ format++; /* no longs here */
+
+ switch (c = *format++) {
+ case 'd':
+ {
+ int value = va_arg(va, int);
+ if (value < 0) {
+ *wptr++ = '-';
+ value = -value;
+ width--;
+ }
+ width -= _uspr(&wptr, value, base, 0);
+ }
+ goto pad;
+ case 'u':
+ base = 10; goto val;
+ case 'b':
+ base = 2; goto val;
+ case 'x':
+ base = 16; goto val;
+ case 'X':
+ base = 16; upperflag = 1; goto val;
+ case 'o':
+ base = 8;
+ val:
+ width -= _uspr(&wptr, va_arg(va, unsigned int), base, upperflag);
+ goto pad;
+
+ case 'c':
+ *wptr++ = va_arg(va, int) & 0xff;
+ width--;
+ goto pad;
+
+ pad:
+ *wptr = '\0';
+ length = strlen(wptr = wbuf);
+ pad7: /* don't modify the string at wptr */
+ if (!ljflag) {
+ while (width-- > 0)
+ if (putcf(prefill) == ERROR)
+ return ERROR;
+ }
+ while (length--) {
+ if (putcf(*wptr++) == ERROR)
+ return ERROR;
+ }
+ if (ljflag) {
+ while (width-- > 0)
+ if (putcf(' ') == ERROR)
+ return ERROR;
+ }
+ break;
+
+ case 's':
+ wptr = va_arg(va, char *);
+ length = strlen(wptr);
+ if (precision < length)
+ length = precision;
+ width -= length;
+ goto pad7;
+
+ case '\0':
+ return OK;
+
+ default:
+ if (putcf(c) == ERROR)
+ return ERROR;
+ }
+ }
+ else if (putcf(c) == ERROR)
+ return ERROR;
+ }
+ return OK;
+}
+
+#ifdef DEBUG
+#include <stdio.h>
+
+void
+test(const char *name, const char *format, ...)
+{
+ char buf1[1024], buf2[1024];
+ va_list va1, va2;
+ va_start(va1, format);
+ va_copy(va2, va1);
+ vsprintf(buf1, format, va1);
+ __vsprintf(buf2, format, va2);
+ va_end(va1);
+ va_end(va2);
+ if (strcmp(buf1, buf2) != 0)
+ printf("ERR!:: %s:%s:%s\n", name, buf1, buf2);
+}
+
+int
+main()
+{
+ test("test1", "testdata, %d, %10d", 10, 200);
+ test("test2", "testdata, %s, %10.10s", "dfadf,", "xxxxxxxxxxxxx");
+ test("test3", "testdata, %x, %10X", 10, 200);
+ test("test4", "testdata, %X, %10x", 10, 200);
+ test("test5", "testdata, %d, %10d", 10, 200);
+ test("test6", "testdata, %d, %10d", 10, 200);
+ test("test7", "testdata, %d, %10d", 10, 200);
+ test("test8", "testdata, %d, %10d", 10, 200);
+ test("test9", "testdata, %d, %10d", 10, 200);
+ return 0;
+}
+
+#endif
diff -ruN nethack-3.6.1.orig/sys/unix/Makefile.src nethack-3.6.1/sys/unix/Makefile.src
--- nethack-3.6.1.orig/sys/unix/Makefile.src 2019-01-29 20:17:36.000000000 +0900
+++ nethack-3.6.1/sys/unix/Makefile.src 2019-01-29 20:33:51.671000000 +0900
@@ -406,7 +406,7 @@
sp_lev.c spell.c steal.c steed.c sys.c teleport.c timeout.c \
topten.c track.c trap.c u_init.c \
uhitm.c vault.c version.c vision.c weapon.c were.c wield.c \
- windows.c wizard.c worm.c worn.c write.c zap.c
+ windows.c wizard.c worm.c worn.c write.c zap.c my_vsprintf.c
# all operating-system-dependent .c (for dependencies and such)
SYSCSRC = ../sys/atari/tos.c ../sys/share/pcmain.c ../sys/share/pcsys.c \
@@ -473,7 +473,7 @@
sys.o \
steal.o steed.o teleport.o timeout.o topten.o track.o trap.o u_init.o \
uhitm.o vault.o vision.o vis_tab.o weapon.o were.o wield.o windows.o \
- wizard.o worm.o worn.o write.o zap.o \
+ wizard.o worm.o worn.o write.o zap.o my_vsprintf.o \
$(REGEXOBJ) $(RANDOBJ) $(JOBJ) $(SYSOBJ) $(WINOBJ) $(HINTOBJ) version.o
# the .o files from the HACKCSRC, SYSSRC, and WINSRC lists
@@ -994,6 +994,7 @@
worn.o: worn.c $(HACK_H)
write.o: write.c $(HACK_H)
zap.o: zap.c $(HACK_H)
+my_vsprintf.o: my_vsprintf.c $(HACK_H)
jconj.o: ../japanese/jconj.c ../include/hack.h
$(CC) $(CFLAGS) -c ../japanese/jconj.c
jlib.o: ../japanese/jlib.c ../include/hack.h
diff -ruN nethack-3.6.1.orig/sys/unix/Makefile.utl nethack-3.6.1/sys/unix/Makefile.utl
--- nethack-3.6.1.orig/sys/unix/Makefile.utl 2018-04-27 21:07:22.000000000 +0900
+++ nethack-3.6.1/sys/unix/Makefile.utl 2019-01-29 20:33:51.671000000 +0900
@@ -111,11 +111,11 @@
# yacc/lex programs to use to generate *_comp.h, *_lex.c, and *_yacc.c.
# if, instead of yacc/lex you have bison/flex, comment/uncomment the following.
-YACC = yacc
-LEX = lex
-# YACC = bison -y
+# YACC = yacc
+# LEX = lex
+YACC = bison -y
# YACC = byacc
-# LEX = flex
+LEX = flex
# these are the names of the output files from YACC/LEX. Under MS-DOS
# and similar systems, they may differ
@@ -162,11 +162,11 @@
HACK_H = ../src/hack.h-t
# utility .c files
-MAKESRC = makedefs.c
-SPLEVSRC = lev_yacc.c lev_lex.c lev_main.c
-DGNCOMPSRC = dgn_yacc.c dgn_lex.c dgn_main.c
+MAKESRC = makedefs.c ../src/my_vsprintf.c
+SPLEVSRC = lev_yacc.c lev_lex.c lev_main.c ../src/my_vsprintf.c
+DGNCOMPSRC = dgn_yacc.c dgn_lex.c dgn_main.c ../src/my_vsprintf.c
RECOVSRC = recover.c
-DLBSRC = dlb_main.c
+DLBSRC = dlb_main.c ../src/my_vsprintf.c
UTILSRCS = $(MAKESRC) panic.c $(SPLEVSRC) $(DGNCOMPSRC) $(RECOVSRC) $(DLBSRC)
# files that define all monsters and objects
@@ -180,19 +180,19 @@
OALLOC = $(OBJDIR)/alloc.o panic.o
# object files for makedefs
-MAKEOBJS = makedefs.o $(OMONOBJ)
+MAKEOBJS = makedefs.o $(OBJDIR)/my_vsprintf.o $(OMONOBJ)
# object files for special levels compiler
-SPLEVOBJS = lev_yacc.o lev_lex.o lev_main.o $(OALLOC) $(ONAMING)
+SPLEVOBJS = lev_yacc.o lev_lex.o lev_main.o $(OBJDIR)/my_vsprintf.o $(OALLOC) $(ONAMING)
# object files for dungeon compiler
-DGNCOMPOBJS = dgn_yacc.o dgn_lex.o dgn_main.o $(OALLOC)
+DGNCOMPOBJS = dgn_yacc.o dgn_lex.o dgn_main.o $(OBJDIR)/my_vsprintf.o $(OALLOC)
# object files for recovery utility
RECOVOBJS = recover.o
# object files for the data librarian
-DLBOBJS = dlb_main.o $(OBJDIR)/dlb.o $(OALLOC)
+DLBOBJS = dlb_main.o $(OBJDIR)/dlb.o $(OBJDIR)/my_vsprintf.o $(OALLOC)
# flags for creating distribution versions of sys/share/*_lex.c, using
# a more portable flex skeleton, which is not included in the distribution.
diff -ruN nethack-3.6.1.orig/sys/unix/hints/termux nethack-3.6.1/sys/unix/hints/termux
--- nethack-3.6.1.orig/sys/unix/hints/termux 1970-01-01 09:00:00.000000000 +0900
+++ nethack-3.6.1/sys/unix/hints/termux 2019-01-29 20:34:28.221000002 +0900
@@ -0,0 +1,47 @@
+#
+# NetHack 3.6 linux $NHDT-Date: 1432512814 2015/05/25 00:13:34 $ $NHDT-Branch: master $:$NHDT-Revision: 1.12 $
+# Copyright (c) Kenneth Lorber, Kensington, Maryland, 2007.
+# NetHack may be freely redistributed. See license for details.
+#
+#-PRE
+# Linux hints file
+# This hints file provides a single-user tty build for Linux, specifically
+# for Ubuntu dapper.
+
+
+PREFIX=/data/data/com.termux/files/usr
+HACKDIR=$(PREFIX)/local/stow/jnethack-3.6.1-0.3/lib/$(GAME)dir
+SHELLDIR=$(PREFIX)/local/stow/jnethack-3.6.1-0.3/bin
+INSTDIR=$(HACKDIR)
+VARDIR=$(HACKDIR)
+
+
+
+POSTINSTALL=cp sys/unix/sysconf $(INSTDIR)/sysconf; $(CHOWN) $(GAMEUID) $(INSTDIR)/sysconf; $(CHGRP) $(GAMEGRP) $(INSTDIR)/sysconf; chmod $(VARFILEPERM) $(INSTDIR)/sysconf;
+
+CFLAGS=-Os -I../include -DNOTPARMDECL -DDLB
+CFLAGS+=-DCOMPRESS=\"$(PREFIX)/bin/gzip\" -DCOMPRESS_EXTENSION=\".gz\"
+CFLAGS+=-DSYSCF -DSYSCF_FILE=\"$(HACKDIR)/sysconf\" -DSECURE
+CFLAGS+=-DTIMED_DELAY
+CFLAGS+=-DHACKDIR=\"$(HACKDIR)\"
+CFLAGS+=-DDUMPLOG
+CFLAGS+=-DCONFIG_ERROR_SECURE=FALSE
+CFLAGS+=-DICU
+CFLAGS+=-Wno-invalid-source-encoding -Wno-comment
+
+LINK=$(CC)
+# Only needed for GLIBC stack trace:
+#LFLAGS=-rdynamic
+
+WINSRC = $(WINTTYSRC)
+WINOBJ = $(WINTTYOBJ)
+WINLIB = $(WINTTYLIB)
+
+WINTTYLIB=-lcurses -licuuc
+
+CHOWN=true
+CHGRP=true
+
+VARDIRPERM = 0755
+VARFILEPERM = 0600
+GAMEPERM = 0755
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment