L
lovecreatesbea...
/*thank you for your time*/
/*read in ps(1) result*/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#define M 256
#define N 32
#define str(n) #n /*from pete's*/
#define xstr(n) str(n)
int main(void)
{
char uid[N + 1];
pid_t pid;
pid_t ppid;
char stime[N + 1];
char cmd[M + 1];
char *ps = "ps -ef";
FILE *fp;
int errnum;
int n;
errno = 0;
if (!(fp = popen(ps, "r"))){
errnum = errno;
fprintf(stderr, "%s\n", strerror(errnum));
return 1;
}
errno = 0;
while((n = fscanf(fp, "%"xstr(N)"s %d %d %*s "
"%"xstr(N)"s %*s %*s %"xstr(M)"[^\n]",
uid, &pid, &ppid, stime, cmd)) != EOF){
if (n != 5){
fscanf(fp, "%*[^\n]");
continue;
}
fprintf(stdout, "%s, %d, %d, %s, %s\n",
uid, (int)pid, (int)ppid, stime, cmd);
errno = 0;
}
errnum = errno;
if (errnum)
fprintf(stderr, "%s\n", strerror(errnum));
pclose(fp);
return 0
}
/*read in ps(1) result*/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#define M 256
#define N 32
#define str(n) #n /*from pete's*/
#define xstr(n) str(n)
int main(void)
{
char uid[N + 1];
pid_t pid;
pid_t ppid;
char stime[N + 1];
char cmd[M + 1];
char *ps = "ps -ef";
FILE *fp;
int errnum;
int n;
errno = 0;
if (!(fp = popen(ps, "r"))){
errnum = errno;
fprintf(stderr, "%s\n", strerror(errnum));
return 1;
}
errno = 0;
while((n = fscanf(fp, "%"xstr(N)"s %d %d %*s "
"%"xstr(N)"s %*s %*s %"xstr(M)"[^\n]",
uid, &pid, &ppid, stime, cmd)) != EOF){
if (n != 5){
fscanf(fp, "%*[^\n]");
continue;
}
fprintf(stdout, "%s, %d, %d, %s, %s\n",
uid, (int)pid, (int)ppid, stime, cmd);
errno = 0;
}
errnum = errno;
if (errnum)
fprintf(stderr, "%s\n", strerror(errnum));
pclose(fp);
return 0
}