numerierung.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sun, 26 Jul 2009 18:21:25 +0200
changeset 103 be3fe4a4f097
parent 85 9568a180fc43
permissions -rw-r--r--
Function Pointer Demo simplified It makes more sense to show the basic usage of fps committer: Markus Bröker <mbroeker@largo.homelinux.org>

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

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>

void usage (char *cmd)
{
    printf ("Usage: %s [-h|-p] [<file>]\n", cmd);
    exit (0);
}

int main (int argc, char **argv)
{
    char buffer[240];

    int counter = 0;

    FILE *f;

    int c, pure = 0;

    struct option options[] = {
        {"help", 0, 0, 'h'},
        {"pure", 0, 0, 'p'},
    };

    while ((c = getopt_long_only (argc, argv, "hp", options, NULL)) != -1) {
        switch (c) {
        case 'h':
            usage (argv[0]);
            break;
        case 'p':
            pure = 1;
            break;
        default:
            printf ("Unknown Parameter: %s\n", argv[optind]);
        }
    }

    if (optind == argc)
        f = stdin;
    else
        f = fopen (argv[optind], "r");

    if (!f) {
        perror ("Error opening file");
        return EXIT_FAILURE;
    }

    *buffer = 0;

    while (fgets (buffer, sizeof (buffer), f) != NULL) {
        if (!pure)
            printf ("%4.4d: %s", counter++, buffer);
        else
            printf ("%s", buffer);
        *buffer = 0;
    }

    fclose (f);

    return EXIT_SUCCESS;
}