main.c (299529) | main.c (302075) |
---|---|
1/* 2 * Copyright (c) 2003-2009 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 2520 unchanged lines hidden (view full) --- 2529 for (i = 0; i < limit; i++) 2530 printf(" %d: %s\n", i, tests[i].name); 2531 exit(1); 2532} 2533 2534static char * 2535get_refdir(const char *d) 2536{ | 1/* 2 * Copyright (c) 2003-2009 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 2520 unchanged lines hidden (view full) --- 2529 for (i = 0; i < limit; i++) 2530 printf(" %d: %s\n", i, tests[i].name); 2531 exit(1); 2532} 2533 2534static char * 2535get_refdir(const char *d) 2536{ |
2537 char tried[512] = { '\0' }; 2538 char buff[128]; 2539 char *pwd, *p; | 2537 size_t tried_size, buff_size; 2538 char *buff, *tried, *pwd = NULL, *p = NULL; |
2540 | 2539 |
2540#ifdef PATH_MAX 2541 buff_size = PATH_MAX; 2542#else 2543 buff_size = 8192; 2544#endif 2545 buff = calloc(buff_size, 1); 2546 if (buff == NULL) { 2547 fprintf(stderr, "Unable to allocate memory\n"); 2548 exit(1); 2549 } 2550 2551 /* Allocate a buffer to hold the various directories we checked. */ 2552 tried_size = buff_size * 2; 2553 tried = calloc(tried_size, 1); 2554 if (tried == NULL) { 2555 fprintf(stderr, "Unable to allocate memory\n"); 2556 exit(1); 2557 } 2558 |
|
2541 /* If a dir was specified, try that */ 2542 if (d != NULL) { 2543 pwd = NULL; | 2559 /* If a dir was specified, try that */ 2560 if (d != NULL) { 2561 pwd = NULL; |
2544 snprintf(buff, sizeof(buff), "%s", d); | 2562 snprintf(buff, buff_size, "%s", d); |
2545 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2546 if (p != NULL) goto success; | 2563 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2564 if (p != NULL) goto success; |
2547 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2548 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); | 2565 strncat(tried, buff, tried_size - strlen(tried) - 1); 2566 strncat(tried, "\n", tried_size - strlen(tried) - 1); |
2549 goto failure; 2550 } 2551 2552 /* Get the current dir. */ 2553#ifdef PATH_MAX 2554 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2555#else 2556 pwd = getcwd(NULL, 0); 2557#endif 2558 while (pwd[strlen(pwd) - 1] == '\n') 2559 pwd[strlen(pwd) - 1] = '\0'; 2560 2561 /* Look for a known file. */ | 2567 goto failure; 2568 } 2569 2570 /* Get the current dir. */ 2571#ifdef PATH_MAX 2572 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2573#else 2574 pwd = getcwd(NULL, 0); 2575#endif 2576 while (pwd[strlen(pwd) - 1] == '\n') 2577 pwd[strlen(pwd) - 1] = '\0'; 2578 2579 /* Look for a known file. */ |
2562 snprintf(buff, sizeof(buff), "%s", pwd); | 2580 snprintf(buff, buff_size, "%s", pwd); |
2563 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2564 if (p != NULL) goto success; | 2581 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2582 if (p != NULL) goto success; |
2565 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2566 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); | 2583 strncat(tried, buff, tried_size - strlen(tried) - 1); 2584 strncat(tried, "\n", tried_size - strlen(tried) - 1); |
2567 | 2585 |
2568 snprintf(buff, sizeof(buff), "%s/test", pwd); | 2586 snprintf(buff, buff_size, "%s/test", pwd); |
2569 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2570 if (p != NULL) goto success; | 2587 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2588 if (p != NULL) goto success; |
2571 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2572 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); | 2589 strncat(tried, buff, tried_size - strlen(tried) - 1); 2590 strncat(tried, "\n", tried_size - strlen(tried) - 1); |
2573 2574#if defined(LIBRARY) | 2591 2592#if defined(LIBRARY) |
2575 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY); | 2593 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY); |
2576#else | 2594#else |
2577 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM); | 2595 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM); |
2578#endif 2579 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2580 if (p != NULL) goto success; | 2596#endif 2597 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2598 if (p != NULL) goto success; |
2581 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2582 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); | 2599 strncat(tried, buff, tried_size - strlen(tried) - 1); 2600 strncat(tried, "\n", tried_size - strlen(tried) - 1); |
2583 2584#if defined(PROGRAM_ALIAS) | 2601 2602#if defined(PROGRAM_ALIAS) |
2585 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS); | 2603 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS); |
2586 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2587 if (p != NULL) goto success; | 2604 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2605 if (p != NULL) goto success; |
2588 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2589 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); | 2606 strncat(tried, buff, tried_size - strlen(tried) - 1); 2607 strncat(tried, "\n", tried_size - strlen(tried) - 1); |
2590#endif 2591 2592 if (memcmp(pwd, "/usr/obj", 8) == 0) { | 2608#endif 2609 2610 if (memcmp(pwd, "/usr/obj", 8) == 0) { |
2593 snprintf(buff, sizeof(buff), "%s", pwd + 8); | 2611 snprintf(buff, buff_size, "%s", pwd + 8); |
2594 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2595 if (p != NULL) goto success; | 2612 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2613 if (p != NULL) goto success; |
2596 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2597 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); | 2614 strncat(tried, buff, tried_size - strlen(tried) - 1); 2615 strncat(tried, "\n", tried_size - strlen(tried) - 1); |
2598 | 2616 |
2599 snprintf(buff, sizeof(buff), "%s/test", pwd + 8); | 2617 snprintf(buff, buff_size, "%s/test", pwd + 8); |
2600 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2601 if (p != NULL) goto success; | 2618 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2619 if (p != NULL) goto success; |
2602 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2603 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); | 2620 strncat(tried, buff, tried_size - strlen(tried) - 1); 2621 strncat(tried, "\n", tried_size - strlen(tried) - 1); |
2604 } 2605 2606failure: 2607 printf("Unable to locate known reference file %s\n", KNOWNREF); 2608 printf(" Checked following directories:\n%s\n", tried); 2609 printf("Use -r option to specify full path to reference directory\n"); 2610#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 2611 DebugBreak(); 2612#endif 2613 exit(1); 2614 2615success: 2616 free(p); 2617 free(pwd); | 2622 } 2623 2624failure: 2625 printf("Unable to locate known reference file %s\n", KNOWNREF); 2626 printf(" Checked following directories:\n%s\n", tried); 2627 printf("Use -r option to specify full path to reference directory\n"); 2628#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 2629 DebugBreak(); 2630#endif 2631 exit(1); 2632 2633success: 2634 free(p); 2635 free(pwd); |
2618 return strdup(buff); | 2636 free(tried); 2637 2638 /* Copy result into a fresh buffer to reduce memory usage. */ 2639 p = strdup(buff); 2640 free(buff); 2641 return p; |
2619} 2620 2621int 2622main(int argc, char **argv) 2623{ 2624 static const int limit = sizeof(tests) / sizeof(tests[0]); 2625 int test_set[sizeof(tests) / sizeof(tests[0])]; 2626 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; --- 343 unchanged lines hidden --- | 2642} 2643 2644int 2645main(int argc, char **argv) 2646{ 2647 static const int limit = sizeof(tests) / sizeof(tests[0]); 2648 int test_set[sizeof(tests) / sizeof(tests[0])]; 2649 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; --- 343 unchanged lines hidden --- |