"sh: 1: Syntax error: word unexpected (expecting") ")" when using dns function

0
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>

void verbose() {
     printf("Ói eu aqui. \n"); 
exit(0);
}//FECHA verbose

void ajuda() {
    printf("Ajuda? Nem a pau \n"); 
    exit(0);
}//FECHA ajuda

void dns(char* url) {
    char cmd[100];
    sprintf(cmd, "host %s", url);
    system(cmd);
    exit(0);
 }// FECHA dns

void main (int argc, char** argv) {

    extern int optind, opterr, optopt;
    extern char *optarg;
    int i;
    char optc = 0;

    struct option OpcoesLongas[] = {
        {"help", no_argument, NULL, 'h'},
        {"verbose", no_argument, NULL, 'v'},
        {"url", required_argument, NULL, 'u'}
    };// FECHA struct

    if(argc == 1) {
        printf("Sem argumentos não dá. \n");}

    while(1){
        switch (optc = getopt_long(argc, argv, "hv:u:", OpcoesLongas, NULL)) {
            case 'h':
                ajuda();
                break;
            case 'v':
                verbose();
                break;
            case 'u':
                dns(argv[*optarg]);
            default:
                exit(0);
        }//FECHA switch
    }//FECHA while
 }//FECHA main
    
asked by anonymous 11.08.2016 / 01:42

1 answer

0

You are accessing a argv element that does not exist. You are not calling the dns() function with what you expect ...

            case 'u':
                printf("optarg tem o valor \"%s\"\n", optarg);
                printf("*optarg tem o valor %d\n", *optarg);
                printf("Chamando dns() com o parametro \"%s\"\n", argv[*optarg]);
                dns(argv[*optarg]); /* OOPS */
                /* NO BREAK -- FALLTHROUGH */
    
11.08.2016 / 10:55