Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
when calling open() for writing with flag O_WRONLY set, and all flags O_CREAT, O_EXCL, and O_TRUNC not set, what is the behavior of the first write() call. what will be seen as happening to the file if it is written to without truncating?
unfortunately, the man pages tend to describe flag behavior when set, but do not describe the effect when not set, or the behavior of combinations with other flags.
if the first operation after the file is open without O_TRUNC is write() (not seek()), where is the data written to? in other words, where is the implied first "seek()" position? is it position 0, the beginning of the file?
what is supposed to happen if O_TRUNC is used with O_RDONLY? i have seen this actually truncate the file to 0 length.
If the file is opened with O_APPEND, the write will append after the last byte.
Otherwise, unless you seek first, it will overwrite the first.
> what is supposed to happen if O_TRUNC is used with O_RDONLY? i have seen this actually truncate the file to 0 length.
that sounds like a bug...
From man 2 open...
O_TRUNC
If the file already exists and is a regular file and the access mode allows writing (i.e., is O_RDWR
or O_WRONLY) it will be truncated to length 0. If the file is a FIFO or terminal device file, the
O_TRUNC flag is ignored. Otherwise, the effect of O_TRUNC is unspecified.
undefined— The behavior for something incorrect, on which the standard does not impose any
requirements. Anything is allowed to happen, from nothing, to a warning message to program
termination, to CPU meltdown, to launching nuclear missiles (assuming you have the correct
hardware option installed).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.