#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#define BUFFER_SIZE 4096
int main(int argc, char **argv) {
char buffer[BUFFER_SIZE];
int n;
while ((n = read(STDIN_FILENO, buffer, BUFFER_SIZE)) > 0) {
if (write(STDOUT_FILENO, buffer, n) != n) {
printf("write error");
exit(1);
}
}
if (n < 0) {
printf("read error");
exit(1);
}
exit(0);
}
|
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int val;
if(argc !=2) {
printf("argc !=2\n");
exit(1);
}
if((val = fcntl(atoi(argv[1]),F_GETFL,0)) < 0) {
printf("fcntl error");
exit(1);
}
switch (val & O_ACCMODE) {
case O_RDONLY:
printf("read only");
break;
case O_WRONLY:
printf("write only");
break;
case O_RDWR:
printf("read write");
break;
default:
printf("unknow acces mode");
}
if(val & O_APPEND)
printf("append");
if(val & O_NONBLOCK)
printf("nonblocking");
#if defined (O_SYNC)
if(val & O_SYNC)
printf("synchronous");
#endif
putchar('\n');
exit(0);
}
|