files.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 68 d549894aa6a9
parent 56 966ad681f25d
child 74 829976007e62
permissions -rw-r--r--
Trivial Changes I have made trivial changes to the fts component and renamed the set_limit function to set_proc_limit. It makes it clear that we are dealing with LIMIT_NPROC and not LIMIT_AS settings. committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/files.c
 * Copyright (C) Markus Broeker
 */

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

#define  MAXDIRS 1000
#define MAXFILES 1000

#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))

char *GetRandomNumberString ()
{
    static char buffer[12];

    sprintf (buffer, "%d", GETRANDOM (65535));

    return buffer;
}

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

    if (argc != 2) {
        printf ("Usage: %s <basedir>\n", argv[0]);
        return EXIT_FAILURE;
    }

    if (chdir (argv[1]) == -1) {
        printf ("Initial Chdir to %s failed, aborting...\n", argv[1]);
        return EXIT_FAILURE;
    }

    srand (time (NULL));

    for (i = 0; i < MAXDIRS; i++) {
        s = GetRandomNumberString ();
        mkdir (s, 0777);
        if (chdir (s) == -1) {
            printf ("Chdir to %s failed, aborting\n", s);
            return EXIT_FAILURE;
        }
        for (j = 0; j < MAXFILES; j++) {
            s = GetRandomNumberString ();
            if ((fd = open (s, O_RDWR | O_CREAT, 0666)) < 3) {
                perror ("CREATE");
                return EXIT_FAILURE;
            }
            close (fd);
        }
        chdir ("..");
    }
    return EXIT_SUCCESS;
}