-4

I'm writing a program that checks who the user is, if it's the correct user logged in it will upload data to my FTP server. It encrypts the data using tar, than openssl. I think that part is correct however when passing system commands to the terminal I'm having an issue with strcpy. I keep getting an error that "there are to many arguments to function strcpy. Specifically where it says "strcpy(encrypt, "tar -cf /home/%s", p);" I think it's because I'm passing the variable p to it. Could anyone assist. Thank you.

int main(int argc, char *argv[])
{
  // checks to see who the user is 
  char *p=getenv("USER");
  if (p=NULL) return EXIT_FAILURE;
  printf("User is %s\n", p);
  char *usr;
  char *encrypt;
  char *upload;

  //encrypts folder using tar, then openssl AES 256 bit

  strcpy(encrypt, "tar -cf /home/%s", p);
  system(encrypt);
  strcpy(encrypt, "openssl enc -aes-256-cbc -in FILE.NAME -out file.enc");
  system(encrypt);


  // uploads data to ftp server using curl
  strcpy(upload, "curl -T /PATH/ SERVER_ADDRESS");
  system(upload);

  return 0;
}
N3Xxus6
  • 11
  • 2
  • 2
    You have already found the problem yourself, so what's the question here? Stop passing 3 parameters to strcpy. – Lundin Dec 22 '21 at 08:33
  • 1
    Oh you also haven't allocated any memory to `strcpy` into, so nothing in this program makes any sense. You have to study arrays, pointers and strings before writing this program. – Lundin Dec 22 '21 at 08:35
  • @Lundin Maybe make an complete answer – Mike Dec 22 '21 at 08:36
  • `p=NULL` is also nonsense, like the compiler told you in the warnings you didn't read. – Lundin Dec 22 '21 at 08:36
  • @Mike No, I try not to answer questions where the answer is to study the basics of C in a beginner level book... – Lundin Dec 22 '21 at 08:37
  • Hmmm It's almost chrismas – Mike Dec 22 '21 at 08:39
  • @Lundin, I didn't get a compiler error for p=NULL lol. Only the error I pointed out, my question is how do I pass the p variable to strcpy so when it runs tar it knows the username of the user – N3Xxus6 Dec 22 '21 at 08:40
  • 1
    @N3Xxus6 [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) As for strcpy, you should make a separate call to strcat appending the user afterwards. But since you didn't allocate any memory anywhere, the whole program will crash and burn no matter. – Lundin Dec 22 '21 at 08:46
  • 1
    [Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer](https://stackoverflow.com/questions/37549594/crash-or-segmentation-fault-when-data-is-copied-scanned-read-to-an-uninitializ) – Lundin Dec 22 '21 at 08:46

1 Answers1

3

you used strcpy incorrectly as compiler log the error this is strcpy prototype

char * strcpy ( char * destination, const char * source );

if you want to copy from source to destination when source is a pattern -> you need to replace strcpy with sprintf or snprintf for more detail you can check this https://linux.die.net/man/3/snprintf

additional, you haven't allocateed for

 char *usr;
  char *encrypt;
  char *upload;

use malloc or calloc for these things before use

long.kl
  • 670
  • 4
  • 12