/// main statement /// @param[in] argc number of arguments /// @param[in] argv array of arguments int main(int argc, char * argv[]) { int c; int opt_index; // getopt options static char short_opt[] = "hV"; static struct option long_opt[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {NULL, 0, 0, 0 } }; while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) { switch(c) { case -1: /* no more arguments */ case 0: /* long options toggles */ break; case 'h': my_usage(); return(0); case 'V': my_version(); return(0); case '?': fprintf(stderr, _("Try `%s --help' for more information.\n"), PROGRAM_NAME); return(1); default: fprintf(stderr, _("%s: unrecognized option `--%c'\n" "Try `%s --help' for more information.\n" ), PROGRAM_NAME, c, PROGRAM_NAME ); return(1); }; }; // checks for extra arguments if (optind != argc) { fprintf(stderr, _("%s: unknown option `%s'\n"), PROGRAM_NAME, argv[optind]); fprintf(stderr, _("Try `%s --help' for more information.\n"), PROGRAM_NAME); return(1); }; // prints byte order if ((is_big_endian())) printf("big endian\n"); else printf("little endian\n"); return(0); }
int main(int argc, char **argv) { int encode=0; CIPHER_HANDLE hd; char buf[4096]; int n, size=4096; int algo; #ifdef HAVE_DOSISH_SYSTEM setmode( fileno(stdin), O_BINARY ); setmode( fileno(stdout), O_BINARY ); #endif i18n_init(); if( argc > 1 && !strcmp(argv[1], "-e") ) { encode++; argc--; argv++; } else if( argc > 1 && !strcmp(argv[1], "-E") ) { encode++; argc--; argv++; size = 10; } else if( argc > 1 && !strcmp(argv[1], "-d") ) { argc--; argv++; } else if( argc > 1 && !strcmp(argv[1], "-D") ) { argc--; argv++; size = 10; } if( argc != 3 ) my_usage(); argc--; argv++; algo = string_to_cipher_algo( *argv ); argc--; argv++; hd = cipher_open( algo, CIPHER_MODE_CFB, 0 ); cipher_setkey( hd, *argv, strlen(*argv) ); cipher_setiv( hd, NULL, 0 ); while( (n = fread( buf, 1, size, stdin )) > 0 ) { if( encode ) cipher_encrypt( hd, buf, buf, n ); else cipher_decrypt( hd, buf, buf, n ); if( fwrite( buf, 1, n, stdout) != n ) log_fatal("write error\n"); } cipher_close(hd); return 0; }
int main(int argc, char * argv[]) { int c; int i; int opts; int option_index; char ** list; static char short_options[] = "acfhLqrvV"; static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"quiet", no_argument, 0, 'q'}, {"silent", no_argument, 0, 'q'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {NULL, 0, 0, 0 } }; opts = 0; option_index = 0; list = NULL; while((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) { switch(c) { case -1: /* no more arguments */ case 0: /* long option toggles */ break; case 'a': opts |= MY_OPT_HIDDEN; break; case 'c': opts |= MY_OPT_CONTINUE; break; case 'f': opts |= MY_OPT_FORCE; break; case 'h': my_usage(); return(0); case 'L': opts |= MY_OPT_LINKS; break; case 'q': opts |= MY_OPT_QUIET; break; case 'r': opts |= MY_OPT_RECURSE; break; case 'v': opts |= MY_OPT_VERBOSE; break; case 'V': my_version(); return(0); case '?': fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); return(1); default: fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); return(1); }; }; if (argc < (optind+1)) { fprintf(stderr, "%s: missing required arguments\n", PROGRAM_NAME); fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); return(1); }; for(i = optind; i < argc; i++) switch (recurse_directory(argv[i], opts, NULL, NULL)) { case -1: return(1); case 0: break; default: if (!(opts & MY_OPT_CONTINUE)) return(1); break; }; return(0); }
int bench_main(int argc, char *argv[]) { double tmin = 0.0; double tol; int repeat = 0; int rounds = 10; int iarounds = 0; int arounds = 1; /* this is too low for precise results */ int c; report = report_verbose; /* default */ verbose = 0; tol = SINGLE_PRECISION ? 1.0e-3 : (QUAD_PRECISION ? 1e-29 : 1.0e-10); main_init(&argc, &argv); bench_srand(1); while ((c = my_getopt (argc, argv, options)) != -1) { switch (c) { case 't' : tmin = strtod(my_optarg, 0); break; case 'r': repeat = atoi(my_optarg); break; case 's': timer_init(tmin, repeat); speed(my_optarg, 0); break; case 'S': timer_init(tmin, repeat); speed(my_optarg, 1); break; case 'd': report_can_do(my_optarg); break; case 'o': useropt(my_optarg); break; case 'v': if (verbose >= 0) { /* verbose < 0 disables output */ if (my_optarg) verbose = atoi(my_optarg); else ++verbose; } break; case 'y': verify(my_optarg, rounds, tol); break; case 'a': accuracy(my_optarg, arounds, iarounds); break; case 'i': report_info(my_optarg); break; case 'I': report_info_all(); break; case 'h': if (verbose >= 0) my_usage(argv[0], options); break; case 300: /* --report-mflops */ report = report_mflops; break; case 310: /* --report-time */ report = report_time; break; case 320: /* --report-benchmark */ report = report_benchmark; break; case 330: /* --report-verbose */ report = report_verbose; break; case 400: /* --print-time-min */ timer_init(tmin, repeat); ovtpvt("%g\n", time_min); break; case 401: /* --verify-rounds */ rounds = atoi(my_optarg); break; case 402: /* --print-precision */ if (SINGLE_PRECISION) ovtpvt("single\n"); else if (QUAD_PRECISION) ovtpvt("quad\n"); else if (LDOUBLE_PRECISION) ovtpvt("long-double\n"); else if (DOUBLE_PRECISION) ovtpvt("double\n"); else ovtpvt("unknown %d\n", sizeof(bench_real)); break; case 403: /* --verify-tolerance */ tol = strtod(my_optarg, 0); break; case 404: /* --random-seed */ bench_srand(atoi(my_optarg)); break; case 405: /* --accuracy-rounds */ arounds = atoi(my_optarg); break; case 406: /* --impulse-accuracy-rounds */ iarounds = atoi(my_optarg); break; case '?': /* my_getopt() already printed an error message. */ cleanup(); return 1; default: abort (); } } /* assume that any remaining arguments are problems to be benchmarked */ while (my_optind < argc) { timer_init(tmin, repeat); speed(argv[my_optind++], 0); } cleanup(); return 0; }
int main(int argc, char * argv[]) { int c; int opt_index; int err; int s; char * str; ssize_t len; int family; int protocol; int socktype; const char * port; struct addrinfo hints; struct addrinfo * hintsp; struct addrinfo * res; char buff[1024]; int timeout; size_t buff_len; char straddr[INET6_ADDRSTRLEN]; struct pollfd fds[1]; socklen_t socklen; union { struct sockaddr sa; struct sockaddr_in sa_in; struct sockaddr_in6 sa_in6; struct sockaddr_storage sa_storage; } addr; static char short_opt[] = "46hm:t:TUV"; static struct option long_opt[] = { { "help", no_argument, 0, 'h'}, { "version", no_argument, 0, 'V'}, { NULL, 0, 0, 0 } }; if ((str = rindex(argv[0], '/')) != NULL) { str++; argv[0] = str; }; protocol = 0; family = PF_UNSPEC; port = "2211"; buff_len = 0; timeout = 0; while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) { switch(c) { case -1: /* no more arguments */ case 0: /* long options toggles */ break; case '4': family = PF_INET; break; case '6': family = PF_INET6; break; case 'h': my_usage(); return(0); case 'm': strncpy(buff, optarg, sizeof(buff)-1); buff[sizeof(buff)-1] = '\0'; buff_len = strlen(buff); break; case 'T': protocol = IPPROTO_TCP; break; case 't': timeout = (int)strtol(optarg, NULL, 0); break; case 'U': protocol = IPPROTO_UDP; break; case 'V': my_version(); return(0); case '?': fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return(1); default: fprintf(stderr, "%s: unrecognized option `--%c'\n", argv[0], c); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return(1); }; }; // check for hostname from CLI if (optind >= argc) { fprintf(stderr, "%s: missing required argument\n", argv[0]); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return(1); }; // check for port from CLI if ((optind+2) <= argc) port = argv[optind+1]; // set defaults if (timeout == 0) timeout = 5000; // timeout is in ms if (buff_len == 0) { strncpy(buff, "Hello World", sizeof(buff)-1); buff[sizeof(buff)-1] = '\0'; buff_len = strlen(buff); }; printf("%s: resolving address ...\n", argv[0]); memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_flags = AI_V4MAPPED|AI_ALL; hints.ai_family = family; hints.ai_socktype = 0; // 0 for any socket type or SOCK_[STREAM|DGRAM|RAW] hints.ai_protocol = protocol; // any protocol (IPPROTO_UDP/IPPROTO_TCP) hintsp = &hints; if ((err = getaddrinfo(argv[optind], port, hintsp, &res)) != 0) { fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(err)); return(1); }; memcpy(&addr, res->ai_addr, res->ai_addrlen); socklen = res->ai_addrlen; protocol = res->ai_protocol; socktype = res->ai_socktype; freeaddrinfo(res); // map binary address into human readable form if ((err = getnameinfo(&addr.sa, socklen, straddr, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST)) != 0) { fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(err)); return(1); }; printf("%s: creating socket ...\n", argv[0]); if ((s = socket(addr.sa.sa_family, socktype, protocol)) == -1) { perror("socket()"); return(1); }; printf("%s: connecting to %s://[%s]:%i ...\n", argv[0], (protocol == IPPROTO_UDP ? "udp" : "tcp"), straddr, ntohs(addr.sa_in6.sin6_port)); if ((err = connect(s, &addr.sa, socklen)) == -1) { perror("connect()"); close(s); return(1); }; printf("%s: sending data ...\n", argv[0]); printf("%s: >>> %s\n", argv[0], buff); if ((len = sendto(s, buff, buff_len, 0, NULL, 0)) == -1) { perror("sendto()"); return(1); }; fds[0].fd = s; fds[0].events = POLLIN; printf("%s: waiting for data ...\n", argv[0]); if ((err = poll(fds, 1, timeout)) == -1) { perror("poll()"); close(s); return(1); }; if (err == 0) { printf("%s: connection timed out ...\n", argv[0]); close(s); return(1); }; printf("%s: receiving data ...\n", argv[0]); if ((len = recvfrom(s, buff, sizeof(buff)-1, 0, NULL, NULL)) == -1) { perror("recvfrom()"); close(s); return(1); }; buff[len] = '\0'; printf("%s: <<< %s\n", argv[0], buff); close(s); return(0); }
int main(int argc, char * argv[]) { int c; int opt_index; int cnfopts; int err; size_t line; int x; const char ** args; bindlecnf * bcnf; char * prog_name; const char * types; static char short_opt[] = "DdehimopQSsV"; static struct option long_opt[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {NULL, 0, 0, 0 } }; prog_name = bindle_basename(argv[0], NULL); cnfopts = 0; while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) { switch(c) { case -1: /* no more arguments */ case 0: /* long options toggles */ break; case 'D': cnfopts |= BINDLE_CNF_QUOTE_DOULE; break; case 'd': cnfopts |= BINDLE_CNF_DEBUG; break; case 'e': cnfopts |= BINDLE_CNF_ESCAPES; break; case 'h': my_usage(prog_name); return(0); case 'i': cnfopts |= BINDLE_CNF_COMMENT_INI; break; case 'm': cnfopts |= BINDLE_CNF_COMMENT_M4; break; case 'o': cnfopts |= BINDLE_CNF_ONESTR; break; case 'p': cnfopts |= BINDLE_CNF_COMMENT_CPP; break; case 'Q': cnfopts |= BINDLE_CNF_QUOTE_DOULE; cnfopts |= BINDLE_CNF_QUOTE_SINGLE; break; case 'S': cnfopts |= BINDLE_CNF_QUOTE_SINGLE; break; case 's': cnfopts |= BINDLE_CNF_COMMENT_SH; break; case 'V': printf("%s no version\n", prog_name); return(0); case '?': fprintf(stderr, "Try `%s --help' for more information.\n", prog_name); return(1); default: fprintf(stderr, "%s: unrecognized option `--%c'\n", prog_name, c); fprintf(stderr, "Try `%s --help' for more information.\n", prog_name); return(1); }; }; if (optind == argc) { fprintf(stderr, "%s: missing required argument\n", prog_name); fprintf(stderr, "Try `%s --help' for more information\n", prog_name); return(1); }; if ((err = bindle_cnfopen(&bcnf, argv[optind], cnfopts)) != BINDLE_SUCCESS) { printf("%s: bindle_cnfopen(%s): %s\n", prog_name, argv[optind], bindle_strerror(err)); return(1); }; while ((err = bindle_cnfread(bcnf, &argc, &args, &types, &line)) == 0) { printf("line %zu (%u args) (%s)\n", line, argc, types); for(x = 0; x < argc; x++) printf(" arg %i: %s\n", x, args[x]); printf("\n"); }; if (err == -1) { printf("%s: bindle_cnfread(): %s\n", prog_name, bindle_strerror(err)); bindle_cnfclose(bcnf); return(1); }; bindle_cnfclose(bcnf); return(0); }