I am trying to implement ps command in C and I am getting a lot of strange errors because of this.
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/procfs.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int fdproc;
DIR *dirp;
struct dirent *DirEntry;
struct elf_prpsinfo pinfo;
/*
* open the /proc directory for reading process statuses
*/
int main()
{
if ((dirp = opendir("/proc")) == (DIR *)NULL)
{
perror("/proc");
exit(1);
}
/*
* loop for each process in the system
*/
while(DirEntry = readdir(dirp))
{
if (DirEntry->d_name[0] != '.')
{
strcpy(procbuf, "/proc/");
strcat(procbuf, DirEntry->d_name);
}
if ((fdproc = open(procbuf, O_RDONLY)) < 0)
{
continue;
}
/*
* get the ps status for the process
*/
if (ioctl(fdproc, PIOCPSINFO, &pinfo) < 0)
{
close(fdproc);
continue;
}
/* process the pinfo stuff here, see
/usr/include/sys/procfs.h for details */
close(fdproc);
}
}
I found this code on the Internet somewhere and after doing some of my stuff in this I am still getting some strange errors like:
‘PIOCPSINFO’ undeclared
‘procbuf’ undeclared
which I think is something pre-defined in Ubuntu. Any suggestions?
for your errors problem:
define char *procbuf
and then try to check whether /proc directory is mounted or not so that you have access or of pid name directories attributes.also read more about ioctl function's argument requests so that you come to know what ind of approach can take you further in this problem.