+static void parse_arguments(int argc, char **argv) {
+ int option;
+
+ while (-1 != (option = getopt(argc, argv, "p:h?"))) {
+ switch (option) {
+ case 'p': {
+ char *position;
+
+ /* -p must have the format host:port. */
+ if (NULL == (position = strchr(optarg, ':'))
+ || position == optarg
+ || 0 == strlen(position + 1)
+ || 0 >= atoi(position + 1)
+ || 0xffff < atoi(position + 1)) {
+ fprintf(stderr, "-p host:port\n");
+ exit(EXIT_FAILURE);
+ }
+
+ use_proxy_host = malloc((size_t)(position - optarg) + 1);
+ if (NULL == use_proxy_host) {
+ perror("malloc()");
+ exit(EXIT_FAILURE);
+ }
+ memcpy(use_proxy_host, optarg, (size_t)(position - optarg));
+ use_proxy_host[position - optarg] = '\0';
+
+ use_proxy_port = malloc(strlen(position + 1) + 1);
+ if (NULL == use_proxy_port) {
+ perror("malloc()");
+ exit(EXIT_FAILURE);
+ }
+ strcpy(use_proxy_port, position + 1);
+
+ break;
+ }
+ case 'h':
+ default: /* '?' */
+ print_usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ if (optind >= argc) {
+ print_usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+}
+static void print_usage(const char *argv) {
+ fprintf(stderr, "Usage: %s [-p host:port] port\n", argv);
+ fprintf(stderr, "\n");
+ fprintf(stderr, "-p proxy hostname and port\n");
+}
+