numerierung.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:15 +0100
changeset 22 0db9235d68d9
parent 9 c3fecc82ade6
child 27 81a574d60c15
permissions -rw-r--r--
mysql_db is untested because we have no sponsor with a mysql database committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/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] [-t] [-v] [file]\n", cmd);
    exit (0);
}

int main (int argc, char **argv)
{
    char buffer[81];
    int counter = 0;
    FILE *f;
    int c, pure = 0;

    while ((c = getopt (argc, argv, "hp")) != -1) {
        printf ("Zeichen: %c\n", c);
        switch (c) {
        case 'h':
            usage (argv[0]);
            break;
        case 'p':
            pure = 1;
            break;
        default:
            printf ("Unknown Parameter: %s\n", argv[optind]);
        }
        printf ("GETOPT: %d : %s\n", optind, 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, 80, f) != NULL) {
        if (!pure)
            printf ("%4.4d: %s", counter++, buffer);
        else
            printf ("%s", buffer);
        *buffer = 0;
    }

    fclose (f);

    return EXIT_SUCCESS;
}