Skip to content

Instantly share code, notes, and snippets.

@smbd smbd/opendir.c
Last active Dec 12, 2015

Embed
What would you like to do?
#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
You can’t perform that action at this time.