file_demo.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 19 Nov 2010 16:43:20 +0100
changeset 161 36f763c8ab16
parent 138 dff18d1ac2af
permissions -rw-r--r--
spell checking: 'cannot set the limit' sounds better... 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;
}