[BACK]Return to file2.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/file2.c, Revision 1.1

1.1     ! maekawa     1: #include <stdio.h>
        !             2: #include <sys/time.h>
        !             3: #include <sys/types.h>
        !             4: #include <unistd.h>
        !             5: #include "file2.h"
        !             6:
        !             7: #ifdef KXX
        !             8: #define GC_malloc(n) malloc(n)
        !             9: #else
        !            10: void *GC_malloc(int size);
        !            11: #endif
        !            12:
        !            13: /*  Note:  1997, 12/6   cf. SS475/kxx/openxxx.tex, SS475/memo1.txt
        !            14:     The functions in file2.c should not accept interruptions
        !            15:     during its critical operations.  The synchronization mechanism
        !            16:     has not yet been implemented.
        !            17:     */
        !            18:
        !            19: static int debug1 = 0;
        !            20: static int checkfp2(FILE2 *fp2,char *s);
        !            21: static int checkfp2(FILE2 *fp2,char *s)
        !            22: {
        !            23:   if (fp2 == NULL) {
        !            24:     fprintf(stderr,"%s:  NULL pointer.\n",s);
        !            25:     return(-1);
        !            26:   }
        !            27:   if (fp2->initialized != 1) {
        !            28:     fprintf(stderr,"%s:  fp is not initialized.\n",s);
        !            29:     return(-1);
        !            30:   }
        !            31:   return(0);
        !            32: }
        !            33:
        !            34: FILE2 *fp2open(int fd) {
        !            35:   FILE2 *fp2;
        !            36:   if (debug1) {
        !            37:     printf("fp2open is called. \n");
        !            38:   }
        !            39:   fp2 = (FILE2 *) GC_malloc(sizeof(FILE2));
        !            40:   if (fd < 0) {
        !            41:     fprintf(stderr,"fp2open  Invalid file descriptor %d\n",fd);
        !            42:     return(NULL);
        !            43:   }
        !            44:   if (fp2 == NULL) {
        !            45:     fprintf(stderr,"fp2open.  No memory.\n");
        !            46:     return(NULL);
        !            47:   }
        !            48:   fp2->fd = fd;
        !            49:   fp2->initialized = 1;
        !            50:   checkfp2(fp2,"fp2open ");
        !            51:   fp2->readpos = 0;
        !            52:   fp2->readsize = 0;
        !            53:   fp2->writepos = 0;
        !            54:   fp2->limit = FILE2BSIZE;
        !            55:   fp2->watch = 0;
        !            56:   fp2->watchFile = NULL;
        !            57:   fp2->mathcapList = NULL;
        !            58:   return(fp2);
        !            59: }
        !            60:
        !            61:
        !            62: int fp2fflush(FILE2 *fp2) {
        !            63:   int r;
        !            64:   if (debug1) {
        !            65:     printf("fp2fflush is called with FILE2 *%x.\n", (int )fp2);
        !            66:     fp2dumpBuffer(fp2);
        !            67:     printf("--------------------------\n");
        !            68:   }
        !            69:   if (checkfp2(fp2,"fp2fflush ") == -1) return(-1);
        !            70:   if (fp2->writepos > 0) {
        !            71:     r = write(fp2->fd,fp2->writeBuf,fp2->writepos);
        !            72:     fp2->writepos = 0;
        !            73:     if (r <= 0) {
        !            74:       fprintf(stderr,"fp2fflush(): write failed on %d.\n",fp2->fd);
        !            75:     }
        !            76:     return(r);
        !            77:   }else{
        !            78:     return(0);
        !            79:   }
        !            80: }
        !            81:
        !            82: int fp2fclose(FILE2 *fp2) {
        !            83:   int r;
        !            84:   if (checkfp2(fp2," fp2fclose ") == -1) return(-1);
        !            85:   r = fp2fflush(fp2);
        !            86:   if (r < 0) {
        !            87:     fprintf(stderr,"fp2fclose: flush error.\n");
        !            88:     return(-1);
        !            89:   }
        !            90:   return(close(fp2->fd));
        !            91: }
        !            92:
        !            93: int fp2fputc(int c,FILE2 *fp2) {
        !            94:   if (debug1) {
        !            95:     printf("fp2fputc is called with %2x, fp2->writepos=%d, ",c,fp2->writepos);
        !            96:     printf("fp2 = %x.\n",(int) fp2);
        !            97:   }
        !            98:   if (fp2->watch) {
        !            99:     if (c >= ' ' && c <='z') {
        !           100:       fprintf(fp2->watchFile," %2x(%c)-> ",c,c);
        !           101:     }else{
        !           102:       fprintf(fp2->watchFile," %2x( )-> ",c);
        !           103:     }
        !           104:     fflush(NULL);
        !           105:   }
        !           106:   if (checkfp2(fp2," fp2fputc ") == -1) return(-1);
        !           107:   (fp2->writeBuf)[fp2->writepos] = c;
        !           108:   (fp2->writepos)++;
        !           109:   if (fp2->writepos < fp2->limit) {
        !           110:     return(c);
        !           111:   }else{
        !           112:     if (fp2fflush(fp2) <0) return(-1);
        !           113:     else return(c);
        !           114:   }
        !           115: }
        !           116:
        !           117: int fp2fgetc(FILE2 *fp2) {
        !           118:   int c;
        !           119:   /* printf("fp2fgetc is called. "); fflush(NULL); */
        !           120:   if (checkfp2(fp2," fp2fgetc ") == -1) return(-1);
        !           121:   if (fp2->readpos < fp2->readsize) {
        !           122:     fp2->readpos++;
        !           123:     c = fp2->readBuf[fp2->readpos -1];
        !           124:     if (fp2->watch) {
        !           125:       if (c >= ' ' && c <= 'z') {
        !           126:        fprintf(fp2->watchFile," %2x(%c) ",c,c);
        !           127:       }else{
        !           128:        fprintf(fp2->watchFile," %2x( ) ",c);
        !           129:       }
        !           130:       fflush(NULL);
        !           131:     }
        !           132:     return(c);
        !           133:   }else{
        !           134:     fp2->readpos = 0;
        !           135:     fp2 ->readsize =
        !           136:       read(fp2->fd, fp2->readBuf, fp2->limit);
        !           137:     if (fp2->readsize == 0) {
        !           138:       if (fp2->watch) {
        !           139:        fprintf(fp2->watchFile," <%2x ",c);
        !           140:        fflush(NULL);
        !           141:       }
        !           142:       return(-1);
        !           143:     }
        !           144:     else {
        !           145:       return(fp2fgetc(fp2));
        !           146:     }
        !           147:   }
        !           148: }
        !           149:
        !           150: int fp2select(FILE2 *fp2, int t) {
        !           151:   if (fp2->readpos < fp2->readsize) return(1);
        !           152:   else {
        !           153: #ifdef KXX
        !           154:     return(oxSocketSelect0(fp2->fd,t));
        !           155: #else
        !           156:     return(KsocketSelect0(fp2->fd,t));
        !           157: #endif
        !           158:   }
        !           159: }
        !           160:
        !           161: int fp2dumpBuffer(FILE2 *fp2) {
        !           162:   int i;
        !           163:   if (checkfp2(fp2," f2pdumpBuf ") == -1) {
        !           164:     return(-1);
        !           165:   }
        !           166:   printf("fd=%d\n",fp2->fd);
        !           167:   printf("initialied=%d\n",fp2->initialized);
        !           168:   printf("readpos=%d\n",fp2->readpos);
        !           169:   printf("readsize=%d\n",fp2->readsize);
        !           170:   printf("writepos=%d\n",fp2->writepos);
        !           171:   printf("limit=%d\n",fp2->limit);
        !           172:   for (i=0; i<fp2->readsize; i++) {
        !           173:     printf("readBuf[%d]=%2x ",i,fp2->readBuf[i]);
        !           174:   }
        !           175:   for (i=0; i<fp2->writepos; i++) {
        !           176:     printf("writeBuf[%d]=%2x ",i,fp2->writeBuf[i]);
        !           177:   }
        !           178:   printf("\n");
        !           179:   return(0);
        !           180: }
        !           181:
        !           182: int fp2clearReadBuf(FILE2 *fp2) {
        !           183:   fd_set readfds;
        !           184:   struct timeval timeout;
        !           185:   extern int errno;
        !           186:   int fd;
        !           187: #define TMP00SIZE  2000
        !           188:   char tmp00[TMP00SIZE];
        !           189:   int n;
        !           190:
        !           191:   if (checkfp2(fp2," fp2clearReadBuf ") == -1) {
        !           192:     return(-1);
        !           193:   }
        !           194:
        !           195:   fp2->readsize=0; fp2->readpos = 0;  /* Clear the buffer. */
        !           196:
        !           197:   fd = fp2->fd;
        !           198:   while (1) {
        !           199:     FD_ZERO(&readfds);
        !           200:     FD_SET(fd,&readfds);
        !           201:     timeout.tv_sec = 0;
        !           202:     timeout.tv_usec = (long) 0;
        !           203:     fp2->readpos = fp2->readsize = 0;
        !           204:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
        !           205:       fprintf(stderr,"fp2clearReadBuf: Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
        !           206:       return(-1);
        !           207:     }
        !           208:     if (FD_ISSET(fd,&readfds)) {
        !           209:       n = read(fd,tmp00, TMP00SIZE);
        !           210:       if (n <=  0) {
        !           211:        fprintf(stderr,"fp2clearReadBuf: File is closed or read error.\n");
        !           212:        return(-1);
        !           213:       }
        !           214:       if ( n < TMP00SIZE ) {
        !           215:        return(0);
        !           216:       }
        !           217:     }else {
        !           218:       return(0);
        !           219:     }
        !           220:   }
        !           221: }
        !           222: int fp2write(FILE2 *os, char *data, int size) {
        !           223:   int i,r;
        !           224:   for (i=0; i<size; i++) {
        !           225:     r = fp2fputc(data[i],os);
        !           226:   }
        !           227:   return(r);
        !           228: }
        !           229:
        !           230: int fp2watch(FILE2 *fp2,FILE *f)
        !           231: {
        !           232:   if (f == NULL) {
        !           233:     fprintf(stderr,"fp2watch FILE *f is NULL.\n");
        !           234:     return(-1);
        !           235:   }
        !           236:   if (fp2 == NULL) {
        !           237:     fprintf(stderr,"fp2watch FILE2 *fp2 is NULL.\n");
        !           238:     return(-1);
        !           239:   }
        !           240:   if (fp2->watch) {
        !           241:     fprintf(stderr,"fp2watch: fp2 is already taking a log.");
        !           242:     return(-1);
        !           243:   }
        !           244:   fp2->watch = 1;
        !           245:   fp2->watchFile = f;
        !           246:   return(0);
        !           247: }
        !           248:
        !           249: int fp2stopWatch(FILE2 *fp2)
        !           250: {
        !           251:   if (fp2 == NULL) {
        !           252:     fprintf(stderr,"fp2stopWatch FILE2 *fp2 is NULL.\n");
        !           253:     return(-1);
        !           254:   }
        !           255:   if (!fp2->watch) {
        !           256:     fprintf(stderr,"fp2stopWatch: fp2 is not taking a log.");
        !           257:     return(-1);
        !           258:   }
        !           259:   fp2->watch=0;
        !           260:   if (fp2->watchFile != stdout) {
        !           261:     return(fclose(fp2->watchFile));
        !           262:   }
        !           263: }
        !           264:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>