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?

5
  • Why you don't want to use strcpy()? Commented Sep 28, 2014 at 6: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. Commented Sep 28, 2014 at 6: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. Commented Sep 28, 2014 at 7:01
  • why are there twice as many { as }? Commented Sep 28, 2014 at 7:02
  • @thang because I was typing too fast. all fixed Commented Sep 28, 2014 at 9:17

2 Answers 2

2

data->infile = strdup(temp_path);

Sign up to request clarification or add additional context in comments.

3 Comments

strdup uses strcpy.
@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().
strdup() has to perform a "physical copy" in any case.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.