aboutsummaryrefslogtreecommitdiffstats
path: root/jnlib/t-stringhelp.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2009-08-26 08:55:57 +0000
committerWerner Koch <[email protected]>2009-08-26 08:55:57 +0000
commit5134fee5b090d68b19924ccad1994ce37732b91c (patch)
tree1f31fc25eabc362c1dba637ee211bf9fb9d83d13 /jnlib/t-stringhelp.c
parentFix debian bug#543530 (diff)
downloadgnupg-5134fee5b090d68b19924ccad1994ce37732b91c.tar.gz
gnupg-5134fee5b090d68b19924ccad1994ce37732b91c.zip
Implement tilde expansion in the same was as 1.4.
Diffstat (limited to 'jnlib/t-stringhelp.c')
-rw-r--r--jnlib/t-stringhelp.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/jnlib/t-stringhelp.c b/jnlib/t-stringhelp.c
index 317b02049..02041d35e 100644
--- a/jnlib/t-stringhelp.c
+++ b/jnlib/t-stringhelp.c
@@ -22,12 +22,43 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#ifdef HAVE_PWD_H
+# include <pwd.h>
+#endif
+#include <unistd.h>
+#include <sys/types.h>
#include "stringhelp.h"
#include "t-support.h"
+static char *home_buffer;
+
+
+const char *
+gethome (void)
+{
+ if (!home_buffer)
+ {
+ char *home = getenv("HOME");
+
+#if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
+ if(home)
+ home_buffer = xstrdup (home);
+ else
+ {
+ struct passwd *pwd;
+
+ pwd = getpwuid (getuid());
+ if (pwd)
+ home_buffer = xstrdup (pwd->pw_dir);
+ }
+#endif
+ }
+ return home_buffer;
+}
+
static void
test_percent_escape (void)
@@ -261,6 +292,110 @@ test_xstrconcat (void)
}
+static void
+test_make_filename_try (void)
+{
+ char *out;
+ const char *home = gethome ();
+ size_t homelen = home? strlen (home):0;
+
+ out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", NULL);
+ if (out)
+ fail (0);
+ else if (errno != EINVAL)
+ fail (0);
+ xfree (out);
+ out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", "4", NULL);
+ if (out)
+ fail (0);
+ else if (errno != EINVAL)
+ fail (0);
+ xfree (out);
+
+ out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
+ "1", "2", NULL);
+ if (!out || strcmp (out,
+ "1/2/3/4/5/6/7/8/9/10/"
+ "1/2/3/4/5/6/7/8/9/10/"
+ "1/2/3/4/5/6/7/8/9/10/"
+ "1/2"))
+ fail (0);
+ xfree (out);
+
+ out = make_filename_try ("foo", "~/bar", "baz/cde", NULL);
+ if (!out || strcmp (out, "foo/~/bar/baz/cde"))
+ fail (1);
+ xfree (out);
+
+ out = make_filename_try ("foo", "~/bar", "baz/cde/", NULL);
+ if (!out || strcmp (out, "foo/~/bar/baz/cde/"))
+ fail (1);
+ xfree (out);
+
+ out = make_filename_try ("/foo", "~/bar", "baz/cde/", NULL);
+ if (!out || strcmp (out, "/foo/~/bar/baz/cde/"))
+ fail (1);
+ xfree (out);
+
+ out = make_filename_try ("//foo", "~/bar", "baz/cde/", NULL);
+ if (!out || strcmp (out, "//foo/~/bar/baz/cde/"))
+ fail (1);
+ xfree (out);
+
+ out = make_filename_try ("", "~/bar", "baz/cde", NULL);
+ if (!out || strcmp (out, "/~/bar/baz/cde"))
+ fail (1);
+ xfree (out);
+
+
+ out = make_filename_try ("~/foo", "bar", NULL);
+ if (!out)
+ fail (2);
+ if (home)
+ {
+ if (strlen (out) < homelen + 7)
+ fail (2);
+ if (strncmp (out, home, homelen))
+ fail (2);
+ if (strcmp (out+homelen, "/foo/bar"))
+ fail (2);
+ }
+ else
+ {
+ if (strcmp (out, "~/foo/bar"))
+ fail (2);
+ }
+ xfree (out);
+
+ out = make_filename_try ("~", "bar", NULL);
+ if (!out)
+ fail (2);
+ if (home)
+ {
+ if (strlen (out) < homelen + 3)
+ fail (2);
+ if (strncmp (out, home, homelen))
+ fail (2);
+ if (strcmp (out+homelen, "/bar"))
+ fail (2);
+ }
+ else
+ {
+ if (strcmp (out, "~/bar"))
+ fail (2);
+ }
+ xfree (out);
+}
+
+
int
main (int argc, char **argv)
{
@@ -271,7 +406,9 @@ main (int argc, char **argv)
test_compare_filenames ();
test_strconcat ();
test_xstrconcat ();
+ test_make_filename_try ();
+ xfree (home_buffer);
return 0;
}