• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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!
2 Solutions

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.
You can do that with a single line script

find <document root directory> -iname "filename"

This will generate the the list of all files in the directory [ <document root directory> ] which match the pattern ["filename"]  you supplied in a case-insensitive fashion
Here, the problem boils down to comparison of strings.
Both the strings contain path to some file.
The first string (actual path on Linux machine), contains the correct path (with correct case for each element in the path).
The second string (coming from browser) may not contain the correct case.
Still it has to work.
If this is what you are trying to do, then you can do it in this way (Since you are in C Programming TA, I assume that you want some C code only):
strcasecmp(string1, string2);
This function unlike strcmp() ignores the case of both the string.
So, whatever random case you are having in your browser, this will give a correct matching.
It returns 0, if the strings match.

Hope this helps.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.

FrazhAuthor Commented:
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?
FrazhAuthor Commented:
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
echo $var
Note that the quotes are backticks ``

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now