From 48b69beda9d38ea8d4211a9f8e037dc1851cc5f9 Mon Sep 17 00:00:00 2001 From: merakor Date: Fri, 24 Jul 2020 09:08:10 +0000 Subject: rename binary programs FossilOrigin-Name: f404639f07097134f93aa22cc5583ff73a6cd281aa4b1bbfd10053ebd97d7bf4 --- bin/cpt-stat.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 bin/cpt-stat.c (limited to 'bin/cpt-stat.c') diff --git a/bin/cpt-stat.c b/bin/cpt-stat.c new file mode 100644 index 0000000..4380503 --- /dev/null +++ b/bin/cpt-stat.c @@ -0,0 +1,41 @@ +/* kiss-stat --- a utility for getting the user name of file owner + * See LICENSE for copyright information + * + * The reason this simple tool exists is because 'stat' is not + * portable and ls is not exactly stable enough for scripting. + * This program is for outputting the owner name, and that's it. + */ + +#include +#include +#include +#include + +struct passwd *pw; +struct stat sb; + +int +main (int argc, char *argv[]) +{ + /* Exit if no or multiple arguments are given. */ + if (argc != 2 || strcmp(argv[1], "--help") == 0) { + fprintf(stderr, "Usage: %s [pathname]\n", argv[0]); + return 1; + } + + /* Exit if file stat cannot be obtained. */ + if (lstat(argv[1], &sb) == -1) { + perror(argv[0]); + return 1; + } + + /* Exit if name of the owner cannot be retrieved. */ + if (!getpwuid(sb.st_uid)) { + return 1; + } + + /* Print the user name of file owner. */ + pw = getpwuid(sb.st_uid); + printf("%s\n", pw->pw_name); + return 0; +} -- cgit v1.2.3