Skip to content

Instantly share code, notes, and snippets.

@rened
Created April 9, 2014 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rened/10294680 to your computer and use it in GitHub Desktop.
Save rened/10294680 to your computer and use it in GitHub Desktop.
Check max size of OSX's read()/write()
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
void main() {
char filename[] = "/tmp/out.txt";
int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, S_IXUSR | S_IWUSR | S_IRUSR);
printf("fd: %d errno: %d\n", fd, errno);
size_t n = (1l << 31)-1;
char *a = malloc(n);
size_t wrote = write(fd, a, n);
printf("wrote %lu of %lu bytes, errno: %d\n", wrote, n, errno);
close(fd);
unlink(filename);
free(a);
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, S_IXUSR | S_IWUSR | S_IRUSR);
n = 1l << 31;
a = malloc(n);
wrote = 0;
wrote = write(fd, a, n);
printf("wrote %lu of %lu bytes, errno: %d\n", wrote, n, errno);
close(fd);
free(a);
unlink(filename);
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, S_IXUSR | S_IWUSR | S_IRUSR);
printf("fd: %d errno: %d\n", fd, errno);
n = (1l << 31)-1;
a = malloc(n);
size_t wrote1 = write(fd, a, n);
size_t wrote2 = write(fd, a, n);
close(fd);
printf("wrote %lu of %lu bytes, errno: %d\n", wrote1+wrote2, 2*n, errno);
fd = open(filename, O_RDONLY | S_IXUSR | S_IWUSR | S_IRUSR);
printf("fd: %d errno: %d\n", fd, errno);
char *b = malloc(2*n);
size_t didread = 0;
didread = read(fd, b, 2*n);
printf("read %lu of %lu bytes, errno: %d\n", didread, 2*n, errno);
didread = read(fd, b, n);
didread += read(fd, b, n);
printf("read %lu of %lu bytes, errno: %d\n", didread, 2*n, errno);
close(fd);
unlink(filename);
size_t i;
int allequal = 1;
for (i = 0; i < 2*n; i++) allequal &= (a[i]==b[i]);
printf("all equal: %d of %lu bytes\n", (int)allequal, i);
free(a);
free(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment