prog_limit.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sun, 19 Jul 2009 19:44:48 +0200
changeset 101 176448af80fa
parent 77 49e0babccb23
child 113 397270b5d21a
permissions -rw-r--r--
Ignore files from the javadb project Do not nerve with class- and derby files committer: Markus Bröker <mbroeker@largo.homelinux.org>

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

#include <stdio.h>
#include <stdlib.h>
#include <wait.h>
#include <unistd.h>

int set_limit (int);

int main (int argc, char **argv)
{
    int i;
    int pid;

    char **args;

    if (argc < 3) {
        printf ("Usage: %s <mem> <cmd> [args]...\n", argv[0]);
        printf ("Report bugs to mbroeker@largo.homelinux.org\n");
        return EXIT_FAILURE;
    }

    if ((args = calloc ((argc - 1), sizeof (char *))) == NULL) {
        perror ("calloc");
        return EXIT_FAILURE;
    }

    for (i = 2; i < argc; i++)
        args[i - 2] = argv[i];

    args[i - 2] = NULL;

    pid = fork ();
    switch (pid) {
    case 0:
        if (set_limit (atoi (argv[1])) == 0) {
            i = execvp (args[0], args);
        } else {
            perror ("Limit Error");
            return EXIT_FAILURE;
        }
    case -1:
        perror ("Fork Error");
        return EXIT_FAILURE;
    default:
        wait (&i);
    }

    if (args != NULL)
        free (args);

    return i;
}