Link to home
Start Free TrialLog in
Avatar of Frazh
Frazh

asked on

C: Convert a string to the right case

Dear coders,
As we all know Linux is unlike Operating systems like Windows case sensitive. One the one hand this is good because it's more accurate but it's a pain when people forget Windows isn't the only OS. This is why I need your help. I am in need of a snippet: A snippet that turns a string of a path with a random case into a string with the right case. Vague? Let me clarify.

It will be for a webserver. Say someone calls http://myhost.com/dir/FoO.ZiP. Windows wouldn't care about this and serve the file. Unix/Linux will however tell us that the file doesn't exist. I still want to serve it the file, so I was wondering if there is a way to turn the string of the random case into a string with the real case, of the existing file. It wont be required to do this for the directory too, however that would be cool if it isnt too much work.

The person who solves this will get 100 points. It's not amazingly much and if the assignment turns out to be more complex I might add some to it. (Im new to this community, forgive my newbieness heh ;))

Thanks in advance!
Avatar of aib_42
aib_42

Frazh,

Before anyone can start working on a solution, there are some things for you to consider:

1) "It will be for a webserver." What is "it"? A program, a function, a module for Apache, a new system call, a new filesystem? This is important because the code to be written has to rely on the environment. A non-FS kernel module will not have easy access to the file system, for example, and a plain function cannot rely on having a memory area kept across calls to itself to store some kind of cache. A standalone program will be easy to write straight away, but it will have problems communicating with the web server. (How will it know that http://myserver/pages/local1/ will correspond to /var/www/htdocs/local1/?)

2) How do you want to handle the case where there are two or more actual files matching the given filename?

IMHO you are too hasty in asking for a piece of code right away, you should discuss what options you have first. If you are using Apache, for example, there may already be a "case-insensitive URL" module of some sort.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Simplest way has already been mentioned-- use "dir TheFunnyname" to find any and all the variations.

From a program, done all with API calls, you'd use FindFirst/FindNext API's to get all the file names, then comapre them in a case-insensitive manner, either by converting both to the same case, then using strcmp(), or just using stricmp() or any other case-insensitive compare function.   Easy.

Avatar of Frazh

ASKER

I accepted the answer of sunnycoder as it seems to be the most easy to implement. This might sound a bit hypocrite since this is in the C Section, but this alternative seems to be OK. If it was possible to give 2 people the points, or donate points, I would do it for sure :)

aib_42: I agree I should have given more details. Return an error would be the best thing to do in my eyes, since there is interference. (End user needs to be warned for that for sure)

The HTTP server is shttpd, Simple HTTPd written in C. So it's not Apache or something. It's ment to be really lightweight. I am going to look into both options.

I'd like to say thanks for the all the people who contributed to this thread. As I said I will look into the find method as it allows me to easily convert a string to an existing file string. I am not a pro-coder so I can not say if this IS the easiest way though.

Btw: Is it hard to execute a binary file and send the stdout to a var?
Avatar of Frazh

ASKER

Edit: Also gave ssnkumar 100 points, didn't know about the split function. (Unable to edit comments?)
>(Unable to edit comments?)
Yep .. You cannot edit comments ... :-)

>Btw: Is it hard to execute a binary file and send the stdout to a var?
From a shell .. no, it rather easy
var=`/path/to/executable`
echo $var
Note that the quotes are backticks ``