aboutsummaryrefslogtreecommitdiff
path: root/bin/cpt-stat.c
blob: 584c2dfe8643d99395a2489acde44deca34527b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* cpt-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>
#include <string.h>

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;
}