void ft_env(char **command, t_env *e) { t_opt opt; char **tmp; int i; ft_memset(&opt, 0, sizeof(opt)); e->ret = 0; if ((opt.env = ft_tabdup(e->env)) == NULL) return (ft_error("malloc failed", &opt, e)); if (!command[1]) ft_puttab(opt.env); else { if ((i = check(command, &opt)) < 0) return (ft_error(NULL, &opt, e)); if (command[i]) { tmp = e->env; e->env = opt.env; check_and_exec(&command[i], e); e->env = tmp; } else ft_puttab(opt.env); } free_opt(&opt); }
static void ft_error(char *err, t_opt *opt, t_env *e) { e->ret = 1; if (err) { ft_putstr_fd("env: ", 2); ft_putstr_fd(err, 2); ft_putchar_fd('\n', 2); } free_opt(opt); }
int main(int argc, char **argv) { int ret, opt_index, i, len; struct optstruct *opt; const char *getopt_parameters = "hvd:wriVl:m"; static struct option long_options[] = { /* * WARNING: For compatibility reasons options marked as "not used" * must still be accepted ! */ {"help", 0, 0, 'h'}, /* clamscan + clamdscan */ {"quiet", 0, 0, 0}, /* clamscan + clamdscan */ {"stdout", 0, 0, 0}, /* clamscan + clamdscan */ {"verbose", 0, 0, 'v'}, /* clamscan + clamdscan */ {"debug", 0, 0, 0}, {"version", 0, 0, 'V'}, /* clamscan + clamdscan */ {"tempdir", 1, 0, 0}, {"leave-temps", 0, 0, 0}, {"config-file", 1, 0, 0}, /* clamdscan */ {"database", 1, 0, 'd'}, {"whole-file", 0, 0, 'w'}, /* not used */ {"force", 0, 0, 0}, {"recursive", 0, 0, 'r'}, {"bell", 0, 0, 0}, {"disable-summary", 0, 0, 0}, /* obsolete */ {"no-summary", 0, 0, 0}, {"infected", 0, 0, 'i'}, {"log", 1, 0, 'l'}, {"log-verbose", 0, 0, 0}, /* not used */ {"threads", 1, 0, 0}, /* not used */ {"one-virus", 0, 0, 0}, /* not used */ {"move", 1, 0, 0}, {"remove", 0, 0, 0}, {"exclude", 1, 0, 0}, {"exclude-dir", 1, 0, 0}, {"include", 1, 0, 0}, {"include-dir", 1, 0, 0}, {"max-files", 1, 0, 0}, {"max-space", 1, 0, 0}, {"max-ratio", 1, 0, 0}, {"max-recursion", 1, 0, 0}, {"max-dir-recursion", 1, 0, 0}, {"disable-archive", 0, 0, 0}, {"no-archive", 0, 0, 0}, {"detect-broken", 0, 0, 0}, {"block-encrypted", 0, 0, 0}, {"block-max", 0, 0, 0}, {"no-pe", 0, 0, 0}, {"no-ole2", 0, 0, 0}, {"no-html", 0, 0, 0}, {"mbox", 0, 0, 'm'}, /* not used */ {"no-mail", 0, 0, 0}, {"mail-follow-urls", 0, 0, 0}, {"unzip", 2, 0, 0}, {"unrar", 2, 0, 0}, {"unace", 2, 0, 0}, /* not used */ {"unarj", 2, 0, 0}, /* not used */ {"arj", 2, 0, 0}, {"zoo", 2, 0, 0}, /* not used */ {"unzoo", 2, 0, 0}, {"lha", 2, 0, 0}, {"jar", 2, 0, 0}, {"tar", 2, 0, 0}, {"tgz", 2, 0, 0}, {"deb", 2, 0, 0}, {0, 0, 0, 0} }; opt=(struct optstruct*) mcalloc(1, sizeof(struct optstruct)); opt->optlist = NULL; if(strstr(argv[0], "clamdscan")) clamdscan_mode = 1; while(1) { opt_index=0; ret=getopt_long(argc, argv, getopt_parameters, long_options, &opt_index); if (ret == -1) break; switch (ret) { case 0: register_long_option(opt, long_options[opt_index].name); break; default: if(strchr(getopt_parameters, ret)) { if(opt_index) register_char_option(opt, ret, long_options[opt_index].name); else register_char_option(opt, ret, NULL); } else { mprintf("!Unknown option passed.\n"); free_opt(opt); if(clamdscan_mode) exit(2); else exit(40); } } } if (optind < argc) { len=0; /* count length of non-option arguments */ for(i=optind; i<argc; i++) len+=strlen(argv[i]); len=len+argc-optind-1; /* add spaces between arguments */ opt->filename=(char*)mcalloc(len + 256, sizeof(char)); for(i=optind; i<argc; i++) { strncat(opt->filename, argv[i], strlen(argv[i])); if(i != argc-1) strncat(opt->filename, "\t", 1); } } ret = clamscan(opt); free_opt(opt); return ret; }
static Options *parse_opt(int argc, char *const argv[]) { Options *opts = malloc(sizeof(Options)); opts->address = strdup(DEFAULT_NET_ADDRESS); opts->port = DEFAULT_NET_PORT; opts->dryrun = 0; opts->loglevel = DEFAULT_LOG_LEVEL; opts->syslog = 0; opts->logfile = NULL; opts->background = 0; opts->pidfile = NULL; opts->usbbus = -1; opts->usbdev = -1; int opt; opterr = 0; while ((opt = getopt(argc, argv, "d:l:p:nsf:br:u:")) != -1) { switch (opt) { case 'd': if (strcmp(optarg, "fatal") == 0) { opts->loglevel = LOG_LEVEL_FATAL; } else if (strcmp(optarg, "error") == 0) { opts->loglevel = LOG_LEVEL_ERROR; } else if (strcmp(optarg, "warn") == 0) { opts->loglevel = LOG_LEVEL_WARN; } else if (strcmp(optarg, "info") == 0) { opts->loglevel = LOG_LEVEL_INFO; } else if (strcmp(optarg, "debug") == 0) { opts->loglevel = LOG_LEVEL_DEBUG; } else { free_opt(opts); return NULL; } break; case 'l': free(opts->address); opts->address = strdup(optarg); break; case 'p': opts->port = (unsigned short) (strtol(optarg, NULL, 0) & 0xffff); break; case 'n': opts->dryrun = 1; break; #ifdef HAVE_VSYSLOG case 's': opts->syslog = 1; break; #endif case 'f': opts->logfile = strdup(optarg); break; case 'b': opts->background = 1; if (!opts->pidfile) { opts->pidfile = strdup(DEFAULT_PID_FILE); } break; case 'r': free(opts->pidfile); opts->pidfile = strdup(optarg); break; case 'u': if (!split_usbdev(optarg, &opts->usbbus, &opts->usbdev)) { free_opt(opts); return NULL; } break; default: free_opt(opts); return NULL; } } return opts; }
int main(int argc, char *const argv[]) { int error = 0; log_debug("Parsing options"); Options *opts = parse_opt(argc, argv); if (!opts) { show_banner(); show_help(); return -1; } log_set_level(opts->loglevel); if (opts->logfile) { log_set_logfile(opts->logfile); log_set_logfile_level(LOG_LEVEL_MAX); } #ifdef HAVE_VSYSLOG if (opts->syslog) { log_set_syslog("daliserver"); } #endif if (opts->background) { daemonize(opts->pidfile); } else { show_banner(); } log_info("Starting daliserver"); log_debug("Initializing dispatch queue"); DispatchPtr dispatch = dispatch_new(); if (!dispatch) { error = -1; } else { //dispatch_set_timeout(dispatch, 100); UsbDaliPtr usb = NULL; if (!opts->dryrun) { log_debug("Initializing USB connection"); usb = usbdali_open(NULL, dispatch, opts->usbbus, opts->usbdev); if (!usb) { error = -1; } } if (opts->dryrun || usb) { log_debug("Initializing server"); ServerPtr server = server_open(dispatch, opts->address, opts->port, DEFAULT_NET_FRAMESIZE, net_frame_handler, usb); if (!server) { error = -1; } else { server_set_connection_destroy_callback(server, net_dequeue_connection, usb); if (usb) { usbdali_set_outband_callback(usb, dali_outband_handler, server); usbdali_set_inband_callback(usb, dali_inband_handler); } log_debug("Creating shutdown notifier"); killsocket = ipc_new(); if (!killsocket) { error = -1; } else { ipc_register(killsocket, dispatch); log_info("Server ready, waiting for events"); running = 1; signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); while (running && dispatch_run(dispatch, usbdali_get_timeout(usb))); log_info("Shutting daliserver down"); ipc_free(killsocket); } server_close(server); } if (usb) { usbdali_close(usb); } } dispatch_free(dispatch); } free_opt(opts); log_info("Exiting"); return error; }