xref: /haiku/src/tests/system/libroot/posix/chmod.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /*
2  * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <ctype.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 
16 
17 extern const char *__progname;
18 
19 
20 int
21 main(int argc, char **argv)
22 {
23 	if (argc < 3) {
24 		fprintf(stderr, "usage: %s <file> <size>\n", __progname);
25 		return 1;
26 	}
27 
28 	struct stat st;
29 	if (stat(argv[1], &st) != 0) {
30 		fprintf(stderr, "%s: cannot stat file \"%s\": %s\n", __progname,
31 			argv[1], strerror(errno));
32 		return 1;
33 	}
34 
35 	int fd = open(argv[1], O_RDONLY);
36 	if (fd < 0) {
37 		fprintf(stderr, "%s: could open the file read-only \"%s\": %s\n",
38 			__progname, argv[1], strerror(errno));
39 		return 1;
40 	}
41 	if (fchmod(fd, 0666) == -1) {
42 		fprintf(stderr, "%s: couldn't chmod a file opened read-only \"%s\": %s\n",
43 			__progname, argv[1], strerror(errno));
44 		close(fd);
45 		return 1;
46 	}
47 	close(fd);
48 
49 	return 0;
50 }
51