2016-03-12 01:29:14 +05:30
|
|
|
/* Unix */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
2019-02-11 23:16:14 +05:30
|
|
|
#include <signal.h>
|
2020-11-14 19:06:10 +05:30
|
|
|
#include <pwd.h>
|
2020-07-25 13:29:22 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2022-04-13 09:25:34 +05:30
|
|
|
#include <zlib.h>
|
2016-03-12 01:29:14 +05:30
|
|
|
|
2022-04-13 01:44:04 +05:30
|
|
|
#ifdef FYAML
|
|
|
|
#include <libfyaml.h>
|
|
|
|
#endif
|
|
|
|
|
2020-11-14 19:06:10 +05:30
|
|
|
#define PATHLEN 4096
|
|
|
|
#define STRLEN 256
|
2016-03-12 01:29:14 +05:30
|
|
|
|
2019-02-11 23:16:14 +05:30
|
|
|
|
2020-11-14 19:06:10 +05:30
|
|
|
int setcwd_c(const char *cwd){
|
|
|
|
return chdir(cwd);
|
2016-09-20 10:38:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-14 19:06:10 +05:30
|
|
|
void getcwd_c(char cwd[], int *stat ){
|
|
|
|
char cwd_tmp[PATHLEN+1];
|
|
|
|
if(getcwd(cwd_tmp, sizeof(cwd_tmp))){
|
|
|
|
strcpy(cwd,cwd_tmp); // getcwd guarantees a NULL-terminated string
|
2016-03-12 01:29:14 +05:30
|
|
|
*stat = 0;
|
2016-05-05 18:41:28 +05:30
|
|
|
}
|
2016-03-12 01:29:14 +05:30
|
|
|
else{
|
|
|
|
*stat = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 10:38:31 +05:30
|
|
|
|
2018-05-26 02:52:32 +05:30
|
|
|
void gethostname_c(char hostname[], int *stat){
|
2020-11-14 19:06:10 +05:30
|
|
|
char hostname_tmp[STRLEN];
|
2016-09-20 10:38:31 +05:30
|
|
|
if(gethostname(hostname_tmp, sizeof(hostname_tmp)) == 0){
|
2020-11-14 19:06:10 +05:30
|
|
|
strncpy(hostname,hostname_tmp,sizeof(hostname_tmp)+1); // gethostname does not guarantee a NULL-terminated string
|
2016-09-20 10:38:31 +05:30
|
|
|
*stat = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
*stat = 1;
|
|
|
|
}
|
2016-03-12 01:29:14 +05:30
|
|
|
}
|
2018-05-26 02:52:32 +05:30
|
|
|
|
|
|
|
|
2020-11-14 19:06:10 +05:30
|
|
|
void getusername_c(char username[], int *stat){
|
2021-01-10 14:47:16 +05:30
|
|
|
struct passwd *pw = getpwuid(getuid());
|
2020-11-14 19:06:10 +05:30
|
|
|
if(pw && strlen(pw->pw_name) <= STRLEN){
|
|
|
|
strncpy(username,pw->pw_name,STRLEN+1);
|
|
|
|
*stat = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
*stat = 1;
|
|
|
|
}
|
2018-05-26 02:52:32 +05:30
|
|
|
}
|
2019-02-11 23:16:14 +05:30
|
|
|
|
2020-11-14 19:06:10 +05:30
|
|
|
|
2019-03-24 16:29:00 +05:30
|
|
|
void signalterm_c(void (*handler)(int)){
|
|
|
|
signal(SIGTERM, handler);
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:16:14 +05:30
|
|
|
void signalusr1_c(void (*handler)(int)){
|
|
|
|
signal(SIGUSR1, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void signalusr2_c(void (*handler)(int)){
|
|
|
|
signal(SIGUSR2, handler);
|
2019-03-24 16:29:00 +05:30
|
|
|
}
|
2020-09-06 21:19:00 +05:30
|
|
|
|
2020-11-14 19:06:10 +05:30
|
|
|
|
2020-09-06 21:19:00 +05:30
|
|
|
void inflate_c(const uLong *s_deflated, const uLong *s_inflated, const Byte deflated[], Byte inflated[]){
|
|
|
|
/* make writable copy, uncompress will write to it */
|
2020-09-28 02:12:40 +05:30
|
|
|
uLong s_inflated_,i;
|
2020-09-06 21:19:00 +05:30
|
|
|
s_inflated_ = *s_inflated;
|
|
|
|
|
|
|
|
if(uncompress((Bytef *)inflated, &s_inflated_, (Bytef *)deflated, *s_deflated) == Z_OK)
|
|
|
|
return;
|
|
|
|
else{
|
2020-09-28 02:12:40 +05:30
|
|
|
for(i=0;i<*s_inflated;i++){
|
2020-09-06 21:19:00 +05:30
|
|
|
inflated[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 01:44:04 +05:30
|
|
|
|
|
|
|
#ifdef FYAML
|
|
|
|
void to_flow_c(char **flow, int* length_flow, const char *mixed){
|
|
|
|
struct fy_document *fyd = NULL;
|
2022-04-16 19:54:29 +05:30
|
|
|
enum fy_emitter_cfg_flags emit_flags = FYECF_MODE_FLOW_ONELINE | FYECF_STRIP_LABELS | FYECF_STRIP_TAGS |FYECF_STRIP_DOC;
|
2022-04-13 01:44:04 +05:30
|
|
|
|
|
|
|
fyd = fy_document_build_from_string(NULL, mixed, -1);
|
|
|
|
if (!fyd) {
|
|
|
|
*length_flow = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int err = fy_document_resolve(fyd);
|
|
|
|
if (err) {
|
|
|
|
*length_flow = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*flow = fy_emit_document_to_string(fyd,emit_flags);
|
|
|
|
*length_flow = strlen(*flow);
|
|
|
|
|
|
|
|
fy_document_destroy(fyd);
|
|
|
|
}
|
|
|
|
#endif
|