void parse_options(int argc, char **argv) { int next_opt = 0, opt_index = 0; char *mitm_tok = NULL; char *dup = NULL; char *saveptr = NULL; /* Option ARGV elements */ while ((next_opt = getopt_long(argc, argv, short_options, long_options, &opt_index)) != -1) { switch (next_opt) { case 0: if (strcmp(long_options[opt_index].name, "no-promisc") == 0) gbls->promisc = 0; else if (strcmp(long_options[opt_index].name, "show-scripts") == 0) bug(__func__, "--show-scripts option not implemented yet"); else if (strcmp(long_options[opt_index].name, "debug-mode") == 0) gbls->script_debug_mode = 1; else if (strcmp(long_options[opt_index].name, "rfmon") == 0) gbls->rfmon = 1; else if (strcmp(long_options[opt_index].name, "no-scan") == 0) gbls->scan = 0; else if (strcmp(long_options[opt_index].name, "selib-dir") == 0) { char *dir = realpath ((char *)optarg, NULL); if (dir == NULL) warning("invalid selib directory: %s", optarg); gbls->selib_dir = str_concat (dir, "/", NULL); free (dir); } else if (strcmp(long_options[opt_index].name, "scripts-dir") == 0) { char *dir = realpath ((char *)optarg, NULL); if (dir == NULL) warning("invalid scripts directory: %s", optarg); gbls->scripts_dir = str_concat (dir, "/", NULL); free (dir); } else if (strcmp(long_options[opt_index].name, "cap-timeout") == 0) gbls->cap_timeout = atoi(optarg); else if (strcmp(long_options[opt_index].name, "cap-snaplen") == 0) gbls->snaplen = atoi(optarg); else fatal(__func__, "'%s' is an invalid option", long_options[opt_index].name); break; case 'h': print_usage(0); break; case 'v': print_version(); break; case 'i': gbls->iface = (char *)optarg; break; case 'M': dup = strdup(optarg); /* Get MiTM type attack */ mitm_tok = strtok_r(dup, ":", &saveptr); if (mitm_tok == NULL || !is_valid_mitm_attack(mitm_tok)) { free(dup); fatal(__func__, "invalid MiTM attack"); } gbls->mitm = strdup(mitm_tok); /* Get options */ mitm_tok = strchr(optarg, ':'); if (mitm_tok == NULL) { free(dup); fatal(__func__, "you must specify the required options for the MiTM attack"); } gbls->mitm_options = (mitm_tok + 1); free(dup); break; default: print_usage(EXIT_FAILURE); break; } } /* Non option ARGV elements */ if(optind < argc) { /* Get script */ char *script = (char *)argv[optind++]; if(strstr(script, ".lua") != NULL) { /* Manual path */ char *path = realpath (script, NULL); if (path != NULL) { gbls->script = path; } else { fatal(__func__, "invalid script: %s", script); } } else { /* Only script name */ gbls->script = append_script_dir(script); } if (strlen(gbls->script) > MAX_SCRIPT_NAME) fatal(__func__, "the script name is too long, max %d character", MAX_SCRIPT_NAME); opt_index = 0; /* Get possible script args */ while (opt_index < MAX_SCRIPT_ARGS && optind < argc) gbls->script_argv[opt_index++] = (char *)argv[optind++]; gbls->script_argc = opt_index; } }
static void parse_file_list(FileData *file_data, int num_paths, const char *lsfile, const char *path_dir, AccessMethod method, int host) { FileState *file_state = NULL; FILE *fp; char *path_file; char *temp; char *text; char *name; time_t time; off_t size; mode_t mode; int type; char *ssh_command; int num_files = 0; fp = fopen(lsfile, "r"); if (fp == NULL) { perror("fopen"); fprintf(stderr, _("Cannot open file `%s'.\n"), lsfile); fatal_error(host); } while ((text = str_fgets(fp)) != NULL) { if (strncmp(text, "total", 5) == 0) { /* skip */ free(text); continue; } if (strncmp(text, "/bin/ls:", 8) == 0) { fprintf(stderr, _("Listing remote directory contents failed: %s\nThe directory doesn't exist or permission denied.\n"), path_dir); fatal_error(host); } switch (method) { case FTP: ftp_extract_file_status(text, &name, &size, &mode, &type, host); break; case SCP: case RSYNC: ssh_extract_file_status(text, &name, &time, &size, &mode, &type, host); break; default: internal_error(__FILE__, __LINE__); } free(text); if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { free(name); continue; } path_file = str_concat(path_dir, "/", name, NULL); if (type == 'l') { /* Check whether the symbolic link is directory or not. */ switch (method) { case FTP: type = ftp_chdir(path_file) ? 'd' : '-'; break; case SCP: case RSYNC: ssh_command = str_dup_printf("ssh %s@%s cd %s", config.login_name[host], config.host_name[host], path_file); type = (system(ssh_command) == 0) ? 'd' : '-'; free(ssh_command); break; default: internal_error(__FILE__, __LINE__); } } if ((type == 'd' && is_match_dir(path_file, config.ignore_remote_dir[host], host)) || (type != 'd' && (is_match_file(path_file, path_dir, config.ignore_remote_file[host], host) || is_match_file(path_file, path_dir, config.ignore_file[host], host)))) { free(path_file); free(name); continue; } if (config.conv_to_lower[host]) { temp = str_tolower(name); free(name); name = temp; free(path_file); path_file = str_concat(path_dir, "/", name, NULL); } if (method == FTP) { if (type != 'd') { time = ftp_get_last_modification_time(path_file, host); } else { time = 0; } } file_state = str_realloc(file_state, (num_files + 1) * sizeof(FileState)); file_state[num_files].name = name; file_state[num_files].time = time; file_state[num_files].size = size; file_state[num_files].mode = mode; file_state[num_files].isdir = (type == 'd'); file_state[num_files].isremoved = FALSE; num_files++; free(path_file); } if (fclose(fp) != 0) { perror("fclose"); fprintf(stderr, _("Cannot close file `%s'.\n"), lsfile); fatal_error(host); } sort_file_state(file_state, num_files); file_data->path_state[num_paths].num_files = num_files; file_data->path_state[num_paths].file_state = file_state; }
static void absolutize_path(int config_num, int host) { int i; char *path_parameter[] = { "IgnoreLocalDir", "IgnoreRemoteDir", "IgnoreLocalFile", "IgnoreRemoteFile", "KeepLocalDir", "KeepRemoteDir", "KeepLocalFile", "KeepRemoteFile", "AsciiLocalDir", "AsciiRemoteDir", "AsciiLocalFile", "AsciiRemoteFile", "PreservePermissionLocalDir", "PreservePermissionRemoteDir", "PreservePermissionLocalFile", "PreservePermissionRemoteFile", "ChangePermissionLocalDir", "ChangePermissionRemoteDir", "ChangePermissionLocalFile", "ChangePermissionRemoteFile", }; cfgList *l; char *temp; char *base_dir; for (i = 0; i < sizeof(path_parameter) / sizeof(path_parameter[0]); i++) { if (strcmp(path_parameter[i], config_table[config_num].parameterName) == 0) { if (strstr(path_parameter[i], "Local") != NULL) { base_dir = config.local_top_dir[host]; } else if (strstr(path_parameter[i], "Remote") != NULL) { base_dir = config.remote_top_dir[host]; } else { internal_error(__FILE__, __LINE__); } if (base_dir == NULL) { return; } for (l = *(*(cfgList ***) (config_table[config_num].value) + host); l != NULL; l = l->next) { if (strcmp(path_parameter[i] + strlen(path_parameter[i]) - 4, "File") == 0) { /* file */ if (strchr(l->str, '/') == NULL) { /* not contain directory */ continue; } } else if (strcmp(path_parameter[i] + strlen(path_parameter[i]) - 3, "Dir") != 0) { internal_error(__FILE__, __LINE__); } if (*(l->str) != '/') { /* relative path */ temp = l->str; if (strncmp(temp, "./", 2) == 0) { /* begin with "./" */ l->str = str_concat(base_dir, "/", temp + 2, NULL); } else { l->str = str_concat(base_dir, "/", temp, NULL); } free(temp); } } return; } } }
static void set_default(max_hosts) { int i, j; int default_section; char *string_default; int integer_default; bool boolean_default; default_section = cfgSectionNameToNumber("default"); if (default_section == -1) { fprintf(stderr, _("Section `default' is required.\n")); exit(1); } for (i = 0; i < max_hosts; i++) { for (j = 0; config_table[j].type != CFG_END; j++) { switch (config_table[j].type) { case CFG_STRING: if (*(*(char ***) (config_table[j].value) + i) == NULL) { /* The section doesn't have the parameter. */ if (*(*(char ***) (config_table[j].value) + default_section) == NULL) { /* Section `default' doesn't have the parameter. */ string_default = get_string_internal_default(config_table[j].parameterName, i); if (string_default != NULL) { /* If weex has internal default value, use it. */ *(*(char ***) (config_table[j].value) + i) = string_default; } } else { /* Section `default' has the parameter. */ if (strcmp(config_table[j].parameterName, "CacheFile") == 0) { /* Add section name to the value, if parameter is `CacheFile'. */ *(*(char ***) (config_table[j].value) + i) = str_concat(*(*(char ***) (config_table[j].value) + default_section), ".", cfgSectionNumberToName(i), NULL); } else { *(*(char ***) (config_table[j].value) + i) = str_dup(*(*(char ***) (config_table[j].value) + default_section)); } } } break; case CFG_INT: if (*(*(int **) (config_table[j].value) + i) == -1) { /* The section doesn't have the parameter. */ if (*(*(int **) (config_table[j].value) + default_section) == -1) { /* Section `default' doesn't have the parameter. */ integer_default = get_integer_internal_default(config_table[j].parameterName); if (integer_default != -1) { /* If weex has internal default value, use it. */ *(*(int **) (config_table[j].value) + i) = integer_default; } } else { /* Section `default' has the parameter. */ *(*(int **) (config_table[j].value) + i) = *(*(int **) (config_table[j].value) + default_section); } } break; case CFG_BOOL: if (*(*(bool **) (config_table[j].value) + i) == -1) { /* The section doesn't have the parameter. */ if (*(*(bool **) (config_table[j].value) + default_section) == -1) { /* Section `default' doesn't have the parameter. */ boolean_default = get_boolean_internal_default(config_table[j].parameterName); if (boolean_default != -1) { /* If weex has internal default value, use it. */ *(*(bool **) (config_table[j].value) + i) = boolean_default; } } else { /* Section `default' has the parameter. */ *(*(bool **) (config_table[j].value) + i) = *(*(bool **) (config_table[j].value) + default_section); } } break; case CFG_STRING_LIST: if (*(*(cfgList ***) (config_table[j].value) + i) == NULL) { /* The section doesn't have the parameter. */ if (*(*(cfgList ***) (config_table[j].value) + default_section) != NULL) { /* Section `default' have the parameter. */ *(*(cfgList ***) (config_table[j].value) + i) = copy_cfg_list(*(*(cfgList ***) (config_table[j].value) + default_section)); } } break; default: internal_error(__FILE__, __LINE__); } remove_trailing_slash(j, i); absolutize_path(j, i); } } }
int main(int argc, char* argv[]) { // Compute full real program path from argv[0] char programpath[MAX_LINE]; realpath(argv[0], programpath); size_t pathend; for(pathend = strlen(programpath); programpath[pathend]!= '/'; pathend--) ; programpath[++pathend] = '\0'; // Check valid argument usage if(argc != 2) { // Invalid usage printf("Usage: %s <folder path>\n", argv[0]); exit(1); } // Open directory specified in argv[1] DIR *dirp; struct dirent *direntp; struct stat stat_entry; if ((dirp = opendir(argv[1])) == NULL) { // Failed to open directory perror(argv[1]); exit(1); } // Add / to end of folder path if it hasn't char* fldr; if(argv[1][strlen(argv[1])-1] != '/') { fldr = str_concat(argv[1], "/"); } else { fldr = (char*) malloc(strlen(argv[1]) * sizeof(char)); strcpy(fldr, argv[1]); } // Check if words.txt exists char* words_path = str_concat(fldr, "words.txt"); int words = open(words_path, O_RDONLY, 0600); if(words < 0) { // words.txt file does not exist perror("words.txt"); exit(1); } close(words); free(words_path); // Check if index.txt exists char* index_path = str_concat(fldr, "index.txt"); int index_tmp = open(index_path, O_RDONLY, 0600); if(index_tmp >= 0) { // index.txt exists, delete it close(index_tmp); unlink(index_path); } free(index_path); // Spawn child processes for each file to process pid_t pid = 0; int children = 0; while ((direntp = readdir(dirp)) != NULL) { // Read directory item info char* file_path = str_concat(fldr, direntp->d_name); if (stat(file_path, &stat_entry) == -1) { perror("stat error"); closedir(dirp); exit(1); } free(file_path); // Check if it's a regular file if (S_ISREG(stat_entry.st_mode)) { // Check if it's not words.txt or some other file not to process if(strcmp(direntp->d_name, "words.txt") != 0 && direntp->d_name[strlen(direntp->d_name) - 1] != '~') { if((pid = fork()) < 0) { // Fork error perror("fork failed"); exit(1); } else if(pid == 0) { // Child process, use sw program on the file strcat(programpath, "sw"); if(chdir(argv[1]) < 0) { perror(argv[1]); exit(1); } execlp(programpath, "sw" , direntp->d_name, NULL); perror("exec sw failed"); exit(1); } else { // Parent, increase number of childs children++; } } } } // Wait for all childs to finish running sw while(children > 0) { int ret; int wpid = wait(&ret); // wait() returned an error if(wpid == -1) perror("wait"); else if(ret != 0) // Child return code is an error printf("sw child (PID=%d) terminated with error code %d.\n", wpid, ret); children--; } // Create new child for csc if((pid = fork()) < 0) { perror("fork failed"); exit(1); } if(pid == 0) { // Child process, use csc program to process the folder strcat(programpath, "csc"); execlp(programpath, "csc" , argv[1], NULL); perror("exec csc failed"); exit(1); } else { // Parent, wait for child process to finish int ret; int wpid = wait(&ret); // wait() returned an error if(wpid == -1) perror("wait"); else if(ret != 0) // Child return code is an error printf("csc child (PID=%d) terminated with error code %d.\n", wpid, ret); } // Go back to the beginning of the directory rewinddir(dirp); // Clean files created by sw while ((direntp = readdir( dirp)) != NULL) { char* file_path = str_concat(fldr, direntp->d_name); if (stat(file_path, &stat_entry) == -1) { perror("stat error"); closedir(dirp); exit(1); } if (S_ISREG(stat_entry.st_mode)) { // Files created by sw end in "_output.txt" if(strstr(direntp->d_name, "_output.txt") != NULL && direntp->d_name[strlen(direntp->d_name)-1] != '~') { unlink(file_path); } } free(file_path); } closedir(dirp); return 0; }
int main(int argc, char** argv) { int ret; setup_dir(); /* init globals */ gcc = sa_concat(gcc, "gcc"); /* replaced laterwards if c++ */ gcc = sa_concat(gcc, "-I."); src_lines = sa_concat(src_lines, "#define __LARGE_C__ " VERSION_INT_STR "\n" "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif\n" "#include <stdio.h>\n" "#include <stdlib.h>\n" "#ifdef __cplusplus\n" "}\n" "#include <iostream>\n" "using namespace std;\n" "#endif\n" "\n" "__LARGE_C_PREFIX__\n"); argv++; { /* parse args, determine cache dir */ char** new_argv = parse_args(argv, NULL, 0); for (; argv != new_argv; argv++) { add_spec(*argv, strlen(*argv) + 1); } if (! keep_files && (oneliner || *argv != NULL)) { struct stat st; if (oneliner) { build_store_dir(); } else if (stat(*argv, &st) == 0) { add_spec(*argv, strlen(*argv) + 1); add_spec(&st.st_size, sizeof(st.st_size)); add_spec(&st.st_mtime, sizeof(st.st_mtime)); build_store_dir(); } } } /* use cache if possible */ if (store_dir != NULL && check_specs()) { char** child_argv = NULL; #ifdef _WIN32 _utime(store_dir, NULL); /* update mtime of the directory */ #else utimes(store_dir, NULL); /* update mtime of the directory */ #endif exec_file = str_concat(str_dup(store_dir), "/"A_OUT); child_argv = sa_concat(child_argv, exec_file); #ifdef _WIN32 { int status; ret = spawn_w32(child_argv, &status); if (status == 0) exit(ret); } #else execv(exec_file, child_argv); #endif // if execv failed, we compile free(exec_file); remove_dir(store_dir); } /* prepare files */ make_temp_dir(); exec_file = str_concat(str_dup(temp_dir), "/"A_OUT); c_file = str_concat(str_dup(temp_dir), "/source.c"); if ((src_fp = fopen(c_file, "wt")) == NULL) { cmd_error("failed to create temporary file: %s : %s\n", c_file, strerror(errno)); } while (src_lines != NULL && *src_lines != NULL) { fputs(*src_lines++, src_fp); } /* write source with adjustments */ if (! oneliner) { FILE* fp; char* file; char* line; int line_no = 0; if (argv[0] == NULL) { fp = stdin; file = "stdin"; } else if (strcmp(argv[0], "-") == 0) { fp = stdin; argv++; file = "stdin"; } else { file = *argv++; if ((fp = fopen(file, "rt")) == NULL) { cmd_error("cannot open file: %s : %s\n", file, strerror(errno)); } fprintf(src_fp, "# 1 \"%s\" 1\n", file); } while ((line = get_line(fp)) != NULL) { int comment_out = 0; line_no++; if (line_no == 1 && strncmp(line, "#!", 2) == 0) { comment_out = 1; } else if (line[0] == '#') { char* buf = str_dup(line + 1); char** tokens = split_tokens(buf); if (*tokens != NULL) { if (strcmp(tokens[0], "option") == 0) { parse_args(tokens + 1, file, line_no); comment_out = 1; } } free(buf); free(tokens); } if (comment_out == 1) { fprintf(src_fp, "// "); } fputs(line, src_fp); } fputs("\n", src_fp); if (fp != stdin) { fclose(fp); } } /* close source file */ fputs("__LARGE_C_SUFFIX__\n", src_fp); fclose(src_fp); src_fp = NULL; /* compile */ if (use_plusplus) { gcc[0] = "g++"; } if (use_main) { gcc = sa_concat(gcc, "-D__LARGE_C_PREFIX__="); gcc = sa_concat(gcc, "-D__LARGE_C_SUFFIX__="); } else { gcc = sa_concat(gcc, "-D__LARGE_C_PREFIX__=int main(int argc, char** argv) {"); gcc = sa_concat(gcc, "-D__LARGE_C_SUFFIX__=; return 0; }"); } gcc = sa_concat(gcc, "-o"); gcc = sa_concat(gcc, show_disassembly ? "-" : exec_file); gcc = sa_concat(gcc, c_file); gcc = sa_merge(gcc, lopts); if ((ret = call_proc(gcc, "could not execute compiler")) != 0) { cleanup(); exit(ret); } if (show_disassembly) { cleanup(); exit(0); } { /* execute */ char** child_argv = NULL; if (use_debugger) { child_argv = sa_concat(child_argv, "gdb"); } child_argv = sa_concat(child_argv, exec_file); child_argv = sa_merge(child_argv, argv); ret = call_proc(child_argv, "could not spawn child process"); } /* move temp_dir to store_dir, if possible. * or, remove work_dir */ if (store_dir == NULL) { cleanup(); } else { save_specs(); update_cache(); if (rename(temp_dir, store_dir) != 0) { cleanup(); } } return ret; }
/**************************************************************** * TclSetNode: ****************************************************************/ int TclSetNode() { int nid; int status; int log; int usageMask; void *ctx = 0; char *nodename; static DYNAMIC_DESCRIPTOR(dsc_nodename); static DYNAMIC_DESCRIPTOR(dsc_status); cli_get_value("NODENAME",&dsc_nodename); cli_get_value("STATUS",&dsc_status); nodename = dsc_nodename.dscA_pointer; l2u(nodename,0); log = cli_present("LOG") & 1; usageMask = -1; while ((status = TreeFindNodeWild(nodename,&nid,&ctx,usageMask)) & 1) { if (dsc_status.dscA_pointer) { int statval = atoi(dsc_status.dscA_pointer); NCI_ITM setnci[] = {{sizeof(int), NciSTATUS, 0, 0},{0,NciEND_OF_LIST,0,0}}; setnci[0].pointer = (unsigned char *)&statval; TreeSetNci(nid,setnci); } switch (cli_present("SUBTREE")) { case CLI_STS_PRESENT: status = TreeSetSubtree(nid); break; case CLI_STS_NEGATED: status = TreeSetNoSubtree(nid); break; } if (!(status & 1)) goto error; if (cli_present("ON") & 1) { status = TreeTurnOn(nid); if (status & 1) TclNodeTouched(nid,on_off); else TclErrorOut(status); } else if (cli_present("OFF") & 1) { status = TreeTurnOff(nid); if (status & 1) TclNodeTouched(nid,on_off); else TclErrorOut(status); } if (!(status & 1)) goto error; { int set_flags; int clear_flags; DYNAMIC_DESCRIPTOR(dsc_path); NCI_ITM get_itmlst[] = { {0,NciPATH,(unsigned char *) &dsc_path,0}, {0,NciEND_OF_LIST}}; NCI_ITM set_itmlst[] = { {0,NciSET_FLAGS,(unsigned char *) &set_flags,0}, {0,NciEND_OF_LIST}}; NCI_ITM clear_itmlst[] = { {0,NciCLEAR_FLAGS,(unsigned char *) &clear_flags,0}, {0,NciEND_OF_LIST}}; set_flags = 0; clear_flags = 0; switch (cli_present("WRITE_ONCE")) { case CLI_STS_PRESENT: set_flags |= NciM_WRITE_ONCE; break; case CLI_STS_NEGATED: clear_flags |= NciM_WRITE_ONCE; break; } switch (cli_present("CACHED")) { case CLI_STS_PRESENT: set_flags |= NciM_CACHED; break; case CLI_STS_NEGATED: clear_flags |= NciM_CACHED; break; } switch (cli_present("COMPRESS_ON_PUT")) { case CLI_STS_PRESENT: set_flags |= NciM_COMPRESS_ON_PUT; break; case CLI_STS_NEGATED: clear_flags |= NciM_COMPRESS_ON_PUT; break; } switch (cli_present("COMPRESS_SEGMENTS")) { case CLI_STS_PRESENT: set_flags |= NciM_COMPRESS_SEGMENTS; break; case CLI_STS_NEGATED: clear_flags |= NciM_COMPRESS_SEGMENTS; break; } switch (cli_present("DO_NOT_COMPRESS")) { case CLI_STS_PRESENT: set_flags |= NciM_DO_NOT_COMPRESS; break; case CLI_STS_NEGATED: clear_flags |= NciM_DO_NOT_COMPRESS; break; } switch (cli_present("SHOT_WRITE")) { case CLI_STS_PRESENT: clear_flags |= NciM_NO_WRITE_SHOT; break; case CLI_STS_NEGATED: set_flags |= NciM_NO_WRITE_SHOT; break; } switch (cli_present("MODEL_WRITE")) { case CLI_STS_PRESENT: clear_flags |= NciM_NO_WRITE_MODEL; break; case CLI_STS_NEGATED: set_flags |= NciM_NO_WRITE_MODEL; break; } switch (cli_present("INCLUDED")) { case CLI_STS_PRESENT: set_flags |= NciM_INCLUDE_IN_PULSE; break; case CLI_STS_NEGATED: clear_flags |= NciM_INCLUDE_IN_PULSE; break; } switch (cli_present("ESSENTIAL")) { case CLI_STS_PRESENT: set_flags |= NciM_ESSENTIAL; break; case CLI_STS_NEGATED: clear_flags |= NciM_ESSENTIAL; break; } if (set_flags) status = TreeSetNci(nid,set_itmlst); if (clear_flags) status = TreeSetNci(nid,clear_itmlst); if (status & 1) { if (log) { TreeGetNci(nid,get_itmlst); str_concat(&dsc_path,"Node: ",&dsc_path," modified",0); TclTextOut(dsc_path.dscA_pointer); str_free1_dx(&dsc_path); } } else goto error; } } TreeFindNodeEnd(&ctx); if (status == TreeNNF) goto error; return 1; error: #ifdef vms lib$signal(status,0); #else MdsMsg(status,0); #endif return status; }