int server(struct conf **confs, const char *conffile, struct lock *lock, int generate_ca_only) { enum serret ret=SERVER_ERROR; int rfds[LISTEN_SOCKETS]; // Sockets for clients to connect to. int sfds[LISTEN_SOCKETS]; // Status server sockets. //return champ_test(confs); init_fds(rfds); init_fds(sfds); if(ca_server_setup(confs)) goto error; if(generate_ca_only) { logp("The '-g' command line option was given. Exiting now.\n"); goto end; } if(get_int(confs[OPT_FORK]) && get_int(confs[OPT_DAEMON])) { if(daemonise() || relock(lock)) goto error; } ssl_load_globals(); while(!gentleshutdown) { if(run_server(confs, conffile, rfds, sfds)) goto error; if(hupreload && !gentleshutdown) { if(reload(confs, conffile, 0, // Not first time. get_int(confs[OPT_MAX_CHILDREN]), get_int(confs[OPT_MAX_STATUS_CHILDREN]))) goto error; } hupreload=0; } end: ret=SERVER_OK; error: close_fds(rfds); close_fds(sfds); // FIX THIS: Have an enum for a return value, so that it is more obvious what // is happening, like client.c does. return ret; }
static void server(int sock) { char buff[BUF_SIZE + 1]; int actual; t_server server; actual = 0; init_server(&server, sock); while (1) { init_fds(&server, actual); if (select(server.max + 1, &server.rdfs, NULL, NULL, NULL) == -1) exit_error("select"); if (FD_ISSET(STDIN_FILENO, &server.rdfs)) break ; else if (FD_ISSET(server.sock, &server.rdfs)) { if (new_client(&server, &actual, buff) == -1) continue ; } else client_talking(&server, &actual, buff); } clear_clients(server.clients, actual); close(server.sock); }
int main(int argc,const char* argv[]){ if(argc!=3){ printf("usage:%s [ip] [port]\n",argv[0]); return 0; } int listen_sock = start_up(argv[1],argv[2]); fds[0] = listen_sock; init_fds(fds,SIZE); while(1){ int max_fd = -1; fd_set rdfds; init_rdfds(fds,SIZE,&rdfds,&max_fd); switch(select(max_fd+1,&rdfds,NULL,NULL,NULL)){ case 0: printf("time out!\n"); break; case -1: perror("select"); break; default:{ //遍历文件描述符表,找已就绪的文件描述符 for(int i=0; i<SIZE; ++i){ if(i==0 && FD_ISSET(fds[i],&rdfds)){ //监听套接字已就绪 struct sockaddr_in client; socklen_t size = sizeof(client); int client_sock = accept(fds[0],(struct sockaddr *)&client,&size); if(client_sock < 0){ perror("accept"); break; } int index = put_fd_in_fds(fds,SIZE,client_sock); printf("%s:%d connected, fd in %d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),index); }else if(i!=0 && FD_ISSET(fds[i],&rdfds)){ //其它读事件已就绪(假设数据量小于1024) char buff[1024] = {0}; ssize_t s = read(fds[i],buff,sizeof(buff)-1); if(s == 0){ printf("someone is disconneced!\n"); fds[i] = -1; close(fds[i]); break; }else if(s < 0){ perror("read"); break; } printf("client#%s",buff); } } } } } return 0; }
void game(t_env *env) { while (stop == 42) { init_fds(env); do_select(env); check_fds(env); apply_timers(env); do_actions(env); } }
int main(int argc, char **argv) { char data[4]; int i, res= 0; printf("\n" "****************************************************************\n" "*** NOTE: ***\n" "*** the HAL must be compiled with Low power quaternion ***\n" "*** and/or DMP screen orientation support. ***\n" "*** 'At least' one of the 4 Android virtual sensors ***\n" "*** must be enabled. ***\n" "*** ***\n" "*** Please perform gestures to see the output. ***\n" "*** Press any key to stop the program. ***\n" "****************************************************************\n" "\n"); res = inv_init_sysfs_attributes(); if (res) { printf("GT:ERR-Can't allocate mem\n"); return -1; } /* init Fds to poll for gesture data */ init_fds(); /* on Gesture/DMP supported features */ if (enable_dmp_features(1) < 0) { printf("GT:ERR-Can't enable Gestures\n"); return -1; } do { for (i = 0; i < NUM_DMP_FEATS; i++) read(pfd[i].fd, data, 4); poll(pfd, NUM_DMP_FEATS, POLL_TIME); parse_events(pfd, NUM_DMP_FEATS); } while (!_kbhit()); /* off Gesture/DMP supported features */ if (enable_dmp_features(0) < 0) { printf("GT:ERR-Can't disable Gestures\n"); return -1; } /* release resources */ close_fds(); if (sysfs_names_ptr) free(sysfs_names_ptr); return res; }
int main_loop(t_cl_prop *cl) { while(1) { //printf("main_loop.\n"); printf("===============================================================\n"); init_fds(cl); cl_select(cl); check_fds(cl); } return (1); }
void do_append_write_test(int* fd, const int fileCount, const int fileSize, const int blockSize, const bool needclose) { char buf[MAX_BLOCK_SIZE] = {0}; if(! needclose) { // Do not need close: // Open file before the test start, mode O_CREAT|O_APPEND|O_SYNC init_fds(fd, fileCount, filename_prefix, path, AW_FILE_MODE); } char mkdircmd[MAX_STR_LEN] = {0}; sprintf( mkdircmd, "mkdir -p %s", path); system(mkdircmd); int i=0; int j=0; for(; (blockSize * j) < fileSize; j++) { for(i=0; i < fileCount; i++) { char filename[MAX_FILENAME_LEN]; snprintf(filename, MAX_FILENAME_LEN, FILENAME_FORMAT, path, filename_prefix, i); struct timeval tv_begin, tv_end; gettimeofday(&tv_begin, NULL); int cur_fd; if(needclose) { cur_fd = open(filename, AW_FILE_MODE, 0666); if(cur_fd <= 0 ) { printf("Open file error: %s\n", filename); } write(cur_fd, buf, blockSize); close(cur_fd); } else { cur_fd = fd[i]; write(cur_fd, buf, blockSize); fsync(cur_fd); } gettimeofday(&tv_end, NULL); long long elapsed = ( ( tv_end.tv_sec * 1000000 + tv_end.tv_usec) - (tv_begin.tv_sec * 1000000 + tv_begin.tv_usec)); if(needclose) { printf("Need Close: "); } else { printf("Do not need Close: "); } printf("%ld us\n", elapsed); } } if(!needclose) { close_fds(fd, fileCount); } }
int create_channel(t_channel **root, char *name, int fd) { t_channel *tmp; if ((tmp = malloc(sizeof(t_channel))) == NULL || (tmp->root = init_fds()) == NULL || add_fd_list(&(tmp->root), fd) == -1) return (-1); tmp->channel_name = strdup(name); tmp->size = 1; tmp->next = (*root); tmp->prev = (*root)->prev; tmp->next->prev = tmp; tmp->prev->next = tmp; return (0); }
int main(int argc, const char* argv[]) { // {{{ if (argc < 2) { fprintf(stderr, "usage: %s input-device [input-device ...] " "(/dev/input/eventX)\n", argv[0]); return 1; } init_key_names(); init_uid_gid(); int fds[argc - 1]; int max_fd = init_fds(argc, argv, fds); if (max_fd < 0) { perror("Couldn't open all input devices, do you forget to run as root?"); return 1; } ++ max_fd; fd_set fds_read; for(;;) { save_record(); struct timeval tv = {1, 0}; FD_ZERO(&fds_read); for (size_t i = 0; i < sizeof(fds) / sizeof(fds[0]); ++ i) { if (fds[i] != -1) FD_SET(fds[i], &fds_read); } if (select(max_fd, &fds_read, NULL, NULL, &tv) > 0) { for (size_t i = 0; i < sizeof(fds) / sizeof(fds[0]); ++ i) { if (fds[i] != -1) { if (FD_ISSET(fds[i], &fds_read)) { read_event(fds[i]); } } } } } return 0; } // }}}
void server(int sock, int gfxsock, t_server *server) { int actual; actual = 0; init_server(server, gfxsock, sock); while (1) { init_time(server, &actual); regen_food(server); do_cmd(server); init_eggs(server); init_fds(server, actual); if (server_select(server, &actual)) continue ; } clear_clients(server->clients, actual); close(server->sock); }
int main(int argc, char **argv) { /* * The + on the front tells GNU getopt not to rearrange argv. */ const char *optlist = "+F:f:v:W;m:bcCd::D::e:E:gh:i:l:L:nNo::Op::MPrStVY"; bool stopped_early = false; int old_optind; int i; int c; char *scan, *src; char *extra_stack; int have_srcfile = 0; SRCFILE *s; /* do these checks early */ if (getenv("TIDYMEM") != NULL) do_flags |= DO_TIDY_MEM; #ifdef HAVE_MCHECK_H #ifdef HAVE_MTRACE if (do_tidy_mem) mtrace(); #endif /* HAVE_MTRACE */ #endif /* HAVE_MCHECK_H */ #if defined(LC_CTYPE) setlocale(LC_CTYPE, ""); #endif #if defined(LC_COLLATE) setlocale(LC_COLLATE, ""); #endif #if defined(LC_MESSAGES) setlocale(LC_MESSAGES, ""); #endif #if defined(LC_NUMERIC) && defined(HAVE_LOCALE_H) /* * Force the issue here. According to POSIX 2001, decimal * point is used for parsing source code and for command-line * assignments and the locale value for processing input, * number to string conversion, and printing output. * * 10/2005 --- see below also; we now only use the locale's * decimal point if do_posix in effect. * * 9/2007: * This is a mess. We need to get the locale's numeric info for * the thousands separator for the %'d flag. */ setlocale(LC_NUMERIC, ""); init_locale(& loc); setlocale(LC_NUMERIC, "C"); #endif #if defined(LC_TIME) setlocale(LC_TIME, ""); #endif #if MBS_SUPPORT /* * In glibc, MB_CUR_MAX is actually a function. This value is * tested *a lot* in many speed-critical places in gawk. Caching * this value once makes a speed difference. */ gawk_mb_cur_max = MB_CUR_MAX; /* Without MBS_SUPPORT, gawk_mb_cur_max is 1. */ /* init the cache for checking bytes if they're characters */ init_btowc_cache(); #endif (void) bindtextdomain(PACKAGE, LOCALEDIR); (void) textdomain(PACKAGE); (void) signal(SIGFPE, catchsig); #ifdef SIGBUS (void) signal(SIGBUS, catchsig); #endif (void) sigsegv_install_handler(catchsegv); #define STACK_SIZE (16*1024) emalloc(extra_stack, char *, STACK_SIZE, "main"); (void) stackoverflow_install_handler(catchstackoverflow, extra_stack, STACK_SIZE); #undef STACK_SIZE myname = gawk_name(argv[0]); os_arg_fixup(&argc, &argv); /* emulate redirection, expand wildcards */ if (argc < 2) usage(EXIT_FAILURE, stderr); /* initialize the null string */ Nnull_string = make_string("", 0); /* Robustness: check that file descriptors 0, 1, 2 are open */ init_fds(); /* init array handling. */ array_init(); /* init the symbol tables */ init_symbol_table(); output_fp = stdout; /* we do error messages ourselves on invalid options */ opterr = false; /* copy argv before getopt gets to it; used to restart the debugger */ save_argv(argc, argv); /* initialize global (main) execution context */ push_context(new_context()); /* option processing. ready, set, go! */ for (optopt = 0, old_optind = 1; (c = getopt_long(argc, argv, optlist, optab, NULL)) != EOF; optopt = 0, old_optind = optind) { if (do_posix) opterr = true; switch (c) { case 'F': add_preassign(PRE_ASSIGN_FS, optarg); break; case 'E': disallow_var_assigns = true; /* fall through */ case 'f': /* * Allow multiple -f options. * This makes function libraries real easy. * Most of the magic is in the scanner. * * The following is to allow for whitespace at the end * of a #! /bin/gawk line in an executable file */ scan = optarg; if (argv[optind-1] != optarg) while (isspace((unsigned char) *scan)) scan++; src = (*scan == '\0' ? argv[optind++] : optarg); (void) add_srcfile((src && src[0] == '-' && src[1] == '\0') ? SRC_STDIN : SRC_FILE, src, srcfiles, NULL, NULL); break; case 'v': add_preassign(PRE_ASSIGN, optarg); break; case 'm': /* * BWK awk extension. * -mf nnn set # fields, gawk ignores * -mr nnn set record length, ditto * * As of at least 10/2007, BWK awk also ignores it. */ if (do_lint) lintwarn(_("`-m[fr]' option irrelevant in gawk")); if (optarg[0] != 'r' && optarg[0] != 'f') warning(_("-m option usage: `-m[fr] nnn'")); break; case 'b': do_binary = true; break; case 'c': do_flags |= DO_TRADITIONAL; break; case 'C': copyleft(); break; case 'd': do_flags |= DO_DUMP_VARS; if (optarg != NULL && optarg[0] != '\0') varfile = optarg; break; case 'D': do_flags |= DO_DEBUG; if (optarg != NULL && optarg[0] != '\0') command_file = optarg; break; case 'e': if (optarg[0] == '\0') warning(_("empty argument to `-e/--source' ignored")); else (void) add_srcfile(SRC_CMDLINE, optarg, srcfiles, NULL, NULL); break; case 'g': do_flags |= DO_INTL; break; case 'h': /* write usage to stdout, per GNU coding stds */ usage(EXIT_SUCCESS, stdout); break; case 'i': (void) add_srcfile(SRC_INC, optarg, srcfiles, NULL, NULL); break; case 'l': (void) add_srcfile(SRC_EXTLIB, optarg, srcfiles, NULL, NULL); break; case 'L': #ifndef NO_LINT do_flags |= DO_LINT_ALL; if (optarg != NULL) { if (strcmp(optarg, "fatal") == 0) lintfunc = r_fatal; else if (strcmp(optarg, "invalid") == 0) { do_flags &= ~DO_LINT_ALL; do_flags |= DO_LINT_INVALID; } } break; case 't': do_flags |= DO_LINT_OLD; break; #else case 'L': case 't': break; #endif case 'n': do_flags |= DO_NON_DEC_DATA; break; case 'N': use_lc_numeric = true; break; case 'O': do_optimize++; break; case 'p': do_flags |= DO_PROFILE; /* fall through */ case 'o': do_flags |= DO_PRETTY_PRINT; if (optarg != NULL) set_prof_file(optarg); else set_prof_file(DEFAULT_PROFILE); break; case 'M': #ifdef HAVE_MPFR do_flags |= DO_MPFR; #endif break; case 'P': do_flags |= DO_POSIX; break; case 'r': do_flags |= DO_INTERVALS; break; case 'S': do_flags |= DO_SANDBOX; break; case 'V': do_version = true; break; case 'W': /* gawk specific options - now in getopt_long */ fprintf(stderr, _("%s: option `-W %s' unrecognized, ignored\n"), argv[0], optarg); break; case 0: /* * getopt_long found an option that sets a variable * instead of returning a letter. Do nothing, just * cycle around for the next one. */ break; case 'Y': #if defined(YYDEBUG) || defined(GAWKDEBUG) if (c == 'Y') { yydebug = 2; break; } #endif /* if not debugging, fall through */ case '?': default: /* * If not posix, an unrecognized option stops argument * processing so that it can go into ARGV for the awk * program to see. This makes use of ``#! /bin/gawk -f'' * easier. * * However, it's never simple. If optopt is set, * an option that requires an argument didn't get the * argument. We care because if opterr is 0, then * getopt_long won't print the error message for us. */ if (! do_posix && (optopt == '\0' || strchr(optlist, optopt) == NULL)) { /* * can't just do optind--. In case of an * option with >= 2 letters, getopt_long * won't have incremented optind. */ optind = old_optind; stopped_early = true; goto out; } else if (optopt != '\0') { /* Use POSIX required message format */ fprintf(stderr, _("%s: option requires an argument -- %c\n"), myname, optopt); usage(EXIT_FAILURE, stderr); } /* else let getopt print error message for us */ break; } if (c == 'E') /* --exec ends option processing */ break; } out: if (do_nostalgia) nostalgia(); /* check for POSIXLY_CORRECT environment variable */ if (! do_posix && getenv("POSIXLY_CORRECT") != NULL) { do_flags |= DO_POSIX; if (do_lint) lintwarn( _("environment variable `POSIXLY_CORRECT' set: turning on `--posix'")); } if (do_posix) { use_lc_numeric = true; if (do_traditional) /* both on command line */ warning(_("`--posix' overrides `--traditional'")); else do_flags |= DO_TRADITIONAL; /* * POSIX compliance also implies * no GNU extensions either. */ } if (do_traditional && do_non_decimal_data) { do_flags &= ~DO_NON_DEC_DATA; warning(_("`--posix'/`--traditional' overrides `--non-decimal-data'")); } if (do_lint && os_is_setuid()) warning(_("running %s setuid root may be a security problem"), myname); #if MBS_SUPPORT if (do_binary) { if (do_posix) warning(_("`--posix' overrides `--characters-as-bytes'")); else gawk_mb_cur_max = 1; /* hands off my data! */ #if defined(LC_ALL) setlocale(LC_ALL, "C"); #endif } #endif if (do_debug) /* Need to register the debugger pre-exec hook before any other */ init_debug(); #ifdef HAVE_MPFR /* Set up MPFR defaults, and register pre-exec hook to process arithmetic opcodes */ if (do_mpfr) init_mpfr(DEFAULT_PREC, DEFAULT_ROUNDMODE); #endif /* load group set */ init_groupset(); #ifdef HAVE_MPFR if (do_mpfr) { mpz_init(Nnull_string->mpg_i); Nnull_string->flags = (MALLOC|STRCUR|STRING|MPZN|NUMCUR|NUMBER); } else #endif { Nnull_string->numbr = 0.0; Nnull_string->flags = (MALLOC|STRCUR|STRING|NUMCUR|NUMBER); } /* * Tell the regex routines how they should work. * Do this before initializing variables, since * they could want to do a regexp compile. */ resetup(); /* Set up the special variables */ init_vars(); /* Set up the field variables */ init_fields(); /* Now process the pre-assignments */ for (i = 0; i <= numassigns; i++) { if (preassigns[i].type == PRE_ASSIGN) (void) arg_assign(preassigns[i].val, true); else /* PRE_ASSIGN_FS */ cmdline_fs(preassigns[i].val); efree(preassigns[i].val); } if (preassigns != NULL) efree(preassigns); if ((BINMODE & 1) != 0) if (os_setbinmode(fileno(stdin), O_BINARY) == -1) fatal(_("can't set binary mode on stdin (%s)"), strerror(errno)); if ((BINMODE & 2) != 0) { if (os_setbinmode(fileno(stdout), O_BINARY) == -1) fatal(_("can't set binary mode on stdout (%s)"), strerror(errno)); if (os_setbinmode(fileno(stderr), O_BINARY) == -1) fatal(_("can't set binary mode on stderr (%s)"), strerror(errno)); } #ifdef GAWKDEBUG setbuf(stdout, (char *) NULL); /* make debugging easier */ #endif if (os_isatty(fileno(stdout))) output_is_tty = true; /* initialize API before loading extension libraries */ init_ext_api(); /* load extension libs */ for (s = srcfiles->next; s != srcfiles; s = s->next) { if (s->stype == SRC_EXTLIB) load_ext(s->fullpath); else if (s->stype != SRC_INC) have_srcfile++; } /* do version check after extensions are loaded to get extension info */ if (do_version) version(); /* No -f or --source options, use next arg */ if (! have_srcfile) { if (optind > argc - 1 || stopped_early) /* no args left or no program */ usage(EXIT_FAILURE, stderr); (void) add_srcfile(SRC_CMDLINE, argv[optind], srcfiles, NULL, NULL); optind++; } /* Select the interpreter routine */ init_interpret(); init_args(optind, argc, do_posix ? argv[0] : myname, argv); #if defined(LC_NUMERIC) /* * FRAGILE! CAREFUL! * Pre-initing the variables with arg_assign() can change the * locale. Force it to C before parsing the program. */ setlocale(LC_NUMERIC, "C"); #endif /* Read in the program */ if (parse_program(& code_block) != 0) exit(EXIT_FAILURE); if (do_intl) exit(EXIT_SUCCESS); if (do_lint) shadow_funcs(); if (do_lint && code_block->nexti->opcode == Op_atexit) lintwarn(_("no program text at all!")); load_symbols(); if (do_profile) init_profiling_signals(); #if defined(LC_NUMERIC) /* * See comment above about using locale's decimal point. * * 10/2005: * Bitter experience teaches us that most people the world over * use period as the decimal point, not whatever their locale * uses. Thus, only use the locale's decimal point if being * posixly anal-retentive. * * 7/2007: * Be a little bit kinder. Allow the --use-lc-numeric option * to also use the local decimal point. This avoids the draconian * strictness of POSIX mode if someone just wants to parse their * data using the local decimal point. */ if (use_lc_numeric) setlocale(LC_NUMERIC, ""); #endif init_io(); output_fp = stdout; if (do_debug) debug_prog(code_block); else interpret(code_block); if (do_pretty_print) { dump_prog(code_block); dump_funcs(); } if (do_dump_vars) dump_vars(varfile); if (do_tidy_mem) release_all_vars(); /* keep valgrind happier */ if (extra_stack) efree(extra_stack); final_exit(exit_val); return exit_val; /* to suppress warnings */ }
int main(int argc, char **argv) { int c; char *scan; /* the + on the front tells GNU getopt not to rearrange argv */ const char *optlist = "+F:f:v:W;m:D"; int stopped_early = FALSE; int old_optind; extern int optind; extern int opterr; extern char *optarg; int i; int stdio_problem = FALSE; /* do these checks early */ if (getenv("TIDYMEM") != NULL) do_tidy_mem = TRUE; if (getenv("WHINY_USERS") != NULL) whiny_users = TRUE; #ifdef HAVE_MCHECK_H if (do_tidy_mem) mtrace(); #endif /* HAVE_MCHECK_H */ #if defined(LC_CTYPE) setlocale(LC_CTYPE, ""); #endif #if defined(LC_COLLATE) setlocale(LC_COLLATE, ""); #endif #if defined(LC_MESSAGES) setlocale(LC_MESSAGES, ""); #endif #if defined(LC_NUMERIC) /* * Force the issue here. According to POSIX 2001, decimal * point is used for parsing source code and for command-line * assignments and the locale value for processing input, * number to string conversion, and printing output. */ setlocale(LC_NUMERIC, "C"); #endif #if defined(LC_TIME) setlocale(LC_TIME, ""); #endif #ifdef MBS_SUPPORT /* * In glibc, MB_CUR_MAX is actually a function. This value is * tested *a lot* in many speed-critical places in gawk. Caching * this value once makes a speed difference. */ gawk_mb_cur_max = MB_CUR_MAX; /* Without MBS_SUPPORT, gawk_mb_cur_max is 1. */ #endif (void) bindtextdomain(PACKAGE, LOCALEDIR); (void) textdomain(PACKAGE); (void) signal(SIGFPE, catchsig); (void) signal(SIGSEGV, catchsig); #ifdef SIGBUS (void) signal(SIGBUS, catchsig); #endif myname = gawk_name(argv[0]); argv[0] = (char *) myname; os_arg_fixup(&argc, &argv); /* emulate redirection, expand wildcards */ /* remove sccs gunk */ if (strncmp(version_string, "@(#)", 4) == 0) version_string += 4; if (argc < 2) usage(1, stderr); /* Robustness: check that file descriptors 0, 1, 2 are open */ init_fds(); /* init array handling. */ array_init(); /* we do error messages ourselves on invalid options */ opterr = FALSE; /* option processing. ready, set, go! */ for (optopt = 0, old_optind = 1; (c = getopt_long(argc, argv, optlist, optab, NULL)) != EOF; optopt = 0, old_optind = optind) { if (do_posix) opterr = TRUE; switch (c) { case 'F': preassigns_add(PRE_ASSIGN_FS, optarg); break; case 'S': disallow_var_assigns = TRUE; /* fall through */ case 'f': /* * a la MKS awk, allow multiple -f options. * this makes function libraries real easy. * most of the magic is in the scanner. * * The following is to allow for whitespace at the end * of a #! /bin/gawk line in an executable file */ scan = optarg; if (argv[optind-1] != optarg) while (ISSPACE(*scan)) scan++; srcfiles_add(SOURCEFILE, (*scan == '\0' ? argv[optind++] : optarg)); break; case 'v': preassigns_add(PRE_ASSIGN, optarg); break; case 'm': /* * Research awk extension. * -mf nnn set # fields, gawk ignores * -mr nnn set record length, ditto */ if (do_lint) lintwarn(_("`-m[fr]' option irrelevant in gawk")); if (optarg[0] != 'r' && optarg[0] != 'f') warning(_("-m option usage: `-m[fr] nnn'")); /* * Set fixed length records for Tandem, * ignored on other platforms (see io.c:get_a_record). */ if (optarg[0] == 'r') { if (ISDIGIT(optarg[1])) MRL = atoi(optarg+1); else { MRL = atoi(argv[optind]); optind++; } } else if (optarg[1] == '\0') optind++; break; case 'W': /* gawk specific options - now in getopt_long */ fprintf(stderr, _("%s: option `-W %s' unrecognized, ignored\n"), argv[0], optarg); break; /* These can only come from long form options */ case 'C': copyleft(); break; case 'd': do_dump_vars = TRUE; if (optarg != NULL && optarg[0] != '\0') varfile = optarg; break; case 'l': #ifndef NO_LINT do_lint = LINT_ALL; if (optarg != NULL) { if (strcmp(optarg, "fatal") == 0) lintfunc = r_fatal; else if (strcmp(optarg, "invalid") == 0) do_lint = LINT_INVALID; } #endif break; case 'p': do_profiling = TRUE; if (optarg != NULL) set_prof_file(optarg); else set_prof_file(DEFAULT_PROFILE); break; case 's': if (optarg[0] == '\0') warning(_("empty argument to `--source' ignored")); else srcfiles_add(CMDLINE, optarg); break; case 'u': usage(0, stdout); /* per coding stds */ break; case 'V': version(); break; case 0: /* * getopt_long found an option that sets a variable * instead of returning a letter. Do nothing, just * cycle around for the next one. */ break; case 'D': #ifdef GAWKDEBUG yydebug = 2; break; #endif /* if not debugging, fall through */ case '?': default: /* * New behavior. If not posix, an unrecognized * option stops argument processing so that it can * go into ARGV for the awk program to see. This * makes use of ``#! /bin/gawk -f'' easier. * * However, it's never simple. If optopt is set, * an option that requires an argument didn't get the * argument. We care because if opterr is 0, then * getopt_long won't print the error message for us. */ if (! do_posix && (optopt == '\0' || strchr(optlist, optopt) == NULL)) { /* * can't just do optind--. In case of an * option with >= 2 letters, getopt_long * won't have incremented optind. */ optind = old_optind; stopped_early = TRUE; goto out; } else if (optopt != '\0') /* Use 1003.2 required message format */ fprintf(stderr, _("%s: option requires an argument -- %c\n"), myname, optopt); /* else let getopt print error message for us */ break; } if (c == 'S') /* --exec ends option processing */ break; } out: if (do_nostalgia) nostalgia(); /* check for POSIXLY_CORRECT environment variable */ if (! do_posix && getenv("POSIXLY_CORRECT") != NULL) { do_posix = TRUE; if (do_lint) lintwarn( _("environment variable `POSIXLY_CORRECT' set: turning on `--posix'")); } if (do_posix) { if (do_traditional) /* both on command line */ warning(_("`--posix' overrides `--traditional'")); else do_traditional = TRUE; /* * POSIX compliance also implies * no GNU extensions either. */ } if (do_traditional && do_non_decimal_data) { do_non_decimal_data = FALSE; warning(_("`--posix'/`--traditional' overrides `--non-decimal-data'")); } if (do_lint && os_is_setuid()) warning(_("running %s setuid root may be a security problem"), myname); /* * Force profiling if this is pgawk. * Don't bother if the command line already set profiling up. */ if (! do_profiling) init_profiling(& do_profiling, DEFAULT_PROFILE); /* load group set */ init_groupset(); /* initialize the null string */ Nnull_string = make_string("", 0); Nnull_string->numbr = 0.0; Nnull_string->type = Node_val; Nnull_string->flags = (PERM|STRCUR|STRING|NUMCUR|NUMBER); /* * Tell the regex routines how they should work. * Do this before initializing variables, since * they could want to do a regexp compile. */ resetup(); /* Set up the special variables */ init_vars(); /* Set up the field variables */ init_fields(); /* Now process the pre-assignments */ for (i = 0; i <= numassigns; i++) if (preassigns[i].stype == PRE_ASSIGN) (void) arg_assign(preassigns[i].val, TRUE); else /* PRE_ASSIGN_FS */ cmdline_fs(preassigns[i].val); free(preassigns); if ((BINMODE & 1) != 0) if (os_setbinmode(fileno(stdin), O_BINARY) == -1) fatal(_("can't set binary mode on stdin (%s)"), strerror(errno)); if ((BINMODE & 2) != 0) { if (os_setbinmode(fileno(stdout), O_BINARY) == -1) fatal(_("can't set binary mode on stdout (%s)"), strerror(errno)); if (os_setbinmode(fileno(stderr), O_BINARY) == -1) fatal(_("can't set binary mode on stderr (%s)"), strerror(errno)); } #ifdef GAWKDEBUG setbuf(stdout, (char *) NULL); /* make debugging easier */ #endif if (isatty(fileno(stdout))) output_is_tty = TRUE; /* No -f or --source options, use next arg */ if (numfiles == -1) { if (optind > argc - 1 || stopped_early) /* no args left or no program */ usage(1, stderr); srcfiles_add(CMDLINE, argv[optind]); optind++; } init_args(optind, argc, (char *) myname, argv); (void) tokexpand(); #if defined(LC_NUMERIC) /* * FRAGILE! CAREFUL! * Pre-initing the variables with arg_assign() can change the * locale. Force it to C before parsing the program. */ setlocale(LC_NUMERIC, "C"); #endif /* Read in the program */ if (yyparse() != 0 || errcount != 0) exit(1); free(srcfiles); if (do_intl) exit(0); if (do_lint && begin_block == NULL && expression_value == NULL && end_block == NULL) lintwarn(_("no program text at all!")); if (do_lint) shadow_funcs(); init_profiling_signals(); #if defined(LC_NUMERIC) /* See comment above. */ setlocale(LC_NUMERIC, ""); #endif #if defined(HAVE_LOCALE_H) loc = *localeconv(); /* Make a local copy of locale numeric info */ #endif /* Whew. Finally, run the program. */ if (begin_block != NULL) { in_begin_rule = TRUE; (void) interpret(begin_block); } in_begin_rule = FALSE; if (! exiting && (expression_value != NULL || end_block != NULL)) do_input(); if (end_block != NULL) { in_end_rule = TRUE; (void) interpret(end_block); } in_end_rule = FALSE; /* * This used to be: * * if (close_io() != 0 && ! exiting && exit_val == 0) * exit_val = 1; * * Other awks don't care about problems closing open files * and pipes, in that it doesn't affect their exit status. * So we no longer do either. */ (void) close_io(& stdio_problem); /* * However, we do want to exit non-zero if there was a problem * with stdout/stderr, so we reinstate a slightly different * version of the above: */ if (stdio_problem && ! exiting && exit_val == 0) exit_val = 1; if (do_profiling) { dump_prog(begin_block, expression_value, end_block); dump_funcs(); } if (do_dump_vars) dump_vars(varfile); if (do_tidy_mem) release_all_vars(); exit(exit_val); /* more portable */ return exit_val; /* to suppress warnings */ }