int my_setenv(char **tab, t_shell *sh) { int i; i = -1; sh->bol = 0; if (!exit_setenv(tab)) return (-1); if (!tab[1]) return (my_env(sh)); while (sh->env[++i]) { if (!strncmp(sh->env[i], tab[1], strlen(tab[1])) && sh->env[i][strlen(tab[1])] == '=') { free(sh->env[i]); sh->env[i] = concat_str(tab[1], tab[2], '='); sh->bol = 1; } } if (!sh->bol) { sh->env = my_realloc(sh->env, ((tab_len(sh->env) + 2) * sizeof(char *))); sh->env[i] = concat_str(tab[1], tab[2], '='); sh->env[i + 1] = NULL; } return (0); }
/* Query MySQL/MariaDB WS_REP Status */ char* mysql_status(dictionary *conf, FILE *log) { MYSQL *curs = mysql_init(NULL); MYSQL_ROW row; char *entry = NULL; if (!curs) { entry = concat_str("ERROR - Could not create MySQL cursor: ", mysql_error(curs), NULL); put_log(log, entry); free(entry); return("0"); } if (mysql_real_connect(curs, get_config(conf, "mysql:host"), get_config(conf, "mysql:user"), get_config(conf, "mysql:pass"), "mysql", 0, NULL, 0) == NULL) { entry = concat_str("ERROR - Could not connect to MySQL server: ", mysql_error(curs), NULL); put_log(log, entry); free(entry); mysql_close(curs); return("0"); } if (mysql_query(curs, "SHOW STATUS LIKE 'wsrep_local_state'")) { entry = concat_str("ERROR - Could not execute query on ws_rep status: ", mysql_error(curs), NULL); put_log(log, entry); free(entry); mysql_close(curs); return("0"); } MYSQL_RES *result = mysql_store_result(curs); if (!result) { entry = concat_str("ERROR - Could not store MySQL result: ", mysql_error(curs), NULL); put_log(log, entry); free(entry); mysql_close(curs); return("0"); } int num_fields = mysql_num_fields(result); char *ws_rep_status = NULL; while ((row = mysql_fetch_row(result))) { for (int i=0; i < num_fields; i++) { ws_rep_status = (row[i]); } } mysql_free_result(result); mysql_close(curs); return ws_rep_status; }
char* parse_multiline_string(const char** input, JzonAllocator* allocator) { if (!is_multiline_string_quotes(*input)) return NULL; *input += 3; char* start = (char*)*input; char* result = ""; while (current(input)) { if (current(input) == '\n' || current(input) == '\r') { unsigned result_len = (unsigned)strlen(result); unsigned line_len = (unsigned)(*input - start); if (result_len > 0) { char* new_result = concat_str(allocator, result, result_len, "\n", 1); allocator->deallocate(result); result = new_result; ++result_len; } skip_whitespace(input); if (line_len != 0) { char* new_result = concat_str(allocator, result, result_len, start, line_len); if (result_len > 0) allocator->deallocate(result); result = new_result; } start = (char*)*input; } if (is_multiline_string_quotes(*input)) { unsigned result_len = (unsigned)strlen(result); char* new_result = concat_str(allocator, result, result_len, start, (unsigned)(*input - start)); allocator->deallocate(result); result = new_result; *input += 3; return result; } next(input); } allocator->deallocate(result); return NULL; }
int main(void) { // http://man7.org/linux/man-pages/man3/crypt.3.html char password[] = "Ab(d3"; char salt[] = "201802221723"; char result1[] = "20338EPKt5xtY"; char result2[] = "$1$Ab(d3$.SgUCw1AqIVAiXBaS6kzU."; char result3[] = "$5$Ab(d3$00nkgDrj2dM68IbqFTZPJ.a3mgai49mY.Ezq5ey0xY0"; char result4[] = "$6$Ab(d3$TB6UIPV7sprvpcQh2esPr2/ye4FTp9lLft8yAj.2x/HcTXPwzGDxdK/tIF10DdVdV9Z2hhc3MaosUBS3fdueZ1"; // 20338EPKt5xtY if (strcmp(result1, crypt(password, salt)) != 0) return -1; // $1$Ab(d3$.SgUCw1AqIVAiXBaS6kzU. if (strcmp(result2, crypt(password, concat_str((char*) "$1$", password))) != 0) return -2; // $5$Ab(d3$00nkgDrj2dM68IbqFTZPJ.a3mgai49mY.Ezq5ey0xY0 if (strcmp(result3, crypt(password, concat_str((char*) "$5$", password))) != 0) return -3; // $6$Ab(d3$TB6UIPV7sprvpcQh2esPr2/ye4FTp9lLft8yAj.2x/HcTXPwzGDxdK/tIF10DdVdV9Z2hhc3MaosUBS3fdueZ1 if (strcmp(result4, crypt(password, concat_str((char*) "$6$", password))) != 0) return -4; // encrypt // http://man7.org/linux/man-pages/man3/encrypt.3.html char key[64]; char buf[64]; char txt[6]; int i, j; for (i = 0; i < 6; i++) { for (j = 0; j < 8; j++) { buf[i * 8 + j] = password[i] >> j & 1; } } setkey(key); encrypt(buf, 0); for (i = 0; i < 6; i++) { for (j = 0, txt[i] = '\0'; j < 8; j++) { txt[i] |= buf[i * 8 + j] << j; } } if (strcmp(password, txt) == 0) return -5; encrypt(buf, 1); for (i = 0; i < 6; i++) { for (j = 0, txt[i] = '\0'; j < 8; j++) { txt[i] |= buf[i * 8 + j] << j; } } return (strcmp(password, txt) == 0) ? 0 : -6; }
int main(){ String s; char* str; init_string( &s ); copy_str( s, "Hello world..." ); print_string( s ); putchar( '\n' ); concat_str( s, " my name is Blur." ); print_string( s ); putchar( '\n' ); copy_str( s, "Hi" ); print_string( s ); putchar( '\n' ); printf( "Is it equal to hi? %d\n", compare_str( s, "hi" ) ); printf( "Is it equal to Hi? %d\n", compare_str( s, "Hi" ) ); printf( "Is it equal to hello? %d\n", compare_str( s, "hello" ) ); printf( "Is it equal to Hello? %d\n", compare_str( s, "Hello" ) ); str = to_c_str( s ); printf( "%s\n", str ); free( str ); clear_string( &s ); return 0; }
/* Parse Configuration File */ dictionary* load_conf(void) { char *path = concat_str(get_execdir(), "/conf/hawkd.ini", NULL); dictionary *file = iniparser_load(path); free(path); return file; }
static int test_path(char **tab, char **env) { int i; char *path_in_str; char **path_in_array; char *cmd; int a; if ((path_in_str = get_var_env(env, "PATH=")) == NULL) path_in_str = create_path(); path_in_array = my_str_to_wordtab_pattern(path_in_str, ":"); i = -1; while (path_in_array[++i]) { cmd = concat_str(path_in_array[i], tab[0], '/'); if ((a = execve(cmd, tab, env)) != -1) { free(cmd); free_tab(path_in_array); return (a); } free(cmd); } free_tab(path_in_array); return (cmd_not_found(tab)); }
int main() { concat( print , hello )(); fprintf(stdout, concat_str("hello", "world!\n")); fprintf(stdout, concat_str1(hello, world!\n)); LDEBUG("hello world: %s", "hmeng\n"); return 0; }
char mysh_cd(t_arg *targ, UNUSED char *path) { int i; if ((i = cd_tiret(targ)) == 1) return (1); if (i != 2 && !access(targ->wtab[1], F_OK)) { if (!access(targ->wtab[1], R_OK)) { if ((i = find_env(targ->env, "PWD=")) < 0) if ((targ->env = add_env(targ->env, "PWD", '=', "/")) == NULL) return (1); if ((targ->pwd = get_flagss(targ->env,"PWD")) == NULL || ((i = find_env(targ->env, "PWD=")) < 0)) return (1); free(targ->env[i]); if ((targ->env[i] = concat_str("PWD", '=', targ->wtab[1])) == NULL || chdir(targ->wtab[1]) < 0) return (1); } else write(2, "Error while accessing path, no rights.\n", 40); } else if (i != 2) write(2, "No such file or directory.\n", 28); return (0); }
char cd_home(t_arg *targ) { int i; char *str3; if (targ->wtab[1] == NULL) { if ((i = find_env(targ->env, "PWD=")) < 0) if ((targ->env = add_env(targ->env, "PWD", '=', "/")) == NULL) return (1); if ((i = find_env(targ->env, "HOME=")) < 0) if ((targ->env = add_env(targ->env, "HOME", '=', "/")) == NULL) return (1); if ((targ->pwd = get_flagss(targ->env, "PWD")) == NULL || (i = find_env(targ->env, "PWD=")) < 0) return (1); if ((str3 = get_flagss(targ->env, "HOME")) == NULL || (targ->env[i] = concat_str("PWD", '=', str3)) == NULL || (chdir(str3) < 0)) return (1); free(str3); return (2); } return (0); }
/* Logging */ FILE* open_logs(void) { FILE *log; errno = 0; char *path = concat_str(get_execdir(), "/log/hawkd.log", NULL); log = fopen("./log/hawkd.log", "a"); if(errno || (NULL == log)) { printf("%s", "FATAL - Failed to open main log file. Exiting..."); fflush(stdout); exit(1); } free(path); return log; }
char cd_tiret(t_arg *targ) { char *tmp; int i; if (targ->wtab[1] != NULL && !my_strncmp(targ->wtab[1], "-", 1) && targ->pwd != NULL) { tmp = targ->pwd; if ((i = find_env(targ->env, "PWD=")) < 0) if ((targ->env = add_env(targ->env, "PWD", '=', "/")) == NULL) return (1); if ((targ->pwd = get_flagss(targ->env, "PWD")) == NULL) return (1); free(targ->env[i]); if ((targ->env[i] = concat_str("PWD", '=', tmp)) == NULL || chdir(tmp) < 0) return (1); return (2); } return (cd_home(targ)); }
/* Initialize Socket */ int socket_init(dictionary *conf) { char *entry = NULL; //Setup socket related structures int listenfd = 0; struct sockaddr_in serv_addr; int flags = 0; char sendBuff[1025]; int port = atoi(get_config(conf, "hawk:port")); int backlog = atoi(get_config(conf, "hawk:total_clients")); //Configure socket listenfd = socket(AF_INET, SOCK_STREAM, 0); //Modify file descriptor for non-blocking socket flags = fcntl(listenfd, F_GETFL, 0); if (flags == -1) { printf("%s", "FATAL - Socket file descriptor could not be modified. Exiting... "); fflush(stdout); exit(1); } fcntl(listenfd, F_SETFL, flags | O_NONBLOCK); flags = fcntl(listenfd, F_GETFL, 0); if ((flags & O_NONBLOCK) != O_NONBLOCK) { printf("%s", "FATAL - Socket file descriptor can not be set to non-blocking. Exiting... "); fflush(stdout); exit(1); } if (listenfd < 0) { entry = concat_str("\n\n", "FATAL - Could not initiate socket: ", strerror(errno), "\n\n", NULL); printf("%s", entry); free(entry); fflush(stdout); exit(1); } memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(port); //Bind to socket if (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { entry = concat_str("\n\n", "FATAL - Could not bind to socket: ", strerror(errno), "\n\n", NULL); printf("%s", entry); free(entry); fflush(stdout); exit(1); } listen(listenfd, (backlog + 1)); return listenfd; }