// // Open stream <f> for fast character input. // int fcfopen(Sfio_t *f) { int n; char *buff; Fcin_t save; errno = 0; _Fcin.fcbuff = _Fcin.fcptr; _Fcin._fcfile = f; fcsave(&save); if (!(buff = (char *)sfreserve(f, SF_UNBOUND, SF_LOCKR))) { fcrestore(&save); _Fcin.fcchar = 0; _Fcin.fcptr = _Fcin.fcbuff = &_Fcin.fcchar; _Fcin.fclast = NULL; _Fcin._fcfile = NULL; return EOF; } n = sfvalue(f); fcrestore(&save); sfread(f, buff, 0); _Fcin.fcoff = sftell(f); buff = (char *)sfreserve(f, SF_UNBOUND, SF_LOCKR); _Fcin.fclast = (_Fcin.fcptr = _Fcin.fcbuff = (unsigned char *)buff) + n; if (sffileno(f) >= 0) *_Fcin.fclast = 0; return n; }
Sfoff_t huffdecode(register Huff_t *hp,Sfio_t *input,Sfio_t *output,int size) { register long buffer; register int left, i, n; register int lev = 0; register unsigned char *outp; register unsigned char *inp; unsigned char *outend; unsigned char *outbuff; Sfoff_t insize = hp->outsize; /* decode the header if called with different hp */ if(lastid!=hp->id) { decode_header(hp); if(!hp->id) hp->id = id++; lastid = hp->id; } /* set up output buffers for faster access */ if(!(outp=outbuff=(unsigned char*)sfreserve(output,SF_UNBOUND,SF_LOCKR))) return(sfvalue(output)); n = sfvalue(output); if(size>=0) { if(n > size) n = size; size -= n; } outend = outp+n; /* set up input buffers for faster access */ infile = input; if(!(inp=inbuff=(unsigned char*)sfreserve(infile,SF_UNBOUND,0))) return(sfvalue(infile)); inend = inp + sfvalue(infile); buffer = hp->buffer; left = hp->left; /* main decoding loop */ while (1) { if(lev==0) { fillbuff(buffer,left,hp->maxlev,inp); i = getbits(buffer,left,CHUNK); if((n=(numbits[i]-1)) >= 0) { putbits(buffer,left,n); *outp++ = outchar[i]; goto pout; } if(hp->excess) { putbits(buffer,left,hp->excess); i >>= hp->excess; } lev = CHUNK-hp->excess; }
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f; char *str, *alpha, *s; char buf[128]; int n; str = "0123456789"; alpha = "abcdefghijklmnop"; if (!(f = sfopen(NULL, alpha, "s"))) terror("Opening stream"); for (n = 9; n >= 0; --n) { if (sfungetc(f, n + '0') != n + '0') terror("Ungetc"); } if (!(s = sfreserve(f, SF_UNBOUND, 0)) || sfvalue(f) != 10) terror("Peek stream1"); if (strncmp(s, str, 10) != 0) terror("Bad data1"); if (!(s = sfreserve(f, SF_UNBOUND, 0)) || sfvalue(f) != (ssize_t)strlen(alpha)) { terror("Peek stream2"); } if (strncmp(s, alpha, strlen(alpha)) != 0) terror("Bad data2"); sfseek(f, (Sfoff_t)0, 0); for (n = 9; n >= 0; --n) { if (sfungetc(f, n + '0') != n + '0') terror("Ungetc2"); } if (sfgetc(f) != '0') terror("Sfgetc"); sfseek(f, (Sfoff_t)0, 0); if (!(s = sfreserve(f, SF_UNBOUND, 0)) || sfvalue(f) != (ssize_t)strlen(alpha)) { terror("Peek stream3"); } if (strncmp(s, alpha, strlen(alpha)) != 0) terror("Bad data2"); sfseek(f, (Sfoff_t)0, 0); if (sfungetc(f, '0') != '0') terror("Ungetc3"); strcpy(buf, "0123456789\n"); if (!(f = sfopen(f, buf, "s+"))) terror("Reopening string"); if (sfungetc(f, '\n') != '\n') terror("Can't unget new-line2"); if (sfungetc(f, 'd') != 'd') terror("Can't unget d"); if (sfungetc(f, 'c') != 'c') terror("Can't unget c"); if (sfungetc(f, '\n') != '\n') terror("Can't unget new-line"); if (sfungetc(f, 'b') != 'b') terror("Can't unget b"); if (sfungetc(f, 'a') != 'a') terror("Can't unget a"); if (!(s = sfgetr(f, '\n', 1)) || strcmp(s, "ab") != 0) terror("Did not get ab"); if (!(s = sfgetr(f, '\n', 1)) || strcmp(s, "cd") != 0) terror("Did not get cd"); if (!(s = sfgetr(f, '\n', 1)) || strcmp(s, "0123456789") != 0) terror("Did not get 0123456789"); texit(0); }
static int verify(char* path, char* old, char* processor, int must) { char* ns; char* os; int nz; int oz; int r; Sfio_t* nf; Sfio_t* of; r = 0; if (nf = sfopen(NiL, path, "r")) { if ((ns = sfgetr(nf, '\n', 1)) && (nz = sfvalue(nf) - 1) > 0) { ns += nz; if ((oz = strlen(processor)) <= nz && !strcmp(processor, ns - oz)) r = 1; else error(2, "%s: %s clashes with %s", path, processor, ns - nz); } if (r && old && sfseek(nf, 0L, 0) == 0 && (of = sfopen(NiL, old, "r"))) { for (;;) { ns = sfreserve(nf, 0, 0); nz = sfvalue(nf); os = sfreserve(of, 0, 0); oz = sfvalue(nf); if (nz <= 0 || oz <= 0) break; if (nz > oz) nz = oz; if (memcmp(ns, os, nz)) break; nz = sfread(nf, ns, nz); oz = sfread(of, os, nz); if (!nz || !oz) break; } sfclose(of); if (!nz && !oz && !touch(old, (time_t)-1, (time_t)-1, 0)) r = 0; } sfclose(nf); } else if (must) error(ERROR_SYSTEM|2, "%s: cannot read", path); return r; }
static int fixedopen(Dssfile_t* file, Dssdisc_t* disc) { if (file->flags & DSS_FILE_READ) { if (!sfreserve(file->io, file->skip, 0)) { if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "header read error"); return -1; } if (!(file->data = (void*)vmnewof(file->dss->vm, 0, State_t, 1, 0))) { if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space"); return -1; } ((State_t*)file->data)->swap = file->ident; } else if (!(file->flags & DSS_FILE_APPEND)) { Fixedheader_t hdr; memset(&hdr, 0, sizeof(hdr)); hdr.magic.magic = MAGICID; strcpy(hdr.magic.name, MAGIC_NAME); strcpy(hdr.magic.type, MAGIC_TYPE); hdr.magic.version = MAGIC_VERSION; hdr.magic.size = sizeof(Netflow_t); sfwrite(file->io, &hdr, sizeof(hdr)); } return 0; }
MAIN() { char buf[1024], *s; Sfio_t* f; f = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,-1,SF_WRITE|SF_STRING); sfsetbuf(sfstdout,buf,sizeof(buf)); sfsetbuf(sfstderr,buf,sizeof(buf)); sfset(sfstdout,SF_SHARE,0); sfset(sfstderr,SF_SHARE,0); if(!sfpool(sfstdout,f,SF_SHARE) || !sfpool(sfstderr,f,SF_SHARE) ) terror("Pooling\n"); if(sfputr(sfstdout,"01234",-1) != 5) terror("Writing to stderr\n"); if(sfputr(sfstderr,"56789",-1) != 5) terror("Writing to stdout\n"); if(sfputc(f,'\0') < 0) terror("Writing to string stream\n"); sfseek(f,(Sfoff_t)0,0); if(!(s = sfreserve(f,SF_UNBOUND,1)) ) terror("Peeking\n"); sfwrite(f,s,0); if(strcmp(s,"0123456789") != 0) terror("Data is wrong\n"); TSTEXIT(0); }
static int fixedread(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc) { register Fixedstate_t* state = (Fixedstate_t*)file->data; register Bgproute_t* rp; if (!(rp = (Bgproute_t*)sfreserve(file->io, sizeof(*rp), 0))) { if (sfvalue(file->io)) { if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "%s: last record incomplete", file->format->name); return -1; } return 0; } if (state->swap) { swapmem(state->swap, rp, rp, (char*)&rp->path - (char*)rp); if (rp->cluster.size) swapmem(state->swap, rp->data + rp->cluster.offset, rp->data + rp->cluster.offset, rp->cluster.size * sizeof(Bgpnum_t)); if (state->swap & 1) { swapmem(state->swap & 1, (char*)&rp->path, (char*)&rp->path, (char*)&rp->bits - (char*)&rp->path); if (rp->path.size) swapmem(state->swap & 1, rp->data + rp->path.offset, rp->data + rp->path.offset, rp->path.size * sizeof(Bgpasn_t)); if (rp->community.size) swapmem(state->swap & 1, rp->data + rp->community.offset, rp->data + rp->community.offset, rp->community.size * sizeof(Bgpasn_t)); } } record->data = rp; record->size = rp->size; return 1; }
MAIN() { Sfio_t* f; char buf[1024], *s; int n; #ifdef DEBUG Sfio_t* logf = sfopen(0,"LOG","a"); sfsetbuf(logf,NIL(Void_t*),0); #endif alarm(10); if(argc > 1) { /* coprocess only */ while((s = sfreserve(sfstdin,-1,0)) ) { #ifdef DEBUG sfwrite(logf, s, sfvalue(sfstdin)); #endif sfwrite(sfstdout, s, sfvalue(sfstdin)); } return 0; } /* make coprocess */ if(!(f = sfpopen(NIL(Sfio_t*), sfprints("%s -p",argv[0]), "r+"))) terror("Opening for read/write\n"); for(n = 0; n < 10; ++n) { sfsprintf(buf,sizeof(buf),"Line %d",n); sfputr(f,buf,'\n'); if(!(s = sfgetr(f,'\n',1))) terror("Did not read back line\n"); if(strcmp(s,buf) != 0) terror("Input=%s, Expect=%s\n",s,buf); } if(sfputr(f,"123456789",'\n') != 10) terror("Bad write"); if(sfread(f,buf,3) != 3) terror("Did not get data back\n"); if(strncmp(s,"123",3) != 0) terror("Wrong data\n"); if(sfwrite(f,"aaa",3) != 3 || sfputc(f,'\n') != '\n') terror("Fail on write\n"); if(!(s = sfgetr(f,'\n',1)) ) terror("Should have gotten 456789\n"); if(strcmp(s,"456789") != 0) terror("Wrong data2\n"); if(!(s = sfgetr(f,'\n',1)) ) terror("Should have gotten aaa\n"); if(strcmp(s,"aaa") != 0) terror("Wrong data3\n"); sfclose(f); TSTEXIT(0); }
// // Position history file at size and find next command number. // static int hist_nearend(History_t *hp, Sfio_t *iop, off_t size) { unsigned char *cp, *endbuff, *buff, marker[4]; int n; int incmd = 1; if (size <= 2) goto begin; if (sfseek(iop, size, SEEK_SET) < 0) goto begin; // Skip to marker command and return the number. Numbering commands occur after a null and begin // with HIST_CMDNO. while (true) { cp = buff = (unsigned char *)sfreserve(iop, SF_UNBOUND, SF_LOCKR | SF_WRITE); if (!cp) break; n = sfvalue(iop); endbuff = cp + n; while (true) { // Check for marker. if (!incmd && *cp++ == HIST_CMDNO && *cp == 0) { n = cp + 1 - buff; incmd = -1; break; } incmd = 0; cp += strnlen((char *)cp, endbuff - cp) + 1; // point past the terminating null if (cp > endbuff) { incmd = 1; break; } if (*cp == 0 && ++cp > endbuff) { break; } } size += n; sfread(iop, (char *)buff, n); if (incmd < 0) { if ((n = sfread(iop, (char *)marker, 4)) == 4) { n = (marker[0] << 16) | (marker[1] << 8) | marker[2]; if (n < size / 2) { hp->histmarker = hp->histcnt = size + 4; return n; } n = 4; } if (n > 0) size += n; incmd = 0; } } begin: sfseek(iop, 2, SEEK_SET); hp->histmarker = hp->histcnt = 2; return 1; }
tmain() { Sfio_t* f; char buf[1024]; char* s; int fd[2]; close(0); if(pipe(fd) < 0 || fd[0] != 0) terror("Making pipe"); strcpy(buf,"1234567890"); if(!(f = sfopen(NIL(Sfio_t*),buf,"s"))) terror("Opening string stream"); if(!sfstack(f,sfstdin)) terror("Stacking"); if(write(fd[1],"ab",2) != 2) terror("Writing ab to pipe"); if(!(s = sfreserve(f,SF_UNBOUND,SF_LOCKR)) || sfvalue(f) != 2) terror("Peeking size1 = %d but should be 2", sfvalue(f)); sfread(f,s,0); if(strncmp(s,"ab",2) != 0) terror("Wrong data1"); if(write(fd[1],"cd",2) != 2) terror("Writing cd to pipe"); close(fd[1]); if(!(s = sfreserve(f,4,0)) ) terror("Peeking size2 = %d but should be 4", sfvalue(f)); if(strncmp(s,"abcd",4) != 0) terror("Wrong data2"); if(!(s = sfreserve(f,10,0)) ) terror("Peeking size3 = %d but should be 10", sfvalue(f)); if(strncmp(s,"1234567890",10) != 0) terror("Wrong data3"); texit(0); }
main() { void* s; size_t size; off_t total; if (!(s = sfreserve(sfstdin, SF_UNBOUND, 0))) { sfprintf(sfstderr, "read error\n"); return 1; } size = sfvalue(sfstdin); total = sfsize(sfstdin); sfprintf(sfstdout, "%d\n", recfmt(s, size, total)); return 0; }
static int nocrnl(Sfio_t *in, Sfio_t *out) { register char *cp, *first, *cpmax; register int lastc=0, defer=0,n; while(cp = sfreserve(in,SF_UNBOUND,0)) { if(defer) sfputc(out,lastc); defer = 0; cpmax = cp + sfvalue(in)-1; lastc= *(unsigned char*)(cpmax); *(unsigned char*)(cpmax) = '\r'; while(1) { first = cp; again: while(*cp++ != '\r'); if(cp>=cpmax) { *cpmax = lastc; if(cp==cpmax) { if(lastc!='\n') cp += 2; else defer = 1; } else if(lastc!='\r') cp++; else defer = 1; } else if(*cp !='\n') goto again; if((n=cp-1-first)>0) sfwrite(out,first,n); if(cp>=cpmax) break; } } if(defer) sfputc(out,lastc); return(1); }
// // Flush the current history command. // void hist_flush(History_t *hp) { char *buff; if (hp) { buff = (char *)sfreserve(hp->histfp, 0, SF_LOCKR); if (buff) { hp->histflush = sfvalue(hp->histfp) + 1; sfwrite(hp->histfp, buff, 0); } else { hp->histflush = 0; } if (sfsync(hp->histfp) < 0) { Shell_t *shp = hp->histshell; hist_close(hp); if (!sh_histinit(shp)) sh_offoption(shp, SH_HISTORY); } else { hp->histflush = 0; } } }
tmain() { Sfio_t *f; if (argc > 1) { if (sfopen(sfstdin, argv[1], "r") != sfstdin) terror("Can't reopen stdin"); sfmove(sfstdin, sfstdout, (Sfoff_t)(-1), -1); return 0; } if (!(f = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Opening to write"); if (sfputc(f, 'a') != 'a') terror("sfputc"); if (sfgetc(f) >= 0) terror("sfgetc"); if (!(f = sfopen(f, tstfile("sf", 0), "r"))) terror("Opening to read"); if (sfgetc(f) != 'a') terror("sfgetc2"); if (sfputc(f, 'b') >= 0) terror("sfputc2"); if (!(f = sfopen(f, tstfile("sf", 0), "r+"))) terror("Opening to read/write"); if (sfgetc(f) != 'a') terror("sfgetc3"); if (sfputc(f, 'b') != 'b') terror("sfputc3"); if (sfclose(f) < 0) terror("sfclose"); if (!(f = sfpopen(NULL, sfprints("%s %s", argv[0], tstfile("sf", 0)), "r"))) terror("sfpopen"); if (sfgetc(f) != 'a') terror("sfgetc4"); if (sfgetc(f) != 'b') terror("sfgetc5"); if (sfgetc(f) >= 0) terror("sfgetc6"); if (!(f = sfopen(f, tstfile("sf", 0), "w"))) terror("sfopen"); if (sfputc(f, 'a') != 'a') terror("sfputc1"); sfsetfd(f, -1); if (sfputc(f, 'b') >= 0) terror("sfputc2"); if (sfclose(f) < 0) terror("sfclose"); if (!(f = sfopen(NULL, tstfile("sf", 0), "a+"))) terror("sfopen2"); sfset(f, SF_READ, 0); if (!sfreserve(f, 0, -1)) terror("Failed on buffer getting"); if (sfvalue(f) <= 0) terror("There is no buffer?"); texit(0); }
static int ftfopen(Dssfile_t* file, Dssdisc_t* disc) { State_t* state; if (!sfreserve(file->io, file->ident & ((1<<20)-1), 0)) { if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "heaxder read error"); return -1; } if (!(state = vmnewof(file->dss->vm, 0, State_t, 1, (file->flags & DSS_FILE_WRITE) ? NETFLOW_PACKET : 0))) { if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space"); return -1; } file->data = state; state->swap = (file->ident >> 28) & ((1<<4)-1); state->version = (file->ident >> 20) & ((1<<8)-1); switch (state->version) { case 1: state->size = sizeof(Rec_1_t); break; case 5: state->size = sizeof(Rec_5_t); break; case 6: state->size = sizeof(Rec_6_t); break; case 7: state->size = sizeof(Rec_7_t); break; } state->chunk = (1024 * 1024 + state->size - 1) / state->size; if (file->flags & DSS_FILE_WRITE) state->data = (char*)(state + 1); state->record.version = state->version; return 0; }
static int dump(Css_t* css, register Connection_t* con, int log, Cssdisc_t* disc) { register State_t* state = (State_t*)disc; char* s; size_t n; int r; n = state->logs[log].offset - con->blocked[log]; if (n > CHUNK) n = CHUNK; if (sfseek(state->logs[log].sp, con->blocked[log], SEEK_SET) != con->blocked[log]) error(ERROR_SYSTEM|3, "%s: cannot seek to %I*d", state->logs[log].name, sizeof(con->blocked[log]), con->blocked[log]); message((-1, "[%d] %s reserve n %I*d offset %I*d", __LINE__, state->logs[log].name, sizeof(n), n, sizeof(con->blocked[log]), con->blocked[log])); if (!(s = sfreserve(state->logs[log].sp, n, 0))) error(ERROR_SYSTEM|3, "%s: cannot reserve %d at %I*d", state->logs[log].name, sizeof(n), n, sizeof(con->blocked[log]), con->blocked[log]); r = note(css, con, log, s, n, 1, disc); if (state->logs[log].sp && sfseek(state->logs[log].sp, state->logs[log].offset, SEEK_SET) != state->logs[log].offset) error(ERROR_SYSTEM|3, "%s: cannot seek to %I*d", state->logs[log].name, sizeof(state->logs[log].offset), state->logs[log].offset); return r; }
static int fixedread(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc) { register State_t* state = (State_t*)file->data; register Netflow_t* rp; if (!(rp = (Netflow_t*)sfreserve(file->io, sizeof(*rp), 0))) { if (sfvalue(file->io)) { if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "%slast record incomplete", cxlocation(file->dss->cx, record)); return -1; } return 0; } if (state->swap) { } record->data = rp; record->size = sizeof(*rp); return 1; }
Sfio_t* tokline(const char* arg, int flags, int* line) { Sfio_t* f; Sfio_t* s; Splice_t* d; char* p; char* e; static int hidden; if (!(d = newof(0, Splice_t, 1, 0))) return 0; if (!(s = sfopen(NiL, NiL, "s"))) { free(d); return 0; } if (!(flags & (SF_STRING|SF_READ))) f = (Sfio_t*)arg; else if (!(f = sfopen(NiL, arg, (flags & SF_STRING) ? "s" : "r"))) { free(d); sfclose(s); return 0; } else if ((p = sfreserve(f, 0, 0)) && sfvalue(f) > 11 && strmatch(p, "#!!! +([-0-9]) *([!\n]) !!!\n*") && (e = strchr(p, '\n'))) { flags = strtol(p + 5, &p, 10); error(flags, "%s:%-.*s", arg, e - p - 4, p); } d->disc.exceptf = spliceline; d->sp = f; *(d->line = line ? line : &hidden) = 0; sfdisc(s, (Sfdisc_t*)d); return s; }
// // Copy the last <n> commands to a new file and make this the history file. // static History_t *hist_trim(History_t *hp, int n) { char *cp; int incmd = 1, c = 0; History_t *hist_new, *hist_old = hp; char *buff, *endbuff, *tmpname = NULL; off_t oldp, newp; struct stat statb; unlink(hist_old->histname); if (access(hist_old->histname, F_OK) >= 0) { // The unlink can fail on windows 95. int fd; char *last, *name = hist_old->histname; sh_close(sffileno(hist_old->histfp)); last = strrchr(name, '/'); if (last) { *last = 0; tmpname = ast_temp_file(name, "hist", &fd, 0); *last = '/'; } else { tmpname = ast_temp_file(".", "hist", &fd, 0); } if (!tmpname) { errormsg(SH_DICT, ERROR_exit(1), e_create, "hist"); __builtin_unreachable(); } close(fd); if (rename(name, tmpname) < 0) { free(tmpname); tmpname = name; } fd = open(tmpname, O_RDONLY | O_CLOEXEC); // What happens if this fails and returns -1? Coverity Scan #310940. (void)sfsetfd(hist_old->histfp, fd); if (tmpname == name) { free(tmpname); tmpname = NULL; } } hist_ptr = NULL; if (fstat(sffileno(hist_old->histfp), &statb) >= 0) { histinit = 1; histmode = statb.st_mode; } if (!sh_histinit(hp->histshell)) { // Use the old history file. hist_ptr = hist_old; return hist_ptr; } hist_new = hist_ptr; hist_ptr = hist_old; if (--n < 0) n = 0; newp = hist_seek(hist_old, ++n); while (1) { if (!incmd) { c = hist_ind(hist_new, ++hist_new->histind); hist_new->histcmds[c] = hist_new->histcnt; if (hist_new->histcnt > hist_new->histmarker + HIST_BSIZE / 2) { char locbuff[HIST_MARKSZ]; hist_marker(locbuff, hist_new->histind); sfwrite(hist_new->histfp, locbuff, HIST_MARKSZ); hist_new->histcnt += HIST_MARKSZ; hist_new->histmarker = hist_new->histcmds[hist_ind(hist_new, c)] = hist_new->histcnt; } oldp = newp; newp = hist_seek(hist_old, ++n); if (newp <= oldp) break; } if (!(buff = (char *)sfreserve(hist_old->histfp, SF_UNBOUND, 0))) break; *(endbuff = (cp = buff) + sfvalue(hist_old->histfp)) = 0; // Copy to null byte. incmd = 0; cp += strlen(cp) + 1; // point past the terminating null if (cp > endbuff) { incmd = 1; } else if (*cp == 0) { cp++; } if (cp > endbuff) cp = endbuff; c = cp - buff; hist_new->histcnt += c; sfwrite(hist_new->histfp, buff, c); } hist_cancel(hist_new); sfclose(hist_old->histfp); if (tmpname) { unlink(tmpname); free(tmpname); } free(hist_old); hist_ptr = hist_new; return hist_ptr; }
Dssfile_t* dssfopen(Dss_t* dss, const char* path, Sfio_t* io, Dssflags_t flags, Dssformat_t* format) { Dssfile_t* file; Vmalloc_t* vm; char* s; size_t n; int i; struct stat st; Sfdisc_t top; char buf[PATH_MAX]; if (flags & DSS_FILE_WRITE) { if (io) { memset(&top, 0, sizeof(top)); if (sfdisc(io, &top)) { n = top.disc == &dss->state->compress_preferred; sfdisc(io, SF_POPDISC); if (n) { sfdisc(io, SF_POPDISC); sfdczip(io, path, dss->meth->compress ? dss->meth->compress : "gzip", dss->disc->errorf); } } } if (dss->flags & DSS_APPEND) flags |= DSS_FILE_APPEND; } if (!path || !*path || streq(path, "-")) { if (flags & DSS_FILE_WRITE) { if (io) path = "output-stream"; else { path = "/dev/stdout"; io = sfstdout; } } else if (io) path = "input-stream"; else { path = "/dev/stdin"; io = sfstdin; } flags |= DSS_FILE_KEEP; } else if (io) flags |= DSS_FILE_KEEP; else if (flags & DSS_FILE_WRITE) { if (!(io = sfopen(NiL, path, (flags & DSS_FILE_APPEND) ? "a" : "w"))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot open", path); return 0; } } else if (!(io = dssfind(path, "", DSS_VERBOSE, buf, sizeof(buf), dss->disc))) return 0; else path = (const char*)buf; if (!(vm = vmopen(Vmdcheap, Vmbest, 0))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space"); return 0; } if (!(file = vmnewof(vm, 0, Dssfile_t, 1, strlen(path) + 1))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space"); if (!(flags & DSS_FILE_KEEP)) sfclose(io); vmclose(vm); return 0; } strcpy(file->path = (char*)(file + 1), path); file->dss = dss; file->vm = vm; file->io = io; file->flags = flags; if (flags & DSS_FILE_WRITE) { if (!(file->format = format) && !(file->format = dss->format)) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "output method format must be specified"); if (!(flags & DSS_FILE_KEEP)) sfclose(io); return 0; } file->readf = noreadf; file->writef = file->format->writef; } else { if (sfsize(file->io) || !fstat(sffileno(file->io), &st) && (S_ISFIFO(st.st_mode) #ifdef S_ISSOCK || S_ISSOCK(st.st_mode) #endif )) { if (sfdczip(file->io, file->path, NiL, dss->disc->errorf) < 0) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: inflate error", file->path); dssfclose(file); return 0; } s = sfreserve(file->io, SF_UNBOUND, SF_LOCKR); n = sfvalue(file->io); if (!s) { if (n && dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot peek", file->path); dssfclose(file); return 0; } for (file->format = (Dssformat_t*)dtfirst(dss->meth->formats); file->format && !(i = (*file->format->identf)(file, s, n, dss->disc)); file->format = (Dssformat_t*)dtnext(dss->meth->formats, file->format)); sfread(file->io, s, 0); if (!file->format) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: unknown %s format", file->path, dss->meth->name); dssfclose(file); return 0; } if (i < 0) return 0; if (format && format != file->format) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s file format %s incompatible with %s", file->path, dss->meth->name, file->format->name, format->name); dssfclose(file); return 0; } if ((dss->flags & DSS_VERBOSE) && dss->disc->errorf) (*dss->disc->errorf)(dss, dss->disc, 1, "%s: %s method %s format", file->path, dss->meth->name, file->format->name); file->readf = file->format->readf; } else { file->format = format ? format : dss->format ? dss->format : (Dssformat_t*)dtfirst(dss->meth->formats); file->readf = nullreadf; } file->writef = nowritef; if (!dss->format) dss->format = file->format; } if (!file->format) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s method did not set file format", file->path, dss->meth->name); dssfclose(file); return 0; } file->record.file = file; if ((*file->format->openf)(file, dss->disc)) { dssfclose(file); return 0; } return file; }
int wc_count(Wc_t *wp, Sfio_t *fd, const char* file) { register char* type = wp->type; register unsigned char* cp; register Sfoff_t nbytes; register Sfoff_t nchars; register Sfoff_t nwords; register Sfoff_t nlines; register Sfoff_t eline = -1; register Sfoff_t longest = 0; register ssize_t c; register unsigned char* endbuff; register int lasttype = WC_SP; unsigned int lastchar; ssize_t n; ssize_t o; unsigned char* buff; wchar_t x; unsigned char side[32]; sfset(fd,SF_WRITE,1); nlines = nwords = nchars = nbytes = 0; wp->longest = 0; if (wp->mb < 0 && (wp->mode & (WC_MBYTE|WC_WORDS))) { cp = buff = endbuff = 0; for (;;) { if (cp >= endbuff || (n = mb2wc(x, cp, endbuff-cp)) < 0) { if ((o = endbuff-cp) < sizeof(side)) { if (buff) { if (o) memcpy(side, cp, o); mbinit(); } else o = 0; cp = side + o; if (!(buff = (unsigned char*)sfreserve(fd, SF_UNBOUND, 0)) || (n = sfvalue(fd)) <= 0) { if ((nchars - longest) > wp->longest) wp->longest = nchars - longest; break; } nbytes += n; if ((c = sizeof(side) - o) > n) c = n; if (c) memcpy(cp, buff, c); endbuff = buff + n; cp = side; x = mbchar(cp); if ((cp-side) < o) { cp = buff; nchars += (cp-side) - 1; } else cp = buff + (cp-side) - o; } else { cp++; x = -1; } if (x == -1 && eline != nlines && !(wp->mode & WC_QUIET)) eline = invalid(file, nlines); } else cp += n ? n : 1; if (x == '\n') { if ((nchars - longest) > wp->longest) wp->longest = nchars - longest; longest = nchars + 1; nlines++; lasttype = 1; } else if (iswspace(x)) lasttype = 1; else if (lasttype) { lasttype = 0; nwords++; } nchars++; } if (!(wp->mode & WC_MBYTE)) nchars = nbytes; } else if (!wp->mb && !(wp->mode & WC_LONGEST) || wp->mb > 0 && !(wp->mode & (WC_MBYTE|WC_WORDS|WC_LONGEST))) { if (!(wp->mode & (WC_MBYTE|WC_WORDS|WC_LONGEST))) { while ((cp = (unsigned char*)sfreserve(fd, SF_UNBOUND, 0)) && (c = sfvalue(fd)) > 0) { nchars += c; endbuff = cp + c; if (*--endbuff == '\n') nlines++; else *endbuff = '\n'; for (;;) if (*cp++ == '\n') { if (cp > endbuff) break; nlines++; } } } else { while ((cp = buff = (unsigned char*)sfreserve(fd, SF_UNBOUND, 0)) && (c = sfvalue(fd)) > 0) { nchars += c; /* check to see whether first character terminates word */ if (c==1) { if (eol(lasttype)) nlines++; if ((c = type[*cp]) && !lasttype) nwords++; lasttype = c; continue; } if (!lasttype && type[*cp]) nwords++; lastchar = cp[--c]; *(endbuff = cp+c) = '\n'; c = lasttype; /* process each buffer */ for (;;) { /* process spaces and new-lines */ do { if (eol(c)) for (;;) { /* check for end of buffer */ if (cp > endbuff) goto beob; nlines++; if (*cp != '\n') break; cp++; } } while (c = type[*cp++]); /* skip over word characters */ while (!(c = type[*cp++])); nwords++; } beob: if ((cp -= 2) >= buff) c = type[*cp]; else c = lasttype; lasttype = type[lastchar]; /* see if was in word */ if (!c && !lasttype) nwords--; } if (eol(lasttype)) nlines++; else if (!lasttype) nwords++; } } else { int lineoff=0; int skip=0; int adjust=0; int state=0; int oldc; int xspace; int wasspace = 1; unsigned char* start; lastchar = 0; start = (endbuff = side) + 1; xspace = iswspace(0xa0) || iswspace(0x85); while ((cp = buff = (unsigned char*)sfreserve(fd, SF_UNBOUND, 0)) && (c = sfvalue(fd)) > 0) { nbytes += c; nchars += c; start = cp-lineoff; /* check to see whether first character terminates word */ if(c==1) { if(eol(lasttype)) nlines++; if((c = type[*cp]) && !lasttype) nwords++; lasttype = c; endbuff = start; continue; } lastchar = cp[--c]; endbuff = cp+c; cp[c] = '\n'; if(mbc(lasttype)) { c = lasttype; goto mbyte; } if(!lasttype && spc(type[*cp])) nwords++; c = lasttype; /* process each buffer */ for (;;) { /* process spaces and new-lines */ spaces: do { if (eol(c)) { /* check for end of buffer */ if (cp > endbuff) goto eob; if(wp->mode&WC_LONGEST) { if((cp-start)-adjust > longest) longest = (cp-start)-adjust-1; start = cp; } nlines++; nchars -= adjust; adjust = 0; } } while (spc(c = type[*cp++])); wasspace=1; if(mbc(c)) { mbyte: do { if(c&WC_ERR) goto err; if(skip && (c&7)) break; if(!skip) { if(!(c&7)) { skip=1; break; } skip = (c&7); adjust += skip; state = 0; if(skip==2 && (cp[-1]&0xc)==0 && (state=(cp[-1]&0x3))) oldc = *cp; else if(xspace && cp[-1]==0xc2) { state = 8; oldc = *cp; } } else { skip--; if(state && (state=chkstate(state,oldc))) { if(state==10) { if(!wasspace) nwords++; wasspace = 1; state=0; goto spaces; } oldc = *cp; } } } while (mbc(c = type[*cp++])); wasspace = 0; if(skip) { if(eol(c) && (cp > endbuff)) goto eob; err: skip = 0; state = 0; if(eline!=nlines && !(wp->mode & WC_QUIET)) eline = invalid(file, nlines); while(mbc(c) && ((c|WC_ERR) || (c&7)==0)) c=type[*cp++]; if(eol(c) && (cp > endbuff)) { c = WC_MB|WC_ERR; goto eob; } if(mbc(c)) goto mbyte; else if(c&WC_SP) goto spaces; } if(spc(c)) { nwords++; continue; } } /* skip over word characters */ while(!(c = type[*cp++])); if(mbc(c)) goto mbyte; nwords++; } eob: lineoff = cp-start; if((cp -= 2) >= buff) c = type[*cp]; else c = lasttype; lasttype = type[lastchar]; /* see if was in word */ if(!c && !lasttype) nwords--; } if ((wp->mode&WC_LONGEST) && ((endbuff + 1 - start) - adjust - (lastchar == '\n')) > longest) longest = (endbuff + 1 - start) - adjust - (lastchar == '\n'); wp->longest = longest; if (eol(lasttype)) nlines++; else if (!lasttype) nwords++; if (wp->mode & WC_MBYTE) nchars -= adjust; else nchars = nbytes; } wp->chars = nchars; wp->words = nwords; wp->lines = nlines; return 0; }
int pzheadread(register Pz_t* pz) { register int i; register int n; register unsigned char* s; size_t m; Pzpart_t* pp; if (pz->flags & PZ_HEAD) return 0; /* * check the header magic */ if (s = (unsigned char*)sfreserve(pz->io, 4, 1)) { i = s[0]; n = s[1]; } else i = n = 0; if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, -1, "%s: pzheadread: f=%08x i=%02x n=%02x partition=%s%s", pz->path, pz->flags, i, n, pz->disc->partition, s ? "" : " (nil)"); if (i != PZ_MAGIC_1 || n != PZ_MAGIC_2 || s[2] == 0 || s[3] >= 10) { sfread(pz->io, s, 0); if (pz->flags & PZ_SPLIT) return 0; if (pz->flags & PZ_DISC) { pz->flags &= ~PZ_POP; return -1; } if (!(pz->flags & (PZ_READ|PZ_WRITE|PZ_STAT)) && (m = pz->prefix.count)) { if (pz->prefix.terminator >= 0) { while (m-- > 0) { if (!sfgetr(pz->io, pz->prefix.terminator, 0)) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix record%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s"); return -1; } } } else if (!sfreserve(pz->io, m, 0)) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix byte%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s"); return -1; } } if (!(n = pz->row)) { if ((pz->flags & PZ_ACCEPT) || !sfsize(pz->io)) n = 1; else if ((n = pzfixed(pz, pz->io, NiL, 0)) <= 0 && pz->disc->partition) { pz->flags |= PZ_ROWONLY; if (!pzpartition(pz, pz->disc->partition)) n = pz->row; pz->flags &= ~PZ_ROWONLY; } } if (n <= 0) { if (!(pz->flags & PZ_DELAY) && (pz->disc->partition || !(pz->flags & PZ_FORCE))) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: unknown input format", pz->path); return -1; } pz->flags |= PZ_UNKNOWN; n = 1; } if (!(pp = vmnewof(pz->vm, 0, Pzpart_t, 1, 0))) return -1; pz->major = PZ_MAJOR; pz->minor = PZ_MINOR; pp->name = ""; pp->row = n; return pzpartinit(pz, pp, NiL); } sfread(pz->io, s, 2); pz->flags &= ~PZ_FORCE; pz->major = sfgetc(pz->io); pz->minor = sfgetc(pz->io); switch (pz->major) { case 1: if (pz->minor <= 2) goto noway; break; case 2: pz->win = sfgetu(pz->io); break; default: goto noway; } pz->flags |= PZ_HEAD; return pzpartread(pz); noway: if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: data %d.%d not supported by implementation %d.%d", pz->path, pz->major, pz->minor, PZ_MAJOR, PZ_MINOR); return -1; }
static void cutcols(Cut_t* cut, Sfio_t* fdin, Sfio_t* fdout) { register int c; register int len; register int ncol = 0; register const int* lp = cut->list; register char* bp; register int skip; /* non-zero for don't copy */ int must; const char* xx; for (;;) { if (len = cut->reclen) bp = sfreserve(fdin, len, -1); else bp = sfgetr(fdin, '\n', 0); if (!bp && !(bp = sfgetr(fdin, 0, SF_LASTR))) break; len = sfvalue(fdin); xx = 0; if (!(ncol = skip = *(lp = cut->list))) ncol = *++lp; must = 1; do { if (cut->nosplit) { register const char* s = bp; register int w = len < ncol ? len : ncol; register int z; while (w > 0) { if (!(*s & 0x80)) z = 1; else if ((z = mblen(s, w)) <= 0) { if (s == bp && xx) { w += s - xx; bp = (char*)(s = xx); xx = 0; continue; } xx = s; if (skip) s += w; w = 0; break; } s += z; w -= z; } c = s - bp; ncol = !w && ncol >= len; } else if (cut->cflag) { register const char* s = bp; register int w = len; register int z; while (w > 0 && ncol > 0) { ncol--; if (!(*s & 0x80) || (z = mblen(s, w)) <= 0) z = 1; s += z; w -= z; } c = s - bp; ncol = !w && (ncol || !skip); } else { if ((c = ncol) > len) c = len; else if (c == len && !skip) ncol++; ncol -= c; } if (!skip && c) { if (sfwrite(fdout, (char*)bp, c) < 0) return; must = 0; } bp += c; if (ncol) break; len -= c; ncol = *++lp; skip = !skip; } while (ncol != HUGE); if (!cut->nlflag && (skip || must || cut->reclen)) { if (cut->ldelim.len > 1) sfwrite(fdout, cut->ldelim.str, cut->ldelim.len); else sfputc(fdout, cut->ldelim.chr); } } }
MAIN() { ssize_t size[N_WRITER][N_RECORD]; int count[N_WRITER]; char record[N_WRITER][128], *s; Sfio_t* fw[N_WRITER]; Sfio_t* fr; int i, r, done; /* create random record sizes */ for(i = 0; i < N_WRITER; ++i) for(r = 0; r < N_RECORD; ++r) size[i][r] = (ssize_t)(vrandom()%64) + 2; /* records for different processes */ for(i = 0; i < N_WRITER; ++i) for(r = 0; r < 128; ++r) record[i][r] = '0'+i; /* create file */ fr = sfopen(NIL(Sfio_t*),tstfile(0),"w+"); /* create records */ for(i = 0; i < N_WRITER; ++i) { fw[i] = sfopen(NIL(Sfio_t*),tstfile(0),"a"); count[i] = 0; } for(done = 0; done < N_WRITER; ) { i = (int)(vrandom()%N_WRITER); if(count[i] < N_RECORD) { r = size[i][count[i]]; if(!(s = sfreserve(fw[i],r,1)) || sfvalue(fw[i]) < r ) terror("sfreserve fails in process %d\n", i); memcpy(s,record[i],r-1); s[r-1] = '\n'; sfwrite(fw[i],s,r); if((count[i] += 1) == N_RECORD) { done += 1; sfclose(fw[i]); } } } for(i = 0; i < N_WRITER; ++i) count[i] = 0; while((s = sfgetr(fr,'\n',0)) ) { if((i = s[0] - '0') < 0 || i >= N_WRITER) terror("Wrong record type\n"); for(r = sfvalue(fr)-2; r > 0; --r) if(s[r] != s[0]) terror("Bad record%d, count=%d\n", i, count[i]); if(sfvalue(fr) != size[i][count[i]]) terror("Record%d count=%d size=%d sfvalue=%d\n", i, count[i], size[i][count[i]], sfvalue(fr)); count[i] += 1; } for(i = 0; i < N_WRITER; ++i) if(count[i] != N_RECORD) terror("Bad count%d %d\n", i, count[i]); TSTEXIT(0); }
static int ftfread(register Dssfile_t* file, register Dssrecord_t* record, Dssdisc_t* disc) { register State_t* state = (State_t*)file->data; register Netflow_t* rp = &state->record; register char* fp; size_t n; Nftime_t boot; while (!state->count--) { if (state->data = (char*)sfreserve(file->io, state->chunk * state->size, 0)) { state->count = state->chunk; break; } if (!(n = sfvalue(file->io))) return 0; if (!(state->chunk = n / state->size)) { if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "%slast packet incomplete", cxlocation(file->dss->cx, record)); return -1; } state->count = 0; } memset(rp, 0, sizeof(*rp)); rp->set = NETFLOW_SET_src_addrv4|NETFLOW_SET_dst_addrv4|NETFLOW_SET_hopv4; fp = state->data; state->data += state->size; switch (state->version) { case 1: if (n = state->swap & 3) { swapmem(n, &R1(fp)->unix_secs, &R1(fp)->unix_secs, (char*)&R1(fp)->input - (char*)&R1(fp)->unix_secs); swapmem(n, &R1(fp)->dPkts, &R1(fp)->dPkts, (char*)&R1(fp)->srcport - (char*)&R1(fp)->dPkts); if (n &= 1) { swapmem(n, &R1(fp)->input, &R1(fp)->input, (char*)&R1(fp)->dPkts - (char*)&R1(fp)->input); swapmem(n, &R1(fp)->srcport, &R1(fp)->srcport, (char*)&R1(fp)->prot - (char*)&R1(fp)->srcport); } } rp->src_addrv4 = R1(fp)->srcaddr; rp->dst_addrv4 = R1(fp)->dstaddr; rp->hopv4 = R1(fp)->nexthop; rp->input = R1(fp)->input; rp->output = R1(fp)->output; rp->packets = R1(fp)->dPkts; rp->bytes = R1(fp)->dOctets; rp->first = R1(fp)->First; rp->last = R1(fp)->Last; rp->src_port = R1(fp)->srcport; rp->dst_port = R1(fp)->dstport; rp->flags = 0; rp->tcp_flags = R1(fp)->tcp_flags; rp->protocol = R1(fp)->prot; rp->src_tos = R1(fp)->tos; rp->time = R1(fp)->unix_secs; rp->nsec = R1(fp)->unix_nsecs; rp->uptime = R1(fp)->sysUpTime; break; case 5: if (n = state->swap & 3) { swapmem(n, &R5(fp)->unix_secs, &R5(fp)->unix_secs, (char*)&R5(fp)->input - (char*)&R5(fp)->unix_secs); swapmem(n, &R5(fp)->dPkts, &R5(fp)->dPkts, (char*)&R5(fp)->srcport - (char*)&R5(fp)->dPkts); if (n &= 1) { swapmem(n, &R5(fp)->input, &R5(fp)->input, (char*)&R5(fp)->dPkts - (char*)&R5(fp)->input); swapmem(n, &R5(fp)->srcport, &R5(fp)->srcport, (char*)&R5(fp)->prot - (char*)&R5(fp)->srcport); swapmem(n, &R5(fp)->src_as, &R5(fp)->src_as, (char*)(R5(fp)+1) - (char*)&R5(fp)->src_as); } } rp->src_addrv4 = R5(fp)->srcaddr; rp->dst_addrv4 = R5(fp)->dstaddr; rp->hopv4 = R5(fp)->nexthop; rp->input = R5(fp)->input; rp->output = R5(fp)->output; rp->packets = R5(fp)->dPkts; rp->bytes = R5(fp)->dOctets; rp->first = R5(fp)->First; rp->last = R5(fp)->Last; rp->src_port = R5(fp)->srcport; rp->dst_port = R5(fp)->dstport; rp->flags = 0; rp->tcp_flags = R5(fp)->tcp_flags; rp->protocol = R5(fp)->prot; rp->src_tos = R5(fp)->tos; rp->engine_type = R5(fp)->engine_type; rp->engine_id = R5(fp)->engine_id; rp->src_as16 = R5(fp)->src_as; rp->dst_as16 = R5(fp)->dst_as; rp->src_maskv4 = R5(fp)->src_mask; rp->dst_maskv4 = R5(fp)->dst_mask; rp->time = R5(fp)->unix_secs; rp->nsec = R5(fp)->unix_nsecs; rp->uptime = R5(fp)->sysUpTime; break; case 6: if (n = state->swap & 3) { swapmem(n, &R6(fp)->unix_secs, &R6(fp)->unix_secs, (char*)&R6(fp)->input - (char*)&R6(fp)->unix_secs); swapmem(n, &R6(fp)->dPkts, &R6(fp)->dPkts, (char*)&R6(fp)->srcport - (char*)&R6(fp)->dPkts); if (n &= 1) { swapmem(n, &R6(fp)->input, &R6(fp)->input, (char*)&R6(fp)->dPkts - (char*)&R6(fp)->input); swapmem(n, &R6(fp)->srcport, &R6(fp)->srcport, (char*)&R6(fp)->prot - (char*)&R6(fp)->srcport); swapmem(n, &R6(fp)->src_as, &R6(fp)->src_as, (char*)(R6(fp)+1) - (char*)&R6(fp)->src_as); } } rp->src_addrv4 = R6(fp)->srcaddr; rp->dst_addrv4 = R6(fp)->dstaddr; rp->hopv4 = R6(fp)->nexthop; rp->input = R6(fp)->input; rp->output = R6(fp)->output; rp->packets = R6(fp)->dPkts; rp->bytes = R6(fp)->dOctets; rp->first = R6(fp)->First; rp->last = R6(fp)->Last; rp->src_port = R6(fp)->srcport; rp->dst_port = R6(fp)->dstport; rp->flags = 0; rp->tcp_flags = R6(fp)->tcp_flags; rp->protocol = R6(fp)->prot; rp->src_tos = R6(fp)->tos; rp->engine_type = R6(fp)->engine_type; rp->engine_id = R6(fp)->engine_id; rp->src_as16 = R6(fp)->src_as; rp->dst_as16 = R6(fp)->dst_as; rp->src_maskv4 = R6(fp)->src_mask; rp->dst_maskv4 = R6(fp)->dst_mask; rp->time = R6(fp)->unix_secs; rp->nsec = R6(fp)->unix_nsecs; rp->uptime = R6(fp)->sysUpTime; break; case 7: if (n = state->swap & 3) { swapmem(n, &R7(fp)->unix_secs, &R7(fp)->unix_secs, (char*)&R7(fp)->input - (char*)&R7(fp)->unix_secs); swapmem(n, &R7(fp)->dPkts, &R7(fp)->dPkts, (char*)&R7(fp)->srcport - (char*)&R7(fp)->dPkts); if (n &= 1) { swapmem(n, &R7(fp)->input, &R7(fp)->input, (char*)&R7(fp)->dPkts - (char*)&R7(fp)->input); swapmem(n, &R7(fp)->srcport, &R7(fp)->srcport, (char*)&R7(fp)->prot - (char*)&R7(fp)->srcport); swapmem(n, &R7(fp)->src_as, &R7(fp)->src_as, (char*)(R7(fp)+1) - (char*)&R7(fp)->src_as); } } rp->src_addrv4 = R7(fp)->srcaddr; rp->dst_addrv4 = R7(fp)->dstaddr; rp->hopv4 = R7(fp)->nexthop; rp->input = R7(fp)->input; rp->output = R7(fp)->output; rp->packets = R7(fp)->dPkts; rp->bytes = R7(fp)->dOctets; rp->first = R7(fp)->First; rp->last = R7(fp)->Last; rp->src_port = R7(fp)->srcport; rp->dst_port = R7(fp)->dstport; rp->flags = 0; rp->tcp_flags = R7(fp)->tcp_flags; rp->protocol = R7(fp)->prot; rp->src_tos = R7(fp)->tos; rp->engine_type = R7(fp)->engine_type; rp->engine_id = R7(fp)->engine_id; rp->src_as16 = R7(fp)->src_as; rp->dst_as16 = R7(fp)->dst_as; rp->src_maskv4 = R7(fp)->src_mask; rp->dst_maskv4 = R7(fp)->dst_mask; rp->time = R7(fp)->unix_secs; rp->nsec = R7(fp)->unix_nsecs; rp->uptime = R7(fp)->sysUpTime; break; } boot = ((Nftime_t)rp->time * MS - (Nftime_t)rp->uptime) * US + (Nftime_t)rp->nsec; rp->start = boot + (Nftime_t)rp->first * US; rp->end = boot + (Nftime_t)rp->last * US; record->size = sizeof(*rp); record->data = rp; return 1; }
int pzheadwrite(Pz_t* pz, Sfio_t* op) { register size_t i; register size_t m; register size_t n; register char* s; if (pz->flags & PZ_HEAD) return 0; pz->oop = op; if (!(pz->flags & PZ_NOGZIP)) sfdcgzip(op, 0); if (pz->flags & PZ_NOPZIP) return 0; sfputc(op, PZ_MAGIC_1); sfputc(op, PZ_MAGIC_2); if (sfsync(op)) return -1; sfputc(op, pz->major = PZ_MAJOR); sfputc(op, pz->minor = PZ_MINOR); sfputu(op, pz->win); if (pz->disc->comment) { sfputc(op, PZ_HDR_comment); m = strlen(pz->disc->comment) + 1; sfputu(op, m); sfwrite(op, pz->disc->comment, m); } if (pz->det && (m = sfstrtell(pz->det))) { sfputc(op, PZ_HDR_options); if (!(s = sfstruse(pz->det))) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, ERROR_SYSTEM|2, "out of space"); return -1; } m++; sfputu(op, m); sfwrite(op, s, m); } if (i = pz->prefix.count) { sfputc(op, PZ_HDR_prefix); if (pz->prefix.terminator >= 0) { m = 0; while (i-- > 0) { if (!(s = sfgetr(pz->io, pz->prefix.terminator, 0))) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix record%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s"); return -1; } m += n = sfvalue(pz->io); sfwrite(pz->tmp, s, n); } s = sfstrseek(pz->tmp, 0, SEEK_SET); } else { m = i; if (!(s = (char*)sfreserve(pz->io, m, 0))) { if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix byte%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s"); return -1; } } sfputu(op, m); sfwrite(op, s, m); } pz->flags |= PZ_HEAD; return pzpartwrite(pz, op); }
// // This routine reads the history file from the present position to the end-of-file and puts the // information in the in-core history table. Note that HIST_CMDNO is only recognized at the // beginning of a command and that HIST_UNDO as the first character of a command is skipped unless // it is followed by 0. If followed by 0 then it cancels the previous command. // void hist_eof(History_t *hp) { char *cp, *first, *endbuff; int n; int incmd = 0; int skip = 0; int oldind = hp->histind; off_t count = hp->histcnt; off_t last = sfseek(hp->histfp, 0, SEEK_END); if (last < count) { last = -1; count = 2 + HIST_MARKSZ; if ((hp->histind -= hp->histsize) < 0) hp->histind = 1; } again: sfseek(hp->histfp, count, SEEK_SET); while ((cp = (char *)sfreserve(hp->histfp, SF_UNBOUND, 0))) { n = sfvalue(hp->histfp); endbuff = cp + n; first = cp += skip; while (1) { while (!incmd) { if (cp > first) { count += (cp - first); n = hist_ind(hp, ++hp->histind); #if 0 // TODO: Figure out if this should be enabled. Originally excluded via `#ifdef future`. if (count == hp->histcmds[n]) { sfprintf(sfstderr, "count match n=%d\n", n); if (histinit) { histinit = 0; return; } } else if (n >= histinit) #endif hp->histcmds[n] = count; first = cp; } switch (*((unsigned char *)(cp++))) { case HIST_CMDNO: { if (*cp == 0) { hp->histmarker = count + 2; cp += (HIST_MARKSZ - 1); hp->histind--; if (!histinit && (cp <= endbuff)) { unsigned char *marker = (unsigned char *)(cp - 4); hp->histind = ((marker[0] << 16) | (marker[1] << 8) | (marker[2] - 1)); } } break; } case HIST_UNDO: { if (*cp == 0) { cp += 1; hp->histind -= 2; } break; } default: { cp--; incmd = 1; } } if (cp >= endbuff) { goto refill; } } first = cp; while (*cp) { if (++cp >= endbuff) goto refill; } incmd = 0; while (*cp == 0) { if (++cp >= endbuff) goto refill; } } refill: count += (cp - first); skip = (cp - endbuff); if (!incmd && !skip) hp->histcmds[hist_ind(hp, ++hp->histind)] = count; } hp->histcnt = count; if (incmd && last) { sfputc(hp->histfp, 0); hist_cancel(hp); count = 2; skip = 0; oldind -= hp->histind; hp->histind = hp->histind - hp->histsize + oldind + 2; if (hp->histind < 0) hp->histind = 1; if (last < 0) { char buff[HIST_MARKSZ]; int fd = open(hp->histname, O_RDWR | O_CLOEXEC); if (fd >= 0) { hist_marker(buff, hp->histind); write(fd, (char *)hist_stamp, 2); write(fd, buff, HIST_MARKSZ); sh_close(fd); } } last = 0; goto again; } }
static void cutfields(Cut_t* cut, Sfio_t* fdin, Sfio_t* fdout) { register unsigned char *sp = cut->space; register unsigned char *cp; register unsigned char *wp; register int c, nfields; register const int *lp = cut->list; register unsigned char *copy; register int nodelim, empty, inword=0; register unsigned char *ep; unsigned char *bp, *first; int lastchar; wchar_t w; Sfio_t *fdtmp = 0; long offset = 0; unsigned char mb[8]; /* process each buffer */ while ((bp = (unsigned char*)sfreserve(fdin, SF_UNBOUND, -1)) && (c = sfvalue(fdin)) > 0) { cp = bp; ep = cp + --c; if((lastchar = cp[c]) != cut->eob) *ep = cut->eob; /* process each line in the buffer */ while (cp <= ep) { first = cp; if (!inword) { nodelim = empty = 1; copy = cp; if (nfields = *(lp = cut->list)) copy = 0; else nfields = *++lp; } else if (copy) copy = cp; inword = 0; do { /* skip over non-delimiter characters */ if (cut->mb) for (;;) { switch (c = sp[*(unsigned char*)cp++]) { case 0: continue; case SP_WIDE: wp = --cp; while ((c = mb2wc(w, cp, ep - cp)) <= 0) { /* mb char possibly spanning buffer boundary -- fun stuff */ if ((ep - cp) < mbmax()) { int i; int j; int k; if (lastchar != cut->eob) { *ep = lastchar; if ((c = mb2wc(w, cp, ep - cp)) > 0) break; } if (copy) { empty = 0; if ((c = cp - copy) > 0 && sfwrite(fdout, (char*)copy, c) < 0) goto failed; } for (i = 0; i <= (ep - cp); i++) mb[i] = cp[i]; if (!(bp = (unsigned char*)sfreserve(fdin, SF_UNBOUND, -1)) || (c = sfvalue(fdin)) <= 0) goto failed; cp = bp; ep = cp + --c; if ((lastchar = cp[c]) != cut->eob) *ep = cut->eob; j = i; k = 0; while (j < mbmax()) mb[j++] = cp[k++]; if ((c = mb2wc(w, (char*)mb, j)) <= 0) { c = i; w = 0; } first = bp = cp += c - i; if (copy) { copy = bp; if (w == cut->ldelim.chr) lastchar = cut->ldelim.chr; else if (w != cut->wdelim.chr) { empty = 0; if (sfwrite(fdout, (char*)mb, c) < 0) goto failed; } } c = 0; } else { w = *cp; c = 1; } break; } cp += c; c = w; if (c == cut->wdelim.chr) { c = SP_WORD; break; } if (c == cut->ldelim.chr) { c = SP_LINE; break; } continue; default: wp = cp - 1; break; } break; } else { while (!(c = sp[*cp++])); wp = cp - 1; } /* check for end-of-line */ if (c == SP_LINE) { if (cp <= ep) break; if (lastchar == cut->ldelim.chr) break; /* restore cut->last character */ if (lastchar != cut->eob) *ep = lastchar; inword++; if (!sp[lastchar]) break; } nodelim = 0; if (--nfields > 0) continue; nfields = *++lp; if (copy) { empty = 0; if ((c = wp - copy) > 0 && sfwrite(fdout, (char*)copy, c) < 0) goto failed; copy = 0; } else /* set to delimiter unless the first field */ copy = empty ? cp : wp; } while (!inword); if (!inword) { if (!copy) { if (nodelim) { if (!cut->sflag) { if (offset) { sfseek(fdtmp,(Sfoff_t)0,SEEK_SET); sfmove(fdtmp,fdout,offset,-1); } copy = first; } } else sfputc(fdout,'\n'); } if (offset) sfseek(fdtmp,offset=0,SEEK_SET); } if (copy && (c=cp-copy)>0 && (!nodelim || !cut->sflag) && sfwrite(fdout,(char*)copy,c)< 0) goto failed; } /* see whether to save in tmp file */ if(inword && nodelim && !cut->sflag && (c=cp-first)>0) { /* copy line to tmpfile in case no fields */ if(!fdtmp) fdtmp = sftmp(BLOCK); sfwrite(fdtmp,(char*)first,c); offset +=c; } } failed: if(fdtmp) sfclose(fdtmp); }
void ed_setup(register Edit_t *ep, int fd, int reedit) { Shell_t *shp = ep->sh; register char *pp; register char *last, *prev; char *ppmax; int myquote = 0, n; register int qlen = 1, qwid; char inquote = 0; ep->e_fd = fd; ep->e_multiline = sh_isoption(SH_MULTILINE)!=0; #ifdef SIGWINCH if(!(shp->sigflag[SIGWINCH]&SH_SIGFAULT)) { signal(SIGWINCH,sh_fault); shp->sigflag[SIGWINCH] |= SH_SIGFAULT; } pp = shp->st.trapcom[SIGWINCH]; shp->st.trapcom[SIGWINCH] = 0; sh_fault(SIGWINCH); shp->st.trapcom[SIGWINCH] = pp; ep->sh->winch = 0; #endif #if SHOPT_EDPREDICT ep->hlist = 0; ep->nhlist = 0; ep->hoff = 0; #endif /* SHOPT_EDPREDICT */ #if KSHELL ep->e_stkptr = stakptr(0); ep->e_stkoff = staktell(); if(!(last = shp->prompt)) last = ""; shp->prompt = 0; #else last = ep->e_prbuff; #endif /* KSHELL */ if(shp->gd->hist_ptr) { register History_t *hp = shp->gd->hist_ptr; ep->e_hismax = hist_max(hp); ep->e_hismin = hist_min(hp); } else { ep->e_hismax = ep->e_hismin = ep->e_hloff = 0; } ep->e_hline = ep->e_hismax; if(!sh_isoption(SH_VI) && !sh_isoption(SH_EMACS) && !sh_isoption(SH_GMACS)) ep->e_wsize = MAXLINE; else ep->e_wsize = ed_window()-2; ep->e_winsz = ep->e_wsize+2; ep->e_crlf = 1; ep->e_plen = 0; pp = ep->e_prompt; ppmax = pp+PRSIZE-1; *pp++ = '\r'; { register int c; while(prev = last, c = mbchar(last)) switch(c) { case ESC: { int skip=0; ep->e_crlf = 0; *pp++ = c; for(n=1; c = *last++; n++) { if(pp < ppmax) *pp++ = c; if(c=='\a' || c==ESC || c=='\r') break; if(skip || (c>='0' && c<='9')) { skip = 0; continue; } if(n>1 && c==';') skip = 1; else if(n>2 || (c!= '[' && c!= ']')) break; } if(c==0 || c==ESC || c=='\r') last--; qlen += (n+1); break; } case '\b': if(pp>ep->e_prompt+1) pp--; break; case '\r': if(pp == (ep->e_prompt+2)) /* quote char */ myquote = *(pp-1); /*FALLTHROUGH*/ case '\n': /* start again */ ep->e_crlf = 1; qlen = 1; inquote = 0; pp = ep->e_prompt+1; break; case '\t': /* expand tabs */ while((pp-ep->e_prompt)%TABSIZE) { if(pp >= ppmax) break; *pp++ = ' '; } break; case '\a': /* cut out bells */ break; default: if(c==myquote) { qlen += inquote; inquote ^= 1; } if(pp < ppmax) { if(inquote) qlen++; else if(!is_print(c)) ep->e_crlf = 0; if((qwid = last - prev) > 1) qlen += qwid - mbwidth(c); while(prev < last && pp < ppmax) *pp++ = *prev++; } break; } } if(pp-ep->e_prompt > qlen) ep->e_plen = pp - ep->e_prompt - qlen; *pp = 0; if(!ep->e_multiline && (ep->e_wsize -= ep->e_plen) < 7) { register int shift = 7-ep->e_wsize; ep->e_wsize = 7; pp = ep->e_prompt+1; strcpy(pp,pp+shift); ep->e_plen -= shift; last[-ep->e_plen-2] = '\r'; } sfsync(sfstderr); if(fd == sffileno(sfstderr)) { /* can't use output buffer when reading from stderr */ static char *buff; if(!buff) buff = (char*)malloc(MAXLINE); ep->e_outbase = ep->e_outptr = buff; ep->e_outlast = ep->e_outptr + MAXLINE; return; } qlen = sfset(sfstderr,SF_READ,0); /* make sure SF_READ not on */ ep->e_outbase = ep->e_outptr = (char*)sfreserve(sfstderr,SF_UNBOUND,SF_LOCKR); ep->e_outlast = ep->e_outptr + sfvalue(sfstderr); if(qlen) sfset(sfstderr,SF_READ,1); sfwrite(sfstderr,ep->e_outptr,0); ep->e_eol = reedit; if(ep->e_multiline) { #ifdef _cmd_tput char *term; if(!ep->e_term) ep->e_term = nv_search("TERM",shp->var_tree,0); if(ep->e_term && (term=nv_getval(ep->e_term)) && strlen(term)<sizeof(ep->e_termname) && strcmp(term,ep->e_termname)) { sh_trap(".sh.subscript=$(tput cuu1 2>/dev/null)",0); if(pp=nv_getval(SH_SUBSCRNOD)) strncpy(CURSOR_UP,pp,sizeof(CURSOR_UP)-1); nv_unset(SH_SUBSCRNOD); strcpy(ep->e_termname,term); } #endif ep->e_wsize = MAXLINE - (ep->e_plen+1); } if(ep->e_default && (pp = nv_getval(ep->e_default))) { n = strlen(pp); if(n > LOOKAHEAD) n = LOOKAHEAD; ep->e_lookahead = n; while(n-- > 0) ep->e_lbuf[n] = *pp++; ep->e_default = 0; } }
int pzfile(Pz_t* pz) { unsigned char* s; int i; int j; size_t n; /* * 0 or more nul's mean clean EOF */ while (!(i = sfgetc(pz->io))); if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, -1, "%s: pzfile: i=%02x", pz->path, i); if (i == -1) return 0; if (i == PZ_MARK_TAIL) { /* * file trailer */ while ((n = sfgetu(pz->io)) && !sferror(pz->io) && !sfeof(pz->io) && (s = (unsigned char*)sfreserve(pz->io, n, 0))) if (pz->disc->eventf && (*pz->disc->eventf)(pz, PZ_TAILREAD, s, n, pz->disc) < 0) return -1; if ((i = sfgetc(pz->io)) == -1) return 0; } j = sfgetc(pz->io); if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, -1, "%s: pzfile: i=%02x j=%02x", pz->path, i, j); if (i == PZ_MAGIC_1 && j == PZ_MAGIC_2) { /* * next file header */ sfungetc(pz->io, j); sfungetc(pz->io, i); return pzopen(pz->disc, (char*)pz, PZ_AGAIN) ? 1 : -1; } if (pz->disc->errorf) (*pz->disc->errorf)(pz, pz->disc, 2, "%s: data corrupted", pz->path); return -1; }