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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include <sys/types.h>
32
33#include "nilorea/n_common.h"
34#include "nilorea/n_log.h"
35#include "nilorea/n_str.h"
36
37#ifndef __windows__
38#include <sys/wait.h>
39#endif
40
41void usage(void) {
42 fprintf(stderr,
43 " -v version\n"
44 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
45 " -h help\n");
46}
47
48void process_args(int argc, char** argv) {
49 int getoptret = 0,
50 log_level = LOG_DEBUG; /* default log level */
51
52 /* Arguments optionnels */
53 /* -v version
54 * -V log level
55 * -h help
56 */
57 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
58 switch (getoptret) {
59 case 'v':
60 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
61 exit(1);
62 case 'V':
63 if (!strcmp("LOG_NULL", optarg))
65 else if (!strcmp("LOG_NOTICE", optarg))
67 else if (!strcmp("LOG_INFO", optarg))
69 else if (!strcmp("LOG_ERR", optarg))
71 else if (!strcmp("LOG_DEBUG", optarg))
73 else {
74 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
75 exit(-1);
76 }
77 break;
78 default:
79 case '?': {
80 if (optopt == 'V') {
81 fprintf(stderr, "\n Missing log level\n");
82 } else if (optopt == 'p') {
83 fprintf(stderr, "\n Missing port\n");
84 } else if (optopt != 's') {
85 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
86 }
87 usage();
88 exit(1);
89 }
90 case 'h': {
91 usage();
92 exit(1);
93 }
94 }
95 }
97} /* void process_args( ... ) */
98
100 n_log(LOG_DEBUG, "Inlined func");
101}
102
103int main(int argc, char** argv) {
104 /* processing args and set log_level */
105 process_args(argc, argv);
106
107 inlined_func();
108
109 n_log(LOG_INFO, "TRUE,true values: %d,%d / FALSE,false value: %d,%d", TRUE, true, FALSE, false);
110
111 n_log(LOG_INFO, "_str(\"TEST\")=\"%s\" , _str(NULL)=\"%s\"", _str("TEST"), _str(NULL));
112 n_log(LOG_INFO, "_strw(\"TEST\")=\"%s\" , _strw(NULL)=\"%s\"", _str("TEST"), _str(NULL));
113 N_STR* nstr = char_to_nstr("TEST");
114 n_log(LOG_INFO, "_nstr(nstrpointer)=\"%s\"", _nstr(nstr));
115 n_log(LOG_INFO, "_nstrp(nstrpointer)=\"%s\"", _nstrp(nstr));
116 free_nstr(&nstr);
117 n_log(LOG_INFO, "_nstr(nstrpointer_freed)=\"%s\"", _nstr(nstr));
118 n_log(LOG_INFO, "_nstrp(nstrpointer_freed)=\"%s\"", _nstrp(nstr));
119
120 char* data = NULL;
121 __n_assert(data, n_log(LOG_INFO, "data is NULL"));
122 Malloc(data, char, 1024);
123 __n_assert(data, n_log(LOG_INFO, "data is NULL"));
124 Free(data);
125 Malloc(data, char, 1024);
126 if (Realloc(data, char, 2048) == FALSE) {
127 n_log(LOG_ERR, "could not realloc data to 2048");
128 }
129 Free(data);
130 Malloc(data, char, 1024);
131 if (Reallocz(data, char, 1024, 2048) == FALSE) {
132 n_log(LOG_ERR, "could not reallocz data to 2048");
133 }
134 FreeNoLog(data);
135 Alloca(data, 2048);
136 // Free( data ); alloca should not be free else it's double freeing on exit
137
138 n_log(LOG_INFO, "next_odd(10):%d , next_odd(11):%d", next_odd(10), next_odd(11));
139 n_log(LOG_INFO, "next_even(10):%d , next_even(11):%d", next_even(10), next_even(11));
140
142 ifzero(0) endif;
143 ifzero(1) endif;
144 ifnull(data) endif;
145 ifnull(NULL) endif;
146 iffalse(FALSE) endif;
147 iftrue(TRUE) endif;
148 checkerror();
149error:
150 if (get_error()) {
151 n_log(LOG_INFO, "got an error while processing test");
152 } else {
153 n_log(LOG_INFO, "All tests for checkerror() are OK");
154 }
155
156 if (file_exist(argv[0])) {
157 n_log(LOG_INFO, "%s exists !", argv[0]);
158 }
159
160 char *dir = NULL, *name = NULL;
161 dir = get_prog_dir();
162 name = get_prog_name();
163 n_log(LOG_INFO, "From %s/%s", dir, name);
164 Free(dir);
165 Free(name);
166
167 N_STR* out = NULL;
168 int ret = -1;
169 if (n_popen("ls -ltr", 2048, (void*)&out, &ret) == TRUE) {
170 n_log(LOG_INFO, "ls returned %d : %s", ret, _nstr(out));
171 } else {
172 n_log(LOG_ERR, "popen s returned an error");
173 }
174 free_nstr(&out);
175
176 char hidden_str[47] = "";
177 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');
178 printf("hidden str:%s\n", hidden_str);
179
180 /* test get_computer_name */
181 char computer_name[256] = "";
182 get_computer_name(computer_name, sizeof(computer_name));
183 n_log(LOG_INFO, "computer name: %s", computer_name);
184
185 /* test n_get_file_extension */
186 const char* ext = n_get_file_extension("archive.tar.gz");
187 n_log(LOG_INFO, "n_get_file_extension(\"archive.tar.gz\"): %s", _str(ext));
188 ext = n_get_file_extension("noextension");
189 n_log(LOG_INFO, "n_get_file_extension(\"noextension\"): %s", _str(ext));
190
191 /* test log_environment */
193
194#ifndef __windows__
195 /* test sigchld_handler_installer */
196 if (sigchld_handler_installer() == TRUE) {
197 n_log(LOG_INFO, "sigchld_handler_installer succeeded");
198 }
199
200 n_log(LOG_INFO, "before system_nb( sleep 3 )");
201 int pid = system_nb("sleep 3", NULL, NULL);
202 n_log(LOG_INFO, "after system_nb( sleep 3 )");
203 n_log(LOG_INFO, "wait for nb sys call");
204 wait(&pid);
205 n_log(LOG_INFO, "done");
206 /* n_daemonize() forks and detaches, disabled so ASan/LSan can check
207 * the rest of the example for leaks (the grandchild's exit is invisible
208 * to the sanitizer). */
209 /* n_daemonize(); */
210#endif
211
212 exit(0);
213}
static void usage(void)
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
void inlined_func(void)
Definition ex_common.c:99
int getoptret
Definition ex_fluid.c:59
int log_level
Definition ex_fluid.c:60
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:272
void log_environment(int loglevel)
log environment variables in syslog
Definition n_common.c:232
char * get_prog_name(void)
get current program name
Definition n_common.c:145
#define get_error()
pop up errors if any
Definition n_common.h:331
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
Definition n_common.c:459
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define _str(__PTR)
define true
Definition n_common.h:193
int get_computer_name(char *computer_name, size_t len)
get the computer name
Definition n_common.c:70
#define ifzero
error checker type if( 0 != toto )
Definition n_common.h:309
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
Definition n_common.c:390
char * n_get_file_extension(char path[])
get extension of path+filename
Definition n_common.c:480
#define iffalse
error checker type if( toto == FALSE )
Definition n_common.h:312
#define Reallocz(__ptr, __struct, __old_size, __size)
Realloc + zero new memory zone Handler to get errors.
Definition n_common.h:247
#define next_odd(__val)
next odd helper
Definition n_common.h:296
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition n_common.h:216
#define endif
close a ifwhatever block
Definition n_common.h:325
#define ifnull
error checker type if( !toto )
Definition n_common.h:306
#define _nstrp(__PTR)
N_STR or NULL pointer for testing purposes.
Definition n_common.h:201
#define next_even(__val)
next even helper
Definition n_common.h:299
char * get_prog_dir(void)
get current program running directory
Definition n_common.c:113
#define FORCE_INLINE
FORCE_INLINE portable macro.
Definition n_common.h:164
#define iftrue
error checker type if( toto == FALSE )
Definition n_common.h:315
#define checkerror()
check for errors
Definition n_common.h:318
int sigchld_handler_installer()
install signal SIGCHLD handler to reap zombie processes
Definition n_common.c:267
#define init_error_check()
init error checking in a function
Definition n_common.h:302
#define Realloc(__ptr, __struct, __size)
Realloc Handler to get errors.
Definition n_common.h:231
int file_exist(const char *filename)
test if file exist and if it's readable
Definition n_common.c:100
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:199
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:180
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:255
A box including a string and his lenght.
Definition n_str.h:61
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.