Back to index

glibc  2.9
tst-dirname.c
Go to the documentation of this file.
00001 /* Test program for dirname function a la XPG.
00002    Copyright (C) 1996, 2000, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #define _GNU_SOURCE  1
00022 #include <libgen.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 
00026 
00027 static int
00028 test (const char *input, const char *result)
00029 {
00030   int retval;
00031   char *cp;
00032   cp = strdupa (input);
00033   cp = dirname (cp);
00034   retval = strcmp (cp, result);
00035   if (retval)
00036     printf ("dirname(\"%s\") should be \"%s\", but is \"%s\"\n",
00037            input, result, cp);
00038   return retval;
00039 }
00040 
00041 int
00042 main (void)
00043 {
00044   int result = 0;
00045 
00046   /* These are the examples given in XPG4.2.  */
00047   result |= test ("/usr/lib", "/usr");
00048   result |= test ("/usr/", "/");
00049   result |= test ("usr", ".");
00050   result |= test ("/", "/");
00051   result |= test (".", ".");
00052   result |= test ("..", ".");
00053 
00054   /* Some more tests.   */
00055   result |= test ("/usr/lib/", "/usr");
00056   result |= test ("/usr", "/");
00057   result |= test ("a//", ".");
00058   result |= test ("a////", ".");
00059   result |= test ("////usr", "/");
00060   result |= test ("////usr//", "/");
00061   result |= test ("//usr", "//");
00062   result |= test ("//usr//", "//");
00063   result |= test ("//", "//");
00064 
00065   /* Other Unix implementations behave like this.  */
00066   result |= test ("x///y", "x");
00067   result |= test ("x/////y", "x");
00068 
00069   return result != 0;
00070 }