aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2009-03-08 07:35:32 -0400
committerMike Frysinger <vapier@gentoo.org>2009-03-08 09:11:14 -0400
commitd4dee0ebe39627e9d3b90c312f770d7ba73a46f5 (patch)
treeb5613010981c5561febfd3b6dd07076b375a5364 /tests/get-user.c
parentlibsandbox: handle symlinks properly (diff)
downloadsandbox-d4dee0ebe39627e9d3b90c312f770d7ba73a46f5.tar.gz
sandbox-d4dee0ebe39627e9d3b90c312f770d7ba73a46f5.tar.bz2
sandbox-d4dee0ebe39627e9d3b90c312f770d7ba73a46f5.zip
tests: add symlink tests and unify code
Add test cases for symlink behavior and unify some of the test code to make adding more test cases even easier. Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'tests/get-user.c')
-rw-r--r--tests/get-user.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/get-user.c b/tests/get-user.c
new file mode 100644
index 0000000..f85e299
--- /dev/null
+++ b/tests/get-user.c
@@ -0,0 +1,43 @@
+#include "tests.h"
+
+static int usage(int status)
+{
+ fputs(
+ "Usage: get-user [args]\n"
+ " `get-user` return current uid\n"
+ " `get-user file` return uid of file\n"
+ " `get-user -user` return uid of user name\n"
+ , status ? stderr : stdout
+ );
+ return status;
+}
+
+int main(int argc, char *argv[])
+{
+ switch (argc) {
+ case 1:
+ printf("%i\n", getuid());
+ return 0;
+
+ case 2:
+ if (!strcmp(argv[1], "-h"))
+ return usage(0);
+
+ if (*argv[1] == '-') {
+ const char *name = argv[1] + 1;
+ struct passwd *pwd = getpwnam(name);
+ if (!pwd)
+ errp("getpwnam(%s) failed", name);
+ printf("%i\n", pwd->pw_uid);
+ } else {
+ const char *file = argv[1];
+ struct stat st;
+ if (lstat(file, &st))
+ errp("lstat(%s) failed", file);
+ printf("%i\n", st.st_uid);
+ }
+ return 0;
+ }
+
+ return usage(1);
+}