lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Dirk Laurie once stated:
> On a Linux system, I get:
> 
> $ lua
> Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> > ok,stream = pcall(io.popen,"unknown_program")
> > sh: 1: unknown_program: not found
> print(ok,stream)
> true    file (0x9be9800)
> 
> I.e. I can't tell from the return values that the program was not found,
> only from the message on stderr.
> 
> Checking that io.open works is no good either. If `unknown_program` exists
> but is not executable, I still get true and a file, with error message
> on stderr.
> 
> Workaround:
> 
> > prog="unknown_program"
> > which = io.popen("which "..prog)
> > if which:read() then stream=io.popen(prog) end
> 
> Am I missing something?

  It's Linux.  Run the following:

	#include <stdio.h>
	#include <stdlib.h>
	#include <errno.h>
	
	int main(void)
	{
	  FILE *fp;
	  
	  fp = popen("unknown_command","r");
	  if (fp == NULL)
	    perror("popen");
	  else
	    pclose(fp);
	  
	  return EXIT_SUCCESS;
	}

You'll get:

	sh: unknown_command: command not found

and not 

	popen: command not found

  The Linux man page states:

       The popen function returns NULL if the fork(2) or pipe(2) calls fail,
       or if it cannot allocate memory.

but it says nothing about if the resulting exec() fails.  So the pipe is
created, the process forks, and thus, popen() is happy.  

  -spc