file_demo.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Wed, 02 May 2012 20:49:41 +0200
changeset 164 e1f4bba1097a
parent 138 dff18d1ac2af
permissions -rw-r--r--
Small Makefile changes committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * 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;
}