From: Holger Schemel Date: Fri, 16 Oct 1998 01:15:07 +0000 (+0200) Subject: rnd-19981016-2 X-Git-Tag: 1.2.0^2~55 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=62573489ded016c3436c4e9eb99f1974ee296e4f;p=rocksndiamonds.git rnd-19981016-2 --- diff --git a/src/misc.c b/src/misc.c index 56b651cb..a482d748 100644 --- a/src/misc.c +++ b/src/misc.c @@ -370,15 +370,14 @@ void Error(int mode, char *format_str, ...) } } -/* like memcpy, but guaranteed to handle overlap when s <= t */ -void copydown(char *s, char *t, int n) +void *checked_malloc(unsigned long size) { - for (; n; n--) - *(s++) = *(t++); -} + void *ptr; -void fatal(char *s) -{ - fprintf(stderr, "%s.\n", s); - exit(1); + ptr = malloc(size); + + if (ptr == NULL) + Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size); + + return ptr; } diff --git a/src/misc.h b/src/misc.h index 90c403e9..dff44fb7 100644 --- a/src/misc.h +++ b/src/misc.h @@ -40,7 +40,6 @@ char *GetLoginName(void); void MarkTileDirty(int, int); void GetOptions(char **); void Error(int, char *, ...); -void copydown(char *, char *, int); -void fatal(char *); +void *checked_malloc(unsigned long); #endif diff --git a/src/netserv.c b/src/netserv.c index aec5d057..bafb1fe8 100644 --- a/src/netserv.c +++ b/src/netserv.c @@ -27,22 +27,14 @@ #include #include "netserv.h" - - - -extern int verbose; - -extern void copydown(char *, char *, int); -extern void fatal(char *); - - +#include "misc.h" static int clients = 0; static int bots = 0; static int onceonly = 0; static int timetoplay = 0; static int is_daemon = 0; -static int level = 5; +static int levelnr = 5; static int mode = -1; static int paused = 0; @@ -91,7 +83,8 @@ static void syserr(char *s) static void addtobuffer(struct user *u, unsigned char *b, int len) { if (u->nwrite + len >= MAX_BUFFER_SIZE) - fatal("Internal error: send buffer overflow"); + Error(ERR_EXIT, "internal error: network send buffer overflow"); + memcpy(u->writbuf + u->nwrite, b, len); u->nwrite += len; } @@ -188,7 +181,7 @@ static void dropuser(struct user *u) if (clients == 0) { mode = -1; - level = 5; + levelnr = 5; timetoplay = 0; } } @@ -198,9 +191,8 @@ static void new_connect(int fd) struct user *u, *v; unsigned char nxn; - u = malloc(sizeof (struct user)); - if (!u) - fatal("Out of memory"); + u = checked_malloc(sizeof (struct user)); + u->fd = fd; u->nick[0] = 0; u->next = user0; @@ -362,11 +354,11 @@ static void Handle_OP_NICKNAME(struct user *u, unsigned int len) sendtoone(u, 2+strlen(v->nick)); } } - if (level != 5) + if (levelnr != 5) { buf[0] = 0; buf[1] = OP_LEVEL; - buf[2] = level; + buf[2] = levelnr; sendtoone(u, 3); } if (mode >= 0) @@ -697,7 +689,7 @@ void NetworkServer(int port, int serveronly) } memcpy(buf, &u->readbuf[4], len); u->nread -= 4 + len; - copydown(u->readbuf, u->readbuf + 4 + len, u->nread); + memmove(u->readbuf, u->readbuf + 4 + len, u->nread); buf[0] = u->number; if (!u->introduced && buf[1] != OP_NICKNAME) @@ -785,7 +777,7 @@ void NetworkServer(int port, int serveronly) break; case OP_LEVEL: - level = buf[2]; + levelnr = buf[2]; if (verbose) printf("RND_SERVER: client %d (%s) sets level %d\n", u->number, u->nick, buf[2]); broadcast(NULL, 3, 0); diff --git a/src/network.c b/src/network.c index 126eb523..28a7d892 100644 --- a/src/network.c +++ b/src/network.c @@ -69,16 +69,6 @@ static void sysmsg(char *s) } } -static void *mmalloc(int n) -{ - void *r; - - r = malloc(n); - if (r == NULL) - fatal("Out of memory"); - return r; -} - static void u_sleep(int i) { struct timeval tm; @@ -104,7 +94,7 @@ static void sendbuf(int len) realbuf[3] = (unsigned char)len; buf[0] = 0; if (nwrite + 4 + len >= MAX_BUFFER_SIZE) - fatal("Internal error: send buffer overflow"); + Error(ERR_EXIT, "internal error: network send buffer overflow"); memcpy(writbuf + nwrite, realbuf, 4 + len); nwrite += 4 + len; @@ -123,7 +113,8 @@ struct user *finduser(unsigned char c) if (u->nr == c) return u; - fatal("Protocol error: reference to non-existing user"); + Error(ERR_EXIT, "protocol error: reference to non-existing user %d", c); + return NULL; /* so that gcc -Wall doesn't complain */ } @@ -178,7 +169,8 @@ BOOL ConnectToServer(char *host, int port) { hp = gethostbyname(host); if (!hp) - fatal("Host not found"); + Error(ERR_EXIT, "cannot locate host '%s'", host); + s.sin_addr = *(struct in_addr *)(hp->h_addr_list[0]); } } @@ -190,9 +182,11 @@ BOOL ConnectToServer(char *host, int port) s.sin_port = htons(port); s.sin_family = AF_INET; + sfd = socket(PF_INET, SOCK_STREAM, 0); if (sfd < 0) - fatal("Out of file descriptors"); + Error(ERR_EXIT, "out of file descriptors"); + if ((tcpproto = getprotobyname("tcp")) != NULL) setsockopt(sfd, tcpproto->p_proto, TCP_NODELAY, (char *)&on, sizeof(int)); @@ -208,18 +202,22 @@ BOOL ConnectToServer(char *host, int port) { u_sleep(500000); close(sfd); + sfd = socket(PF_INET, SOCK_STREAM, 0); if (sfd < 0) - fatal("Out of file descriptors"); - setsockopt(sfd, tcpproto->p_proto, TCP_NODELAY, (char *)&on, sizeof(int)); + Error(ERR_EXIT, "out of file descriptors"); + + setsockopt(sfd, tcpproto->p_proto, TCP_NODELAY, + (char *)&on, sizeof(int)); + if (connect(sfd, (struct sockaddr *)&s, sizeof(s)) >= 0) break; } if (i==6) - fatal("Can't connect to server"); + Error(ERR_EXIT, "cannot connect to server"); } else - fatal("Can't connect to server"); + Error(ERR_EXIT, "cannot connect to server"); } return(TRUE); @@ -381,11 +379,10 @@ static void Handle_OP_NUMBER_WANTED() u = finduser(old_client_nr); u->nr = new_client_nr; - if (old_client_nr == local_player->client_nr) /* local player switched */ + if (old_player == local_player) /* local player switched */ local_player = new_player; - TestPlayer = new_index_nr; } else if (old_client_nr == me.nr) /* failed -- local player? */ @@ -432,7 +429,7 @@ static void Handle_OP_PLAYER_CONNECTED() v = u; } - v->next = u = mmalloc(sizeof(struct user)); + v->next = u = checked_malloc(sizeof(struct user)); u->nr = new_client_nr; u->name[0] = '\0'; u->next = NULL; @@ -583,7 +580,7 @@ static void Handle_OP_MOVE_FIGURE(unsigned int len) */ } -static void handlemessages() +static void HandleNetworkingMessages() { unsigned int len; @@ -591,11 +588,11 @@ static void handlemessages() { len = readbuf[3]; if (readbuf[0] || readbuf[1] || readbuf[2]) - fatal("Wrong server line length"); + Error(ERR_EXIT, "wrong network server line length"); memcpy(buf, &readbuf[4], len); nread -= 4 + len; - copydown(readbuf, readbuf + 4 + len, nread); + memmove(readbuf, readbuf + 4 + len, nread); switch(buf[1]) { @@ -689,10 +686,7 @@ void HandleNetworking() r = select(sfd + 1, &rfds, NULL, NULL, &tv); if (r < 0 && errno != EINTR) - { - perror("select"); - fatal("fatal: select() failed"); - } + Error(ERR_EXIT, "HandleNetworking(): select() failed"); if (r < 0) FD_ZERO(&rfds); @@ -704,11 +698,13 @@ void HandleNetworking() r = read(sfd, readbuf + nread, MAX_BUFFER_SIZE - nread); if (r < 0) - fatal("Error reading from server"); + Error(ERR_EXIT, "error reading from network server"); + if (r == 0) - fatal("Connection to server lost"); + Error(ERR_EXIT, "connection to network server lost"); + nread += r; - handlemessages(); + HandleNetworkingMessages(); } } diff --git a/src/new.c b/src/new.c index 901df518..508f7393 100644 --- a/src/new.c +++ b/src/new.c @@ -59,9 +59,9 @@ void newRGBMapData(RGBMap *rgb, unsigned int size) void freeRGBMapData(RGBMap *rgb) { - lfree((byte *)rgb->red); - lfree((byte *)rgb->green); - lfree((byte *)rgb->blue); + free((byte *)rgb->red); + free((byte *)rgb->green); + free((byte *)rgb->blue); } Image *newBitImage(unsigned int width, unsigned int height) @@ -109,17 +109,17 @@ void freeImageData(Image *image) { if (image->title) { - lfree((byte *)image->title); + free((byte *)image->title); image->title= NULL; } freeRGBMapData(&(image->rgb)); - lfree(image->data); + free(image->data); } void freeImage(Image *image) { freeImageData(image); - lfree((byte *)image); + free((byte *)image); } byte *lmalloc(unsigned int size) @@ -155,8 +155,3 @@ byte *lcalloc(unsigned int size) return(area); } - -void lfree(byte *area) -{ - free(area); -} diff --git a/src/send.c b/src/send.c index 8b51d362..f0aeee1a 100644 --- a/src/send.c +++ b/src/send.c @@ -231,8 +231,8 @@ Image *zoom(Image *oimage, unsigned int xzoom, unsigned int yzoom) } image->title = dupString(oimage->title); - lfree((byte *)xindex); - lfree((byte *)yindex); + free((byte *)xindex); + free((byte *)yindex); printf("done\n"); @@ -335,8 +335,8 @@ void compress(Image *image) image->rgb.used = next_index; /* clean up */ - lfree(map); - lfree(used); + free(map); + free(used); /* if (badcount) @@ -539,10 +539,10 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, */ fprintf(stderr, "imageToXImage: XAllocColor failed on a TrueColor/Directcolor visual\n"); - lfree((byte *)redvalue); - lfree((byte *)greenvalue); - lfree((byte *)bluevalue); - lfree((byte *)ximageinfo); + free((byte *)redvalue); + free((byte *)greenvalue); + free((byte *)bluevalue); + free((byte *)ximageinfo); return(NULL); } @@ -828,9 +828,9 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, if (redvalue) { - lfree((byte *)redvalue); - lfree((byte *)greenvalue); - lfree((byte *)bluevalue); + free((byte *)redvalue); + free((byte *)greenvalue); + free((byte *)bluevalue); } return(ximageinfo); @@ -878,13 +878,13 @@ void freeXImage(Image *image, XImageInfo *ximageinfo) { if (ximageinfo->no > 0 && !ximageinfo->rootimage) /* don't free root colors */ XFreeColors(ximageinfo->disp, ximageinfo->cmap, ximageinfo->index, ximageinfo->no, 0); - lfree(ximageinfo->index); + free(ximageinfo->index); } if (ximageinfo->gc) XFreeGC(ximageinfo->disp, ximageinfo->gc); - lfree((byte *)ximageinfo->ximage->data); + free((byte *)ximageinfo->ximage->data); ximageinfo->ximage->data= NULL; XDestroyImage(ximageinfo->ximage); - lfree((byte *)ximageinfo); + free((byte *)ximageinfo); /* should we free private color map to ??? */ } diff --git a/src/xli.h b/src/xli.h index dc60fb92..50826558 100644 --- a/src/xli.h +++ b/src/xli.h @@ -141,7 +141,6 @@ void newRGBMapData(); void freeRGBMapData(); byte *lcalloc(); byte *lmalloc(); -void lfree(); Image *gifLoad(); Image *monochrome();