(10-13-2015, 03:29 AM)m0dem Wrote: Your first paragraph is good ... useful constructive criticism. I'll give you that.
Otherwise, go troll someone else...
Would you care to elaborate on how this is "horribly written code." And to whose standard? Yours?
Frankly, I don't care if someone doesn't "like" my code. If it works beautifully, it is beautiful.
(not that my program does work perfectly)
^ COMPLETELY pointless
Show me your skillz...
I'm not trolling. This is just you not being able to accept valid points.
Horribly written code:
1. memset() comes from the <string.h> header where it is properly prototyped, which you haven't included in your program. This is also the same header where strlen() comes from.
2. You define pointer variables that you don't assign to NULL for safety, but you also separate declaration and definition for no reason in a few places even if this is ANSI C compliant code (but it isn't because you use VLA's).
Code:
FILE *fp;
fp = popen(cmd, "r");
3. 'const char HOST[20]' could have been 'const char HOST[]'
4. Even if socket creation fails, you print an error and continue with the program to printf("Socket created.\n") which is ridiculous.
5. In some cases where function call errors will set a value that WSAGetLastError() can provide, you don't call it, but you return from main() still.
6. You memset() the command buffer, even though you set a null terminator which is redundancy
7. if recv() fails, you puts("recv failed") and continue on with the regular program flow as if nothing bad happened
8. You never call the respective pclose() function for any of your popen() calls
9. You never call WSACleanup() in any of the error branches after WSAStartup()
- Minimal error checking if any in some places
- No functions
* Bad code formatting with indentation
I could continue on too...
And why is my suggestion about the size of the HOST buffer completely pointless? IMO it's more pointless that you don't know or don't allocate for the space that you need. Additionally 20 is not a great number for anything really.
If you took the time to look around the forum you'd see my contributions btw. Clearly you're too lazy for that kind of task.
As for your mention about "Simple" being in the title. "Simple" is not a synonym for badly written code. If anything, it should make it more prone to well-written code, which isn't the case here, so that is a poor excuse. This code would also never be optimized for any code caches because the numbers you've chosen look like they were picked from a rabbits ass and not anything that required meaningful thought. HOST as 20 is proof enough for that.