Skip to content

Instantly share code, notes, and snippets.

@gyulkkajo
Created January 29, 2018 08:06
Show Gist options
  • Save gyulkkajo/a47402fd7fcce382b4cceac608efc5e1 to your computer and use it in GitHub Desktop.
Save gyulkkajo/a47402fd7fcce382b4cceac608efc5e1 to your computer and use it in GitHub Desktop.
Short script to query a size of a file by open and seek.
#define _GNU_SOURCE // Needed to use O_LARGEFILE and lseek64
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd;
off64_t sz;
if (argc != 2) {
fprintf(stderr, "Usage: ./filesz FILE\n");
return -1;
}
fd = open(argv[1], O_RDONLY | O_LARGEFILE);
if (fd < 0) {
fprintf(stderr, "Err: open ret(%d) (%s)\n", fd, strerror(errno));
return -1;
}
sz = lseek64(fd, 0, SEEK_END);
if (sz < 0) {
fprintf(stderr, "Err: lseek ret(%lld) (%s)\n", sz, strerror(errno));
return -1;
}
printf("File size : %lld (0x%llx)\n", sz, sz);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment