Skip to content

Instantly share code, notes, and snippets.

@smbd
Last active December 12, 2015 06:49
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 smbd/4732469 to your computer and use it in GitHub Desktop.
Save smbd/4732469 to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(){
int fd;
fd = open("/tmp", O_RDONLY);
if ( fd < 0 ) {
char err[] = "open failed";
write(2, err, strlen(err));
return 1;
}
while (1) {
char buf[256];
int read_size;
read_size = read(fd, buf, sizeof(buf));
if ( read_size > 0 ) {
write(1, buf, read_size);
} else if ( read_size == 0 ) {
break;
} else {
char err[] = "read failed\n";
write(2, err, strlen(err));
write(2, strerror(errno), strlen(strerror(errno)));
return 2;
}
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment