-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcc-env.c
91 lines (80 loc) · 3.13 KB
/
gcc-env.c
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
/*
* env.c - Display environment vairables.
*
* Copyright(C) 2018 MEJT
*
* Prints the command line arguments and environment vairables if debuging
* is enabled using a macro that will work GCC and older compilers that use
* the ANSI C standard.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 07 Apr 13 - Initial version - MEJT
* 24 Feb 16 0.1 - Lists environemnt vairables - MEJT
* 22 Apr 18 - Added DEBUG macro - MEJT
*
*/
#include <stdio.h>
#include <stdlib.h>
/* Execute code if DEBUG is True */
#ifndef debug /* Don't redefine macro if already defined. */
#define debug(code) do {if(DEBUG){code;}} while(0)
#endif
/* Print a message to stderr if DEBUG is True */
#ifndef print /* Don't redefine macro if already defined. */
#define print(_fmt, ...) do { \
if(DEBUG){ \
fprintf(stderr, "Debug\t: %s line : %d (%s) : " _fmt "\n", \
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
} \
} while(0)
#endif
/* Print warning to stderr if DEBUG is True */
#ifndef warning /* Don't redefine macro if already defined. */
#define warning(_fmt, ...) do { \
if(DEBUG){ \
fprintf(stderr, "Warning\t: %s line : %d (%s) : " _fmt "\n", \
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
} \
} while(0)
#endif
/* Print an error message stderr and exit */
#ifndef error /* Don't redefine macro if already defined. */
#define error(_fmt, ...) do { \
fprintf(stderr, "Error\t: %s line : %d (%s) : " _fmt "\n", \
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
exit (1); \
} while(0)
#endif
#define DEBUG 1 /* Enable debugging*/
int main(int argc, char **argv, char **envp){
register char **pointer; /* Used as a pointer to the string arrays */
int status = 0;
/* Print out the argument list using the number of arguments in argc */
debug(
fprintf(stderr, "Argv[]\t: ");
for(pointer = argv; argc > 0; argc--,pointer++){
fprintf(stderr, "%s", *pointer);
/* Print spaces between arguments and a newline at the end */
if(argc >1)fprintf(stderr, " "); else fprintf(stderr, "\n");
});
/* Print the current environment vairables
* Note - The environment table is terminated by a NULL entry, so we just
* loop while the pointer isn't a NULL */
for(pointer = envp; *pointer; pointer++){
debug(fprintf(stderr, "%s\n", *pointer)); /* Use debug inside loop */
}
if (status) error("Error"); else print("Status\t: %d", status);
exit(status);
}