PDA

View Full Version : C case insensitive file open in unix



jero3n
January 25th, 2010, 08:00 PM
I need an open function in C (unix) that the filename parameter is case insensitive.

I wrote this ugly function:



int open_ci(const char *path, const char *filename)
{
DIR *dir; struct dirent *entry; int i;
char tmp[PATH_MAX+1], entryname[PATH_MAX+1];

if((dir = opendir(path)) == NULL) {return -1;}
while((entry = readdir(dir))) {
i=0; while( (entryname[i] = toupper(entry->d_name[i])) ) i++;
i=0; while( (tmp[i] = toupper(filename[i])) ) i++;
if(strncmp(tmp, entryname, PATH_MAX) == 0) {
i = open(entry->d_name, O_RDONLY);
closedir(dir);
return i;
}
}
return -1;
}

Is there any easier way?

LKjell
January 25th, 2010, 08:07 PM
Then you have a problem if there exist files name "hey" and "heY" . How do your program knows which file to open?

lisati
January 25th, 2010, 08:07 PM
Short of actually reading the disk directory or using some kind of "best guess" routine where you take a potshot and hope it works I can't think of any way at the moment...

Edit: nicely said, LKjell

jero3n
January 25th, 2010, 08:14 PM
Then you have a problem if there exist files name "hey" and "heY" . How do your program knows which file to open?

my program is aware of this.. in this specific application i need some files that are always unique in each path. but they are not written in the same way as they come from windows.


Short of actually reading the disk directory or using some kind of "best guess" routine where you take a potshot and hope it works I can't think of any way at the moment...

yes.. but it is very slow when there are lots of files in the directory..
i hoped that there were some built-in function in unix..

lisati
January 25th, 2010, 08:18 PM
my program is aware of this.. in this specific application i need some files that are always unique in each path. but they are not written in the same way as they come from windows.



yes.. but it is very slow when there are lots of files in the directory..
i hoped that there were some built-in function in unix..

I haven't heard of one. And by the way, this is a Linux (http://lmgtfy.com?q=linux) support forum, not a Unix support forum.....

jero3n
January 25th, 2010, 08:23 PM
I haven't heard of one. And by the way, this is a Linux (http://lmgtfy.com?q=linux) support forum, not a Unix support forum.....

:)

Well in fact I need this for Linux. Does it make any difference?

jflaker
January 25th, 2010, 08:25 PM
---Move the input and filename/foldername to temporary variable
---convert the input and the filename/foldername temporary variables to UPPER or lower case (your choice)...if they match, that is the one that you want....
---use the original filename/foldername variable for file operations as that contains the real file/folder name.

---Caveat: Linux can have FileName and Filename and be different entities altogether, so you MAY get the wrong target filename/foldername.




I need an open function in C (unix) that the filename parameter is case insensitive.

I wrote this ugly function:



int open_ci(const char *path, const char *filename)
{
DIR *dir; struct dirent *entry; int i;
char tmp[PATH_MAX+1], entryname[PATH_MAX+1];

if((dir = opendir(path)) == NULL) {return -1;}
while((entry = readdir(dir))) {
i=0; while( (entryname[i] = toupper(entry->d_name[i])) ) i++;
i=0; while( (tmp[i] = toupper(filename[i])) ) i++;
if(strncmp(tmp, entryname, PATH_MAX) == 0) {
i = open(entry->d_name, O_RDONLY);
closedir(dir);
return i;
}
}
return -1;
}

Is there any easier way?

MadCow108
January 25th, 2010, 09:02 PM
use strncasecmp, its (unlike its name suggests) case insensitive

jero3n
January 25th, 2010, 09:23 PM
thanks for the replies.

PanP5
April 13th, 2010, 11:04 PM
use strncasecmp, its (unlike its name suggests) case insensitive

This isn't working for me. The compiler is complaining:

error: ‘strncasecmp’ was not declared in this scope

What's the deal? Here's my code:



#include <iostream>
#include <string>
using namespace std;


int main()
{
string s1;
string s2;

cout << "First word: ";
cin >> s1;
cout << "Second word: ";
cin >> s2;

if(strncasecmp(s1,s2)>0)
cout << s1 << " is greater. " <<endl;

else if(strncasecmp(s1,s2)<0)
cout << s2 << " is greater. " <<endl;

else if(strncasecmp(s1,s2)==0)
cout << "They are equal" << endl;

else
cout << "What happened?" << endl;

return 0;
}

kknd
April 13th, 2010, 11:21 PM
You must include string.h . Since you're using C++, use:



#include <cstring>

PanP5
April 14th, 2010, 02:07 PM
You must include string.h . Since you're using C++, use:



#include <cstring>


Thanks!

I assumed <string> was a superset of everything in <string.h>. Guess not.