-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.c
More file actions
executable file
·94 lines (83 loc) · 2.73 KB
/
Copy pathutilities.c
File metadata and controls
executable file
·94 lines (83 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* $Id: utilities.c,v 1.5 2024/07/23 14:25:45 leavens Exp $ */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "utilities.h"
// to turn off debugging support (assertions and debug_print)
// define the symbol NDEBUG (by writing uncommenting the following)
// #define NDEBUG
#ifdef NDEBUG
#define debug_print() ((void)0)
#else
// otherwise debugging is on, and debug_print is defined as follows...
// (note that assert is a macro defined in <assert.h>
static void vdebug_print(const char *fmt, va_list args);
// If debugging is false, do nothing, otherwise (when debugging)
// flush stderr and stdout, then print the message given on stderr,
// using printf formatting from the format string fmt.
// This function returns normally.
void debug_print(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vdebug_print(fmt, args);
va_end(args);
}
// The variadic version of debug_print
static void vdebug_print(const char *fmt, va_list args)
{
// flush output streams to synchronize outputs
fflush(stdout);
fflush(stderr);
vfprintf(stderr, fmt, args);
fflush(stderr);
}
#endif
static void vbail_with_error(const char* fmt, va_list args);
// Format a string error message and print it followed by a newline on stderr
// using perror (for an OS error, if the errno is not 0)
// then exit with a failure code, so a call to this does not return.
void bail_with_error(const char *fmt, ...)
{
fflush(stdout); // flush so output comes after what has happened already
va_list(args);
va_start(args, fmt);
vbail_with_error(fmt, args);
}
// The variadic version of bail_with_error
static void vbail_with_error(const char* fmt, va_list args)
{
extern int errno;
char buff[2048];
vsprintf(buff, fmt, args);
if (errno != 0) {
perror(buff);
} else {
fprintf(stderr, "%s\n", buff);
}
fflush(stderr);
exit(EXIT_FAILURE);
}
// Print an error message on stderr
// starting with the file name and line number from the floc argument
// (prints: filename, a colon, " line ", the line number, and a space)
// and then the message.
// Then exit with a failure code, so this function does not return.
void bail_with_prog_error(file_location floc, const char *fmt, ...)
{
fflush(stdout); // flush so output comes after what has happened already
// print file, line, column information
fprintf(stderr, "%s: line %d ", floc.filename, floc.line);
va_list(args);
va_start(args, fmt);
vbail_with_error(fmt, args);
}
// print a newline on out and flush out
void newline(FILE *out)
{
fprintf(out, "\n");
fflush(out);
}