-1

when i use directory n gpg error happens: gpg: no such directory or file but it has i have:

char directory[100]="/tmp/hello.txt"

there is a code

int s = system("echo password | gpg -c --passphrase-fd 0 directory");

For information if I write instead of directory '/tmp/hello.txt' it will work maybe the problem with ' '

John
  • 1
  • 2
  • 1
    Btw, calling external tools with `system()` is something you should avoid if possible (manageability of dependencies is problematic, a shell is forked with it, and so on). In this case, look at [GPGme](https://www.gnupg.org/(es)/related_software/gpgme/index.html), it *might* suit your needs. –  Oct 17 '15 at 14:54
  • also gpg might not be in your path – arved Oct 17 '15 at 15:22
  • Don't do this on a multi user machine: Everybody will be able to read your passphrase while GnuPG is executed! – Jens Erat Oct 17 '15 at 15:41

2 Answers2

1

C doesn't automatically replace occurences of an identifier with its value. The preprocessor does that, though. You could define a macro

#define directory "/tmp/hello.txt"

and then do

int s = system("echo password | gpg -c --passphrase-fd 0 " directory);

This concatenates the strings at "preprocessing time", even before compile time. Another way would be to use strncat to concatenate the two strings at runtime:

char str[128] = "echo password | gpg -c --passphrase-fd 0 ";
strncat(str, directory, sizeof(str) - strlen(str));

To be able to reappend the string you could store the strlen(str), write a null byte to it every time and then call strncat:

void append(const char* app) {
    static const size_t len = strlen(str);

    str[len] = '\0';
    strncat(str, app, sizeof(str) - len);
}
Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • Thanks, but my directory depends on buffer so the directory is not constant it changes – John Oct 17 '15 at 14:46
  • so is there other solution] – John Oct 17 '15 at 14:59
  • @John You can use it: `int s = system(str);` where `str` refers to the string in my answer. – cadaniluk Oct 17 '15 at 15:00
  • @John, please read some introductory text about C. This is not a "C interactive tutorial" site and there are plenty of resources out there ... –  Oct 17 '15 at 15:01
1

this is a duplicate question from: pass parameter using system command

shows how to pass local variable contents to the system command

Here is the suggested code, Note: username and password are local variables:

char cmdbuf[256];
snprintf(cmdbuf, sizeof(cmdbuf), 
      "net use x: \\\\server1\\shares /user:%s %s", 
      username, password);
int err = system(cmdbuf);
if (err) 
{ 
    fprintf(stderr, "failed to %s\n", cmdbuf); 
        exit(EXIT_FAILURE); 
}
Community
  • 1
  • 1
user3629249
  • 16,402
  • 1
  • 16
  • 17