I'm using the scandir() function for one of my C programs. Within scandir(), I ask it to alphasort my directory. Yet, when I have files such as: alpha,bob, they're placed under files like DARK or Decomp.txt. I assume this is because of how the ascii values work. But is there are way so that my ordering would be: alpha,bob,DARK,decomp.txt rather than DARK,Decomp.txt,alpha,bob.
Because essentially I am supposed to model the tree unix command and need it to be sorted in such a manner.
My code below only shows how i'm printing.
void listdir(const char *name, int level, int hidden, int access)
{
DIR *dir;
struct dirent **entry;
int n = 2;
int num;
char path[1024];
int len;
int count = 0;
if (!(dir = opendir(name)))
{
printf("%s [error opening dir]\n", name);
return;
}
num = scandir(name,&entry,NULL,alphasort);
if(num<0)
{
perror("scandir");
}
while(n<num){
/* Bunch of formatting to print files/directory */
/* Pseudocode */
/* if( it is a directory)
print current directory
recursive call on function
else
print current file */
n++;
}
closedir(dir);
}