I have written a program which uses a pid to check if the process is currently running and return a value based on the system call result.
But the program core dumps
Code:
#include <stdio.h>
#include <string.h>
int main( argc, argv )
int argc;
char * argv[];
{
int p_pid = 99;
char buff[1000];
const char *program_name = basename(argv[0]);
memset( buff, '\0', sizeof( buff));
sprintf(buff, " ps -ef | grep %s | grep %d ", program_name, p_pid);
printf("this is %s", buff);
if( system(buff) == 0 )
{
printf(" system call returns 0 ");
}
else
{
printf("system call is non zero");
}
}
What is the mistake in this code and is it portable in both unix/linux , is the method secure (grepping for program name )?