System Calls using C w/BASH


 
Thread Tools Search this Thread
Top Forums Programming System Calls using C w/BASH
# 1  
Old 03-23-2012
System Calls using C w/BASH

Hello all,

I'm attempting to write a basic application that appends an arbitrary list of txt files to one txt file.

So, for example, if the application is run like so:

appendFileToFile file1.txt file2.txt file3.txt the system command should, hopefully be, cat [theFiles] >> testfile.txt

I'm using C, then making a call to the system in BASH. However, I keep getting segmentation faults.

My code below:

Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    int i;
    char theCommand[] = "cat ";
  
    for (i = 1; i < argc; i++)
    {
        strcat(theCommand, argv[i]);
    }
    strcat(theCommand, " >> testfile.txt");
    system(theCommand);

    return 0;
}

Thanks for you time.
# 2  
Old 03-23-2012
Your buffer only has five bytes of space.

Writing beyond that is rampaging through stack space, overwriting every local variable behind it until it finds and corrupts your stack frame itself, crashing your program the next time it tries to do anything to the stack.

Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    int i;
    char theCommand[16384] = "cat ";
  
    for (i = 1; i < argc; i++)
    {
        strcat(theCommand, argv[i]);
    }
    strcat(theCommand, " >> testfile.txt");
    system(theCommand);

    return 0;
}

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-23-2012
Quote:
Originally Posted by Corona688
Your buffer only has five bytes of space.

Writing beyond that is rampaging through stack space, overwriting every local variable behind it until it finds and corrupts your stack frame itself, crashing your program the next time it tries to do anything to the stack.

Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    int i;
    char theCommand[16384] = "cat ";
  
    for (i = 1; i < argc; i++)
    {
        strcat(theCommand, argv[i]);
    }
    strcat(theCommand, " >> testfile.txt");
    system(theCommand);

    return 0;
}


D'oh! That would explain the segmentation fault, then.

Thank you very much for your help!

I had to put some white space in between the files because it was concatenating them together like: file.txtfile2.txtfile3.txt, is this the acceptable way of doing it (below)?

Code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    int i;
    char theCommand[16384] = "cat ";
  
    for (i = 1; i < argc; i++)
    {
        strcat(theCommand, argv[i]);
        strcat(theCommand, " ");
    }
    strcat(theCommand, " >> testfile.txt");
    system(theCommand);

    return 0;
 }

# 4  
Old 03-23-2012
It should work fine.

Try putting it before the command instead of after, though. Then you can just make your command "cat" instead of "cat ".
# 5  
Old 03-23-2012
I would suggest you prefer strncat to strcat. strncat is the secure sister of strcat:

Quote:
char * strncat ( char * destination, char * source, size_t num ); Append characters from string
Appends the first num characters of source to destination, plus a terminating null-character. If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
strncat - C++ Reference

usage of strncat is the same as strcat. The only difference is that strncat has an aditional third argument, that you use to tell the function the maximum amount of characters that should be copied to the destination buffer. This is very important to prevent your programs being vulnerable to buffer overflow errors.

Code:
for (i = 1; i < argc; i++)
    {
        strncat(theCommand, argv[i], sizeof(theCommand) - 1);
    }


Last edited by Corona688; 03-23-2012 at 03:44 PM.. Reason: horizontally stretching the window
These 2 Users Gave Thanks to pflynn For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

system calls in C

Hello, how would i be able to call ps in C programming? thanks, ---------- Post updated at 01:39 AM ---------- Previous update was at 01:31 AM ---------- here's the complete system call, ps -o pid -p %d, getpit() (2 Replies)
Discussion started by: l flipboi l
2 Replies

2. Shell Programming and Scripting

c++ calls bash

hi, i'm a noob i have a quuestion: is possible to call and run the bash script by c++ program? if so, is it posible in grafic? specially Qt ? thanks (8 Replies)
Discussion started by: 3.14.TR
8 Replies

3. UNIX for Dummies Questions & Answers

About system calls.

Hi all, I am new here . I want to know about system call in detail. As system calls are also function .How system identifies it.:) (2 Replies)
Discussion started by: vishwasrao
2 Replies

4. BSD

system calls

what is the functions and relationship between fork,exec,wait system calls as i am a beginer just want the fundamentals. (1 Reply)
Discussion started by: sangramdas
1 Replies

5. Programming

System calls

why user is not able to switch from user to kernel mode by writing the function whose code is identical to system call. (1 Reply)
Discussion started by: joshighanshyam
1 Replies

6. UNIX Desktop Questions & Answers

Using system calls

Hi, I'm new to UNIX system calls. Can someone share your knowledge as to how exactly system calls should be executed? Can they be typed like commands such as mkdir on the terminal itself? Also, are there any websites which will show me an example of the output to expect when a system call like... (1 Reply)
Discussion started by: ilavenil
1 Replies

7. Solaris

System calls ?

where can i find the differences in System calls between solaris and aix? also is it possible to find a comprehensive list of them? (1 Reply)
Discussion started by: TECHRAMESH
1 Replies

8. UNIX for Dummies Questions & Answers

System calls?

open, creat, read, write, lseek and close Are they all primitive? :confused: *Another Question: is there a different between a system call, and an i/o system call? (2 Replies)
Discussion started by: PlunderBunny
2 Replies

9. UNIX for Dummies Questions & Answers

System calls for cp and mv

Which system calls are made for operations cp and mv (2 Replies)
Discussion started by: gaurava99
2 Replies

10. UNIX for Dummies Questions & Answers

System Calls

What does the system call "dup" do? What is the difference between dup and dup2 I have a fair idea of what it does but I am confused when its coming down to the exact details... Please help me!:confused: (2 Replies)
Discussion started by: clickonline1
2 Replies
Login or Register to Ask a Question