numerierung.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 53 6b3d7e3418c1
parent 27 81a574d60c15
child 77 49e0babccb23
permissions -rw-r--r--
getch.c renamed to cross_getch.c 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;
}