Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_file.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_files.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 Realloc(data, char, 2048);
127 Free(data);
128 Malloc(data, char, 1024);
129 Reallocz(data, char, 1024, 2048);
130 FreeNoLog(data);
131 Alloca(data, 2048);
132 // Free( data ); alloca should not be free else it's double freeing on exit
133
134 n_log(LOG_INFO, "next_odd(10):%d , next_odd(11):%d", next_odd(10), next_odd(11));
135 n_log(LOG_INFO, "next_even(10):%d , next_even(11):%d", next_even(10), next_even(11));
136
138 ifzero(0) endif;
139 ifzero(1) endif;
140 ifnull(data) endif;
141 ifnull(NULL) endif;
142 iffalse(FALSE) endif;
143 iftrue(TRUE) endif;
144 checkerror();
145error:
146 if (get_error()) {
147 n_log(LOG_INFO, "got an error while processing test");
148 } else {
149 n_log(LOG_INFO, "All tests for checkerror() are OK");
150 }
151
152 if (file_exist(argv[0])) {
153 n_log(LOG_INFO, "%s exists !", argv[0]);
154 }
155
156 char *dir = NULL, *name = NULL;
157 dir = get_prog_dir();
158 name = get_prog_name();
159 n_log(LOG_INFO, "From %s/%s", dir, name);
160 Free(dir);
161 Free(name);
162
163 N_STR* out = NULL;
164 int ret = -1;
165 if (n_popen("ls -ltr", 2048, (void*)&out, &ret) == TRUE) {
166 n_log(LOG_INFO, "ls returned %d : %s", ret, _nstr(out));
167 } else {
168 n_log(LOG_ERR, "popen s returned an error");
169 }
170 free_nstr(&out);
171
172 char hidden_str[47] = "";
173 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');
174 printf("hidden str:%s\n", hidden_str);
175
176 /* test n_scan_dir and n_free_file_info from n_files.h */
177 LIST* file_list = new_generic_list(0);
178 if (n_scan_dir(".", file_list, 0) == TRUE) {
179 n_log(LOG_INFO, "n_scan_dir found %zu files in current directory", file_list->nb_items);
180 list_foreach(fnode, file_list) {
181 N_FILE_INFO* fi = (N_FILE_INFO*)fnode->ptr;
182 if (fi && fi->name) {
183 n_log(LOG_DEBUG, " file: %s (time: %ld)", fi->name, (long)fi->filetime);
184 }
185 }
186 } else {
187 n_log(LOG_ERR, "n_scan_dir failed");
188 }
189 list_destroy(&file_list);
190
191#ifndef __windows__
192 n_log(LOG_INFO, "before system_nb( sleep 3 )");
193 int pid = system_nb("sleep 3", NULL, NULL);
194 n_log(LOG_INFO, "after system_nb( sleep 3 )");
195 n_log(LOG_INFO, "wait for nb sys call");
196 wait(&pid);
197 n_log(LOG_INFO, "done");
198 n_daemonize();
199#endif
200 n_abort("Testing abort before exit");
201 n_log(LOG_INFO, "abort done");
202 exit(0);
203}
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
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
#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
#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
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:53
int n_daemonize(void)
Daemonize program.
Definition n_common.c:284
#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
size_t nb_items
number of item currently in the list
Definition n_list.h:61
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:89
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:548
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:37
Structure of a generic LIST container.
Definition n_list.h:59
#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
time_t filetime
file creation time
Definition n_files.h:50
char * name
file name
Definition n_files.h:48
int n_scan_dir(const char *dir, LIST *result, const int recurse)
Scan a directory and append only regular files into a sorted list (oldest first).
Definition n_files.c:181
common file information
Definition n_files.h:46
#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
Files configuration header.
Generic log system.
N_STR and string function declaration.