Sponsored Content
Full Discussion: System Calls using C w/BASH
Top Forums Programming System Calls using C w/BASH Post 302611727 by whyte_rhyno on Friday 23rd of March 2012 11:14:03 AM
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;
 }

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
STRCAT(3)						     Linux Programmer's Manual							 STRCAT(3)

NAME
strcat, strncat - concatenate two strings SYNOPSIS
#include <string.h> char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t n); DESCRIPTION
The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs. The strncat() function is similar, except that * it will use at most n bytes from src; and * src does not need to be null-terminated if it contains n or more bytes. As with strcat(), the resulting string in dest is always null-terminated. If src contains n or more bytes, strncat() writes n+1 bytes to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1. A simple implementation of strncat() might be: char * strncat(char *dest, const char *src, size_t n) { size_t dest_len = strlen(dest); size_t i; for (i = 0 ; i < n && src[i] != '' ; i++) dest[dest_len + i] = src[i]; dest[dest_len + i] = ''; return dest; } RETURN VALUE
The strcat() and strncat() functions return a pointer to the resulting string dest. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +--------------------+---------------+---------+ |Interface | Attribute | Value | +--------------------+---------------+---------+ |strcat(), strncat() | Thread safety | MT-Safe | +--------------------+---------------+---------+ CONFORMING TO
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. NOTES
Some systems (the BSDs, Solaris, and others) provide the following function: size_t strlcat(char *dest, const char *src, size_t size); This function appends the null-terminated string src to the string dest, copying at most size-strlen(dest)-1 from src, and adds a terminat- ing null byte to the result, unless size is less than strlen(dest). This function fixes the buffer overrun problem of strcat(), but the caller must still handle the possibility of data loss if size is too small. The function returns the length of the string strlcat() tried to create; if the return value is greater than or equal to size, data loss occurred. If data loss matters, the caller must either check the arguments before the call, or test the function return value. strlcat() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsd library. EXAMPLE
Because strcat() and strncat() must find the null byte that terminates the string dest using a search that starts at the beginning of the string, the execution time of these functions scales according to the length of the string dest. This can be demonstrated by running the program below. (If the goal is to concatenate many strings to one target, then manually copying the bytes from each source string while maintaining a pointer to the end of the target string will provide better performance.) Program source #include <string.h> #include <time.h> #include <stdio.h> int main(int argc, char *argv[]) { #define LIM 4000000 int j; char p[LIM]; time_t base; base = time(NULL); p[0] = ''; for (j = 0; j < LIM; j++) { if ((j % 10000) == 0) printf("%d %ld ", j, (long) (time(NULL) - base)); strcat(p, "a"); } } SEE ALSO
bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3), wcscat(3), wcsncat(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. GNU
2017-09-15 STRCAT(3)
All times are GMT -4. The time now is 10:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy