int main(int argc, char *argv[]) { int val; argv++; for (; *argv != 0; ++argv) { // ./factor -a -> "factor all the integers" if (argv[0][0] == '-' && argv[0][1] == 'a') { int i; for (i = 1; ; i++) { pr_fact(i); } // never exits } if (argv[0][0] == '-') { printf(2, "negative numbers aren't permitted."); continue; } val = atoi(argv[0]); if (val == 0) { printf(2, "error parsing %s", argv[0]); continue; } pr_fact(val); } exit(); }
int main(int argc, char *argv[]) { BIGNUM *val; int ch; char *p, buf[LINE_MAX]; /* > max number of digits. */ ctx = BN_CTX_new(); val = BN_new(); if (val == NULL) errx(1, "can't initialise bignum"); while ((ch = getopt(argc, argv, "h")) != -1) switch (ch) { case 'h': hflag++; break; case '?': default: usage(); } argc -= optind; argv += optind; /* No args supplied, read numbers from stdin. */ if (argc == 0) for (;;) { if (fgets(buf, sizeof(buf), stdin) == NULL) { if (ferror(stdin)) err(1, "stdin"); exit (0); } for (p = buf; isblank(*p); ++p); if (*p == '\n' || *p == '\0') continue; if (*p == '-') errx(1, "negative numbers aren't permitted."); if (BN_dec2bn(&val, buf) == 0 && BN_hex2bn(&val, buf) == 0) errx(1, "%s: illegal numeric format.", buf); pr_fact(val); } /* Factor the arguments. */ else for (; *argv != NULL; ++argv) { if (argv[0][0] == '-') errx(1, "negative numbers aren't permitted."); if (BN_dec2bn(&val, argv[0]) == 0 && BN_hex2bn(&val, argv[0]) == 0) errx(1, "%s: illegal numeric format.", argv[0]); pr_fact(val); } exit(0); }
int main(int argc, char *argv[]) { u_int64_t val; int ch; char *p, buf[100]; /* > max number of digits. */ if (pledge("stdio", NULL) == -1) err(1, "pledge"); while ((ch = getopt(argc, argv, "h")) != -1) { switch (ch) { case 'h': default: usage(); } } argc -= optind; argv += optind; /* No args supplied, read numbers from stdin. */ if (argc == 0) { for (;;) { if (fgets(buf, sizeof(buf), stdin) == NULL) { if (ferror(stdin)) err(1, "stdin"); return 0; } buf[strcspn(buf, "\n")] = '\0'; for (p = buf; isblank((unsigned char)*p); ++p) ; if (*p == '\0') continue; if (*p == '-') errx(1, "negative numbers aren't permitted."); errno = 0; val = strtouq(buf, &p, 10); if (errno) err(1, "%s", buf); for (; isblank((unsigned char)*p); ++p) ; if (*p != '\0') errx(1, "%s: illegal numeric format.", buf); pr_fact(val); } /* Factor the arguments. */ } else { for (; *argv != NULL; ++argv) { if (argv[0][0] == '-') errx(1, "negative numbers aren't permitted."); errno = 0; val = strtouq(argv[0], &p, 10); if (errno) err(1, "%s", argv[0]); if (*p != '\0') errx(1, "%s: illegal numeric format.", argv[0]); pr_fact(val); } } return 0; }