Sponsored Content
Top Forums Programming C system() how to send the output to an array?? Post 302388277 by Jess83 on Wednesday 20th of January 2010 02:01:26 AM
Old 01-20-2010
Yeah i got it to work with popen the following way

Code:
  FILE *in;
  extern FILE *popen();
  char buff[512];
  char newline[100];
  char nstat[512];
  
  strcpy(newline, "\r\n");  



  if (!(in = popen("netstat -n", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in)) {
    strcat(nstat, buff);
    }

  strcat(nstat, newline); 
 

  pclose(in);

Works fine on Gentoo but crashed to "segmentation fault" on Ubuntu and I foresee it do the same on MAC OS X due to memory protection... I dumped the core and ran it through gdb now I know its in the strcat() function so my understanding of it is that is that after buff is wrote once to nstat then nstat is then made into read only so when newline tries to be appended to nstat it can't then fails to segmentation fault right? How can I get around this? anyone??
 

10 More Discussions You Might Find Interesting

1. Programming

can we send arguments to system() call

Hi friends my C code is int main() { system("cp <source> <destination>"); } my question is how to set variables for <source> and <destination> how can we pass it to system() call. can you suggest me thankyou kingskar (6 Replies)
Discussion started by: kingskar
6 Replies

2. UNIX for Advanced & Expert Users

To send a message to another system

How to send a message to another unix terminal along with the date specified (4 Replies)
Discussion started by: aajan
4 Replies

3. UNIX for Dummies Questions & Answers

send message to a remote system

I am analyzing snoop output and want to send "Hello world" to a remote system. I want to see if the message received is encrypted or not. can I use ping to send a text message? like ping "helloworld" <IP Addr> Please help. Thank you (5 Replies)
Discussion started by: rakeshou
5 Replies

4. Solaris

how to send the output

Hi, how to send the output of a command directly to a file. For eg: am issuing metastat -a. How to send the output of this command to a file as well as mail. (3 Replies)
Discussion started by: rogerben
3 Replies

5. Shell Programming and Scripting

awk - Pre-populating an array from system command output

So, here's a scenario that requires the same logic as what I'm working on: Suppose that you have a directory containing files named after users. For awk's purposes, the filename is a single field-- something parse-friendly, like john_smith. Now, let's say that I'd like to populate an array in... (2 Replies)
Discussion started by: treesloth
2 Replies

6. Shell Programming and Scripting

System Output in to an Array or variable

hey guys in only new to scripting as such, but i have a problem. i want to take the output of a search i do in the command line to then be in a variable but only a certain part of the output. this this what im doing: -bash-2.05b$ ldapsearch -x '(dn:=dc)' dc|grep dc= # base... (1 Reply)
Discussion started by: jmorey
1 Replies

7. Filesystems, Disks and Memory

iostat output vs TPC output (array layer)

Hi Guys, I've been having some arguments with my colleagues about one thing. Always my thought was that as as far as disk performance is concern by looking at the output of the iostat command (AIX) you would be able to identify if you have a hot disk and then by moving some files out that disk... (3 Replies)
Discussion started by: arizah
3 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Reading Array And Send An Email

I am trying to find an example for reading an array instead of reading a file and send out an email in ksh. Can you please help is that possible? Algorithm #!/bin/ksh i=0 set -A ARR if then let i=$ ARR="A does n't match with B" fi if then let i=$ ARR="P does n't match with Q"... (11 Replies)
Discussion started by: Ariean
11 Replies

9. UNIX for Dummies Questions & Answers

Send files automatically to a remote system

Greetings, basically what I want to do is take the standard error from a cron file and store it on a file in a remote host (from Solaris to Linux). I want to create a cron file to do this everyday or put it on a script that will send it everyday to the other system (that doesnt matter). Any ideas... (2 Replies)
Discussion started by: picolin
2 Replies

10. Shell Programming and Scripting

I want query output to send on mail using table tag and output should be in table

#! /bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1"; pzTitle="$2"; pzMsg="$3"; pzFrom="$4"; pzTo="$5"; pzFiles="$6"; pzReplyTo="$7" ( ... (21 Replies)
Discussion started by: ankit.mca.aaidu
21 Replies
POPEN(3)						     Linux Programmer's Manual							  POPEN(3)

NAME
popen, pclose - process I/O SYNOPSIS
#include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FILE *stream); DESCRIPTION
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. The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. The mode argument is a pointer to a null-terminated string which must be either `r' for reading or `w' for writing. The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called popen(), unless this is altered by the command itself. Conversely, reading from a ``popened'' stream reads the com- mand's standard output, and the command's standard input is the same as that of the process that called popen. Note that output popen streams are fully buffered by default. The pclose function waits for the associated process to terminate and returns the exit status of the command as returned by wait4. RETURN VALUE
The popen function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory. The pclose function returns -1 if wait4 returns an error, or some other error is detected. ERRORS
The popen function does not set errno if memory allocation fails. If the underlying fork() or pipe() fails, errno is set appropriately. If the mode argument is invalid, and this condition is detected, errno is set to EINVAL. If pclose() cannot obtain the child status, errno is set to ECHILD. CONFORMING TO
POSIX.2 BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoided by calling fflush(3) before popen. Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. HISTORY
A popen() and a pclose() function appeared in Version 7 AT&T UNIX. SEE ALSO
fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), stdio(3), system(3) BSD MANPAGE
1998-05-07 POPEN(3)
All times are GMT -4. The time now is 06:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy