Thread: gzip system() linux - program to unite 2 commands into 1 command

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    UK
    Posts
    2

    gzip system() linux - program to unite 2 commands into 1 command

    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
    Last edited by cfree; 09-15-2013 at 12:08 PM. Reason: code comment clarification

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Well the basic problem seems to be that you haven't read the manual page for system()

    It takes a single string.
    It does not take a comma separated list of strings and magically transforms them into a usable command line.

    Try using say sprintf() to construct a string from all your substrings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Tar should be able to handle file.tar.gz directly... at least I had no problem running something like
    Code:
    tar -zxf ../mpc-1.0.1.tar.gz
    on linux
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2013
    Location
    UK
    Posts
    2
    Thanks for your help guys

    I thought it would have given compiler errors if I'd used system() wrong; should have followed that inkling and looked up system() heh.

    Completely missing that tar can do all the gunzip stuff is about as dumb as it gets, still I'll use it [my program] to get rid of needing to use the options (I always forget that stuff) and add unrar to it too

    I was misinformed that the problem was using argv[1] and that I should first put argv[1] into an array and then pass that to system(), so on the basis of a combination of my own stupidity and misinformation I've learned about pointers, arrays, for loops, and a couple of functions ahead of time (we just covered string constants on my course), feels very much like coming up smelling of roses hehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execute system command/script/etc. (Linux)
    By cnewbie1 in forum C Programming
    Replies: 5
    Last Post: 12-22-2009, 04:47 PM
  2. Calling System Commands from within your program
    By xiver0m in forum C Programming
    Replies: 13
    Last Post: 04-18-2007, 12:28 PM
  3. Running linux system commands
    By Creini in forum C Programming
    Replies: 2
    Last Post: 05-27-2006, 06:56 AM
  4. system commands. deletion of file after program terminates
    By xddxogm3 in forum C++ Programming
    Replies: 12
    Last Post: 11-24-2003, 10:41 PM
  5. Replies: 1
    Last Post: 03-11-2003, 05:36 PM

Tags for this Thread