get file size using stat, which seems to be more reliable

This commit is contained in:
asvitkine 2011-12-27 21:01:32 +00:00
parent 521da856b5
commit 2d14b5fb8b

View File

@ -30,6 +30,7 @@
#include <mach/vm_prot.h>
#include <mach-o/loader.h>
#include <mach-o/fat.h>
#include <sys/stat.h>
static const char progname[] = "lowmem";
static const char *filename;
@ -134,6 +135,7 @@ main(int argc, const char *argv[])
struct mach_header_64 *machhead64;
#endif
struct fat_header *fathead;
struct stat f;
if (argc != 2) {
(void)fprintf(stderr, "Usage: %s executable\n", progname);
@ -142,6 +144,13 @@ main(int argc, const char *argv[])
filename = argv[1];
if (stat(filename, &f)) {
(void)fprintf(stderr, "%s: could not stat %s: %s\n",
progname, filename, strerror(errno));
exit(1);
}
file_size = f.st_size;
fd = open(filename, O_RDWR, 0);
if (fd == -1) {
(void)fprintf(stderr, "%s: could not open %s: %s\n",
@ -149,17 +158,6 @@ main(int argc, const char *argv[])
exit(1);
}
file_size = lseek(fd, 0, SEEK_END);
if (file_size == -1) {
// for some mysterious reason, this sometimes fails...
file_size = 0x1000;
#if 0
(void)fprintf(stderr, "%s: could not get size of %s: %s\n",
progname, filename, strerror(errno));
exit(1);
#endif
}
/*
* Size does not really matter, it will be rounded-up to a multiple
* of the page size automatically.