MAIN() { char *s = "1234567890\n"; Sfoff_t n, i; Sfio_t *f; char buf[1024]; char* addr; if(sfopen(sfstdout,tstfile(0),"w+") != sfstdout) terror("Opening output file\n"); for(i = 0; i < 10000; ++i) if(sfputr(sfstdout,s,-1) < 0) terror("Writing data\n"); if(!(f = sfopen((Sfio_t*)0,tstfile(1),"w"))) terror("Opening output file \n"); sfseek(sfstdout,(Sfoff_t)0,0); if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i) terror("Move %d lines, Expect %d\n",n,i); sfseek(sfstdout,(Sfoff_t)0,0); sfseek(f,(Sfoff_t)0,0); sfsetbuf(sfstdout,buf,sizeof(buf)); if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i) terror("Move %d lines, Expect %d\n",n,i); sfopen(sfstdin,tstfile(0),"r"); sfopen(sfstdout,tstfile(1),"w"); sfmove(sfstdin,sfstdout,(Sfoff_t)SF_UNBOUND,-1); if(!sfeof(sfstdin)) terror("Sfstdin is not eof\n"); if(sferror(sfstdin)) terror("Sfstdin is in error\n"); if(sferror(sfstdout)) terror("Sfstdout is in error\n"); sfseek(sfstdin,(Sfoff_t)0,0); sfseek(sfstdout,(Sfoff_t)0,0); sfsetbuf(sfstdin,buf,sizeof(buf)); addr = (char*)sbrk(0); sfmove(sfstdin,sfstdout,(Sfoff_t)((unsigned long)(~0L)>>1),-1); if((ssize_t)((char*)sbrk(0)-addr) > 256*1024) terror("Too much space allocated in sfmove\n"); if(!sfeof(sfstdin)) terror("Sfstdin is not eof2\n"); if(sferror(sfstdin)) terror("Sfstdin is in error2\n"); if(sferror(sfstdout)) terror("Sfstdout is in error2\n"); TSTEXIT(0); }
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); }
// // 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() { char buf[100]; Sfio_t *fp; int i; char *s; if(!(fp = sftmp(8))) terror("Can't open temp file"); sfset(fp,SF_LINE,1); for(i = 0; i < 1000; ++i) { sfsprintf(buf,sizeof(buf),"Number: %d",i); if(sfputr(fp,buf,'\n') <= 0) terror("Writing %s",buf); } sfseek(fp,(Sfoff_t)0,0); for(i = 0; i < 1000; ++i) { sfsprintf(buf,sizeof(buf),"Number: %d",i); if(!(s = sfgetr(fp,'\n',1))) terror("Reading %s",buf); if(strcmp(s,buf) != 0) terror("Input=%s, Expect=%s",s,buf); } sfseek(fp,(Sfoff_t)0,0); s = sfgetr(fp,'\0',1); if(s) terror("Expecting a null string"); s = sfgetr(fp,'\0',-1); if(!s) terror("Expecting a non-null string"); if(sfvalue(fp) != sfsize(fp)) terror("Wrong size"); sfclose(fp); if(!(fp = sfnew(0, buf, 12, 1, SF_WRITE)) ) terror("Opening a test stream"); sfsetbuf(fp, buf, 12); sfset(fp, SF_LINE, 0); sfdisc(fp, &Disc); if(sfputr(fp, "0123456789", '\n') != 11) terror("Sfputr failed1"); if(sfputr(fp, "0", -1) != 1) terror("Sfputr failed2"); if(sfputr(fp, "1", -1) != 1) terror("Sfputr failed3"); texit(0); }
main() { Sfio_t* null; Sfio_t* f; char buf[256*1024], b[256*1024]; int k, n; if(!(null = sfopen(NIL(Sfio_t*),"/dev/null","w")) ) terror("Opening /dev/null"); sfsetbuf(null,NIL(char*),(size_t)SF_UNBOUND); if(!SFISNULL(null) ) terror("Not /dev/null?"); if(!(f = sfopen(NIL(Sfio_t*), Kpv[0], "w+")) ) terror("Creating %s", Kpv[0]); sfwrite(f,"1234",4); sfseek(f,(Sfoff_t)1,0); sfsync(f); sfsetfd(null,-1); sfsetfd(null,sffileno(f)); sfsync(null); sfseek(f,(Sfoff_t)0,0); if(sfread(f,buf,4) != 4 || strncmp(buf,"1234",4) != 0) terror("Bad data"); for(k = 0; k < sizeof(buf); ++k) buf[k] = 1; for(k = sizeof(buf)/4; k < sizeof(buf)/2; ++k) /* make a big hole */ buf[k] = 0; if(!(f = sfopen(f, Kpv[0], "w+")) ) terror("Creating %s", Kpv[0]); n = sizeof(buf)-127; if(sfwrite(f,buf,n) != n) terror("Writing large buffer"); sfseek(f,(Sfoff_t)0,0); if(sfread(f,b,n) != n) terror("Reading large buffer"); for(k = 0; k < n; ++k) if(b[k] != buf[k]) terror("Bad data"); rmkpv(); return 0; }
static off_t lineput(char* s) { off_t off; modify(); off = ed.tmpoff; if (sfseek(ed.tmp, off, SEEK_SET) != off) error(ERROR_SYSTEM|2, "temp file write seek error"); if (sfputr(ed.tmp, s, 0) < 0) error(ERROR_SYSTEM|2, "temp file write error at offset %I*d", sizeof(off), off); if ((ed.tmpoff = sfseek(ed.tmp, (off_t)0, SEEK_CUR)) == (off_t)-1) error(ERROR_SYSTEM|2, "temp file tell error"); return off; }
tmain() { int i; char wbuf[1023]; char rbuf[1023]; Sfio_t *fp; for(i = 0; i < sizeof(wbuf); ++i) wbuf[i] = (i%26)+'a'; wbuf[sizeof(wbuf)-1] = '\0'; if(!(fp = sftmp(0))) terror("Opening temp file"); for(i = 0; i < 256; ++i) if(sfwrite(fp,wbuf,sizeof(wbuf)) != sizeof(wbuf)) terror("Writing"); sfseek(fp,(Sfoff_t)0,0); sfset(fp,SF_WRITE,0); sfsetbuf(fp,NIL(char*),0); sfsetbuf(fp,NIL(char*),(size_t)SF_UNBOUND); for(i = 0; i < 256; ++i) { if(sfread(fp,rbuf,sizeof(rbuf)) != sizeof(rbuf)) terror("Reading"); if(strcmp(rbuf,wbuf) != 0) terror("Unmatched record"); } texit(0); }
tmain() { Sfio_t* f1; Sfio_t* f2; char* s; if(!(f1 = sfopen(NIL(Sfio_t*), tstfile("sf", 0),"w+")) ) terror("Can't open file"); if(sfwrite(f1,"0123456789\n",11) != 11) terror("Can't write to file"); sfclose(sfstdin); if(sfswap(f1,sfstdin) != sfstdin) terror("Can't swap with sfstdin"); sfseek(sfstdin,(Sfoff_t)0,0); if(!(s = sfgetr(sfstdin,'\n',1)) ) terror("sfgetr failed"); if(strcmp(s,"0123456789") != 0) terror("Get wrong data"); if(!(f1 = sfswap(sfstdin,NIL(Sfio_t*))) ) terror("Failed swapping to NULL"); if(!sfstack(sfstdout,f1) ) terror("Failed stacking f1"); if(!(f2 = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r")) ) terror("Can't open for read"); if(sfswap(f1,f2) != NIL(Sfio_t*) ) terror("sfswap should have failed"); texit(0); }
// // Copy command <command> from history file to s1. At most <size> characters copied. If s1==0 the // number of lines for the command is returned. Set line=linenumber for emacs copy and only this // line of command will be copied. Set line < 0 for full command copy. // // Return -1 if there is no history file. // int hist_copy(char *s1, int size, int command, int line) { int c; History_t *hp = shgd->hist_ptr; int count = 0; char *s1max = s1 + size; if (!hp) return -1; hist_seek(hp, command); while ((c = sfgetc(hp->histfp)) && c != EOF) { if (c == '\n') { if (count++ == line) { break; } else if (line >= 0) { continue; } } if (s1 && (line < 0 || line == count)) { if (s1 >= s1max) { *--s1 = 0; break; } *s1++ = c; } } sfseek(hp->histfp, (off_t)0, SEEK_END); if (s1 == 0) return count; if (count && (c = *(s1 - 1)) == '\n') s1--; *s1 = '\0'; return count; }
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); }
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f1, *f2; char *s; Sfoff_t p; char buf[1024]; int r, w; if (!(f1 = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Can't open f1"); if (!(f1 = sfopen(f1, tstfile("sf", 0), "a+"))) terror("Can't open f1"); if (!(f2 = sfopen(NULL, tstfile("sf", 0), "a+"))) terror("Can't open f2"); if (sfwrite(f1, "012345678\n", 10) != 10 || sfsync(f1) < 0) terror("Writing to f1"); if ((p = sftell(f1)) != 10) terror("Bad sftell1 %ld", p); if (sfwrite(f2, "abcdefghi\n", 10) != 10 || sfsync(f2) < 0) terror("Writing to f2"); if ((p = sftell(f2)) != 20) terror("Bad sftell2"); if ((p = sfseek(f1, (Sfoff_t)0, 0)) != 0) terror("Bad seek"); if (!(s = sfgetr(f1, '\n', 1))) terror("Bad getr1"); if (strcmp(s, "012345678") != 0) terror("Bad input1"); if ((p = sftell(f1)) != 10) terror("Bad sftell3"); if (sfwrite(f1, "012345678\n", 10) != 10 || sfsync(f1) < 0) terror("Writing to f1"); if ((p = sftell(f1)) != 30) terror("Bad sftell4"); if ((p = sfseek(f2, (Sfoff_t)10, 0)) != 10) terror("Bad seek"); if (!(s = sfgetr(f2, '\n', 1))) terror("Bad getr2"); if (strcmp(s, "abcdefghi") != 0) terror("Bad input2"); if (!(s = sfgetr(f2, '\n', 1))) terror("Bad getr3"); if (strcmp(s, "012345678") != 0) terror("Bad input3"); if (!(f1 = sfopen(f1, tstfile("sf", 0), "w"))) terror("Can't open file to write"); for (r = 0; r < 1024; ++r) buf[r] = 'a'; if ((w = sfwrite(f1, buf, 1024)) != 1024) terror("writing w=%d", w); if (!(f1 = sfopen(f1, tstfile("sf", 0), "a"))) terror("Can't open file to append"); sfseek(f1, (Sfoff_t)0, 0); if ((w = sfwrite(f1, buf, 64)) != 64) terror("writing w=%d", w); if ((r = (int)sftell(f1)) != (1024 + 64)) terror("seek position wrong s=%d", r); texit(0); }
int b_comm(int argc, char *argv[], Shbltin_t* context) { register int mode = C_FILE1|C_FILE2|C_COMMON; register char *cp; Sfio_t *f1, *f2; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case '1': mode &= ~C_FILE1; continue; case '2': mode &= ~C_FILE2; continue; case '3': mode &= ~C_COMMON; continue; case ':': error(2, "%s",opt_info.arg); break; case '?': error(ERROR_usage(2), "%s",opt_info.arg); break; } break; } argv += opt_info.index; argc -= opt_info.index; if(error_info.errors || argc!=2) error(ERROR_usage(2),"%s",optusage(NiL)); cp = *argv++; if(streq(cp,"-")) f1 = sfstdin; else if(!(f1 = sfopen(NiL, cp,"r"))) error(ERROR_system(1),"%s: cannot open",cp); cp = *argv; if(streq(cp,"-")) f2 = sfstdin; else if(!(f2 = sfopen(NiL, cp,"r"))) error(ERROR_system(1),"%s: cannot open",cp); if(mode) { if(comm(f1,f2,sfstdout,mode) < 0) error(ERROR_system(1)," write error"); } else if(f1==sfstdin || f2==sfstdin) sfseek(sfstdin,(Sfoff_t)0,SEEK_END); if(f1!=sfstdin) sfclose(f1); if(f2!=sfstdin) sfclose(f2); return error_info.errors; }
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 char* lineget(off_t off) { char* s; off &= ~(LINE_GLOBAL|LINE_MARKED); if (sfseek(ed.tmp, off, SEEK_SET) != off) error(ERROR_SYSTEM|2, "temp file read seek error"); if (!(s = sfgetr(ed.tmp, 0, 0))) error(ERROR_SYSTEM|2, "temp file read error at offset %I*d", sizeof(off), off); return s; }
MAIN() { char buf[100]; Sfio_t *fp; int i; char *s; if(!(fp = sftmp(8))) terror("Can't open temp file\n"); sfset(fp,SF_LINE,1); for(i = 0; i < 1000; ++i) { sfsprintf(buf,sizeof(buf),"Number: %d",i); if(sfputr(fp,buf,'\n') <= 0) terror("Writing %s\n",buf); } sfseek(fp,(Sfoff_t)0,0); for(i = 0; i < 1000; ++i) { sfsprintf(buf,sizeof(buf),"Number: %d",i); if(!(s = sfgetr(fp,'\n',1))) terror("Reading %s\n",buf); if(strcmp(s,buf) != 0) terror("Input=%s, Expect=%s\n",s,buf); } sfseek(fp,(Sfoff_t)0,0); s = sfgetr(fp,'\0',1); if(s) terror("Expecting a null string\n"); s = sfgetr(fp,'\0',-1); if(!s) terror("Expecting a non-null string\n"); if(sfvalue(fp) != sfsize(fp)) terror("Wrong size\n"); TSTEXIT(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; }
// Output `here` documents. static_fn void here_body(const struct ionod *iop) { Sfio_t *infile; #if 0 // TODO: Figure out if this should be enabled. Originally excluded via `#ifdef xxx`. if (iop->iolink) here_body((struct inode *)iop->iolink); iop->iolink = 0; #endif if (iop->iofile & IOSTRG) { infile = sfnew(NULL, iop->ioname, iop->iosize, -1, SF_STRING | SF_READ); } else { sfseek(infile = sh.heredocs, iop->iooffset, SEEK_SET); } sfmove(infile, outfile, iop->iosize, -1); if (iop->iofile & IOSTRG) sfclose(infile); sfputr(outfile, iop->iodelim, '\n'); }
/* * output here documents */ static void here_body(register const struct ionod *iop) { Sfio_t *infile; #ifdef xxx if(iop->iolink) here_body((struct inode*)iop->iolink); iop->iolink = 0; #endif if(iop->iofile&IOSTRG) infile = sfnew((Sfio_t*)0,iop->ioname,iop->iosize,-1,SF_STRING|SF_READ); else sfseek(infile=sh.heredocs,iop->iooffset,SEEK_SET); sfmove(infile,outfile,iop->iosize,-1); if(iop->iofile&IOSTRG) sfclose(infile); sfputr(outfile,iop->iodelim,'\n'); }
tmain() { UNUSED(argc); UNUSED(argv); char buf[1024]; sfsetbuf(sfstdout, buf, sizeof(buf)); sfset(sfstdout, SF_LINE, 0); if (sfdisc(sfstdout, &seekable) != &seekable) terror("Can't set discipline"); if (sfseek(sfstdout, (Sfoff_t)0, 0) < 0) terror("Sfstdout should be seekable"); if (sfwrite(sfstdout, "123\n", 4) != 4) terror("Can't write"); if (sfwrite(sfstdout, "123\n", 4) != 4) terror("Can't write"); if (sfdisc(sfstdout, NULL) != &seekable) terror("Can't pop discipline"); if (buffer != buf || size != 8 || count != 1) terror("Wrong calls to write"); texit(0); }
tmain() { Sfio_t *f; int i, c; if(!(f = sftmp(8))) terror("Can't open temp file"); for(i = 0; i < 10000; ++i) if(sfputc(f,(i%26)+'a') < 0) terror("Writing %c",(i%26)+'a'); sfseek(f,(Sfoff_t)0,0); for(i = 0; i < 10000; ++i) if((c = sfgetc(f)) != ((i%26)+'a')) terror("Input=%#o, Expect=%c",c,(i%26)+'a'); texit(0); }
static char *walk_class(register Namval_t *np, int dlete, struct dcclass *dcp) { static Sfio_t *out; Sfio_t *outfile; int savtop = stktell(stkstd); char *savptr = stkfreeze(stkstd,0); if(dlete) outfile = 0; else if(!(outfile=out)) outfile = out = sfnew((Sfio_t*)0,(char*)0,-1,-1,SF_WRITE|SF_STRING); else sfseek(outfile,0L,SEEK_SET); genvalue(outfile,&dcp->sclass,0,np); stkset(stkstd,savptr,savtop); if(!outfile) return((char*)0); sfputc(out,0); return((char*)out->_data); }
MAIN() { unsigned int i, r; Sfio_t *fp; if(!(fp = sftmp(8))) terror("Can't open temp file\n"); for(i = 10000; i <= 100000; i += 9) if(sfputu(fp,i) < 0) terror("Writing %u\n",i); sfseek(fp,(Sfoff_t)0,0); for(i = 10000; i <= 100000; i += 9) if((r = (unsigned int)sfgetu(fp)) != i) terror("Input=%u, Expect=%u\n",r,i); TSTEXIT(0); }
tmain() { int i, r; Sfio_t *fp; if(!(fp = sftmp(8))) terror("Can't open temp file"); for(i = -5448; i <= 5448; i += 101) if(sfputl(fp,(long)i) < 0) terror("Writing %d",i); sfseek(fp,(Sfoff_t)0,0); for(i = -5448; i <= 5448; i += 101) if((r = (int)sfgetl(fp)) != i) terror("Input=%d, Expect=%d",r,i); texit(0); }
tmain() { UNUSED(argc); UNUSED(argv); unsigned int i, r; Sfio_t *fp; if (!(fp = sftmp(8))) terror("Can't open temp file"); for (i = 10000; i <= 100000; i += 9) { if (sfputu(fp, i) < 0) terror("Writing %u", i); } sfseek(fp, (Sfoff_t)0, 0); for (i = 10000; i <= 100000; i += 9) { if ((r = (unsigned int)sfgetu(fp)) != i) terror("Input=%u, Expect=%u", r, i); } texit(0); }
main() { double f, v; int i; Sfio_t *fp; if(!(fp = sfopen(NIL(Sfio_t*), Kpv[0], "w+")) ) terror("Can't open temp file\n"); for(f = 1e-10; f < 1e-10 + 1.; f += .001) if(sfputd(fp,f) < 0) terror("Writing %f\n",f); sfseek(fp,(Sfoff_t)0,0); for(f = 1e-10, i = 0; f < 1e-10 + 1.; f += .001, ++i) if((v = sfgetd(fp)) != f) terror("Element=%d Input=%f, Expect=%f\n",i,v,f); rmkpv(); return 0; }
void hist_list(History_t *hp, Sfio_t *outfile, off_t offset, int last, const char *nl) { int oldc = 0; int c; if (offset < 0 || !hp) { sfputr(outfile, sh_translate(e_unknown), '\n'); return; } sfseek(hp->histfp, offset, SEEK_SET); while ((c = sfgetc(hp->histfp)) != EOF) { if (c && oldc == '\n') { sfputr(outfile, nl, -1); } else if (last && (c == 0 || (c == '\n' && oldc == last))) { return; } else if (oldc) { sfputc(outfile, oldc); } oldc = c; if (c == 0) return; } return; }
MAIN() { Sfio_t *f; int fd; off_t sk; if(!(f = sfopen((Sfio_t*)0,tstfile(0),"w+"))) terror("Opening file\n"); fd = sffileno(f); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd1\n"); if(sfputc(f,'a') >= 0) terror("sfputc\n"); if(sfsetfd(f,fd) != fd) terror("setfd2\n"); if(sfwrite(f,"123456789\n",10) != 10) terror("sfwrite\n"); sfseek(f,(Sfoff_t)0,0); if(sfgetc(f) != '1') terror("sfgetc1\n"); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd2\n"); if((sk = lseek(fd, (off_t)0, 1)) != (off_t)1) terror("Bad seek address %lld\n", (Sfoff_t)sk ); if(sfgetc(f) >= 0) terror("sfgetc2\n"); if(sfsetfd(f,fd) != fd) terror("setfd2\n"); if(sfgetc(f) != '2') terror("sfgetc3\n"); TSTEXIT(0); }
tmain() { Sfio_t *f; int fd; off_t sk; if(!(f = sfopen((Sfio_t*)0,tstfile("sf", 0),"w+"))) terror("Opening file"); fd = sffileno(f); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd1"); if(sfputc(f,'a') >= 0) terror("sfputc"); if(sfsetfd(f,fd) != fd) terror("setfd2"); if(sfwrite(f,"123456789\n",10) != 10) terror("sfwrite"); sfseek(f,(Sfoff_t)0,0); if(sfgetc(f) != '1') terror("sfgetc1"); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd2"); if((sk = lseek(fd, (off_t)0, 1)) != (off_t)1) terror("Bad seek address %lld", (Sfoff_t)sk ); if(sfgetc(f) >= 0) terror("sfgetc2"); if(sfsetfd(f,fd) != fd) terror("setfd2"); if(sfgetc(f) != '2') terror("sfgetc3"); texit(0); }
// // Search for <string> in history file starting at location <offset>. If coffset==0 then line must // begin with string. // // Returns the line number of the match if successful, otherwise -1. // int hist_match(History_t *hp, off_t offset, char *string, int *coffset) { char *first, *cp; int m, n, c = 1, line = 0; sfseek(hp->histfp, offset, SEEK_SET); cp = first = sfgetr(hp->histfp, 0, 0); if (!cp) return -1; m = sfvalue(hp->histfp); n = (int)strlen(string); while (m > n) { if (*cp == *string && strncmp(cp, string, n) == 0) { if (coffset) *coffset = (cp - first); return line; } if (!coffset) break; if (*cp == '\n') line++; c = mblen(cp, MB_CUR_MAX); if (c < 0) c = 1; cp += c; m -= c; } return -1; }
static struct ionod *r_redirect(Shell_t* shp) { register long l; register struct ionod *iop=0, *iopold, *ioptop=0; while((l=sfgetl(infile))>=0) { iop = (struct ionod*)getnode(shp->stk,ionod); if(!ioptop) ioptop = iop; else iopold->ionxt = iop; iop->iofile = l; iop->ioname = r_string(shp->stk); if(iop->iodelim = r_string(shp->stk)) { iop->iosize = sfgetl(infile); if(shp->heredocs) iop->iooffset = sfseek(shp->heredocs,(off_t)0,SEEK_END); else { shp->heredocs = sftmp(512); iop->iooffset = 0; } sfmove(infile,shp->heredocs, iop->iosize, -1); } iopold = iop; if(iop->iofile&IOVNM) iop->iovname = r_string(shp->stk); else iop->iovname = 0; iop->iofile &= ~IOVNM; } if(iop) iop->ionxt = 0; return(ioptop); }