1

I am having a bit of trouble using gzip to compress a file from a C program. I am using this line of code execl("/usr/bin/gzip", "-f", filePath, NULL); to compress the file given by filePath. It works fine the first time (i.e. when there is no existing .gz file), but in subsequent executions of the program I am prompted whether or not I would like to overwrite the existing .gz file.

Am I using execl() incorrectly, because I am pretty sure that the -f switch forces gzip to overwrite without a prompt?

TommoM
  • 23
  • 4
  • 3
    Can you replicate the problem outside of C? – tadman Oct 15 '22 at 08:11
  • I tried not overwriting it so that I would have `/tmp/file` and `/tmp/file.gz`. Then I executed `gzip -f /tmp/file` and it successfully overwrote the existing .gz file without a prompt – TommoM Oct 15 '22 at 08:25

1 Answers1

4

You need to provide argv[0] too. So, like this:

execl("/usr/bin/gzip",
      "gzip", "-f", filePath,
      NULL); 

In your code, you set argv[0] to be "-f", so it is essentially ignored.

Gzip is one of the few programs where argument 0 actually matters, as gzip and gunzip are usually (at least on Unixy systems) a symbolic link to the same binary, and argument 0 is used to determine the default mode. If your "-f" worked, it means gzip is default.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • `argv[0]` does matter in this case, since gzip looks at the name used to invoke it to decide whether to compress or decompress. So the use of the program name at `argv[0]` is more than customary here. – Mark Adler Oct 15 '22 at 15:47
  • @MarkAdler Thanks, edited. I kinda knew that, having used *busybox* extensively, so I did not verify now, I hope you're right :-D – hyde Oct 16 '22 at 06:49