Reputation: 5280
From this commit https://github.com/torvalds/linux/commit/a0673fdbcd42105261646cd4f3447455b5854a32
I learnt that there are some 32bit-spec syscalls like creat
, which have been removed on architectures like arm64.
From the glibc manual for creat:
creat()
A call to
creat()
is equivalent to callingopen()
with flags equal toO_CREAT|O_WRONLY|O_TRUNC
.
IIUC, creat
can virtually be implemented via open
, but I also learnt that creat
comes after open
. It should be more convenient to call creat
if I want to create instead of opening a file. Of course, we can always use glibc version of create:
/* Create FILE with protections MODE. */
int
__creat64 (const char *file, mode_t mode)
{
#if defined __OFF_T_MATCHES_OFF64_T && defined __NR_creat
return SYSCALL_CANCEL (creat, file, mode);
#else
/* We need to pass O_LARGEFILE. */
return __open64 (file, O_WRONLY | O_CREAT | O_TRUNC, mode);
#endif
}
weak_alias (__creat64, creat64)
#ifdef __OFF_T_MATCHES_OFF64_T
strong_alias (__creat64, __creat)
weak_alias (__creat64, creat)
#endif
which will fade to open syscall
if creat syscall
is not available. But I still cannot figure out what's wrong if we still have creat
as syscall
in 64-bit architectures.
Upvotes: 0
Views: 337
Reputation: 755016
The creat()
call is still required by POSIX, but there's no obvious reason it can't be implemented as:
static inline int creat(const char *name, int mode)
{
return open(name, O_WRONLY|O_CREAT|O_TRUNC, mode);
}
Indeed, POSIX says that it should be implemented as if it was like that (give or take the static inline
qualifiers).
When creat()
was created, open()
didn't have options to create files; it could only open existing files. It didn't have all the O_xyz
names; you used 0
(for O_RDONLY
), 1
(for O_WRONLY
) and 2
(for O_RDWR
) — and those were all the options available.
Nowadays, with the 'variable arguments' version of open()
(yes, the third, mode argument is optional), you no longer need creat()
— you can do it all with open()
and then some.
There's really no need for creat()
to be a system call separate from open()
, therefore, and most modern code (say, code written in the current millennium) does not use creat()
anyway. I don't know when I last wrote code using creat()
— it was a long time ago. (I searched my source code; there were two programs still with creat()
calls, but those calls were both in version 1.1 of the code, dated January and February 1990. I don't have any record of having used it in my own code since then.)
Upvotes: 4