Programming in C and stackoverflow.com

30 Oct 2008

I recently had to stroll from the PHP path, at work we required a very simple program that had to be run as setuid. Now, as far as I can tell, both debian and ubuntu do not allow scripts to setuid, only binaries. This led to me flexing my C skills, which haven't been touched since I was following NeHe's OpenGL tutorials for my graphics module back at uni. I couldn't actually remember how to do anything, so I had a quick read through a few chapters from my namesakes guide (I'm gunning for his place in the google rankings for my name) and set about on my task.

I managed to do everything I needed using the guide, except for one simple task. In PHP, we have the file_exists function, which works a treat. There's no such function in C, which is understandable as it is a much higher level language, so I had to hit the search engines to find an example. The result at the top of the list was actually a forum post asking the exact same question I was asking google. Now being the naive person I am, I took the first correct response as gospel and implemented it, it worked and I was happy. Now, a few hours later my program was finished and I as read through the code again, the file_exists function I'd created was nagging at me. The function basically ran fopen on the file, returning true upon success, which I assumed was slightly overkill.

I've been keeping my eye on stackoverflow since the first announcement, including listening to all the podcasts and participating in the private beta. Joel Spolsky and Jeff Atwood have done extremely well in building a giant community of programmers and I figured I'd get the answer I needed there. I asked my question and in moments I had several answers. Turned out that there were two methods better than my own and I had to make a decision and award the 'correct' answer to one of them. Even so, the other answer was well voted up, so what I considered the second best response, was directly under my accepted answer. A week later and I thought I'd check the google rankings, to see how close to the top my question would be. It could be closer, but a search for file exists in C shows my question just on the first page and I'm sure with time, will make it to the top. In doing so, I had another look at the original forum thread that I got the fopen code from and low and behold, the last three posts detailed the same two answers that I got from stackoverflow. The difference? When people go to my stackoverflow.com question, the best solution is the first thing they see after the question.

Maintainable PHP Apps with Silex & BDD

I'm currently writing a book, Maintainable PHP Apps with Silex & BDD, leave me your email address and I'll keep you up to date on my progress.

Twitter Icon If you liked this post, you should follow me on twitter here
blog comments powered by Disqus

About

Photo of Dave Marshall

Dave Marshall has been building web applications with various technologies since around 2004. Dave is a TDD enthusiast, blogs quite regularly at davedevelopment.co.uk and has recently increased his efforts to give back further, by contributing to OS projects such as Silex and Mockery

Read more about Dave

Maintainable PHP Apps with Silex & BDD

I'm currently writing a book, Maintainable PHP Apps with Silex & BDD, leave me your email address and I'll keep you up to date on my progress.

Follow Dave: