int main(int argc, char **argv) { struct stat st; netpgp_t netpgp; prog_t p; int homeset; int optindex; int ret; int ch; int i; (void) memset(&p, 0x0, sizeof(p)); (void) memset(&netpgp, 0x0, sizeof(netpgp)); homeset = 0; p.progname = argv[0]; p.numbits = DEFAULT_NUMBITS; if (argc < 2) { print_usage(usage, p.progname); exit(EXIT_ERROR); } /* set some defaults */ netpgp_setvar(&netpgp, "sshkeydir", "/etc/ssh"); netpgp_setvar(&netpgp, "res", "<stdout>"); netpgp_setvar(&netpgp, "hash", DEFAULT_HASH_ALG); netpgp_setvar(&netpgp, "format", "human"); optindex = 0; while ((ch = getopt_long(argc, argv, "S:Vglo:s", options, &optindex)) != -1) { if (ch >= LIST_KEYS) { /* getopt_long returns 0 for long options */ if (!setoption(&netpgp, &p, options[optindex].val, optarg, &homeset)) { (void) fprintf(stderr, "Bad setoption result %d\n", ch); } } else { switch (ch) { case 'S': netpgp_setvar(&netpgp, "ssh keys", "1"); netpgp_setvar(&netpgp, "sshkeyfile", optarg); break; case 'V': printf( "%s\nAll bug reports, praise and chocolate, please, to:\n%s\n", netpgp_get_info("version"), netpgp_get_info("maintainer")); exit(EXIT_SUCCESS); case 'g': p.cmd = GENERATE_KEY; break; case 'l': p.cmd = LIST_KEYS; break; case 'o': if (!parse_option(&netpgp, &p, optarg, &homeset)) { (void) fprintf(stderr, "Bad parse_option\n"); } break; case 's': p.cmd = LIST_SIGS; break; default: p.cmd = HELP_CMD; break; } } } if (!homeset) { netpgp_set_homedir(&netpgp, getenv("HOME"), netpgp_getvar(&netpgp, "ssh keys") ? "/.ssh" : "/.gnupg", 1); } /* initialise, and read keys from file */ if (!netpgp_init(&netpgp)) { if (stat(netpgp_getvar(&netpgp, "homedir"), &st) < 0) { (void) mkdir(netpgp_getvar(&netpgp, "homedir"), 0700); } if (stat(netpgp_getvar(&netpgp, "homedir"), &st) < 0) { (void) fprintf(stderr, "can't create home directory '%s'\n", netpgp_getvar(&netpgp, "homedir")); exit(EXIT_ERROR); } } /* now do the required action for each of the command line args */ ret = EXIT_SUCCESS; if (optind == argc) { if (!netpgp_cmd(&netpgp, &p, NULL)) { ret = EXIT_FAILURE; } } else { for (i = optind; i < argc; i++) { if (!netpgp_cmd(&netpgp, &p, argv[i])) { ret = EXIT_FAILURE; } } } netpgp_end(&netpgp); exit(ret); }
int main(int argc, char **argv) { netpgp_t netpgp; prog_t p; int homeset; int optindex; int ret; int ch; int i; (void) memset(&p, 0x0, sizeof(p)); (void) memset(&netpgp, 0x0, sizeof(netpgp)); p.progname = argv[0]; p.overwrite = 1; p.output = NULL; if (argc < 2) { print_usage(usage, p.progname); exit(EXIT_ERROR); } /* set some defaults */ netpgp_setvar(&netpgp, "hash", DEFAULT_HASH_ALG); /* 4 MiB for a memory file */ netpgp_setvar(&netpgp, "max mem alloc", "4194304"); homeset = 0; optindex = 0; while ((ch = getopt_long(argc, argv, "S:Vdeo:sv", options, &optindex)) != -1) { if (ch >= ENCRYPT) { /* getopt_long returns 0 for long options */ if (!setoption(&netpgp, &p, options[optindex].val, optarg, &homeset)) { (void) fprintf(stderr, "Bad option\n"); } } else { switch (ch) { case 'S': netpgp_setvar(&netpgp, "ssh keys", "1"); netpgp_setvar(&netpgp, "sshkeyfile", optarg); break; case 'V': printf( "%s\nAll bug reports, praise and chocolate, please, to:\n%s\n", netpgp_get_info("version"), netpgp_get_info("maintainer")); exit(EXIT_SUCCESS); case 'd': /* for decryption, we need the seckey */ netpgp_setvar(&netpgp, "need seckey", "1"); p.cmd = DECRYPT; break; case 'e': /* for encryption, we need a userid */ netpgp_setvar(&netpgp, "need userid", "1"); p.cmd = ENCRYPT; break; case 'o': if (!parse_option(&netpgp, &p, optarg, &homeset)) { (void) fprintf(stderr, "Bad option\n"); } break; case 's': /* for signing, we need a userid and a seckey */ netpgp_setvar(&netpgp, "need seckey", "1"); netpgp_setvar(&netpgp, "need userid", "1"); p.cmd = SIGN; break; case 'v': p.cmd = VERIFY; break; default: p.cmd = HELP_CMD; break; } } } if (!homeset) { netpgp_set_homedir(&netpgp, getenv("HOME"), netpgp_getvar(&netpgp, "ssh keys") ? "/.ssh" : "/.gnupg", 1); } /* initialise, and read keys from file */ if (!netpgp_init(&netpgp)) { printf("can't initialise\n"); exit(EXIT_ERROR); } /* now do the required action for each of the command line args */ ret = EXIT_SUCCESS; if (optind == argc) { if (!netpgp_cmd(&netpgp, &p, NULL)) { ret = EXIT_FAILURE; } } else { for (i = optind; i < argc; i++) { if (!netpgp_cmd(&netpgp, &p, argv[i])) { ret = EXIT_FAILURE; } } } netpgp_end(&netpgp); exit(ret); }