err 2.0.0
Small error-printing library written in C
Loading...
Searching...
No Matches
files.c
1/**
2 * @example files.c
3 * This example demonstrates when and how the ewarn() function is used.
4 *
5 * err() is called when a fatal error occurs. ewarn() is called when a nonfatal
6 * error occurs.
7 *
8 * Compare this example with hello.c, which demonstrates warn(). In hello.c,
9 * warn() is called after an unusual but managable situation with the program
10 * arguments is found. In this example, ewarn() is called after a necessary
11 * library function fails. We will get a more detailed error message with
12 * ewarn() because it looks at errno when writing its message.
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17
18#include "../err.h"
19
20int
21main(int argc, char *argv[])
22{
23 int i;
24
26
27 if (argc < 2)
28 err("no files provided");
29
30 for (i = 1; i < argc; i++) {
31 FILE *fp;
32 int c;
33
34 if ((fp = fopen(argv[i], "r")) == NULL) {
35 ewarn("%s", argv[i]);
36 continue;
37 }
38
39 while ((c = fgetc(fp)) != EOF)
40 putchar(c);
41
42 fclose(fp);
43 }
44
45 return EXIT_SUCCESS;
46}
char * program_invocation_name
Global value for the program's name.
Definition: err.c:29
void ewarn(const char *fmt,...)
Prints a formatted error message to stderr.
Definition: err.c:97
void err(const char *fmt,...)
Prints a formatted error message to stderr and exits.
Definition: err.c:123