]> granicus.if.org Git - nethack/commitdiff
fix "Patch for dos mode nethackrc file on linux"
authorPatR <rankin@nethack.org>
Fri, 25 Dec 2015 00:00:50 +0000 (16:00 -0800)
committerPatR <rankin@nethack.org>
Fri, 25 Dec 2015 00:00:50 +0000 (16:00 -0800)
Reported directly to devteam (12 Dec), user had a config file originally
from MSDOS or Windows and used it on a linux system.  That works as-is
except when it contained an invalid option line.  Feedback was
"ad option line: "whatever-the-line-was
because of the carriage return character staying in the option buffer
after linefeed was stripped off from CR+LF line end.

He included a patch which replaced this existing fixup after fgets()
if ((p = index(buf, '\n')) != 0) *p = '\0';
with a loop over the whole string changing either '\n' or '\r' to '\0'.
This uses
if ((p = index(buf, '\n')) != 0) {
if (p > buf && *(p - 1) == '\r') --p;
*p = '\0';
}
instead.  Ordinarily I would have just cloned the original line and then
substituted \r for \n in the copy, but the report mentioned "I couldn't
get index to work with carriage return".  I don't know what he tried to
do or why simple index(buf,'\r') might not work as intended on his
platform, so I went with something that will work even if index()
behaves as strangely as the report suggested.

(We already have a couple of index(string,'\r') calls in use, but I'm
not going to change those unless someone complains about a problem.)

doc/fixes36.1
src/files.c

index a2e3933f873d80bef853614dab55269f0a140ca2..84c2a161f3b77d424318d40efa6c7c20ccb7e170 100644 (file)
@@ -68,6 +68,9 @@ tty: specifying all four of role, race, gender, and alignment still prompted
 unix/X11: in top level Makefile, some commented out definitions of VARDATND
        misspelled pilemark.xbm (as pilemark.xpm)
 unix/tty: fix compile warning about 'has_colors' for some configurations
+unix: options file with CR+LF line ends and an invalid option line resulted in
+       "ad option line: "whatever-the-line-was
+       because embedded carriage return character changed cursor's position
 win32gui: getversionstring() was overflowing the provided Help About buffer
 win32gui: guard against buffer overflow in in mswin_getlin()
 MacOSX: initial binary release was built from out of date source code that
index 9e4f1d21e3d87cf780fc35ba4e8b515aeaf2d2a9..18481a5f46719addcb1879566149cf37523c1d7d 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.6 files.c $NHDT-Date: 1449830204 2015/12/11 10:36:44 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.194 $ */
+/* NetHack 3.6 files.c $NHDT-Date: 1451001643 2015/12/25 00:00:43 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.197 $ */
 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -2581,8 +2581,12 @@ line at this level.
 OR: Forbid multiline stuff for alternate config sources.
 */
 #endif
-        if ((p = index(buf, '\n')) != 0)
-            *p = '\0';
+        if ((p = index(buf, '\n')) != 0) {
+            /* in case file has CR+LF format on non-CR+LF platform */
+            if (p > buf && *(p - 1) == '\r')
+                --p;
+            *p = '\0'; /* strip newline */
+        }
         if (!parse_config_line(fp, buf, src)) {
             static const char badoptionline[] = "Bad option line: \"%s\"";