tst-dirname.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Test program for dirname function a la XPG.
  2. Copyright (C) 1996-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #define _GNU_SOURCE 1
  16. #include <libgen.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. static int
  20. test (const char *input, const char *result)
  21. {
  22. int retval;
  23. char *cp;
  24. cp = strdupa (input);
  25. cp = dirname (cp);
  26. retval = strcmp (cp, result);
  27. if (retval)
  28. printf ("dirname(\"%s\") should be \"%s\", but is \"%s\"\n",
  29. input, result, cp);
  30. return retval;
  31. }
  32. static int
  33. do_test (void)
  34. {
  35. int result = 0;
  36. /* These are the examples given in XPG4.2. */
  37. result |= test ("/usr/lib", "/usr");
  38. result |= test ("/usr/", "/");
  39. result |= test ("usr", ".");
  40. result |= test ("/", "/");
  41. result |= test (".", ".");
  42. result |= test ("..", ".");
  43. /* Some more tests. */
  44. result |= test ("/usr/lib/", "/usr");
  45. result |= test ("/usr", "/");
  46. result |= test ("a//", ".");
  47. result |= test ("a////", ".");
  48. result |= test ("////usr", "/");
  49. result |= test ("////usr//", "/");
  50. result |= test ("//usr", "//");
  51. result |= test ("//usr//", "//");
  52. result |= test ("//", "//");
  53. /* Other Unix implementations behave like this. */
  54. result |= test ("x///y", "x");
  55. result |= test ("x/////y", "x");
  56. return result != 0;
  57. }
  58. #define TEST_FUNCTION do_test ()
  59. #include "../test-skeleton.c"