This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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