Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open() system call header file requirements

I am using x86_64 GNU/Linux with gcc.
SYNOPSIS section of man -s2 open says:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);

Now when I try to compile the following code snippet, gcc doesn't throw a warning/error.

#include <stdio.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd;

    fd = open("foo.txt", O_RDWR, 0777);
    if(fd == -1)
        perror("open");

    fd = creat("foo.txt", 0777);
    if(fd == -1)
        perror("creat");

    close(fd);
    return 0;
}

So are types.h and stat.h optional? What purpose do they serve in manpage of open()?

like image 279
rootkea Avatar asked Feb 19 '15 14:02

rootkea


People also ask

Which header file is required for the open system call?

#include <sys/types. h> #include <sys/stat.

What is the header for open () in C?

The open and creat functions are declared in the header file fcntl. h , while close is declared in unistd. h .

What is open () system call in C?

2. open: Used to Open the file for reading, writing or both. Syntax in C language #include<sys/types.h> #include<sys/stat.h> #include <fcntl.h> int open (const char* Path, int flags [, int mode ]); Parameters. Path: path to file which you want to use.

Is open () a system call?

The open() system call opens the file specified by pathname. If the specified file does not exist, it may optionally (if O_CREAT is specified in flags) be created by open().


1 Answers

The man page serves as an instruction both to programmers and to compiler manufacturers.

It is possible that you don't need to include them on your system. However, the man page describes a portable way to use the methods, so you should include them anyway.

like image 129
Klas Lindbäck Avatar answered Oct 19 '22 09:10

Klas Lindbäck