Back to index

glibc  2.9
tst-getcwd.c
Go to the documentation of this file.
00001 /* Test of getcwd function.
00002    Copyright (C) 2000, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
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 #include <errno.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <sys/param.h>
00027 
00028 
00029 #define TEST_FUNCTION do_test ()
00030 static int
00031 do_test (void)
00032 {
00033   char thepath[4096];       /* Yes, this limits the environment this test
00034                         can run it but I honestly don't care about
00035                         people which have this problem.  */
00036   char *bufs[10];
00037   size_t lens[10];
00038   size_t sbs;
00039   size_t len, i;
00040 
00041   if (getcwd (thepath, sizeof thepath) == NULL)
00042     {
00043       if (errno == ERANGE)
00044        /* The path is too long, skip all tests.  */
00045        return 0;
00046 
00047       puts ("getcwd (thepath, sizeof thepath) failed");
00048       return 1;
00049     }
00050   len = strlen (thepath);
00051 
00052   sbs = 1;
00053   while (sbs < len + 1)
00054     sbs <<= 1;
00055 
00056   for (i = 0; i < 4; ++i)
00057     {
00058       lens[i] = sbs;
00059       bufs[i] = (char *) malloc (sbs);
00060     }
00061 
00062   bufs[i] = getcwd (NULL, sbs);
00063   lens[i] = sbs;
00064   if (bufs[i] == NULL)
00065     {
00066       puts ("getcwd (NULL, sbs) failed");
00067       return 1;
00068     }
00069   ++i;
00070 
00071   for (; i < 10; sbs >>= 1, ++i)
00072     {
00073       bufs[i] = (char *) malloc (MAX (1, sbs));
00074       lens[i] = sbs;
00075     }
00076 
00077   /* Before we test the result write something in the memory to see
00078      whether the allocation went right.  */
00079   for (i = 0; i < 10; ++i)
00080     if (i != 4 && bufs[i] != NULL)
00081       memset (bufs[i], '\xff', lens[i]);
00082 
00083   if (strcmp (thepath, bufs[4]) != 0)
00084     {
00085       printf ("\
00086 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
00087              bufs[4], thepath);
00088       return 1;
00089     }
00090 
00091   /* Now overwrite all buffers to see that getcwd allocated the buffer
00092      of right size.  */
00093   for (i = 0; i < 10; ++i)
00094     memset (bufs[i], i, lens[i]);
00095 
00096   for (i = 0; i < 10; ++i)
00097     free (bufs[i]);
00098 
00099   /* Test whether the function signals success despite the buffer
00100      being too small.  */
00101   if (getcwd (NULL, len) != NULL)
00102     {
00103       puts ("getcwd (NULL, len) didn't failed");
00104       return 1;
00105     }
00106 
00107   bufs[0] = malloc (len);
00108   bufs[1] = malloc (len);
00109   bufs[2] = malloc (len);
00110   if (bufs[1] != NULL)
00111     {
00112       if (getcwd (bufs[1], len) != NULL)
00113        {
00114          puts ("getcwd (bufs[1], len) didn't failed");
00115          return 1;
00116        }
00117       free (bufs[0]);
00118       free (bufs[1]);
00119       free (bufs[2]);
00120     }
00121 
00122   memset (thepath, '\xfe', sizeof (thepath));
00123   if (getcwd (thepath, len) != NULL)
00124     {
00125       puts ("getcwd (thepath, len) didn't failed");
00126       return 1;
00127     }
00128 
00129   for (i = len; i < sizeof thepath; ++i)
00130     if (thepath[i] != '\xfe')
00131       {
00132        puts ("thepath[i] != '\xfe'");
00133        return 1;
00134       }
00135 
00136   /* Now test handling of correctly sized buffers.  */
00137   bufs[0] = getcwd (NULL, len + 1);
00138   if (bufs[0] == NULL)
00139     {
00140       puts ("getcwd (NULL, len + 1) failed");
00141       return 1;
00142     }
00143   free (bufs[0]);
00144 
00145   memset (thepath, '\xff', sizeof thepath);
00146   if (getcwd (thepath, len + 1) == NULL)
00147     {
00148       puts ("getcwd (thepath, len + 1) failed");
00149       return 1;
00150     }
00151 
00152   for (i = len + 1; i < sizeof thepath; ++i)
00153     if (thepath[i] != '\xff')
00154       {
00155        printf ("thepath[%zd] != '\xff'\n", i);
00156        return 1;
00157       }
00158 
00159   puts ("everything OK");
00160 
00161   return 0;
00162 }
00163 
00164 #include "../test-skeleton.c"