c - fcntl not working -
i have small program tires change files access mode after has been opened.
int main(int argc, char* argv[]) { int fd; char *filename = argv[1]; char data[1]; int curval; //current flag value int newval; //new flag value fd = open(filename, o_rdonly); while(read(fd, data, 1)>0) { write(stdout_fileno, data, 1); } lseek(fd, 0, seek_set); if((curval = fcntl(fd, f_getfl, 0))<0) { perror("file flag failed"); }
printf("%d\n", curval); newval = curval | o_wronly | o_append; printf("%d\n", newval);
if(fcntl(fd, f_setfl, newval)<0) { perror("file flag set failed"); } if(write(fd, argv[2], strlen(argv[2]))<0) //appending more data file { perror("write failed"); } lseek(fd, 0, seek_set); while(read(fd, data, 1)>0) { write(stdout_fileno, data, 1); } close (fd); return 0;
}
here output when run program text file input.
$ cat input inital data $ ./a.out input newdata inital data 0 1025 write failed: bad file descriptor inital data
why write in program failing? i'm not able find file status flag constants defined. checked in usr/include/
the behavior trying perform not allowed. fcntl(2)
man page:
f_setfl (long) set file status flags value specified arg. file access mode (o_rdonly, o_wronly, o_rdwr) , file creation flags (i.e., o_creat, o_excl, o_noctty, o_trunc) in arg ignored. on linux command can change o_append, o_async, o_direct, o_noatime, , o_nonblock flags.
Comments
Post a Comment