[plug] [OT] segmentation fault

Anthony J. Breeds-Taurima tony at cantech.net.au
Thu May 30 13:46:41 WST 2002


On 30 May 2002, Richard wrote:

> I know this is a little OT, but it's starting to drive me nuts, so I
> hope someone can help. I'm writing a small C program that takes
> arguments (file names) off the command-line, prepends a command using
> strcat(command, argv[x]), and then creates a pipe to the command and
> simply reads in and then prints out the results. When I run the program
> I keep getting segmentation fault errors. I have narrowed it down the
> the strcat(command, argv[x]) line, but I can't see any problem with it.
> Would any kind person like to offer a hint?
> 
> The exact line is next = strcat(command, argv[count + 1]);

command is malloced to be:
strlen("/path/to/commnd") + strlen(argv[count +1) + 3

???

Also why not:
-------
#!/usr/bin/perl -w

use strict;

my $command = "/path/to/command";

foreach my $filename (@ARGV) {
	my $output;
	open(CMD, "|$command $filename") or die "Burp $!\n";
	{
		local $/ = undef;
		$output = <CMD>;
	}
	close(CMD);
	print $output
}
-------

or 
--------
#!/bin/sh

for i in "$*" ; do
	/path/to/command $i
done
---------
or

find -type f -name '<mask>' -exec /path/to/command {} \;

or
find -type f -name '<mask>' | xargs -n 1 /path/to/command

Yours Tony

Jan 22-26 2003      Linux.Conf.AU       http://conf.linux.org.au/
         The Australian Linux Technical Conference!



More information about the plug mailing list