0

I have written some code to get a updated GPG files from the path /home/user/GPG_FILES. It works fine when the files exist in the path but my issue is when the path doesn't contain any GPG files at that time it prints ("ls: cannot access *.gpg: No such file or directory") in my shell which I don't want it to print in my shell. I want to handle this error in my code by having my own error description.

int main()
{
    while( 1 )
    {
        char buffer[ 1024 + 1 ];
        char stip_file_path[ 2048 + 1  ];
        char gpg_file_name[ 2048 + 1  ];
        FILE *p;

        memset( stip_file_path, 0, sizeof( stip_file_path ));
        sprintf( stip_file_path, "%s/%s/%s", "home", "user", "GPG_FILE" );
        chdir( stip_file_path );
        p = popen("ls -ltr *.gpg| grep ^- | tail -1 | awk '{ print $(NF) }'", "r");
        if(!p)
        {
            printf("Error opening pipe");
            return -1;
        }
        memset( buffer, 0, sizeof( buffer ));
        char *ptr = buffer;
        while(!feof(p))
        {
            fread( ptr, 1, 1, p );
            ptr++;
        }
        if( pclose(p) == -1 )
        {
            printf("Error opening close");
            return -1;
        }
        printf("get_updated_file success(%s)", gpg_file_name );
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Deepak Mj
  • 21
  • 1
  • 2
    Please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Feb 06 '19 at 08:20
  • 1
    `opendir()` and `readdir()` are your friends. – Swordfish Feb 06 '19 at 08:22
  • Also, never return negative numbers from `main`, especially on a POSIX system (like Linux or macOS). Either return `0` on success (which is implicitly done if you're missing a final `return` statement) or a small positive number on error. – Some programmer dude Feb 06 '19 at 08:22
  • And why do you print `gpg_file_name`? It's never initialized or used anywhere else, which means that your final `printf` could lead to *undefined behavior*. – Some programmer dude Feb 06 '19 at 08:23
  • Lastly, C or C++? They are two very different languages with very different rules. Only use the tag of the language you're actually programming in. – Some programmer dude Feb 06 '19 at 08:24
  • Possible duplicate of [how to control popen stdin, stdout, stderr redirection?](https://stackoverflow.com/questions/280571/how-to-control-popen-stdin-stdout-stderr-redirection) – KamilCuk Feb 06 '19 at 08:32
  • If you don't want to see the error messages, you can use `ls -ltr *.gpg 2>/dev/null | ` to send the errors to `/dev/null`. If you want to know whether there are any such error messages, you have to work a lot harder; it is not clear that `popen()` is the correct choice then. You could drop the `l` from `-ltr` so you only get the names in order. You would then simplify the rest of the shell script. Do you ever plan to have directories or other non-regular files in the directory? How much do you need to worry about that? Consider using `readdir()` et al in your code, instead of `popen()`. – Jonathan Leffler Feb 06 '19 at 08:33
  • Parsing `ls` output is wrong and your script is just confusing (filtering files using `grep ^-` ??) , just do `find . -type f -name '*.gpg' -maxdepth 1 | wc -l`. You will get just a single integer, which you can just even `scanf`. – KamilCuk Feb 06 '19 at 08:34
  • @KamilCuk: agree parsing `ls` output is risky, but the `grep` is looking for regular files and the `-tr` parts of the `ls` options are sorting the files in time order, which `find` won't do. So, your replacement isn't correct; the original gets the name of the most recent `.gpg` file. Your proposed `find … | wc -l` does a wholly different job. – Jonathan Leffler Feb 06 '19 at 08:37
  • Incidentally, you should probably include `-d` in the `ls` options so that the command lists the directory by name, rather than expanding its contents. – Jonathan Leffler Feb 06 '19 at 08:38
  • @JonathanLeffler That's good advice in in general, but in practice not usually necessary when you use a wildcard that will only match ordinary files, like `*.gpg`. – Barmar Feb 06 '19 at 09:00
  • @Barmar — if there’s a directory `a.gpg`, the wildcard `*.gpg` will match it. – Jonathan Leffler Feb 06 '19 at 09:01

0 Answers0