LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2007, 11:06 AM   #1
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Rep: Reputation: 0
Getting entry type with scandir() on FAT drive?


I need to create a file/dir list sorted alphabetically and with directories first.
That was pretty easy with scandir() but it doesn't work with fat and ntfs drives
I thought about using stat() but with scandir() I don't get the full path.
Any ideas how to proceed?
BR, Thomas Fogh

Code:
int file_select(const struct dirent* entry)
{
  if ( (strcmp(entry->d_name, ".") == 0) ||
       (strcmp(entry->d_name, "..") == 0) ||
       ((entry->d_type != DT_DIR) &&
	(entry->d_type != DT_REG)) )
    return 0;
  else
    return 1;
}

int file_sort(const struct dirent** d1, const struct dirent** d2)
{
  if ((*d1)->d_type == DT_DIR && (*d2)->d_type == DT_REG)
    return -1;
  else if ((*d1)->d_type == DT_REG && (*d2)->d_type == DT_DIR)
    return 1;
  else return strcmp((*d1)->d_name, (*d2)->d_name);
}

void print_dirs(char* dir)
{
  struct dirent **files;
  int count, i;

  count = scandir(dir, &files, file_select, file_sort);

  for (i = 0; i < count; i++) {
    if (files[i]->d_type == DT_DIR) {
      char* temp_name = malloc(strlen(dir)+strlen(files[i]->d_name)+2);
      strcpy(temp_name, dir);
      strcat(temp_name, "/");
      strcat(temp_name, files[i]->d_name);
      printf("[%s]\n", temp_name);
      print_dirs(temp_name);
      free(temp_name);
    } else if (files[i]->d_type == DT_REG) {
      printf("%s\n", files[i]->d_name);
    }
    free(files[i]);
  }
}

int main(int argc, char** argv)
{
  print_dirs("/svn");
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving hard drive from one system type to another type SlowCoder Linux - Newbie 7 11-26-2007 10:38 AM
can mount C not D FAT: invalid media value (0x45) mount: wrong fs type, bad option Emmanuel_uk Linux - Newbie 10 11-29-2005 02:47 AM
Mounting FAT 32 Partition Changes Type to Hidden dystopian Linux - General 7 10-08-2005 11:28 PM
Resizing a fat 32 drive... RoaCh Of DisCor Linux - Software 1 12-09-2004 11:04 AM
Find out FAT type jlluis Linux - General 6 10-04-2003 08:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration