0
int main(int argc, char** argv) {
    int file_size_limit = atoi(argv[1]);

    char buf[BUFSIZ];
    snprintf(buf, sizeof(buf), "find . -size +%dc -printf %M ", file_size_limit);
    system(buf);

    return 0;
}

The above code does not work and gives the warning: unknown conversion type character ‘M’ in format, how am I able to display only file permission with this structure. Thanks in advance!

I have tried to and double quotes on %M but this also gives an error, I cannot find a way to encode the %M deceleration into the find code since the whole command is passed in as an argument of the snprintf function

  • There really is a C way to do this instead of using C as a bash script. – tadman Jan 22 '23 at 23:42
  • [`nftw()`](https://man7.org/linux/man-pages/man3/ftw.3.html) is your friend. – Shawn Jan 22 '23 at 23:44
  • But you need to re-read the [`snprintf()` documentation](https://en.cppreference.com/w/c/io/fprintf). – Shawn Jan 22 '23 at 23:47
  • 1
    Does this answer your question? [How to escape the % (percent) sign in C's printf](https://stackoverflow.com/questions/1860159/how-to-escape-the-percent-sign-in-cs-printf) – Shawn Jan 22 '23 at 23:51

1 Answers1

0

If you want to printf func to print a %, you need to prepend it with another %

snprintf(buf, sizeof(buf), "find . -size +%dc -printf %%M ", file_size_limit);

Also i suggest you to just printf to stdout the cmdline you just builded, you can then test the correctness of your code.

snprintf(buf, BUFSIZ, "find . -size +%dc -printf %%M ", file_size_limit);
fputs( buf, stdout );