The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 08-24-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,748
try man getutxent - this should be on your system. It automatically opens a file:
/etc/utmp
/etc/utmpx or
/var/adm/wtmp

/etc/utmp may not exist depending on how you set up the system. If you disabled any accounting, then it may not exist.

Also try using popen and a shell script - I just made up a function foo() - you get to change it and the command it uses:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
pid_t foo(char *pid_to_find)
{
    char cmd[296]={0x0}; /* 40 + 256 */
    char tmp[256]={0x0};
    FILE *in=NULL;
    int retval=0;
    pid_t the_pid_I_found=0;

    snprintf(cmd,sizeof(cmd)-40,
       "ps -ef | grep  %s | awk '{print $2}'",pid_to_find);
    in=popen(cmd, "r");
    if(in==NULL)
    {
         perror("Shell execution error");
         exit(EXIT_FAILURE);
    }
    while(fgets(tmp,4096,in)!=NULL)
    {
         the_pid_I_found=atol(tmp);
         break;
    }
    retval=pclose(in);
    return the_pid_I_found;
}