aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/kiss-stat.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/bin/kiss-stat.c b/bin/kiss-stat.c
new file mode 100644
index 0000000..d03bd13
--- /dev/null
+++ b/bin/kiss-stat.c
@@ -0,0 +1,32 @@
+// 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 <pwd.h>
+#include <sys/stat.h>
+#include <stdio.h>
+
+int main (int argc, char *argv[]) {
+ struct stat sb;
+
+ // Exit if no or multiple arguments are given
+ if (argc != 2) {
+ 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);
+ }
+
+ // Print the user name of file owner
+ struct passwd *pw = getpwuid(sb.st_uid);
+ printf("%s\n", pw->pw_name);
+ return(0);
+}