Need help with running the tar command using system() call in C | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Need help with running the tar command using system() call in C

Programming


Tags
tar command

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-10-2008
vsanjit vsanjit is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 10 November 2008, 6:04 AM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Need help with running the tar command using system() call in C

Hey everyone,

I've been trying to use the system(cmd) call in C to tar multiple files together.
When i do so i specify the absolute paths of the tar file as well as the files to be included in the tar file.

Eg: system("tar -cf /tmp/example.tar /mnt/john/1.xml");
system("tar -uf /tmp/example.tar /mnt/john/2.xml");
system("tar -uf /tmp/example.tar /mnt/john/3.xml");

I was expecting that upon extraction of example.tar i'd get 1.xml,2.xml and 3.xml. However upon extraction 'mnt' and 'john' directories are again created in the extraction directory and inside these reside the desired xml files.

Is there a way i can prevent these directories from being created and get only the xml files upon extraction?

Thanks.
Sponsored Links
    #2  
Old 11-10-2008
wempy's Avatar
wempy wempy is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 18 July 2013, 6:39 AM EDT
Location: Harpenden, UK
Posts: 219
Thanks: 0
Thanked 3 Times in 3 Posts
tar always preserves paths, so the only way to do what you want is to change to the directory where the files are, and add them from there using just thier name not path/name

Code:
system("tar -cf /tmp/example.tar 1.xml");
system("tar -uf /tmp/example.tar 2.xml");
system("tar -uf /tmp/example.tar 3.xml");

so either run your C program from the directory where the files are, or set that as being the current directory before issuing the system call
Sponsored Links
    #3  
Old 11-10-2008
vsanjit vsanjit is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 10 November 2008, 6:04 AM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Wempy !
How do i change the current directory using C ?

Does system("cd /mnt/john/"); work?
or do i use the chroot() system call ? But this would alter the system's root directory. I could change it back to / again though when my process is terminated.

Which one's preferred here?
    #4  
Old 11-10-2008
wempy's Avatar
wempy wempy is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 18 July 2013, 6:39 AM EDT
Location: Harpenden, UK
Posts: 219
Thanks: 0
Thanked 3 Times in 3 Posts
chroot is not what you want, it changes the / path to be the named dir, so any subsequent calls will have that as their / path - not a good idea

chdir() is what you need
man 2 chdir will give you information on chdir (on a linux machine anyway)
Sponsored Links
    #5  
Old 11-10-2008
vsanjit vsanjit is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 10 November 2008, 6:04 AM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Ahh..didn't know about this call at all.

Thanks so much man! Really appreciate it.
Sponsored Links
    #6  
Old 11-10-2008
migurus migurus is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 26 August 2014, 5:06 PM EDT
Location: US
Posts: 209
Thanks: 25
Thanked 20 Times in 19 Posts
Make sure you check result code, also take care of error/warning that tar spits out on stderr
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to call the System command twice in the same perl script... msrahman Shell Programming and Scripting 1 04-15-2011 05:49 AM
Running a script in system() call and want the script's output amio Shell Programming and Scripting 9 09-13-2010 02:02 AM
how to call dot c file using system command goraya430 Programming 4 05-14-2010 01:10 PM
how to call dot c file using system command goraya430 Shell Programming and Scripting 1 05-14-2010 09:58 AM
how to differentiate system call from library call muru UNIX for Advanced & Expert Users 2 07-19-2007 11:20 PM



All times are GMT -4. The time now is 09:52 PM.