I'm writing a program to turn the two commands "gunzip file.tar.gz" then "tar -xvf file.tar" into "x file.tar.gz", but I've ran into a problem I can't find a solution to, here are the outputs I get depending on if I use a gunzip -f flag in my system() call or not:
if I use: (void)system("gunzip ", store );
> x file.tar.gz
gzip: compressed data not read from a terminal. Use -f to force decompression.
For help, type: gzip -h
store 1: file.tar.gz
tar: option requires an argument -- 'f'
Try `tar --help' or `tar --usage' for more information.
store 2: file.tar
if i use: (void)system("gunzip -f ", store );
> x file.tar.gz
^Cstore 1: file.tar.gz
tar: option requires an argument -- 'f'
Try `tar --help' or `tar --usage' for more information.
store 2: file.tar
(note that ^C is where I had to press ctrl + c as the program just sits there - as far as I know - doing nothing).
Here's my code:
Code:
/* zippy */
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] )
{
if ( argc != 2 ) {
printf("Usage: unzippy filename.tar.gz\n");
}
else {
/* variables */
int x, y;
char store[40];
char *ptr = argv[1];
/* expressions */
y = strlen( argv[1] );
/* store argv[1] for gunzip */
for (x=0; x<y; x++) {
store[x] = *ptr;
ptr = ptr + 1;
store[x+1] = '\0';
}
(void)system( "gunzip ", store ); // or (void)system( "gunzip -f ", store );
printf("store 1: %s\n", store );
// printf("*ptr: %c\n", *ptr );
/* reset pointer ready for second for loop */
ptr = argv[1];
/* store argv[1]-3 for tar -xvf */
for (x=0; x<y-3; x++) {
store[x] = *ptr;
ptr = ptr + 1;
store[x+1] = '\0';
}
(void)system( "tar -xvf ", store );
printf("store 2: %s\n", store );
// printf("*ptr: %c\n", *ptr );
}
return 0;
}
I don't know if the problem is something to do with my code or something to do with gunzip and if it is to do with gunzip if I can even solve it with my code? I've tried for a while to find more info and read the gunzip man file, ran "gunzip file.tar.gz" outside of my program and found that it works normally producing file.tar so I suspect the problem is related to system()?
Thanks in advance for any help with the problem itself or for any advice