?
Solved

C: Convert a string to the right case

Posted on 2005-03-13
7
Medium Priority
?
285 Views
Last Modified: 2010-04-15
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!
0
Comment
Question by:Frazh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:aib_42
ID: 13530944
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.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 400 total points
ID: 13532085
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
0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 400 total points
ID: 13532841
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.

-ssnkumar
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

 
LVL 22

Expert Comment

by:grg99
ID: 13534094
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.

0
 

Author Comment

by:Frazh
ID: 13535690
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?
0
 

Author Comment

by:Frazh
ID: 13535718
Edit: Also gave ssnkumar 100 points, didn't know about the split function. (Unable to edit comments?)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13540892
>(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 ``
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question