nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

using shell commands in C

This is a discussion on using shell commands in C within the Coding in General forums, part of the Development/Scripting category; I program in C. while doing some operations, i created some temporary files in my harddisk. However after the execution ...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Linux answers from nixCraft.


Coding in General Discussion on PHP/Perl/Python/Ruby/GNU C or C++. MySQL, PgSQL and (X)HTML or any other programming languages you want.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 14-10-2005, 07:31 AM
steve
Guest
 
Posts: n/a
Default using shell commands in C

I program in C. while doing some operations, i created some temporary files in my harddisk. However after the execution of the programs, i would like to remove the files. How can i remove these files? I only thought if it was possible to use shell command "rm" within the C program.
How is it possible?
Reply With Quote
  #2 (permalink)  
Old 14-10-2005, 08:35 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
Default

I suggest to use C API that removes file. Here is Linux GCC API (code) to remove file:
Code:
#include<stdio.h>
#include <unistd.h> /* API to remove file */

int main(){
        if ( unlink("/tmp/file.tmp") == 0 ){
                printf("File deleted\n");
        }
        else{
                printf("Error deleting file\n");
        }
}
You need to use unlink("/path/to/file"); API -- unlink deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.

However if you wish to use call shell command like rm then you need to use system() API - execute a shell command.

Code:
#include<stdio.h>
#include <stdlib.h> /* system() */

int main(){
     if ( system("rm -f /tmp/file.tmp") == -1 ){
            printf("Deleted\n");
     }
    else {
            printf("Cannot delete or call rm command");
    }
}
Use any one of the code. For more info read man pages
Code:
man 2 unlink 
man system
Reply With Quote
  #3 (permalink)  
Old 14-10-2005, 09:25 AM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
OS: Suse, RHEL, Vista
Posts: 199
Thanks: 12
Thanked 9 Times in 9 Posts
Rep Power: 7
sweta has a spectacular aura about sweta has a spectacular aura about
Default

well monk has pointed you right direction, howerver you should use popen() function insted of system()/exec() if you need to call a rm or other shell script. It is bit secure to use popen() then system(), IMPO

Quote:
The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.
__________________
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
See what commands have been run on your Linux server sweta Getting started tutorials 5 30-04-2008 03:38 AM
How to monitor RSH commands mformit Solaris/OpenSolaris 0 11-04-2008 09:44 PM
basic commands? seshaionline Getting started tutorials 7 24-03-2008 11:30 PM
Commands required to monitor a Solaris servers ramrishie Solaris/OpenSolaris 2 22-01-2008 05:17 AM
Log all SSH commands at ssh-client abhijith Getting started tutorials 4 24-11-2007 10:17 PM


All times are GMT +5.5. The time now is 06:06 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38