src/file_c.c File Reference

#include <dirent.h>
#include "copyright.h"
#include "config.h"
#include "interface.h"
#include "mudconf.h"
#include "command.h"
#include "file_c.h"
#include "alloc.h"
#include "externs.h"

Include dependency graph for file_c.c:

Go to the source code of this file.

Data Structures

struct  filecache_hdr
struct  filecache_block
struct  filecache_block::filecache_block_hdr

Defines

#define FBLOCK_SIZE   (MBUF_SIZE - sizeof(FBLKHDR))
#define MAX_CONN   100

Typedefs

typedef filecache_hdr FCACHE
typedef filecache_block_hdr FBLKHDR
typedef filecache_block FBLOCK

Functions

void do_list_file (dbref player, dbref cause, int extra, char *arg)
static FBLOCKfcache_fill (FBLOCK *fp, char ch)
static int fcache_read (FBLOCK **cp, char *filename)
void fcache_read_dir (char *dir, FCACHE foo[], int *cnt, int max)
void fcache_rawdump (int fd, int num)
void fcache_dumpbase (DESC *d, FCACHE fc[], int num)
void fcache_dump (DESC *d, int num)
void fcache_dump_conn (DESC *d, int num)
void fcache_send (dbref player, int num)
void fcache_load (dbref player)
void fcache_init (void)

Variables

FCACHE fcache []
NAMETAB list_files []
int fcache_conn_c = 0
FCACHE fcache_conn [MAX_CONN]


Define Documentation

#define FBLOCK_SIZE   (MBUF_SIZE - sizeof(FBLKHDR))

Definition at line 35 of file file_c.c.

#define MAX_CONN   100

Definition at line 78 of file file_c.c.

Referenced by fcache_load().


Typedef Documentation

typedef struct filecache_block_hdr FBLKHDR

Definition at line 18 of file file_c.c.

typedef struct filecache_block FBLOCK

Definition at line 19 of file file_c.c.

typedef struct filecache_hdr FCACHE

Definition at line 17 of file file_c.c.


Function Documentation

void do_list_file ( dbref  player,
dbref  cause,
int  extra,
char *  arg 
)

Definition at line 82 of file file_c.c.

References display_nametab(), fcache_send(), list_files, and search_nametab().

00083 {
00084         int flagvalue;
00085 
00086         flagvalue = search_nametab(player, list_files, arg);
00087         if(flagvalue < 0) {
00088                 display_nametab(player, list_files,
00089                                                 (char *) "Unknown file.  Use one of:", 1);
00090                 return;
00091         }
00092         fcache_send(player, flagvalue);
00093 }

void fcache_dump ( DESC d,
int  num 
)

Definition at line 258 of file file_c.c.

References FC_LAST, fcache, and fcache_dumpbase().

Referenced by check_connect(), failconn(), fcache_send(), shutdownsock(), and welcome_user().

00259 {
00260         if((num < 0) || (num > FC_LAST))
00261                 return;
00262         fcache_dumpbase(d, fcache, num);
00263 }

void fcache_dump_conn ( DESC d,
int  num 
)

Definition at line 265 of file file_c.c.

References fcache_conn, and fcache_dumpbase().

Referenced by welcome_user().

00266 {
00267         fcache_dumpbase(d, fcache_conn, num);
00268 }

void fcache_dumpbase ( DESC d,
FCACHE  fc[],
int  num 
)

Definition at line 246 of file file_c.c.

References filecache_block::data, filecache_hdr::fileblock, filecache_block::hdr, filecache_block::filecache_block_hdr::nchars, filecache_block::filecache_block_hdr::nxt, and queue_write().

Referenced by fcache_dump(), and fcache_dump_conn().

00247 {
00248         FBLOCK *fp;
00249 
00250         fp = fc[num].fileblock;
00251 
00252         while (fp != NULL) {
00253                 queue_write(d, fp->data, fp->hdr.nchars);
00254                 fp = fp->hdr.nxt;
00255         }
00256 }

static FBLOCK* fcache_fill ( FBLOCK fp,
char  ch 
) [static]

Definition at line 95 of file file_c.c.

References alloc_mbuf, filecache_block::data, filecache_block::hdr, MBUF_SIZE, filecache_block::filecache_block_hdr::nchars, and filecache_block::filecache_block_hdr::nxt.

Referenced by fcache_read().

00096 {
00097         FBLOCK *tfp;
00098 
00099         if(fp->hdr.nchars >= (MBUF_SIZE - sizeof(FBLKHDR))) {
00100 
00101                 /*
00102                  * We filled the current buffer.  Go get a new one. 
00103                  */
00104 
00105                 tfp = fp;
00106                 fp = (FBLOCK *) alloc_mbuf("fcache_fill");
00107                 fp->hdr.nxt = NULL;
00108                 fp->hdr.nchars = 0;
00109                 tfp->hdr.nxt = fp;
00110         }
00111         fp->data[fp->hdr.nchars++] = ch;
00112         return fp;
00113 }

void fcache_init ( void   ) 

Definition at line 311 of file file_c.c.

References fcache, fcache_load(), filecache_hdr::fileblock, filecache_hdr::filename, and NOTHING.

Referenced by main().

00312 {
00313         FCACHE *fp;
00314 
00315         for(fp = fcache; fp->filename; fp++) {
00316                 fp->fileblock = NULL;
00317         }
00318         fcache_load(NOTHING);
00319 }

void fcache_load ( dbref  player  ) 

Definition at line 279 of file file_c.c.

References alloc_lbuf, alloc_sbuf, confdata::conn_dir, filecache_hdr::desc, fcache, fcache_conn, fcache_read(), fcache_read_dir(), filecache_hdr::fileblock, filecache_hdr::filename, free_lbuf, free_sbuf, MAX_CONN, mudconf, NOTHING, notify, Quiet, and safe_str.

Referenced by do_readcache(), and fcache_init().

00280 {
00281         FCACHE *fp;
00282         char *buff, *bufc, *sbuf;
00283         int i;
00284 
00285         buff = bufc = alloc_lbuf("fcache_load.lbuf");
00286         sbuf = alloc_sbuf("fcache_load.sbuf");
00287         for(fp = fcache; fp->filename; fp++) {
00288                 i = fcache_read(&fp->fileblock, fp->filename);
00289                 if((player != NOTHING) && !Quiet(player)) {
00290                         sprintf(sbuf, "%d", i);
00291                         if(fp == fcache)
00292                                 safe_str((char *) "File sizes: ", buff, &bufc);
00293                         else
00294                                 safe_str((char *) "  ", buff, &bufc);
00295                         safe_str((char *) fp->desc, buff, &bufc);
00296                         safe_str((char *) "...", buff, &bufc);
00297                         safe_str(sbuf, buff, &bufc);
00298                 }
00299         }
00300         *bufc = '\0';
00301         if(*mudconf.conn_dir)
00302                 fcache_read_dir(mudconf.conn_dir, fcache_conn, &fcache_conn_c,
00303                                                 MAX_CONN);
00304         if((player != NOTHING) && !Quiet(player)) {
00305                 notify(player, buff);
00306         }
00307         free_lbuf(buff);
00308         free_sbuf(sbuf);
00309 }

void fcache_rawdump ( int  fd,
int  num 
)

Definition at line 220 of file file_c.c.

References filecache_block::data, FC_LAST, fcache, filecache_hdr::fileblock, filecache_block::hdr, filecache_block::filecache_block_hdr::nchars, filecache_block::filecache_block_hdr::nxt, and WRITE.

Referenced by accept_new_connection().

00221 {
00222         int cnt, remaining;
00223         char *start;
00224         FBLOCK *fp;
00225 
00226         if((num < 0) || (num > FC_LAST))
00227                 return;
00228         fp = fcache[num].fileblock;
00229 
00230         while (fp != NULL) {
00231                 start = fp->data;
00232                 remaining = fp->hdr.nchars;
00233                 while (remaining > 0) {
00234 
00235                         cnt = WRITE(fd, start, remaining);
00236                         if(cnt < 0)
00237                                 return;
00238                         remaining -= cnt;
00239                         start += cnt;
00240                 }
00241                 fp = fp->hdr.nxt;
00242         }
00243         return;
00244 }

static int fcache_read ( FBLOCK **  cp,
char *  filename 
) [static]

Definition at line 115 of file file_c.c.

References alloc_lbuf, alloc_mbuf, fcache_fill(), free_lbuf, free_mbuf, filecache_block::hdr, LBUF_SIZE, log_error(), LOG_PROBLEMS, filecache_block::filecache_block_hdr::nchars, and filecache_block::filecache_block_hdr::nxt.

Referenced by fcache_load(), and fcache_read_dir().

00116 {
00117         int n, nmax, tchars, fd;
00118         char *buff;
00119         FBLOCK *fp, *tfp;
00120 
00121         /*
00122          * Free a prior buffer chain 
00123          */
00124 
00125         fp = *cp;
00126         while (fp != NULL) {
00127                 tfp = fp->hdr.nxt;
00128                 free_mbuf(fp);
00129                 fp = tfp;
00130         }
00131         *cp = NULL;
00132 
00133         /*
00134          * Read the text file into a new chain 
00135          */
00136 
00137         if((fd = open(filename, O_RDONLY)) == -1) {
00138 
00139                 /*
00140                  * Failure: log the event 
00141                  */
00142 
00143                 log_error(LOG_PROBLEMS, "FIL", "OPEN", "Couldn't open file '%s'.",
00144                                   filename);
00145 
00146                 return -1;
00147         }
00148         buff = alloc_lbuf("fcache_read.temp");
00149 
00150         /*
00151          * Set up the initial cache buffer to make things easier 
00152          */
00153 
00154         fp = (FBLOCK *) alloc_mbuf("fcache_read.first");
00155         fp->hdr.nxt = NULL;
00156         fp->hdr.nchars = 0;
00157         *cp = fp;
00158         tchars = 0;
00159 
00160         /*
00161          * Process the file, one lbuf at a time 
00162          */
00163 
00164         nmax = read(fd, buff, LBUF_SIZE);
00165         while (nmax > 0) {
00166 
00167                 for(n = 0; n < nmax; n++) {
00168                         switch (buff[n]) {
00169                         case '\n':
00170                                 fp = fcache_fill(fp, '\r');
00171                                 fp = fcache_fill(fp, '\n');
00172                                 tchars += 2;
00173                         case '\0':
00174                         case '\r':
00175                                 break;
00176                         default:
00177                                 fp = fcache_fill(fp, buff[n]);
00178                                 tchars++;
00179                         }
00180                 }
00181                 nmax = read(fd, buff, LBUF_SIZE);
00182         }
00183         free_lbuf(buff);
00184         close(fd);
00185 
00186         /*
00187          * If we didn't read anything in, toss the initial buffer 
00188          */
00189 
00190         if(fp->hdr.nchars == 0) {
00191                 *cp = NULL;
00192                 free_mbuf(fp);
00193         }
00194         return tchars;
00195 }

void fcache_read_dir ( char *  dir,
FCACHE  foo[],
int *  cnt,
int  max 
)

Definition at line 197 of file file_c.c.

References fcache_read(), and LBUF_SIZE.

Referenced by fcache_load().

00198 {
00199         DIR *d;
00200         struct dirent *de;
00201         char buf[LBUF_SIZE];
00202 
00203         bzero(&foo[0], sizeof(FCACHE) * max);
00204         if(!(d = opendir(dir)))
00205                 return;
00206         for(*cnt = 0; *cnt < max;) {
00207                 if(!(de = readdir(d)))
00208                         break;
00209                 if(de->d_name[0] == '.')
00210                         continue;
00211                 if(!strstr(de->d_name, ".txt"))
00212                         continue;
00213                 sprintf(buf, "%s/%s", dir, de->d_name);
00214                 fcache_read(&(foo[*cnt].fileblock), buf);
00215                 (*cnt)++;
00216         }
00217         closedir(d);
00218 }

void fcache_send ( dbref  player,
int  num 
)

Definition at line 270 of file file_c.c.

References DESC_ITER_PLAYER, and fcache_dump().

Referenced by do_list_file(), and do_motd().

00271 {
00272         DESC *d;
00273 
00274         DESC_ITER_PLAYER(player, d) {
00275                 fcache_dump(d, num);
00276         }
00277 }


Variable Documentation

FCACHE fcache[]

Initial value:

 {
        {mudconf.conn_file, NULL, "Conn"}
        ,
        {mudconf.site_file, NULL, "Conn/Badsite"}
        ,
        {mudconf.down_file, NULL, "Conn/Down"}
        ,
        {mudconf.full_file, NULL, "Conn/Full"}
        ,
        {mudconf.guest_file, NULL, "Conn/Guest"}
        ,
        {mudconf.creg_file, NULL, "Conn/Reg"}
        ,
        {mudconf.crea_file, NULL, "Crea/Newuser"}
        ,
        {mudconf.regf_file, NULL, "Crea/RegFaill"}
        ,
        {mudconf.motd_file, NULL, "Motd"}
        ,
        {mudconf.wizmotd_file, NULL, "Wizmotd"}
        ,
        {mudconf.quit_file, NULL, "Quit"}
        ,
        {NULL, NULL, NULL}
}

Definition at line 37 of file file_c.c.

Referenced by fcache_dump(), fcache_init(), fcache_load(), and fcache_rawdump().

FCACHE fcache_conn[MAX_CONN]

Definition at line 80 of file file_c.c.

Referenced by fcache_dump_conn(), and fcache_load().

int fcache_conn_c = 0

Definition at line 79 of file file_c.c.

NAMETAB list_files[]

Initial value:

 {
        {(char *) "badsite_connect", 1, CA_WIZARD, FC_CONN_SITE},
        {(char *) "connect", 2, CA_WIZARD, FC_CONN},
        {(char *) "create_register", 2, CA_WIZARD, FC_CREA_REG},
        {(char *) "down", 1, CA_WIZARD, FC_CONN_DOWN},
        {(char *) "full", 1, CA_WIZARD, FC_CONN_FULL},
        {(char *) "guest_motd", 1, CA_WIZARD, FC_CONN_GUEST},
        {(char *) "motd", 1, CA_WIZARD, FC_MOTD},
        {(char *) "newuser", 1, CA_WIZARD, FC_CREA_NEW},
        {(char *) "quit", 1, CA_WIZARD, FC_QUIT},
        {(char *) "register_connect", 1, CA_WIZARD, FC_CONN_REG},
        {(char *) "wizard_motd", 1, CA_WIZARD, FC_WIZMOTD},
        {NULL, 0, 0, 0}
}

Definition at line 63 of file file_c.c.

Referenced by do_list_file().


Generated on Mon May 28 04:25:29 2007 for BattletechMUX by  doxygen 1.4.7