file_demo.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 138 dff18d1ac2af
permissions -rw-r--r--
Proper Datatypes to leave the 80's

/**
 * file_demo.c
 * Copyright (C) 2008 Markus Broeker
 */

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

#define TESTFILE1 "/tmp/test1.txt"
#define TESTFILE2 "/tmp/test2.txt"

int main (int argc, char **argv)
{
    int fd1;
    int fd2;
    int mode;

    char *str = "Schreib mal was!";

    if (argc != 2) {
        printf ("Usage: %s <MODE>\n", argv[0]);
        return -1;
    }
    mode = strtoul (argv[1], NULL, 8);

    unlink (TESTFILE1);
    unlink (TESTFILE2);

    /*
     * Opens ReadOnly
     */
    fd1 = open (TESTFILE1, O_CREAT, mode);
    fd2 = open (TESTFILE2, O_RDWR | O_CREAT, mode);

    if (fd1)
        if (write (fd1, str, strlen (str)) == -1)
            perror ("FD1");

    if (fd2)
        if (write (fd2, str, strlen (str) + 1) == -1)
            perror ("FD2");

    close (fd1);
    close (fd2);

    return EXIT_SUCCESS;
}