0

I have code that looks something like this:

typedef struct
{
  char mode;       //e = encrypt, d = decrypt
  char* infile;    //name of infile
  char* outfile;   //name of outfile
  char* password;  //password string
} cipher_t;

int check_files(cipher_t *data)
{
  char temp_path[] = "temp-XXXXX";

  if( /** infile == stdin *//)
  {
    mkstemp(temp_path);
    *data.infile = temp_path;
  }

  //do stuff and return

}

Basically, what I'm trying to do is detect if the user wants to input data from stdin and if so make a temporary file where I can do stuff.

The problem here is that when I set my infile path as shown above, that data is not retained upon exiting the function because it's a local variable. So when I exit the function the temporary file path is lost in the structure. Other than physically copying the string, is there anything else I can do to retain the value?

audiFanatic
  • 2,296
  • 8
  • 40
  • 56
  • Why you don't want to use `strcpy()`? – ani627 Sep 28 '14 at 06:02
  • you have not allocated any space to the pointers that you are using, i.e. cipher_t *data and char *infile. Both of these are pointers and unless you allocate them some space by using malloc, they will keep on giving segmentation fault since they dont have any valid address to point to. – Abhishek Choubey Sep 28 '14 at 06:16
  • *"Other than physically copying the string, is there anything else I can do to retain the value?"* No, strings created on the stack cannot be preserved without copying the string to off-stack storage. So your only other option is to not put the string on the stack in the first place, e.g. change the declaration of `infile` to `char infile[32]` so that the storage for the string is in the structure itself. – user3386109 Sep 28 '14 at 07:01
  • why are there twice as many { as }? – thang Sep 28 '14 at 07:02
  • @thang because I was typing too fast. all fixed – audiFanatic Sep 28 '14 at 09:17

2 Answers2

2

data->infile = strdup(temp_path);

wuqiang
  • 634
  • 3
  • 8
  • [strdup](http://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c) uses strcpy. – ani627 Sep 28 '14 at 06:10
  • 1
    @Ani The documentation doesn't say anything about that. It might use, might not, as long as it work as intented its up to the compiler implementation of the libc to decide, not to the C standard. In fact it doesn't make much sense to use `strcpy()` if you already know the size of what you are about to copy (requirement to alloc the buffer for the new string), so an optimal implementation of `strdup()` would only envolve `strlen()`, `malloc()` and `memcpy()`. – Havenard Sep 28 '14 at 06:33
  • 1
    `strdup()` has to perform a "*physical copy*" in any case. – alk Sep 28 '14 at 09:33
1

Other than physically copying the string, is there anything else I can do to retain the value?

You can declare it static, which would let the "string" live for the whole program's live time.

static char temp_path[] = "temp-XXXXX";

But be aware, that temp_path exists only once, so accessing it by multiple threads might lead to confusion.

alk
  • 69,737
  • 10
  • 105
  • 255
  • This also means that the `check_files` function can only be called once in the entire lifetime of the program. It doesn't matter whether it's multi-threaded or not. Calling the function a second time is guaranteed to overwrite the first filename. – user3386109 Sep 28 '14 at 14:41