Card32 fileSniff(Card8 which) { IntX count = 0; Card32 value = 0; fileSeekAbsNotBuffered(which, 0); if (which == 1) filep = &file1; else filep = &file2; count = sysRead(filep->id, filep->buf, 4, filep->name); if (count == 0) value = 0xBADBAD; else { filep->next = filep->buf; value = *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; } fileSeekAbsNotBuffered(which, 0); return value; }
/* Fill buffer */ static void fillBuf(_file_ *file) { IntX count = sysRead(file->id, file->buf, BUFSIZ, file->name); if (count == 0) fatal("file error <premature EOF> [%s]\n", file->name); file->end = file->buf + count; file->next = file->buf; }
/* Fill buffer */ static void fillBuf(void) { IntX count = sysRead(file.id, file.buf, BUFSIZ, file.name); if (count == 0) fatal(SPOT_MSG_EARLYEOF, file.name); file.end = file.buf + count; file.next = file.buf; }
/* Load a .class file normally from the local disk */ static ClassClass * LoadClassFromFile(char *fn, char *dir, char *class_name) { extern int OpenCode(char *, char *, char *, struct stat*); struct stat st; ClassClass *cb = 0; int codefd = -1; int retryCount = 0; unsigned char *external_class; char *detail; codefd = OpenCode(fn, NULL, dir, &st); if (codefd < 0) /* open failed */ return 0; /* Snarf the file into memory. */ external_class = (unsigned char *)sysMalloc(st.st_size); if (external_class == 0) goto failed; if (sysRead(codefd, external_class, st.st_size) != st.st_size) goto failed; sysClose(codefd); codefd = -1; /* Create the internal class */ cb = allocClassClass(); if (cb == NULL || !createInternalClass(external_class, external_class + st.st_size, cb, NULL, class_name, &detail)) { sysFree(external_class); goto failed; } sysFree(external_class); if (verbose) jio_fprintf(stderr, "[Loaded %s]\n", fn); return cb; failed: if (codefd >= 0) sysClose(codefd); if (cb != 0) FreeClass(cb); return 0; }
static void makeArgs(char *filename) { int state; long i; long length; IntX file; char *start = NULL; /* Suppress optimizer warning */ cmdlinetype *cmdl; /* Read whole file into buffer */ file = sysOpenSearchpath(filename); if (file < 0) fatal(SPOT_MSG_BADSCRIPTFILE, filename); length = sysFileLen(file); if (length < 1) fatal(SPOT_MSG_BADSCRIPTFILE, filename); script.buf = memNew(length + 2); message(SPOT_MSG_ECHOSCRIPTFILE, filename); sysSeek(file, 0, 0, filename); sysRead(file, (Card8 *)script.buf, length, filename); sysClose(file, filename); script.buf[length] = '\n'; /* Ensure termination */ script.buf[length+1] = '\0'; /* Ensure termination */ /* Parse buffer into args */ state = 0; da_INIT(script.cmdline, 10, 10); cmdl = da_NEXT(script.cmdline); da_INIT(cmdl->args, 10, 10); *da_NEXT(cmdl->args) = global.progname; for (i = 0; i < length + 1; i++) { char c = script.buf[i]; switch (state) { case 0: switch ((int)c) { case '\n': case '\r': cmdl = da_NEXT(script.cmdline); da_INIT(cmdl->args, 10, 10); *da_NEXT(cmdl->args) = global.progname; break; case '\f': case '\t': break; case ' ': break; case '#': state = 1; break; case '"': start = &script.buf[i + 1]; state = 2; break; default: start = &script.buf[i]; state = 3; break; } break; case 1: /* Comment */ if (c == '\n' || c == '\r') state = 0; break; case 2: /* Quoted string */ if (c == '"') { script.buf[i] = '\0'; /* Terminate string */ *da_NEXT(cmdl->args) = start; state = 0; } break; case 3: /* Space-delimited string */ if (isspace((int)c)) { script.buf[i] = '\0'; /* Terminate string */ *da_NEXT(cmdl->args) = start; state = 0; if ((c == '\n') || (c == '\r')) { cmdl = da_NEXT(script.cmdline); da_INIT(cmdl->args, 10, 10); *da_NEXT(cmdl->args) = global.progname; } } break; } } }