A few unfortunate individuals may know something of this beast. The title is an allusion to the system name.
Deep within the bowls of a large financial institution lurks this monster system, written by a single person (Highly Paid Consultant, before you ask). The Objective-C GUI is a work of art. The ANSI C server code follows the same pattern...
Here are some of finest gems of true WTF-ery I've accumulated from the beast over the years. A lot of it's normal bad code, cut&paste and messed up. But the pieces here are true gems that make you sit up and ask yourself the same question that has driven scores of maintenance programmers mad over the years:
"WTF!?!?"
Say which... what?
if (what == 0) {
// Code Block A
}
else if (what == 2) {
// Code Block B
}
else if ((what == 1) || (what == 3) || (what == 5)) {
if (what == 1) which = 0;
if (what == 3) which = 1;
if (what == 5) which = 1;
}
// Code Block C omitted for sanity/brevity
if (what == 5) FooBar(position, 3);
else doWibble(position, what);
}
I was going to leave out mystery Code Block C, but actually it's good a few laughs too:
if (manualrequests[position].systemid == 1) {
if (strcmp(clients[i].type, "AAA") == 0) {
found = 1;
temp_fd = clients[i].fd;
send_data(1, temp_fd, retstr);
}
}
else if (manualrequests[position].systemid == 2) {
if (strcmp(clients[i].type, "BBB") == 0) {
found = 1;
temp_fd = clients[i].fd;
send_data(1, temp_fd, retstr);
}
}
.....etc.....
else if (manualrequests[position].systemid == 10) {
if (strcmp(clients[i].type, "XXX") == 0) {
found = 1;
temp_fd = clients[i].fd;
send_data(1, temp_fd, retstr);
}
}
Oh except for case 3 which actually looks like this:
else if (manualrequests[position].systemid == 3) {
if (clients[i].g1 != 999) continue;
temp_fd = clients[i].fd;
send_data(1, temp_fd, retstr);
}
If at first you cannot bind; bind, bind then bind again...
The original HPC didn't understand really how TCP works, particularly closing sockets and that whole damn TIMEDWAIT crap, when he wrote his network code. As the system was inevitably a massive distributed system built around TCP/IP sockets (which would certainly be what I'd write if I didn't understand sockets), we have some interesting network code. Like this:
for(;;) {
myport++;
if ((myport - LISTENPORT) >= 5) {
myport = LISTENPORT;
WARNING(func, "bind() failedon all ports from " << LISTENPORT);
SLEEP(func, 5, "No idea");
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(myport);
if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
std::strstream msg;
msg << "bind() to " << myport << " failed with ";
if (errno == EADDRNOTAVAIL) msg << "EADDRNOTAVAIL";
else if (errno == EADDRINUSE) msg << "EADDRINUSE";
else if (errno == EBADF) msg << "EBADF";
else if (errno == EINVAL) msg << "EINVAL";
else if (errno == ENOTSOCK) msg << "ENOTSOCK";
else msg << errno;
ERROR(func, msg.str());
// close(listenfd);
continue;
}
if ((ret = listen(listenfd, LISTENQ)) != 0) {
LOG_WARNING(func, "listen() failed - myport = " << myport);
// close(listenfd);
continue;
}
break;
}
Ironically, one of the reasons why the system was made distributed was for increased reliability and fault tolerance. That is, if you could ever get restarted processes to connect again...
This code block is used in around 20 different processes, in various cut&pasted modified variants.
Other gems
There are too many code snippets to fully list here are a few good ones:
if ((onoff != 99) && (onoff != 98)) {
clients[found].foobarregister[group] = onoff;
if (onoff == 0) checkFoobarRegistered(group);
}
---
static char months[13][4] = {" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static char monthslow[13][4] = {" ", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
---
if ((int_year > 80) && (int_year < 100)) int_year +=1900;
if ((int_year >= 0) && (int_year < 20)) int_year +=2000;
But of course, in a full-scale disaster movie like this, the damage isn't limited to only the code - invariably, there's a database involved as well...
One day, the HPC was thinking deeply...
Hmm... how can we store stuff that can be on or off?
Aha - simple:
SELECT * FROM ONOFFTABLE
keycode text
------- -----
0 OFF
1 ON
Just the flexibility we need to add
3 FILE_NOT_FOUND
at a future date! But, dang, the "text" column is only a VARCHAR2(5)... who needs pesky Boolean values. Also ironically, ono
And there's plenty more...