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 <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <sys/stat.h> 13 #include <unistd.h> 14 15 16 extern const char *__progname; 17 18 19 int 20 main(int argc, char **argv) 21 { 22 if (argc < 3) { 23 fprintf(stderr, "usage: %s <file> <size>\n", __progname); 24 return 1; 25 } 26 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; 32 } 33 34 off_t newSize = strtoll(argv[2], NULL, 0); 35 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); 40 41 char yes[10]; 42 if (fgets(yes, sizeof(yes), stdin) == NULL || tolower(yes[0]) != 'y') 43 return 0; 44 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; 49 } 50 51 return 0; 52 } 53