C to Shell -- Summary and another question

Rose Oguz rose at baby.swmed.utexas.edu
Fri Jun 1 05:02:40 AEST 1990


A few people asked me to post the solution to my interfacing C code to a 
shell script question.  The consenus seems to be to use popen().  The 
syntax follows:

	FILE *popen(command, type)
	char *command, *type;

	command is a shell command line
	type is I/O mode: r or w

So, I used
	char f_names[20];
	char *ret_stat;
	long stat;
	FILE *fp
	...
	if ((fp = popen("ls *c", "r") != NULL)
	{
		while ((stat=fscanf(fp, "%s", f_names)) != EOF)
		{
			...
		}
	}
	pclose(fp);


I also changed the fscanf line to

		while ((ret_stat=fgets(f_names, 2, fp)) != NULL)


Neither seems to work.  A file pointer is returned, but the while loop
is never executed.  For the fscanf, -1 (EOF) is returned and for the
fgets, NULL (also, EOF since I opened the file with popen) is
returned.  I'm running this in my source directory; so, I know that
files exist.  What am I doing wrong?  Any ideas?



More information about the Comp.unix.questions mailing list