comparison src/joystick/linux/SDL_sysjoystick.c @ 1379:c0a74f199ecf

Use only safe string functions
author Sam Lantinga <slouken@libsdl.org>
date Sun, 19 Feb 2006 23:46:34 +0000
parents 19418e4422cb
children d910939febfa
comparison
equal deleted inserted replaced
1378:dc0e13e7e1ae 1379:c0a74f199ecf
177 int coef[3]; 177 int coef[3];
178 } abs_correct[ABS_MAX]; 178 } abs_correct[ABS_MAX];
179 #endif 179 #endif
180 }; 180 };
181 181
182 static char *mystrdup(const char *string)
183 {
184 char *newstring;
185
186 newstring = (char *)SDL_malloc(SDL_strlen(string)+1);
187 if ( newstring ) {
188 SDL_strcpy(newstring, string);
189 }
190 return(newstring);
191 }
192
193 182
194 #ifndef NO_LOGICAL_JOYSTICKS 183 #ifndef NO_LOGICAL_JOYSTICKS
195 184
196 static int CountLogicalJoysticks(int max) 185 static int CountLogicalJoysticks(int max)
197 { 186 {
202 191
203 for(i = 0; i < max; i++) { 192 for(i = 0; i < max; i++) {
204 name = SDL_SYS_JoystickName(i); 193 name = SDL_SYS_JoystickName(i);
205 194
206 if (name) { 195 if (name) {
207 for(j = 0; j < SDL_TABLESIZE(joystick_logicalmap); j++) { 196 for(j = 0; j < SDL_arraysize(joystick_logicalmap); j++) {
208 if (!SDL_strcmp(name, joystick_logicalmap[j].name)) { 197 if (!SDL_strcmp(name, joystick_logicalmap[j].name)) {
209 198
210 prev = i; 199 prev = i;
211 SDL_joylist[prev].map = joystick_logicalmap+j; 200 SDL_joylist[prev].map = joystick_logicalmap+j;
212 201
301 290
302 numjoysticks = 0; 291 numjoysticks = 0;
303 292
304 /* First see if the user specified a joystick to use */ 293 /* First see if the user specified a joystick to use */
305 if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) { 294 if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) {
306 SDL_strncpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path)); 295 SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
307 path[sizeof(path)-1] = '\0';
308 if ( stat(path, &sb) == 0 ) { 296 if ( stat(path, &sb) == 0 ) {
309 fd = open(path, O_RDONLY, 0); 297 fd = open(path, O_RDONLY, 0);
310 if ( fd >= 0 ) { 298 if ( fd >= 0 ) {
311 /* Assume the user knows what they're doing. */ 299 /* Assume the user knows what they're doing. */
312 SDL_joylist[numjoysticks].fname =mystrdup(path); 300 SDL_joylist[numjoysticks].fname = SDL_strdup(path);
313 if ( SDL_joylist[numjoysticks].fname ) { 301 if ( SDL_joylist[numjoysticks].fname ) {
314 dev_nums[numjoysticks] = sb.st_rdev; 302 dev_nums[numjoysticks] = sb.st_rdev;
315 ++numjoysticks; 303 ++numjoysticks;
316 } 304 }
317 close(fd); 305 close(fd);
318 } 306 }
319 } 307 }
320 } 308 }
321 309
322 for ( i=0; i<SDL_TABLESIZE(joydev_pattern); ++i ) { 310 for ( i=0; i<SDL_arraysize(joydev_pattern); ++i ) {
323 for ( j=0; j < MAX_JOYSTICKS; ++j ) { 311 for ( j=0; j < MAX_JOYSTICKS; ++j ) {
324 SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j); 312 SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j);
325 313
326 /* rcg06302000 replaced access(F_OK) call with stat(). 314 /* rcg06302000 replaced access(F_OK) call with stat().
327 * stat() will fail if the file doesn't exist, so it's 315 * stat() will fail if the file doesn't exist, so it's
355 } 343 }
356 #endif 344 #endif
357 close(fd); 345 close(fd);
358 346
359 /* We're fine, add this joystick */ 347 /* We're fine, add this joystick */
360 SDL_joylist[numjoysticks].fname =mystrdup(path); 348 SDL_joylist[numjoysticks].fname = SDL_strdup(path);
361 if ( SDL_joylist[numjoysticks].fname ) { 349 if ( SDL_joylist[numjoysticks].fname ) {
362 dev_nums[numjoysticks] = sb.st_rdev; 350 dev_nums[numjoysticks] = sb.st_rdev;
363 ++numjoysticks; 351 ++numjoysticks;
364 } 352 }
365 } else 353 } else
487 handled = SDL_TRUE; 475 handled = SDL_TRUE;
488 } 476 }
489 } 477 }
490 478
491 /* Special joystick support */ 479 /* Special joystick support */
492 for ( i=0; i < SDL_TABLESIZE(special_joysticks); ++i ) { 480 for ( i=0; i < SDL_arraysize(special_joysticks); ++i ) {
493 if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) { 481 if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) {
494 482
495 joystick->naxes = special_joysticks[i].naxes; 483 joystick->naxes = special_joysticks[i].naxes;
496 joystick->nhats = special_joysticks[i].nhats; 484 joystick->nhats = special_joysticks[i].nhats;
497 joystick->nballs = special_joysticks[i].nballs; 485 joystick->nballs = special_joysticks[i].nballs;
501 } 489 }
502 } 490 }
503 491
504 /* User environment joystick support */ 492 /* User environment joystick support */
505 if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) { 493 if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) {
506 SDL_strcpy(env_name, ""); 494 *env_name = '\0';
507 if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 ) 495 if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 )
508 env += SDL_strlen(env_name)+2; 496 env += SDL_strlen(env_name)+2;
509 else if ( SDL_sscanf(env, "%s", env_name) == 1 ) 497 else if ( SDL_sscanf(env, "%s", env_name) == 1 )
510 env += SDL_strlen(env_name); 498 env += SDL_strlen(env_name);
511 499