}
}
-/* 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;
}
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
#include <netdb.h>
#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;
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;
}
if (clients == 0)
{
mode = -1;
- level = 5;
+ levelnr = 5;
timetoplay = 0;
}
}
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;
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)
}
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)
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);
}
}
-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;
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;
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 */
}
{
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]);
}
}
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));
{
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);
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? */
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;
*/
}
-static void handlemessages()
+static void HandleNetworkingMessages()
{
unsigned int len;
{
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])
{
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);
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();
}
}
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)
{
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)
return(area);
}
-
-void lfree(byte *area)
-{
- free(area);
-}
}
image->title = dupString(oimage->title);
- lfree((byte *)xindex);
- lfree((byte *)yindex);
+ free((byte *)xindex);
+ free((byte *)yindex);
printf("done\n");
image->rgb.used = next_index;
/* clean up */
- lfree(map);
- lfree(used);
+ free(map);
+ free(used);
/*
if (badcount)
*/
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);
}
if (redvalue)
{
- lfree((byte *)redvalue);
- lfree((byte *)greenvalue);
- lfree((byte *)bluevalue);
+ free((byte *)redvalue);
+ free((byte *)greenvalue);
+ free((byte *)bluevalue);
}
return(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 ??? */
}
void freeRGBMapData();
byte *lcalloc();
byte *lmalloc();
-void lfree();
Image *gifLoad();
Image *monochrome();