From: David Griffith Date: Thu, 31 Jan 2019 17:12:26 +0000 (-0800) Subject: Moved local strdup() and strndup() implementations to from dumb to core. X-Git-Url: https://scope-eye.net/git/?a=commitdiff_plain;h=001335cb79c19045092cc35af83ebdf88cd93264;p=liskon_frotz.git Moved local strdup() and strndup() implementations to from dumb to core. --- diff --git a/Makefile b/Makefile index bdfdb32..de8d43f 100644 --- a/Makefile +++ b/Makefile @@ -71,21 +71,28 @@ COLOR ?= yes # If this matters, you can choose -lcurses or -lncurses CURSES ?= -lncurses -# Uncomment this if you're compiling Unix Frotz on a machine that lacks -# the strrchr() libc library call. If you don't know what this means, -# leave it alone. +# Uncomment this if you want to disable the compilation of Blorb support. # -#NO_STRRCHR = yes +#NO_BLORB = yes -# Uncomment this if you're compiling Unix Frotz on a machine that lacks -# the memmove(3) system call. If you don't know what this means, leave it -# alone. -# + +# These are for enabling local version of certain functions which may be +# missing or behave differently from what's expected in modern system. +# If you're running on a system made in the past 20 years, you should be +# safe leaving these alone. If not or you're using something modern, +# but very strange intended for very limited machines, you probably know +# what you're doing. Therefore further commentary on what these +# functions do is probably not necessary. + +# For missing memmove() #NO_MEMMOVE = yes -# Uncomment this if you want to disable the compilation of Blorb support. -# -#NO_BLORB = yes +# For missing strdup() and strndup() +NO_STRDUP = yes + +# For missing strrchr() +#NO_STRRCHR = yes + ######################################################################### # This section is where Frotz is actually built. @@ -223,14 +230,17 @@ $(COMMON_DEFINES): @echo "** Generating $@" @echo "#ifndef COMMON_DEFINES_H" > $@ @echo "#define COMMON_DEFINES_H" >> $@ +ifdef NO_BLORB + @echo "#define NO_BLORB" >> $@ +endif ifdef NO_STRRCHR @echo "#define NO_STRRCHR" >> $@ endif ifdef NO_MEMMOVE @echo "#define NO_MEMMOVE" >> $@ endif -ifdef NO_BLORB - @echo "#define NO_BLORB" >> $@ +ifdef NO_STRDUP + @echo "#define NO_STRDUP" >> $@ endif @echo "#endif /* COMMON_DEFINES_H */" >> $@ diff --git a/src/common/missing.c b/src/common/missing.c index e1ecfbb..cb32e13 100644 --- a/src/common/missing.c +++ b/src/common/missing.c @@ -19,12 +19,13 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include "frotz.h" #ifdef NO_MEMMOVE /* * Unix-ish operating systems based on 4.2BSD or older or SYSVR3 or older - * lack the memmove(3) system call. + * lack memmove(3). * */ void *my_memmove(void *dest, const void *src, size_t n) @@ -44,3 +45,44 @@ void *my_memmove(void *dest, const void *src, size_t n) return dest; } #endif /* NO_MEMMOVE */ + + +#ifdef NO_STRDUP +/* + * Unix-ish operating systems based on 4.2BSD or older or SYSVR3 or older + * lack strdup(3) and strndup(3). + * + */ +char *my_strdup(const char *src) +{ + char *str; + char *p; + int len = 0; + + while (src[len]) + len++; + str = malloc(len + 1); + p = str; + while (*src) + *p++ = *src++; + *p = '\0'; + return str; +} + +char *my_strndup(const char *src, size_t n) +{ + char *str; + char *p; + int len = 0; + + while (src[len] && len < n) + len++; + str = malloc(len + 1); + p = str; + while (len--) { + *p++ = *src++; + } + *p = '\0'; + return str; +} +#endif /* NO_STRDUP */ diff --git a/src/common/missing.h b/src/common/missing.h index b622346..8d84b5d 100644 --- a/src/common/missing.h +++ b/src/common/missing.h @@ -14,5 +14,11 @@ void *my_memmove(void *, const void *, size_t); #define memmove my_memmove #endif +#ifdef NO_STRDUP +char *my_strdup(const char *); +char *my_strndup(const char *, size_t); +#define strdup my_strdup +#define strndup my_strndup +#endif #endif /* MISSING_H */ diff --git a/src/dumb/dumb_frotz.h b/src/dumb/dumb_frotz.h index 89fa887..fb46efd 100644 --- a/src/dumb/dumb_frotz.h +++ b/src/dumb/dumb_frotz.h @@ -16,9 +16,6 @@ #include #include -extern char *my_strdup(char *); -extern char *my_strndup(char *, size_t); - /* from ../common/setup.h */ extern f_setup_t f_setup; diff --git a/src/dumb/dumb_init.c b/src/dumb/dumb_init.c index 0b1d678..da3be11 100644 --- a/src/dumb/dumb_init.c +++ b/src/dumb/dumb_init.c @@ -114,7 +114,7 @@ void os_process_arguments(int argc, char *argv[]) case 'i': f_setup.ignore_errors = 1; break; case 'I': f_setup.interpreter_number = atoi(zoptarg); break; case 'L': f_setup.restore_mode = 1; - f_setup.tmp_save_name = my_strdup(zoptarg); + f_setup.tmp_save_name = strdup(zoptarg); break; case 'm': do_more_prompts = FALSE; break; case 'o': f_setup.object_movement = 1; break; @@ -122,7 +122,7 @@ void os_process_arguments(int argc, char *argv[]) case 'P': f_setup.piracy = 1; break; case 'p': plain_ascii = 1; break; case 'r': dumb_handle_setting(zoptarg, FALSE, TRUE); break; - case 'R': f_setup.restricted_path = my_strndup(zoptarg, PATH_MAX); break; + case 'R': f_setup.restricted_path = strndup(zoptarg, PATH_MAX); break; case 's': user_random_seed = atoi(zoptarg); break; case 'S': f_setup.script_cols = atoi(zoptarg); break; case 't': user_tandy_bit = 1; break; @@ -147,8 +147,8 @@ void os_process_arguments(int argc, char *argv[]) /* Save the story file name */ - f_setup.story_file = my_strdup(argv[zoptind]); - f_setup.story_name = my_strdup(basename(argv[zoptind])); + f_setup.story_file = strdup(argv[zoptind]); + f_setup.story_name = strdup(basename(argv[zoptind])); /* Now strip off the extension */ p = strrchr(f_setup.story_name, '.'); @@ -293,41 +293,6 @@ void os_init_setup(void) f_setup.sound = 1; f_setup.err_report_mode = ERR_DEFAULT_REPORT_MODE; f_setup.restore_mode = 0; - -} - -char *my_strdup(char *src) -{ - char *str; - char *p; - int len = 0; - - while (src[len]) - len++; - str = malloc(len + 1); - p = str; - while (*src) - *p++ = *src++; - *p = '\0'; - return str; -} - -char *my_strndup(char *src, size_t n) -{ - char *str; - char *p; - int len = 0; - - while (src[len] && len < n) - len++; - str = malloc(len + 1); - p = str; - while (len--) - { - *p++ = *src++; - } - *p = '\0'; - return str; } static void print_version(void) diff --git a/src/dumb/dumb_input.c b/src/dumb/dumb_input.c index d01c150..805b3f6 100644 --- a/src/dumb/dumb_input.c +++ b/src/dumb/dumb_input.c @@ -430,7 +430,7 @@ int os_read_file_name (char *file_name, const char *default_name, int flag) break; } } - tempname = my_strdup(default_name + i); + tempname = strdup(default_name + i); sprintf(prompt, "Please enter a filename [%s]: ", tempname); } else sprintf(prompt, "Please enter a filename [%s]: ", default_name); @@ -451,7 +451,7 @@ int os_read_file_name (char *file_name, const char *default_name, int flag) break; } } - tempname = my_strdup(file_name + i); + tempname = strdup(file_name + i); strcpy(file_name, f_setup.restricted_path); if (file_name[strlen(file_name)-1] != PATH_SEPARATOR) { strcat(file_name, "/");