Posts for March 2006
Blog moved.
For those who haven't noticed (as everything is still redirected from the old address), The URL of the blog has changed, and this blog has changed and is now http://www.nullis.net/blog/.
The move was one which was a long time coming, but finally done. The rest of the nullis.net site hopefully will be up shortly, as well as a new blog theme.
But everything in small steps ;-)
So, update your links/rss feeds if needed.
The real reason governments don't want to move to linux....
Thanks to linux_galore for the link:
Have a read, made me chuckle, a lot.
http://www.centos.org/modules/news/article.php?storyid=127
In other news...
It seems that a 22 year old Victorian man has been arrested for his attacks on Australian IRC Networks.
It's glad to see the AHTCC and other departments getting involved and stopping these guys who just seem to want to destroy things for all of us.
awk and sed.
Another of my subjects this semester at uni, had me using a bit of awk and sed this week just gone, and I must say, that I now do have a more understanding knowledge of both of these tools.
Before I did understand how to do rather small things with each (like change instances of words, to another word, and pull out every 4th word from a sentence to use elsewhere), I never did try and go any more into either of them.
As you can see from the following code snippets, I think I have dug a bit more into these tools.
sed: Change the order, of the first two parameters called by a function
s/somefunc((.*),(.*)(,.*))/somefunc(2,13)/g
awk: add line numbers to each line of a source file, without skipping numbers. Comments should not be numbered.
{ if ($1 ~////) { printf("%5st%sn", "", $0); next } }
{ if ($1 ~//*/) { z = 1; printf("%5st%sn", "", $0); next } }
{ if ($1 ~/*//) { z = 0; printf("%5st%sn", "", $0); next } }
{ if (z > 0) { printf("%5st%sn", "", $0); next } }
{ printf("%5st%sn", (NF? ++a ":" :""), $0) }
A sample output for this awk script would be:
1: #include
// main routine
2: int main(int argc, char argv[])
3: {
/ Incredibly complex function here --
* it prints hello world
*/
4: printf("%s", "Hello, world");
5: return 0; // return zero!
6: }
And comparing what I did, to some other features of these tools, this is still only the beginning of their abilities.
Win32 API vs. Linux - Round One
Before we start today's fight, we shall introduce you to the contestants, and a bit of background on each of them.
In the Red Corner, we have code written to work well with the Win32 API. The Win32 API is known for being part of Windows, and being able to kill its opponents easily, with the sheer size and complexity of itself. In the Blue Corner, we have code written to work well on Linux. Linux code being known to be quite easy to deal with, and not having heaps of levels of abstraction when it isn't neccesary.
These two come together today, to see who can write the better program that can: * Open a file, specified on the command line, for exclusive read only access. * Report if there was a problem opening the file, with an appropriate error message. * Close the file.
We would like to thank today's sponsor for this event, namely the Systems Programming 2 class that I am currently undertaking at University, and is making me learn how to code with the Windows API.
So that is enough, talk, lets see how the fight goes.
Windows starts off first by allowing a bit more complexity to its CreateFile() routine, but this is unfortunately useless, and not able to withstand the sheer simplicity of Linux's open() call, which beats it hands down.
In an attempt to reclaim, the match, Windows tries to claim that it can give a more superior error message handling routine, for when that file open does not work. Unfortunately, while Windows was still trying to type the function name for the routine to get the error message, Linux had already finished writing code to get and display the error message, thanks to perror(). In fact, Linux was so efficient, it decided to go one step better, and customise the error message all together.
In the end, it came down to closing the handle/file descriptor, to see if Windows would get a foot in, in this round, and it seems that it was a tie. Both Windows and Linux successfully made closing the open file nice and easy.
However, it the clear winner this round is the Linux code. Score: 2.5 - 0.5 -- Linux code wins.
To see the code used in this contest, and to agree with me that Linux definately wins this round in terms of better code/less code for this task, see the code below.
Windows
include <windows.h>
include <stdio.h>
int main(int argc, char *argv[]) { HANDLE hFileToRead; LPTSTR pszErrorMessageBuf; SECURITY_ATTRIBUTES lpSecurityAtrribs;
lpSecurityAtrribs.nLength = sizeof(lpSecurityAtrribs); lpSecurityAtrribs.lpSecurityDescriptor = NULL; lpSecurityAtrribs.bInheritHandle = TRUE; if (argc < 2) { fprintf(stderr, "Syntax: %s <filename>", argv[0]); return -1; } hFileToRead = CreateFile( argv[1], GENERIC_READ, 0, lpSecurityAtrribs, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFileToRead == INVALID_HANDLE_VALUE) { FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, (LPTSTR)&pstrErrorMessageBuf, 0, NULL); fprintf(stderr, "Could not open file '%s': [%d] %s", argv[1], GetLastError(), pstrErrorMessageBuf); LocalFree(pstrErrorMessageBuf); return -1; } else printf("File successfully opened.\n"); if (!CloseHandle(hFileToRead)) fprintf(stderr, "Error closing file...\n"); return 0;}
Linux code
include <stdio.h>
include <sys/types.h>
include <sys/stat.h>
include <fcntl.h>
include <errno.h>
include <string.h>
include <unistd.h>
int main(int argc, char *argv[]) { int fd;
if (argc < 2) { fprintf(stderr, "Syntax: %s <filename>\n", argv[0]); return -1; } fd = open(argv[1], O_EXCL|O_RDONLY); if (fd == -1) { fprintf(stderr, "Could not open file '%s': [%d] %s\n", argv[1], errno, strerror(errno)); return -1; } else printf("File successfully opened.\n"); if (close(fd)) fprintf(stderr, "Error closing file...\n"); return 0;}

29th March, 2006
0 Comments
Add Comment