From: Peep P. <so...@us...> - 2004-03-28 18:10:05
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31728 Modified Files: dfuns.c Log Message: atoi(), read_file() Index: dfuns.c =================================================================== RCS file: /cvsroot/agd/server/src/dfuns.c,v retrieving revision 1.16 retrieving revision 1.17 diff -u -d -r1.16 -r1.17 --- dfuns.c 21 Mar 2004 18:32:35 -0000 1.16 +++ dfuns.c 28 Mar 2004 17:58:46 -0000 1.17 @@ -6,7 +6,7 @@ #include <netdb.h> /* for gethostbyaddr() */ extern time_t startup_time; /* for uptime() */ -extern variable_t *fp, *sp; + void (**dfptr)(void); extern array_t global_ids; @@ -54,19 +54,6 @@ return len; } -#if 0 -char *do_time(void) -{ - static char buf[512]; - time_t t; - struct tm *tm; - time(&t); - tm = localtime(&t); - strftime(buf, 512, "%T %d %B %Y", tm); - return buf; -} -#endif - void shout(char *s, object_t *exclude) { list_t *p; @@ -183,7 +170,8 @@ object_t *ret; push_control_stack(); - ret = clone_object(/*saved_*/fp->u.s); + fp++; + ret = clone_object(csp[-1].fp->u.s); pop_control_stack(1); pop_stack(); @@ -192,7 +180,8 @@ void df_interactivep(void) { - int ret = interactivep(fp->u.ob); + object_t *ob = fp->u.ob; + int ret = ob && ob->iaob; pop_stack(); push_int(ret); } @@ -243,6 +232,7 @@ push_int(ret); } +/* Numbers. */ void df_random(void) { int i, j; @@ -252,6 +242,14 @@ push_int(j); } +void df_atoi(void) +{ + int i; + i = atoi(fp->u.s); + pop_stack(); + push_int(i); +} + /* Time. */ void df_uptime(void) { @@ -285,6 +283,7 @@ t = fp[0].u.i; tm = localtime(&t); + buf[0] = '\0'; strftime(buf, 512, fp[1].u.s, tm); pop_stack(); pop_stack(); @@ -295,7 +294,7 @@ void df_strlen(void) { int i; - i = strlen(fp->u.s); + i = fp->u.s ? strlen(fp->u.s) : 0; pop_stack(); push_int(i); } @@ -323,7 +322,36 @@ push_string(s, ST_MALLOC); } -void df_report_obs(void) +/* Filesystem. */ +df_read_file(void) { - report_obs(); + FILE *f; + char *path; + int file_size; + char *file_str, *p; + char buf[256]; + + path = absolute_path(fp->u.s); + f = fopen(path, "r"); + if(!f) { + /* I'd rather use a dynamically allocated buffer + * and also show the filename, but runtime() does + * longjmp(), and memory will be lost. */ + runtime("can't open file for reading!"); + } + + file_str = NULL; + file_size = 0; + while(fgets(buf, 256, f)) { + int len = strlen(buf); + file_str = realloc(file_str, file_size + len + 1); + p = file_str + file_size; + file_size += len; + strcpy(p, buf); + } + fclose(f); + + pop_stack(); + push_string(file_str, ST_MALLOC); } + |