summaryrefslogtreecommitdiffstats
path: root/common/t-stringhelp.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2014-04-15 16:40:48 +0200
committerWerner Koch <wk@gnupg.org>2014-04-22 15:58:33 +0200
commit71a54313adf7b57b7b27bb9ad07b142a34306260 (patch)
tree5c37da59f48b7c740f0ade7e85c0f22ac089d5a0 /common/t-stringhelp.c
parentcommon: Add function gnupg_getcwd. (diff)
downloadgnupg2-71a54313adf7b57b7b27bb9ad07b142a34306260.tar.xz
gnupg2-71a54313adf7b57b7b27bb9ad07b142a34306260.zip
common: Add functions make_absfilename and make_absfilename_try.
* common/stringhelp.c (do_make_filename): Add modes 2 and 3. (make_absfilename): New. (make_absfilename_try): New.
Diffstat (limited to 'common/t-stringhelp.c')
-rw-r--r--common/t-stringhelp.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c
index 990a8004a..dcd5a453f 100644
--- a/common/t-stringhelp.c
+++ b/common/t-stringhelp.c
@@ -71,6 +71,34 @@ gethome (void)
}
+static char *
+mygetcwd (void)
+{
+ char *buffer;
+ size_t size = 100;
+
+ for (;;)
+ {
+ buffer = xmalloc (size+1);
+#ifdef HAVE_W32CE_SYSTEM
+ strcpy (buffer, "/"); /* Always "/". */
+ return buffer;
+#else
+ if (getcwd (buffer, size) == buffer)
+ return buffer;
+ xfree (buffer);
+ if (errno != ERANGE)
+ {
+ fprintf (stderr,"error getting current cwd: %s\n",
+ strerror (errno));
+ exit (2);
+ }
+ size *= 2;
+#endif
+ }
+}
+
+
static void
test_percent_escape (void)
{
@@ -407,6 +435,50 @@ test_make_filename_try (void)
}
+static void
+test_make_absfilename_try (void)
+{
+ char *out;
+ char *cwd = mygetcwd ();
+ size_t cwdlen = strlen (cwd);
+
+ out = make_absfilename_try ("foo", "bar", NULL);
+ if (!out)
+ fail (0);
+ if (strlen (out) < cwdlen + 7)
+ fail (0);
+ if (strncmp (out, cwd, cwdlen))
+ fail (0);
+ if (strcmp (out+cwdlen, "/foo/bar"))
+ fail (0);
+ xfree (out);
+
+ out = make_absfilename_try ("./foo", NULL);
+ if (!out)
+ fail (1);
+ if (strlen (out) < cwdlen + 5)
+ fail (1);
+ if (strncmp (out, cwd, cwdlen))
+ fail (1);
+ if (strcmp (out+cwdlen, "/./foo"))
+ fail (1);
+ xfree (out);
+
+ out = make_absfilename_try (".", NULL);
+ if (!out)
+ fail (2);
+ if (strlen (out) < cwdlen)
+ fail (2);
+ if (strncmp (out, cwd, cwdlen))
+ fail (2);
+ if (strcmp (out+cwdlen, ""))
+ fail (2);
+ xfree (out);
+
+ xfree (cwd);
+}
+
+
int
main (int argc, char **argv)
{
@@ -418,6 +490,7 @@ main (int argc, char **argv)
test_strconcat ();
test_xstrconcat ();
test_make_filename_try ();
+ test_make_absfilename_try ();
xfree (home_buffer);
return 0;