C-Program with System Calls

C-Program with System Calls

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

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

    if (argc <= 1) return 1;
    fd = open(argv[1], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);

    if (fd < 0)
    {
        perror("open");
        return 1;
    }

    if (write(fd, "hello world\n", 12) < 0)
    {
        perror("write()");
        return 1;
    }

    if (close(fd) < 0)
    {
        perror("close()");
        return 1;
    }

    return 0;
}