I've written a function which I should be able to use with the scandir function. Basically it should sort a list of numbers, this almost works, i.e. it sorts them but not all of them eg. i get the following
3
4
5
1000
1001
1002
1
2
400
..
..
..
..
..
800
Anyway here's the code that so nearly works.
Code:
int main(void){
int result,count,index; /*Set Conditions*/
char path[] = "/proc"; /*Path to the proc directory*/
struct dirent **direntp; /*Directory entry pointer*/
struct stat sbuf; /*Holds information about files*/
if (stat(path,&sbuf) != -1){
if(S_ISDIR(sbuf.st_mode)){
count = scandir(path,&direntp,NULL,intcompare);/*THE CALL*/
if(count){
char temp[FILENAME_MAX];
printf("PID TTY TIME CMD\n");
for(index=0; index<count; ++index){
strcpy(temp,path);
result = matchDetails(direntp[index]->d_name,temp);
if(result > 0) /*If directory name was matched*/
printDetails(direntp[index]->d_name,temp);
free(direntp[index]);
}
}else{
fprintf(stderr,"Can't open %s\n",path);
exit(EXIT_FAILURE);
}
}else
return;
}else{
fprintf(stderr,"Can't stat %s\n",path);
return;
}
return EXIT_SUCCESS;
}
....
....
....
/*Used for sorting processes*/
int intcompare(const struct dirent **x,const struct dirent **y){
int *xp = (int*) x;
int *yp = (int*) y;
if(*xp < *yp)
return -1;
else if(*xp > *yp)
return +1;
else
return 0;
}