]> granicus.if.org Git - nethack/commitdiff
add_sound_mapping cleanup
authorcohrs <cohrs>
Thu, 21 Feb 2002 03:33:42 +0000 (03:33 +0000)
committercohrs <cohrs>
Thu, 21 Feb 2002 03:33:42 +0000 (03:33 +0000)
- avoid several buffer overflows
- move use of access() to files.c in new can_read_file() function
- remove extra newlines in raw_print() calls
- get ready for lint, eg sprintf -> Sprintf
- generally make the code look like core code, not Qt code

include/extern.h
src/files.c
src/sounds.c

index ca661a9286b2acc6fa109f2f2f952090be57a637..2f129af6f04e6d7eefaa2468905ec1edaf135197 100644 (file)
@@ -630,6 +630,9 @@ E void FDECL(compress, (const char *));
 E void FDECL(uncompress, (const char *));
 E boolean FDECL(lock_file, (const char *,int,int));
 E void FDECL(unlock_file, (const char *));
+#ifdef USER_SOUNDS
+E boolean FDECL(can_read_file, (const char *));
+#endif
 E void FDECL(read_config_file, (const char *));
 E void FDECL(check_recordfile, (const char *));
 #if defined(WIZARD)
@@ -1793,7 +1796,9 @@ E void FDECL(yelp, (struct monst *));
 E void FDECL(whimper, (struct monst *));
 E void FDECL(beg, (struct monst *));
 E int NDECL(dotalk);
-
+#ifdef USER_SOUNDS
+E int FDECL(add_sound_mapping, (const char *));
+#endif
 
 /* ### sys/msdos/sound.c ### */
 
index 5a7137c32b9cc04b13f3c75a93e28af45a553c16..98e1de24e4143c8cbbf4022aabf443e6f4768cdc 100644 (file)
@@ -103,7 +103,6 @@ static int lockptr;
 #endif
 
 #ifdef USER_SOUNDS
-extern int FDECL(add_sound_mapping, (const char* mapping));
 extern char *sounddir;
 #endif
 
@@ -1523,6 +1522,15 @@ char             *tmp_levels;
        return 1;
 }
 
+#ifdef USER_SOUNDS
+boolean
+can_read_file(filename)
+const char *filename;
+{
+       return (access(filename, 4) == 0);
+}
+#endif /* USER_SOUNDS */
+
 void
 read_config_file(filename)
 const char *filename;
index dc14635ee7e9d431591b02a18958883a0943e1c9..4c8c9527f59e9d9b70e529942dfdeee7c9465daf 100644 (file)
@@ -918,63 +918,65 @@ extern void FDECL(play_usersound, (const char*, int));
 
 typedef struct audio_mapping_rec {
        struct re_pattern_buffer regex;
-       charfilename;
+       char *filename;
        int volume;
-       struct audio_mapping_recnext;
+       struct audio_mapping_rec *next;
 } audio_mapping;
 
-static audio_mapping* soundmap=0;
+static audio_mapping *soundmap = 0;
 
-char* sounddir=".";
+char* sounddir = ".";
 
+/* adds a sound file mapping, returns 0 on failure, 1 on success */
 int
 add_sound_mapping(mapping)
-const charmapping;
+const char *mapping;
 {
        char text[256];
        char filename[256];
        char filespec[256];
        int volume;
 
-       if (sscanf(mapping, "MESG \"%[^\"]\"%*[\t ]\"%[^\"]\" %d",
-               text, filename, &volume)==3)
-       {
-               const char* err;
-               audio_mapping* new_map;
-
-               sprintf(filespec,"%s/%s",sounddir,filename);
-
-               if (access(filespec, R_OK)==0) {
-                       new_map=(audio_mapping*)alloc(sizeof(audio_mapping));
-                       new_map->regex.translate=0;
-                       new_map->regex.fastmap=0;
-                       new_map->regex.buffer=0;
-                       new_map->regex.allocated=0;
-                       new_map->regex.regs_allocated=REGS_FIXED;
-                       new_map->filename=strdup(filespec);
-                       new_map->volume=volume;
-                       new_map->next=soundmap;
-
-                       err=re_compile_pattern(text, strlen(text), &new_map->regex);
-
-                       if (err) {
-                               sprintf(text, "%s\n", err);
-                               raw_print(text);
-                               free(new_map->filename);
-                               free(new_map);
-                               return 0;
-                       } else {
-                               soundmap=new_map;
-                       }
+       if (sscanf(mapping, "MESG \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d",
+                  text, filename, &volume) == 3) {
+           const char *err;
+           audio_mapping *new_map;
+
+           if (strlen(sounddir) + strlen(filename) > 254) {
+               raw_print("sound file name too long");
+               return 0;
+           }
+           Sprintf(filespec, "%s/%s", sounddir, filename);
+
+           if (can_read_file(filespec)) {
+               new_map = (audio_mapping *)alloc(sizeof(audio_mapping));
+               new_map->regex.translate = 0;
+               new_map->regex.fastmap = 0;
+               new_map->regex.buffer = 0;
+               new_map->regex.allocated = 0;
+               new_map->regex.regs_allocated = REGS_FIXED;
+               new_map->filename = strdup(filespec);
+               new_map->volume = volume;
+               new_map->next = soundmap;
+
+               err = re_compile_pattern(text, strlen(text), &new_map->regex);
+
+               if (err) {
+                   raw_print(err);
+                   free(new_map->filename);
+                   free(new_map);
+                   return 0;
                } else {
-                       sprintf(text, "%s not readable.\n", filespec);
-                       raw_print(text);
-                       return 0;
+                   soundmap = new_map;
                }
-       } else {
-               sprintf(text, "syntax error in SOUND\n");
+           } else {
+               Sprintf(text, "cannot read %.243s", filespec);
                raw_print(text);
                return 0;
+           }
+       } else {
+           raw_print("syntax error in SOUND");
+           return 0;
        }
 
        return 1;
@@ -984,13 +986,13 @@ void
 play_sound_for_message(msg)
 const char* msg;
 {
-       audio_mapping* cursor=soundmap;
+       audio_mapping* cursor = soundmap;
 
        while (cursor) {
-               if (re_search(&cursor->regex, msg, strlen(msg), 0, 9999, 0)>=0) {
-                       play_usersound(cursor->filename, cursor->volume);
-               }
-               cursor=cursor->next;
+           if (re_search(&cursor->regex, msg, strlen(msg), 0, 9999, 0) >= 0) {
+               play_usersound(cursor->filename, cursor->volume);
+           }
+           cursor = cursor->next;
        }
 }