1

I am not sure where i am missing. I want to catch some characters from command line. I am using getopt but not sure how to copy from optarg. Please help me i am not very sure about character/string handling in c.

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

main(int argc , char *argv[]) {
    char *file;
    int opt;
    while ( ( opt = getopt(argc, argv, "f:") ) != -1 ){
    switch(opt){
        case 'f':
        file=(char *) malloc(2);
        strcpy(file,optarg);
        printf("\nValue of file is %c\n",file);
    break;
    default :
    return(1);
    }
}
return(0);
}
Abhishek Dave
  • 699
  • 1
  • 8
  • 21
  • 1
    possible duplicate of [How to get a value from optarg](http://stackoverflow.com/questions/1973742/how-to-get-a-value-from-optarg) –  Jul 21 '13 at 20:29
  • 1
    Also, [do **not** cast the return value of `malloc()`.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) –  Jul 21 '13 at 20:29
  • there's a terrific example on the [man page](http://linux.die.net/man/3/optarg) – levengli Jul 21 '13 at 20:31
  • @levengli But that's the *documentation!* What a blasphemy! –  Jul 21 '13 at 20:31
  • maybe he just switches from C to C++ often, so he casts the malloc in both languages – nio Jul 21 '13 at 20:32
  • 2
    your code is a bit fragile, if a user writes a longer option than 1 character you will get a memory overwrite. better to use strdup on the argument instead of strcpy then you dont need to malloc since strdup does that for you and allocates enough space regardless of length – AndersK Jul 21 '13 at 20:32
  • 2
    I humbly request forgiveness, @H2CO3, for using the "D" word :) – levengli Jul 21 '13 at 20:32
  • 2
    @levengli God may be forgiving to your soul and may he save you from fixing bugs in other developers' code! –  Jul 21 '13 at 20:34
  • @levengli - actually there isn't. there are examples of converting to an integer and printing the value, but not copying it. – tvanfosson Jul 21 '13 at 20:36
  • Seriously @tvanfosson?! Using some more [documentation](http://www.cplusplus.com/reference/cstdlib/atoi/) one can establish that `atoi` takes a `const char *` as an input. Even some more [documentaiton](http://www.cplusplus.com/reference/cstdio/printf/?kw=printf) will reveal the secret of printing a string to a file. – levengli Jul 21 '13 at 20:39
  • @levengli the OP states that he's unsure about character/string handling. If you're going to point to an example, it ought to be one that clearly demonstrates the answer to the question otherwise you'll risk confusing someone even more. – tvanfosson Jul 21 '13 at 20:46

2 Answers2

5

To fix an error that @claptrap suggests, replace:

file=(char *) malloc(2);
strcpy(file,optarg);

with safer:

file = strdup(optarg);

It will allocate and duplicate the string for you automatically, whatever length it has. You have the strdup defined in string.h which you already have included.

After you use the file string, you should free it from memory using:

free(file);

Strdup manpage. Also check out strncpy function which is safer to use than strcpy, because it knows how many character it can copy into target buffer before overflowing it.

nio
  • 5,141
  • 2
  • 24
  • 35
0

I think you should take a look of the offical manual.(https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html). You don't need to copy the string pointed from optarg, because optarg is a pointer point to argv[]. So look the end line of the manual. You will solve it!

Ning Ben
  • 35
  • 6