int main (int argc, char **argv) { struct ftpdir_host *hosts = 0; char *default_host = 0; int interpret = 0; struct ftp_conn_params params = { 0 }; char *sep = "\n"; char *pfx = "%s:%s:\n"; int use_pfx = 0; int errs = 0; /* Parse our options... */ error_t parse_opt (int key, char *arg, struct argp_state *state) { switch (key) { case ARGP_KEY_ARG: { char *host, *dir; if (state->next < state->argc) use_pfx = 1; /* Multiple arguments. */ dir = index (arg, ':'); if (dir) { host = arg; *dir++ = '\0'; if (*host == '\0') /* An argument of `:' */ host = default_host; } else { host = default_host; dir = arg; } if (host) { struct ftpdir_host *h = get_host_conn (host, ¶ms, &hosts); if (h) { if (state->arg_num > 0) fputs (sep, stdout); if (use_pfx) printf (pfx, h->name, dir); if ((use_pfx && *pfx) || (state->arg_num > 0 && *sep)) fflush (stdout); if (interpret) errs |= ftpdir2 (dir, h); else errs |= ftpdir (dir, h); } errs = 1; } else { error (0, 0, "%s: No default host", arg); errs = 1; } } break; case ARGP_KEY_NO_ARGS: if (default_host) { struct ftpdir_host *h = get_host_conn (default_host, ¶ms, &hosts); if (h) errs |= ftpdir (0, h); } else { error (0, 0, "No default host"); errs = 1; } break; return EINVAL; case 'u': params.user = arg; break; case 'p': params.pass = arg; break; case 'a': params.acct = arg; break; case 'h': default_host = arg; break; case 'D': conn_hooks.cntl_debug = cntl_debug; break; case 'P': pfx = arg; use_pfx = 1; break; case 'S': sep = arg; break; case 'i': interpret = 1; break; default: return ARGP_ERR_UNKNOWN; } return 0; } struct argp argp = {options, parse_opt, args_doc, doc}; argp_parse (&argp, argc, argv, 0, 0, 0); if (errs) exit (10); else exit (0); }
void main(int argc, char *argv[]) { char *host = NULL; char *user = NULL; char *pass = NULL; char *root = NULL; char mode; char *fspec = NULL; int opt; int i; while ((opt = getopt(argc,argv,"ail:p:r:v:")) != -1) { if (opt == '?') { fprintf(stderr,"Unknown option %s\n",argv[optind]); exit(2); } switch (optopt) { case 'a' : mode = 'A'; break; case 'i' : mode = 'I'; break; case 'l' : if (opt == ':') { fprintf(stderr,"Missing value for -l\n"); exit(2); } user = optarg; break; case 'p' : if (opt == ':') { fprintf(stderr,"Missing value for -p\n"); exit(2); } pass = optarg; break; case 'r' : if (opt == ':') { fprintf(stderr,"Missing value for -r\n"); exit(2); } root = optarg; break; case 'v' : if (opt == ':') ftplib_debug++; else ftplib_debug = atoi(optarg); break; } } for (i=1;i<argc;i++) { if (*argv[i] == '-') { switch (argv[i][1]) { case 'l': user = argv[++i]; break; case 'p': pass = argv[++i]; break; case 'd': ftplib_debug++; break; case 'r': root = argv[++i]; break; default: usage(argv[0]); } } else if (host == NULL) host = argv[i]; else if (fspec == NULL) fspec = argv[i]; else usage(argv[0]); } ftpInit(); if (user == NULL) { user = "******"; if (pass == NULL) { char *u,h[64]; u = getenv(ENV_USER); if (gethostname(h,64) < 0) { perror("gethostname"); exit(2); } if ((u != NULL) && (h != NULL)) { static char xxx[64]; sprintf(xxx,"%s@%s",u,h); pass = xxx; } } } if ((host == NULL) || (user == NULL) || (pass == NULL)) usage(argv[0]); if (!ftpdir(host,user,pass,root,fspec)) exit(2); exit(0); }