int main(int argc, char** argv) { my_log_init(MESSAGE, "logs.txt"); if(argc < 2) { my_log(ERROR, "Input file name hasn't been found."); return 0; } char file_name[256] = {0}; strcpy(file_name, argv[1]); char* file_type = NULL; char dot = '.'; file_type = strtok(argv[1], &dot); if(strcmp(file_type, file_name)) { file_type = strtok(NULL, &dot); } int rezult = 0; if(strcmp(file_type, "mgz")) { rezult = my_compress(file_name); my_log(MESSAGE, "File %s was compressed with result: %s", file_name, rezult == 0 ? "failed" : "success"); } else { rezult = my_decompress(file_name); my_log(MESSAGE, "File %s was decompressed with result: %s", file_name, rezult == 0 ? "failed" : "success"); } my_log_end(); return 0; }
int main(int argc, char *argv[]) { int opt_c; char buf[FILENAME_MAX]; int rc = 1; me = strrchr(argv[0], '/'); if (me == NULL ) { me = argv[0]; } else { me++; } my_log_init(me); my_conf = my_conf_create(); if (!my_conf) { goto _MY_ERR_conf_create; } while ((opt_c = getopt(argc, argv, "C:L:Vh?")) != -1) switch (opt_c) { case 'C': my_conf->cfg_file = optarg; break; case 'L': my_conf->log_file = optarg; break; case 'P': my_conf->pid_file = optarg; break; case 'V': my_show_version(); rc = 0; goto _MY_EXIT; case '?': case 'h': my_show_usage(); rc = 0; goto _MY_EXIT; default: MY_ERROR("unknow argument '-%c'", (int)opt_c); my_show_usage(); rc = 1; goto _MY_EXIT; } my_conf->log_level = -1; if (my_conf->cfg_file == NULL) { snprintf(buf, sizeof(buf), "%s/%s.conf", MY_CFG_DIR, me); my_conf->cfg_file = strdup(buf); } if (my_conf_parse(my_conf) != 0) { goto _MY_ERR_conf_parse; } if (my_conf->log_file == NULL) { #ifdef MY_DEBUGGING my_conf->log_file = "stderr"; #else my_conf->log_file = "syslog"; #endif } if (my_conf->log_level < 0) { #ifdef MY_DEBUGGING my_conf->log_level = MY_LOG_DEBUG; #else my_conf->log_level = MY_LOG_NOTICE; #endif } if (my_conf->pid_file == NULL) { snprintf(buf, sizeof(buf), "%s/%s.pid", MY_RUN_DIR, me); my_conf->pid_file = strdup(buf); } if (my_log_open(my_conf->log_file, my_conf->log_level) != 0) { goto _MY_ERR_log_open; } my_log(MY_LOG_NOTICE, "started"); my_core = my_core_create(); if (!my_core) { goto _MY_ERR_core_create; } if (my_core_init(my_core, my_conf) != 0) { goto _MY_ERR_core_init; } signal(SIGINT, my_sig_handler); signal(SIGTERM, my_sig_handler); #ifdef MY_DEBUGGING my_conf_dump(my_conf); my_core_dump(my_core); #endif my_core_loop(my_core); my_core_destroy(my_core); my_log(MY_LOG_NOTICE, "ended"); my_log_close(); return 0; _MY_ERR_core_init: my_core_destroy(my_core); _MY_ERR_core_create: my_log_close(); _MY_ERR_log_open: _MY_ERR_conf_parse: _MY_EXIT: my_conf_destroy(my_conf); _MY_ERR_conf_create: return rc; }