/* This software is Copyright 1989, 1990, 1992, 1993 by various individuals. Please see the accompanying file COPYRIGHT for details. */ #include #include #include #include #include #include #include "db.h" #include "config.h" #include "externs.h" /* remove the first occurence of what in list headed by first */ dbref remove_first(dbref first, dbref what) { dbref prev; /* special case if it's the first one */ if(first == what) return DBFETCH(first)->next; else { /* have to find it */ DOLIST(prev, first) { if(DBFETCH(prev)->next == what) { DBSTORE(prev, next, DBFETCH(what)->next); return first; } } return first; } } int member(dbref thing, dbref list) { DOLIST(list, list) { if(list == thing) return 1; } return 0; } dbref reverse(dbref list) { dbref newlist; dbref rest; newlist = NOTHING; while(list != NOTHING) { rest = DBFETCH(list)->next; PUSH(list, newlist); DBDIRTY(newlist); list = rest; } return newlist; } int Read(int fd, void *buf, int nb) { char *bp; int left; int did; int done; bp = buf; left = nb; done = 0; while (left > 0) { did = read(fd,bp,left); if (did < 0) return(-1); if (did == 0) { errno = 0; return(-1); } bp += did; left -= did; done += did; } return(0); } const char *ntop(const struct sockaddr *sa) { #define NRV 8 #define RVLEN 64 static char rbuf[NRV][RVLEN]; static int hand = 0; const char *rv; if (hand > 0) hand --; else hand = NRV-1; switch (sa->sa_family) { case AF_INET: rv = inet_ntop(AF_INET,&((const struct sockaddr_in *)sa)->sin_addr,&rbuf[hand][0],RVLEN); break; case AF_INET6: rv = inet_ntop(AF_INET6,&((const struct sockaddr_in6 *)sa)->sin6_addr,&rbuf[hand][0],RVLEN); break; default: snprintf(&rbuf[hand][0],RVLEN,"(? af %d)",sa->sa_family); rv = &rbuf[hand][0]; break; } return(rv); #undef NRV #undef RVLEN } const char *addr_and_port(const struct sockaddr *sa) { #define NRV 8 #define RVLEN 100 static char rbuf[NRV][RVLEN]; static int hand = 0; const char *astr; static char atxt[RVLEN]; int port; if (hand > 0) hand --; else hand = NRV-1; switch (sa->sa_family) { case AF_INET: astr = inet_ntop(AF_INET,&((const struct sockaddr_in *)sa)->sin_addr,&atxt[0],RVLEN); port = ntohs(((const struct sockaddr_in *)sa)->sin_port); break; case AF_INET6: astr = inet_ntop(AF_INET6,&((const struct sockaddr_in6 *)sa)->sin6_addr,&atxt[0],RVLEN); port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port); break; default: snprintf(&rbuf[hand][0],RVLEN,"(? af %d)",sa->sa_family); return(&rbuf[hand][0]); break; } snprintf(&rbuf[hand][0],RVLEN,"%.*s/%d",RVLEN-8,astr,port); return(&rbuf[hand][0]); #undef NRV #undef RVLEN } int malprintf(char **spp, const char *fmt, ...) { va_list ap; char *s; int len; va_start(ap,fmt); len = vasprintf(&s,fmt,ap); va_end(ap); *spp = dup_string(s); (free)(s); return(len); }