Pregunta

I'm trying to do some basic option parsing with getopt_long. My specific problem is a default int value being overwritten when the option is not used. I've read through docs and some explanations on getopt but haven't seen anything on preserving default values/optional arguments.

Compiling and running it, I expect a default value for port/p of 4567. When I specify no options, or use -p 5050, things work. When I use on of the other options (-t), the value for -p is also changed.

gcc -o tun2udp_dtls tun2udp_dtls.c
$ /tun2udp_dtls
port 4567 // correct
$ /tun2udp_dtls -p 5050
port 5050 // correct
$ ./tun2udp_dtls -t foo
port 0 // wtf!?

The code:

#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h> 

int main (int argc, char *argv[]) {
    // Vars
    char devname[128];
    int port = 4567;

    int c;
    while (1) {
        static struct option long_options[] = {
            {"tdev",  required_argument, NULL, 't'},
            {"port",  required_argument, NULL, 'p'},
            {0, 0, 0, 0}
        };
        int option_index = 0;
        c = getopt_long (argc, argv, "t:p:", long_options, &option_index);

        if (c == -1) break;
        switch (c) {
            case 't':
                strncpy(devname, optarg, sizeof(devname));
                devname[sizeof(devname)-1] = 0;
            case 'p':
                port = atoi(optarg);
            default:
                break;
        }
    }

    // Temporary debug printout
    printf("tdev '%s'\n", devname);
    printf("port %i\n", port);
}
¿Fue útil?

Solución

Missing break before case 'p':. That's why control reaches port=atoi(optarg); when "t" is processed; then atoi(optarg) returts 0 for non-numeric device name, and this 0 is assigned to port.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top