Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_common.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include <stdio.h>
28#include <errno.h>
29#include <string.h>
30#include <sys/types.h>
31
32#include "nilorea/n_common.h"
33#include "nilorea/n_log.h"
34#include "nilorea/n_str.h"
35
36#ifndef __windows__
37#include <sys/wait.h>
38#endif
39
40void usage(void) {
41 fprintf(stderr,
42 " -v version\n"
43 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
44 " -h help\n");
45}
46
47void process_args(int argc, char** argv) {
48 int getoptret = 0,
49 log_level = LOG_DEBUG; /* default log level */
50
51 /* Arguments optionnels */
52 /* -v version
53 * -V log level
54 * -h help
55 */
56 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
57 switch (getoptret) {
58 case 'v':
59 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
60 exit(1);
61 case 'V':
62 if (!strcmp("LOG_NULL", optarg))
64 else if (!strcmp("LOG_NOTICE", optarg))
66 else if (!strcmp("LOG_INFO", optarg))
68 else if (!strcmp("LOG_ERR", optarg))
70 else if (!strcmp("LOG_DEBUG", optarg))
72 else {
73 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
74 exit(-1);
75 }
76 break;
77 default:
78 case '?': {
79 if (optopt == 'V') {
80 fprintf(stderr, "\n Missing log level\n");
81 } else if (optopt == 'p') {
82 fprintf(stderr, "\n Missing port\n");
83 } else if (optopt != 's') {
84 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
85 }
86 usage();
87 exit(1);
88 }
89 case 'h': {
90 usage();
91 exit(1);
92 }
93 }
94 }
96} /* void process_args( ... ) */
97
99 n_log(LOG_DEBUG, "Inlined func");
100}
101
102int main(int argc, char** argv) {
103 /* processing args and set log_level */
104 process_args(argc, argv);
105
106 inlined_func();
107
108 n_log(LOG_INFO, "TRUE,true values: %d,%d / FALSE,false value: %d,%d", TRUE, true, FALSE, false);
109
110 n_log(LOG_INFO, "_str(\"TEST\")=\"%s\" , _str(NULL)=\"%s\"", _str("TEST"), _str(NULL));
111 n_log(LOG_INFO, "_strw(\"TEST\")=\"%s\" , _strw(NULL)=\"%s\"", _str("TEST"), _str(NULL));
112 N_STR* nstr = char_to_nstr("TEST");
113 n_log(LOG_INFO, "_nstr(nstrpointer)=\"%s\"", _nstr(nstr));
114 n_log(LOG_INFO, "_nstrp(nstrpointer)=\"%s\"", _nstrp(nstr));
115 free_nstr(&nstr);
116 n_log(LOG_INFO, "_nstr(nstrpointer_freed)=\"%s\"", _nstr(nstr));
117 n_log(LOG_INFO, "_nstrp(nstrpointer_freed)=\"%s\"", _nstrp(nstr));
118
119 char* data = NULL;
120 __n_assert(data, n_log(LOG_INFO, "data is NULL"));
121 Malloc(data, char, 1024);
122 __n_assert(data, n_log(LOG_INFO, "data is NULL"));
123 Free(data);
124 Malloc(data, char, 1024);
125 if (Realloc(data, char, 2048) == FALSE) {
126 n_log(LOG_ERR, "could not realloc data to 2048");
127 }
128 Free(data);
129 Malloc(data, char, 1024);
130 if (Reallocz(data, char, 1024, 2048) == FALSE) {
131 n_log(LOG_ERR, "could not reallocz data to 2048");
132 }
133 FreeNoLog(data);
134 Alloca(data, 2048);
135 // Free( data ); alloca should not be free else it's double freeing on exit
136
137 n_log(LOG_INFO, "next_odd(10):%d , next_odd(11):%d", next_odd(10), next_odd(11));
138 n_log(LOG_INFO, "next_even(10):%d , next_even(11):%d", next_even(10), next_even(11));
139
141 ifzero(0) endif;
142 ifzero(1) endif;
143 ifnull(data) endif;
144 ifnull(NULL) endif;
145 iffalse(FALSE) endif;
146 iftrue(TRUE) endif;
147 checkerror();
148error:
149 if (get_error()) {
150 n_log(LOG_INFO, "got an error while processing test");
151 } else {
152 n_log(LOG_INFO, "All tests for checkerror() are OK");
153 }
154
155 if (file_exist(argv[0])) {
156 n_log(LOG_INFO, "%s exists !", argv[0]);
157 }
158
159 char *dir = NULL, *name = NULL;
160 dir = get_prog_dir();
161 name = get_prog_name();
162 n_log(LOG_INFO, "From %s/%s", dir, name);
163 Free(dir);
164 Free(name);
165
166 N_STR* out = NULL;
167 int ret = -1;
168 if (n_popen("ls -ltr", 2048, (void*)&out, &ret) == TRUE) {
169 n_log(LOG_INFO, "ls returned %d : %s", ret, _nstr(out));
170 } else {
171 n_log(LOG_ERR, "popen s returned an error");
172 }
173 free_nstr(&out);
174
175 char hidden_str[47] = "";
176 N_HIDE_STR(hidden_str, 'T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'w', 'i', 'l', 'l', ' ', 'b', 'e', ' ', 'h', 'i', 'd', 'd', 'e', 'n', ' ', 'i', 'n', ' ', 't', 'h', 'e', ' ', 'f', 'i', 'n', 'a', 'l', ' ', 'b', 'i', 'n', 'a', 'r', 'y', '\0');
177 printf("hidden str:%s\n", hidden_str);
178
179 /* test get_computer_name */
180 char computer_name[256] = "";
181 get_computer_name(computer_name, sizeof(computer_name));
182 n_log(LOG_INFO, "computer name: %s", computer_name);
183
184 /* test n_get_file_extension */
185 const char* ext = n_get_file_extension("archive.tar.gz");
186 n_log(LOG_INFO, "n_get_file_extension(\"archive.tar.gz\"): %s", _str(ext));
187 ext = n_get_file_extension("noextension");
188 n_log(LOG_INFO, "n_get_file_extension(\"noextension\"): %s", _str(ext));
189
190 /* test log_environment */
192
193#ifndef __windows__
194 /* test sigchld_handler_installer */
195 if (sigchld_handler_installer() == TRUE) {
196 n_log(LOG_INFO, "sigchld_handler_installer succeeded");
197 }
198
199 n_log(LOG_INFO, "before system_nb( sleep 3 )");
200 int pid = system_nb("sleep 3", NULL, NULL);
201 n_log(LOG_INFO, "after system_nb( sleep 3 )");
202 n_log(LOG_INFO, "wait for nb sys call");
203 wait(&pid);
204 n_log(LOG_INFO, "done");
205 /* n_daemonize() forks and detaches — disabled so ASan/LSan can check
206 * the rest of the example for leaks (the grandchild's exit is invisible
207 * to the sanitizer). */
208 /* n_daemonize(); */
209#endif
210
211 exit(0);
212}
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
void inlined_func(void)
Definition ex_common.c:98
int main(void)
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:271
void log_environment(int loglevel)
log environment variables in syslog
Definition n_common.c:231
char * get_prog_name(void)
get current program name
Definition n_common.c:144
#define get_error()
pop up errors if any
Definition n_common.h:330
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
Definition n_common.c:458
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:203
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define _str(__PTR)
define true
Definition n_common.h:192
int get_computer_name(char *computer_name, size_t len)
get the computer name
Definition n_common.c:69
#define ifzero
error checker type if( 0 != toto )
Definition n_common.h:308
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
Definition n_common.c:389
char * n_get_file_extension(char path[])
get extension of path+filename
Definition n_common.c:479
#define iffalse
error checker type if( toto == FALSE )
Definition n_common.h:311
#define Reallocz(__ptr, __struct, __old_size, __size)
Realloc + zero new memory zone Handler to get errors.
Definition n_common.h:246
#define next_odd(__val)
next odd helper
Definition n_common.h:295
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition n_common.h:215
#define endif
close a ifwhatever block
Definition n_common.h:324
#define ifnull
error checker type if( !toto )
Definition n_common.h:305
#define _nstrp(__PTR)
N_STR or NULL pointer for testing purposes.
Definition n_common.h:200
#define next_even(__val)
next even helper
Definition n_common.h:298
char * get_prog_dir(void)
get current program running directory
Definition n_common.c:112
#define FORCE_INLINE
FORCE_INLINE portable macro.
Definition n_common.h:163
#define iftrue
error checker type if( toto == FALSE )
Definition n_common.h:314
#define checkerror()
check for errors
Definition n_common.h:317
int sigchld_handler_installer()
install signal SIGCHLD handler to reap zombie processes
Definition n_common.c:266
#define init_error_check()
init error checking in a function
Definition n_common.h:301
#define Realloc(__ptr, __struct, __size)
Realloc Handler to get errors.
Definition n_common.h:230
int file_exist(const char *filename)
test if file exist and if it's readable
Definition n_common.c:99
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
int n_popen(char *cmd, size_t read_buf_size, void **nstr_output, int *ret)
launch a command and return output and status
Definition n_common.c:179
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
A box including a string and his lenght.
Definition n_str.h:60
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.