Things you could improve:
Dependencies:
You are way overdoing this problem. By a lot. You can generate pseudo-random on your computer just fine. There is no reason to create a dependency on the internet for that. What if you don't have access to the internet? Right now your program fails to function, because of that huge and unnecessary dependency.
If you absolutely must use the internet, use
libcurl
instead of your own DIY way of connecting to the internet. And make sure to support "offline-mode".
libcurl
is a free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunneling and more!
libcurl
is highly portable, it builds and works identically on numerous platforms, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows, Amiga, OS/2, BeOs, Mac OS X, Ultrix, QNX, OpenVMS, RISC OS, Novell NetWare, DOS and more...
Syntax/Styling:
Neal Stephenson thinks it's cute to name his labels 'dengo'
Yes, there are some rare situations where you may find it necessary to use it. This is not one of them.
if (initdata(cd, url)) goto LBL_INIT; while (1) { if (fetchpage(html, sizeof html, cd)) goto LBL_FETCH; if (process(data, html)) goto LBL_PROCESS; if (save(data)) goto LBL_SAVE; if (gotenough()) break; sleep(afewseconds); } if (killdata(cd)) goto LBL_KILL; goto LBL_FINISH; LBL_KILL: fprintf(stderr, "Killing error (never happens)\n"); goto LBL_FINISH; LBL_SAVE: fprintf(stderr, "Saving error.\n"); goto LBL_FINISH; LBL_PROCESS: fprintf(stderr, "Processing error.\n"); goto LBL_FINISH; LBL_FETCH: fprintf(stderr, "Fetching page error.\n"); goto LBL_FINISH; LBL_INIT: fprintf(stderr, "Initializing data.\n"); goto LBL_FINISH; LBL_FINISH: return 0;
All you are doing is printing a simple unique error message for every goto
label. And then you go to LBL_FINISH
, where you indicate a successful return whether you have an error or not. Get rid of the goto
s completely, and return unique error indicators.
typedef
yourstruct
s.
struct Data { char datetime[20]; int die[5]; };
The `typedef` means you no longer have to write `struct` all over the place. That not only saves some space, it also can make the code cleaner since it provides a bit more abstraction.
typedef struct
{
char datetime[20];
int die[5];
} Data;
- Use parenthesis with
sizeof
sizeof hints
sizeof(hints)
- This isn't a standard way to do a counter.
int gotenough(void) { static int n = 0; n += 1; return (n == 10); }
Memory/Data Handling:
- Is your method
killdata()
trying to erase yourstruct
?
int killdata(struct ConnData *cd) { /* nothing to do */ (void)cd; return 0; }
Throwing your data into a `void` container doesn't "kill" it. You need to go through each of the `struct` members and set them to `NULL`.
Final Code:
I've re-written your code completely into a more portable program, that accomplishes the basic functionality that your program does. As you can see from the length of my program to your program, this isn't a very hard task to accomplish without using the internet.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand((unsigned int) time(NULL));
for (int i = 0; i < 100; i++)
{
printf("%d\n", (rand() % 6) + 1);
}
return 0;
}