equal
deleted
inserted
replaced
|
1 /** |
|
2 * $Id: numerierung.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ |
|
3 * $Source: /development/c/demos/numerierung.c,v $ |
|
4 * |
|
5 */ |
|
6 |
|
7 #include <stdio.h> |
|
8 #include <stdlib.h> |
|
9 #include <errno.h> |
|
10 #include <getopt.h> |
|
11 |
|
12 void usage (char *cmd) |
|
13 { |
|
14 printf ("Usage: %s [-h] [-t] [-v] [file]\n", cmd); |
|
15 exit (0); |
|
16 } |
|
17 |
|
18 int main (int argc, char **argv) |
|
19 { |
|
20 char buffer[81]; |
|
21 int counter = 0; |
|
22 FILE *f; |
|
23 int c, pure = 0; |
|
24 |
|
25 while ((c = getopt (argc, argv, "hp")) != -1) { |
|
26 printf ("Zeichen: %c\n", c); |
|
27 switch (c) { |
|
28 case 'h': |
|
29 usage (argv[0]); |
|
30 break; |
|
31 case 'p': |
|
32 pure = 1; |
|
33 break; |
|
34 default: |
|
35 printf ("Unknown Parameter: %s\n", argv[optind]); |
|
36 } |
|
37 printf ("GETOPT: %d : %s\n", optind, argv[optind]); |
|
38 } |
|
39 |
|
40 if (optind == argc) |
|
41 f = stdin; |
|
42 else |
|
43 f = fopen (argv[optind], "r"); |
|
44 |
|
45 if (!f) { |
|
46 perror ("Error opening file"); |
|
47 return EXIT_FAILURE; |
|
48 } |
|
49 |
|
50 *buffer = 0; |
|
51 |
|
52 while (fgets (buffer, 80, f) != NULL) { |
|
53 if (!pure) |
|
54 printf ("%4.4d: %s", counter++, buffer); |
|
55 else |
|
56 printf ("%s", buffer); |
|
57 *buffer = 0; |
|
58 } |
|
59 |
|
60 fclose (f); |
|
61 return EXIT_SUCCESS; |
|
62 } |