nixCraft Linux Forum

nixCraft

Linux 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

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-14-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
Sponsored Links
  #2 (permalink)  
Old 10-14-2005, 08:35 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 481
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
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 10-14-2005, 09:25 AM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
My distro: Suse, RHEL, Vista
Posts: 151
Rep Power: 4
sweta will become famous soon enough
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.
__________________
Friends - v-nessa - missyAdmin
Reply With Quote
Reply

Bookmarks


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 On

Similar Threads

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


All times are GMT +5.5. The time now is 11:08 PM.


Powered by vBulletin® Version 3.7.3 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

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