05-30-07 06:21 AM
>For C, on Lynox -
Lynox?
>What is the command that check if the file exists (direct access) ?
There are various possibilities. stat(), lstat(), open(), access(),
and unlink() come to mind. unlink() is kinda brutal: if it returns
successfully, the file used to exist.
>Open return -1 if fails, and also lstat,
>but I don't know if "fails" means the file doesn't exist.
It is perfectly legitimate for the OS to return a value which means
"You do not have sufficient permission to know whether it exists
or not". Values of errno such as ENOENT or ENOTDIR suggest that
nothing by that name exists (but you might not be able to create
it either, at least not without creating intermediate directories
first). Values such as EACCES suggest that you're not allowed to
know.
access() might not work like you want it to if it is used in or
invoked from a setuid program.
Remember that if you intend to check if the file exists and then
create it if it doesn't, the best you can do is check if the file
exists and then A WHOLE LOT LATER (maybe even an entire machine
instruction) try to create it if it hasn't already been created in
the meantime. Watch for race conditions. open() with the O_EXCL
option might be useful here.
[ Post a follow-up to this message ]
|