tst-xpg-basename.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright (C) 1999-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Andreas Jaeger <aj@suse.de>, 1999.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <libgen.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. static struct
  20. {
  21. const char *path;
  22. const char *file;
  23. } tests [] =
  24. {
  25. { "/dir/file", "file" },
  26. { "file", "file"},
  27. { "/dir/file///", "file" },
  28. { "../file", "file" },
  29. { "/", "/" },
  30. { NULL, "."},
  31. { "", "."}
  32. };
  33. static int
  34. do_test (void)
  35. {
  36. size_t i = 0;
  37. int errors = 0;
  38. char path[1024];
  39. char *file;
  40. for (i = 0; i < sizeof (tests) / sizeof (tests [0]); ++i)
  41. {
  42. if (tests [i].path == NULL)
  43. file = __xpg_basename (NULL);
  44. else
  45. {
  46. strcpy (path, tests [i].path);
  47. file = __xpg_basename (path);
  48. }
  49. if (strcmp (file, tests [i].file))
  50. {
  51. printf ("Test with `%s' failed: Result is: `%s'.\n",
  52. (tests [i].path == NULL ? "NULL" : tests [i].path), file);
  53. errors = 1;
  54. }
  55. }
  56. return errors;
  57. }
  58. #define TEST_FUNCTION do_test ()
  59. #include "../test-skeleton.c"