29#include <linux/limits.h>
42 va_start(args, format);
44 vsnprintf(str,
sizeof str, format, args);
46 fprintf(stderr,
"%s", str);
60 memset(computer_name, 0, len);
63 DWORD bufCharCount = len;
64 if( GetComputerName( infoBuf, &bufCharCount ) )
66 memcpy( computer_name , infoBuf , len );
70 strcpy(computer_name,
"Unknown_Host_Name");
74 if( gethostname(computer_name, len) == -1 )
77 strcpy(computer_name,
"Unknown_Host_Name");
78 n_log(
LOG_ERR ,
"%s on gethostname !" , strerror( error ) );
95 if( ( file = fopen( filename,
"r" ) ) != NULL )
111 char strbuf[ PATH_MAX ] =
"" ;
115 unsigned long int bytes = GetModuleFileName( NULL, strbuf, PATH_MAX );
119 return strdup( dirname(strbuf ) );
122 char procbuf[ PATH_MAX ] =
"" ;
124 sprintf( procbuf,
"/proc/%d/exe", (
int)getpid() );
126 sprintf( procbuf,
"/proc/%d/path/a.out", (
int)getpid() );
128 int bytes = MIN( readlink( procbuf, strbuf, PATH_MAX ), PATH_MAX - 1 );
132 strbuf[ bytes ] =
'\0';
133 return strdup( dirname( strbuf ) );
136 fprintf( stderr,
"%s", strerror( error ) );
147 char strbuf[ PATH_MAX ] =
"" ;
150 unsigned long int bytes = GetModuleFileName( NULL, strbuf, PATH_MAX );
154 return strdup( basename(strbuf ) );
157 char procbuf[ PATH_MAX ] =
"" ;
159 sprintf( procbuf,
"/proc/%d/exe", (
int)getpid() );
161 sprintf( procbuf,
"/proc/%d/path/a.out", (
int)getpid() );
163 int bytes = MIN( readlink( procbuf, strbuf, PATH_MAX ), PATH_MAX - 1 );
167 strbuf[ bytes ] =
'\0';
168 return strdup( basename( strbuf ) );
171 fprintf( stderr,
"%s", strerror( error ) );
185int n_popen(
char *cmd,
int read_buf_size,
void **nstr_output,
int *ret )
193 char read_buf[ read_buf_size ] ;
195 fp = popen( cmd,
"r" );
199 n_log(
LOG_ERR,
"popen( %s ) returned NULL , %s (%d)", cmd, strerror( err ), err );
204 while( fgets( read_buf, read_buf_size, fp ) != NULL )
206 length = strlen( read_buf );
207 if( read_buf[ length - 1 ] ==
'\n' )
209 read_buf[ length - 1 ] =
'\0' ;
213 status = pclose( fp );
217 n_log(
LOG_ERR,
"pclose( %s ) returned -1 , %s (%d)", cmd, strerror( err ), err );
218 if( WIFEXITED( status ) )
220 n_log(
LOG_ERR,
"Child exited with RC=%d", WEXITSTATUS( status ) );
221 (*ret) = WEXITSTATUS( status );
225 (*ret) = WEXITSTATUS( status );
226 (*nstr_output) = output_pointer ;
242 n_log(
LOG_ERR,
"Error: unable to fork child: %s", strerror( errno ) );
251 for(
int x = 0 ; x < sysconf( _SC_OPEN_MAX ) ; x++ )
255 int fd = open(
"/dev/null",O_RDWR, 0);
258 dup2 (fd, STDIN_FILENO);
259 dup2 (fd, STDOUT_FILENO);
260 dup2 (fd, STDERR_FILENO);
274 n_log(
LOG_ERR,
"Error: unable to set session leader with setsid(): %s", strerror( errno ) );
293pid_t
system_nb(
const char * command,
int * infp,
int * outfp)
297 int p_stdin[2] = { -1, -1 };
298 int p_stdout[2] = { -1, -1 };
301 if( pipe( p_stdin ) == -1 )
303 n_log(
LOG_ERR,
"Couldn't pipe stdin for command %s", command );
306 if (pipe(p_stdout) == -1)
310 n_log(
LOG_ERR,
"Couldn't pipe stdout for command %s", command );
329 dup2(p_stdout[1], 1);
330 dup2( open(
"/dev/null", O_RDONLY), 2);
333 for(
int i = 3 ; i < sysconf( _SC_OPEN_MAX ) ; i++ )
338 execl(
"/bin/sh",
"sh",
"-c", command, (
char *)NULL);
343 close( p_stdout[1] );
358 *outfp = p_stdout[0];
376 va_start (args, buf);
380 while((arg = va_arg(args,
int)))
396 char *result = NULL ;
403 while ((i > 0) && (path[i] !=
'.') && (path[i] !=
'/') && (path[i] !=
'\\')) {
406 if ((i > 0) && (i < n - 1) && (path[i] ==
'.') && (path[i - 1] !=
'/') && (path[i - 1] !=
'\\')) {
char * get_prog_name(void)
get current program name
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
#define __n_assert(__ptr, __ret)
macro to assert things
int get_computer_name(char *computer_name, size_t len)
abort program with a text
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
char * n_get_file_extension(char path[])
get extension of path+filename
int n_popen(char *cmd, int read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
char * get_prog_dir(void)
get current program running directory
void n_abort(char const *format,...)
abort program with a text
int n_daemonize(void)
Daemonize program.
int file_exist(const char *filename)
test if file exist and if it's readable
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
#define LOG_NOTICE
normal but significant condition
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
#define nstrprintf_cat(__nstr_var,...)
Macro to quickly allocate and sprintf and cat to a N_STR *.
A box including a string and his lenght.
Common headers and low-level hugly functions & define.
N_STR and string function declaration.