repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / tests / system / libroot / posix / truncate.cpp
blobc0256760d3f675bef9dc98d313a3f016a0a8d510
1 /*
2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <ctype.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
16 extern const char *__progname;
19 int
20 main(int argc, char **argv)
22 if (argc < 3) {
23 fprintf(stderr, "usage: %s <file> <size>\n", __progname);
24 return 1;
27 struct stat st;
28 if (stat(argv[1], &st) != 0) {
29 fprintf(stderr, "%s: cannot stat file \"%s\": %s\n", __progname,
30 argv[1], strerror(errno));
31 return 1;
34 off_t newSize = strtoll(argv[2], NULL, 0);
36 printf("size %10Ld\n", st.st_size);
37 printf("wanted %10Ld\n", newSize);
38 printf("Do you really want to truncate the file [y/N]? ");
39 fflush(stdout);
41 char yes[10];
42 if (fgets(yes, sizeof(yes), stdin) == NULL || tolower(yes[0]) != 'y')
43 return 0;
45 if (truncate(argv[1], newSize) != 0) {
46 fprintf(stderr, "%s: cannot truncate file \"%s\": %s\n", __progname,
47 argv[1], strerror(errno));
48 return 1;
51 return 0;