Code:
/*Print the process information*/
void printDetails(char* filename,char* path){
FILE *f_name;
int pid,tty,stime;
char pname[FILENAME_MAX];
char* t_name,time;
/*Format the path*/
strcat(path,"/");
strcat(path,"stat");
if((f_name = fopen(path,"r")) != NULL){
fscanf(f_name,"%d %s %*c %*d %*d %*d %d %*d
%*u %*u %*u %*u %*u %*d %d",&pid,&pname,&tty,&stime);
t_name = ttyname(tty);
printf("B4 getTime\n");
time = getTime(stime); /*HERE IT IS - THE BASTARD*/
if(t_name == NULL)
t_name = "?";
printf("%-9d%-12s%-6s %s\n",pid,t_name,time,pname);
fclose(f_name);
}else
fprintf(stderr,"Can't open %s\n",path);
}
char* getTime(int clockticks){
float jiffies,result;
int min,hr,seconds;
char* time;
jiffies = 0.01f;
result = jiffies * clockticks;
seconds = result;
min = seconds/60;
hr = min/60;
seconds %= 60; /*MOD it into 0-59*/
min %= 60; /*MOD it into 0-59*/
if(hr < 10 && min < 10 && seconds < 10)
sprintf(time,"0%d:0%d:0%d",hr,min,seconds);
else if(hr < 10 && min < 10)
sprintf(time,"0%d:0%d:%d",hr,min,seconds);
else if(hr < 10 && seconds < 10)
sprintf(time,"0%d:%d:0%d",hr,min,seconds);
else if(min < 10 && seconds < 10)
sprintf(time,"%d:0%d:0%d",hr,min,seconds);
else if(hr < 10)
sprintf(time,"0%d:%d:%d",hr,min,seconds);
else if(min < 10)
sprintf(time,"%d:0%d:%d",min,hr,seconds);
else if(seconds < 10)
sprintf(time,"%d:%d:0%d",min,hr,seconds);
else
sprintf(time,"%d:%d:%d",min,hr,seconds);
return time;
}