int main(int argc, char *argv[], void *extra) { register int n; register char *cp; char *delim = "#"; int width = 80; NoP(argc); NoP(extra); error_info.id = "banner"; while (n = optget(argv, usage)) switch (n) { case 'd': delim = opt_info.arg; break; case 'w': width = opt_info.num; break; case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } argv += opt_info.index; if(error_info.errors || !*argv) error(ERROR_usage(2), "%s", optusage((char*)0)); sfset(sfstdout,SF_LINE,0); while(cp = *argv++) banner(cp,delim,width); exit(0); }
MAIN() { Sfio_t* f; Sfio_t sf; if(sfopen(sfstdout,"abc","s") != sfstdout) terror("Bad reopening of sfstdout\n"); if(sfopen(sfstdin,"123","s") != sfstdin) terror("Bad reopening of sfstdin\n"); sfclose(sfstdin); if(!(f = sfopen(NIL(Sfio_t*),"123","s")) ) terror("Opening a stream\n"); sfclose(f); if(sfopen(f,"123","s") != NIL(Sfio_t*)) terror("can't reopen a closed stream!\n"); if(sfnew(&sf,NIL(char*),(size_t)SF_UNBOUND,0,SF_EOF|SF_READ) != &sf) terror("Did not open sf\n"); sfset(&sf,SF_STATIC,1); if(sfclose(&sf) < 0 || !(sfset(&sf,0,0)&SF_STATIC)) terror("Did not close sf\n"); /* test for exclusive opens */ unlink(tstfile(0)); if(!(f = sfopen(NIL(Sfio_t*),tstfile(0),"wx") ) ) terror("sfopen failed\n"); if((f = sfopen(f,tstfile(0),"wx") ) ) terror("sfopen should not succeed here\n"); TSTEXIT(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); }
tmain() { Sfio_t* f; Sfio_t sf; if(sfopen(sfstdout,"abc","s") != sfstdout) terror("Bad reopening of sfstdout"); if(sfopen(sfstdin,"123","s") != sfstdin) terror("Bad reopening of sfstdin"); sfclose(sfstdin); if(!(f = sfopen(NIL(Sfio_t*),"123","s")) ) terror("Opening a stream"); sfclose(f); if(sfopen(f,"123","s") != NIL(Sfio_t*)) terror("can't reopen a closed stream!"); if(sfnew(&sf,NIL(char*),(size_t)SF_UNBOUND,0,SF_EOF|SF_READ) != &sf) terror("Did not open sf"); sfset(&sf,SF_STATIC,1); if(!sfclose(&sf) || errno != EBADF) terror("sfclose(sf) should fail with EBADF"); if(!(sfset(&sf,0,0)&SF_STATIC)) terror("Did not close sf"); /* test for exclusive opens */ unlink(tstfile("sf", 0)); if(!(f = sfopen(NIL(Sfio_t*),tstfile("sf", 0),"wx") ) ) terror("sfopen failed"); if((f = sfopen(f,tstfile("sf", 0),"wx") ) ) terror("sfopen should not succeed here"); texit(0); }
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); }
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); }
static void tee_cleanup(register Tee_t* tp) { register int* hp; register int n; if (tp) { sfdisc(sfstdout, NiL); if (tp->line >= 0) sfset(sfstdout, SF_LINE, tp->line); for (hp = tp->fd; (n = *hp) >= 0; hp++) close(n); } }
void PerlIO_init(void) { /* Force this file to be included in perl binary. Which allows * this file to force inclusion of other functions that may be * required by loadable extensions e.g. for FileHandle::tmpfile */ /* Hack * sfio does its own 'autoflush' on stdout in common cases. * Flush results in a lot of lseek()s to regular files and * lot of small writes to pipes. */ sfset(sfstdout,SF_SHARE,0); }
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; 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); }
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); }
tmain() { UNUSED(argc); UNUSED(argv); char *s; char buf[1024]; int n; if (pipe(Fd) < 0) terror("Can't make pipe"); if (sfnew(sfstdin, NULL, (size_t)SF_UNBOUND, Fd[0], SF_READ) != sfstdin) { terror("Can't renew stdin"); } sfset(sfstdin, SF_SHARE, 1); if (sfpkrd(Fd[0], (void *)buf, 10, -1, 1000, 1) >= 0) terror("There isn't any data yet"); if ((n = sfpkrd(Fd[0], (void *)buf, sizeof(buf), -1, 0L, 0)) >= 0) { terror("Wrong data size %d, expecting < 0", n); } if (write(Fd[1], "abcd", 4) != 4) terror("Couldn't write to pipe"); if ((n = sfpkrd(Fd[0], (void *)buf, sizeof(buf), -1, 0L, 0)) != 4) { terror("Wrong data size %d, expecting 4", n); } signal(SIGALRM, alarmhandler); alarm(2); if (!(s = sfgetr(sfstdin, '\n', 1)) || strcmp(s, "01234") != 0) terror("Expecting 01234"); if (sfstdin->next < sfstdin->endb) terror("Sfgetr read too much"); if (!(s = sfgetr(sfstdin, '\n', 1)) || strcmp(s, "56789") != 0) terror("Expecting 56789"); texit(0); }
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; } }
tmain() { Sfio_t *f, *g, *str, *fr, *fw, *sf[2]; int c; char *s; int fd[2]; if(argc > 1) { while((s = sfgetr(sfstdin, '\n', 1)) ) { sfputr(sfstdout, s, '\n'); sfsync(sfstdout); } texit(0); } if(!(str = sfopen(NIL(Sfio_t*),"abc","s")) ) terror("Opening string stream"); if(pipe(fd) < 0) terror("pipe failed"); if(!(fr = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND, fd[0],SF_READ)) ) terror("Opening read pipe stream"); if(!(fw = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND, fd[1],SF_WRITE)) ) terror("Opening write pipe stream"); sf[0] = fr; sf[1] = str; if((c = sfpoll(sf,2,0)) != 1 || sf[0] != str) terror("Only str should be available c=%d",c); sf[0] = fr; if(sfpoll(sf,1,0) != 0 ) terror("Pipe stream should not be ready"); sfputc(fw,'a'); sfsync(fw); sf[0] = fr; if(sfpoll(sf,1,0) != 1 ) terror("Pipe read should be ready"); if((c = sfgetc(fr)) != 'a') terror("Didn't get back right data"); sf[0] = fr; sf[1] = str; if(sfpoll(sf,2,0) != 1 || sf[0] != str) terror("Only str should be available2"); sf[0] = fw; sf[1] = str; if(sfpoll(sf,2,0) != 2) terror("Both str&pipe write should be available"); if(pipe(fd) < 0) terror("Can't create pipe"); if(!(fr = sfnew(fr,NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ)) ) terror("Can't create stream"); if(write(fd[1],"0123456789",10) != 10) terror("Can't write to pipe"); if(sfpoll(&fr,1,1000) != 1) terror("Data should be available"); s = sfprints("%s 1", argv[0]); if(!(f = sfpopen(0, s, "w+")) ) terror("Can't create read/write process"); /* this write does not flush yet */ if(sfwrite(f, "abc\n",4) != 4) terror("Writing to pipe"); if(sfpoll(&f, 1, 0) != 1) terror("Poll should succeed"); if(sfvalue(f)&SF_READ) /* data has not been flushed to the child yet */ terror("Read should not be ready"); if(!(sfvalue(f)&SF_WRITE) ) terror("Write should be ready"); if(sfsync(f) < 0) /* now flush data to the child process */ terror("Bad sync"); if(sfpoll(&f, 1, 1000) != 1) terror("Poll should succeed2"); if(!(sfvalue(f)&SF_READ) ) /* the child should have read and rewritten */ terror("Read should be ready"); if(!(sfvalue(f)&SF_WRITE) ) terror("Write should be ready"); if(!(s = sfgetr(f,'\n',1)) || strcmp(s, "abc") != 0) terror("Bad read"); #if _lib_socketpair if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) terror("socketpair failed"); if(!(f = sfnew(0,NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ|SF_WRITE)) ) terror("Can't create stream with socket file descriptor"); if(!(g = sfnew(0,NIL(Void_t*),(size_t)SF_UNBOUND,fd[1],SF_READ|SF_WRITE)) ) terror("Can't create stream with socket file descriptor"); /* turn off write-capability for f */ sfset(f,SF_WRITE,0); sf[0] = f; sf[1] = g; if(sfpoll(sf,2,0) != 1) terror("Exactly one stream should be ready!"); if(sf[0] != g) terror("Stream g should be ready"); if(sfvalue(g)&SF_READ) terror("Read should not be ready for g"); if(!(sfvalue(g)&SF_WRITE) ) terror("Write should be ready for g"); if(sfwrite(g, "abc\n", 4) != 4 || sfsync(g) < 0) terror("Writing to g socket"); if(sfpoll(sf, 2, 0) != 2) terror("Poll should succeed with both streams"); if(!(sfvalue(f)&SF_READ) ) terror("Read should be ready for f"); if(sfgetc(f) != 'a' ) terror("sfgetc failed"); /* turn back on write-capability for f */ sfset(f,SF_WRITE,1); if(sfwrite(f,"def\n",4) != 4 || sfsync(f) < 0) terror("Writing to f socket"); if(sfpoll(sf, 2, 0) != 2) terror("Poll should succeed for both streams"); if(!sfvalue(f)&SF_READ) terror("Read should be ready for f"); if(!sfvalue(g)&SF_READ) terror("Read should be ready for g"); if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"bc") != 0) terror("f gets wrong data"); if(!(s = sfgetr(g,'\n',1)) || strcmp(s,"def") != 0) terror("g gets wrong data"); if(sfpoll(sf, 2, 0) != 2) terror("Poll should succeed for both streams"); if(sfvalue(f)&SF_READ) terror("Read should not be ready for f"); if(sfvalue(g)&SF_READ) terror("Read should not be ready for g"); #endif texit(0); }
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 b_tee(int argc, register char** argv, void* context) { register Tee_t* tp = 0; register int oflag = O_WRONLY|O_TRUNC|O_CREAT|O_BINARY; register int* hp; register char* cp; int line; if (argc <= 0) { if (context && (tp = (Tee_t*)sh_context(context)->data)) { sh_context(context)->data = 0; tee_cleanup(tp); } return 0; } cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_CALLBACK); line = -1; for (;;) { switch (optget(argv, usage)) { case 'a': oflag &= ~O_TRUNC; oflag |= O_APPEND; continue; case 'i': signal(SIGINT, SIG_IGN); continue; case 'l': line = sfset(sfstdout, 0, 0) & SF_LINE; if ((line == 0) == (opt_info.num == 0)) line = -1; else sfset(sfstdout, SF_LINE, !!opt_info.num); continue; case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } break; } if (error_info.errors) error(ERROR_usage(2), "%s", optusage(NiL)); argv += opt_info.index; argc -= opt_info.index; #if _ANCIENT_BSD_COMPATIBILITY if (*argv && streq(*argv, "-")) { signal(SIGINT, SIG_IGN); argv++; argc--; } #endif if (argc > 0) { if (tp = (Tee_t*)stakalloc(sizeof(Tee_t) + argc * sizeof(int))) { memset(&tp->disc, 0, sizeof(tp->disc)); tp->disc.writef = tee_write; if (context) sh_context(context)->data = (void*)tp; tp->line = line; hp = tp->fd; while (cp = *argv++) { if ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) error(ERROR_system(0), "%s: cannot create", cp); else hp++; } if (hp == tp->fd) tp = 0; else { *hp = -1; sfdisc(sfstdout, &tp->disc); } } else error(ERROR_exit(0), "out of space"); } if ((sfmove(sfstdin, sfstdout, SF_UNBOUND, -1) < 0 || !sfeof(sfstdin)) && errno != EPIPE) error(ERROR_system(0), "read error"); if (sfsync(sfstdout)) error(ERROR_system(0), "write error"); tee_cleanup(tp); return error_info.errors; }
MAIN() { int i, n, k; Sfoff_t o; char buf[1024], *s; char bigbuf[1024*8]; int fd[2]; Sfio_t* f; if(!(f = sfopen(0, tstfile(0), "w")) ) terror("Opening file to write"); if(sfwrite(f,"0123456789",10) != 10 || sfwrite(f,"abcdefgh",8) != 8) terror("Writing data"); if(!(f = sfopen(f, tstfile(0), "r")) ) terror("Opening file to read"); sfsetbuf(f, buf, sizeof(buf)); if(!(s = (char*)sfreserve(f,10,0)) ) terror("sfreserve failed"); if(strncmp(s,"0123456789",10) != 0) terror("Did not get correct data"); if((s = (char*)sfreserve(f,10,0)) ) terror("sfreserve should not have succeeded"); if(sfvalue(f) != 8) terror("sfreserve should have left the right unread record length"); if(!(s = (char*)sfreserve(f,4,0)) ) terror("sfreserve should return 4 bytes"); if(strncmp(s,"abcd",4) != 0) terror("Got wrong data"); if((s = (char*)sfreserve(f,10,0)) ) terror("sfreserve should not have succeeded2"); if(sfvalue(f) != 4) terror("sfreserve should have left 4 bytes length"); if(!(s = (char*)sfreserve(f,0,SF_LASTR)) ) terror("sfreserve should have returned last unread record"); if(strncmp(s,"efgh",4) != 0) terror("Record has wrong data"); sfclose(f); sfsetbuf(sfstdout,buf,sizeof(buf)); sfset(sfstdout,SF_SHARE|SF_PUBLIC,0); if((s = (char*)sfreserve(sfstdout,0,0)) != buf) terror("Wrong buffer\n"); if((n = sfwrite(sfstdout,"foobar",6)) != 6) terror("Write failed\n"); if((char*)sfreserve(sfstdout,0,0) != s+6) terror("Wrong reserved pointer\n"); sfpurge(sfstdout); if(sfopen(sfstdout, tstfile(0),"w") != sfstdout) terror("Opening file\n"); sfsetbuf(sfstdout,NIL(char*),0); if(!(s = sfreserve(sfstdout,0,1)) ) terror("Could not lock stdout\n"); if(sfputc(sfstdout,'1') >= 0) terror("stdout wasn't locked\n"); if(sfwrite(sfstdout,s,0) != 0) terror("stdout can't be unlocked\n"); sfsetbuf(sfstdout,NIL(char*),sizeof(buf)/2); for(i = 0; i < sizeof(buf); ++i) buf[i] = (i%26) + 'a'; n = 0; for(i = 0; i < 33; ++i) { if(!(s = sfreserve(sfstdout,sizeof(buf),1)) ) terror("Can't reserve write buffer\n"); memcpy(s,buf,sizeof(buf)); if(sfwrite(sfstdout,s,sizeof(buf)) != sizeof(buf) ) terror("Writing to file\n"); else n += sizeof(buf); } sfsync(sfstdout); if(sfopen(sfstdin, tstfile(0),"r") != sfstdin) terror("Opening file2\n"); sfsetbuf(sfstdin,NIL(char*),8*sizeof(buf)); if(sfsize(sfstdin) != n) terror("Wrong size for file\n"); i = 0; for(;;) { if(!(s = sfreserve(sfstdin,16*sizeof(buf),0)) ) break; else i += 16*sizeof(buf); } if(sfvalue(sfstdin) > 0) i += sfvalue(sfstdin); if(i != n) terror("Did not read data\n"); if(sfseek(sfstdin,(Sfoff_t)0,0) != 0) terror("sfseek failed0\n"); sfsetbuf(sfstdin,bigbuf,sizeof(bigbuf)); i = 0; for(;;) { if(!(s = sfreserve(sfstdin,16*sizeof(buf),0)) ) break; else i += 16*sizeof(buf); } if(sfvalue(sfstdin) > 0) i += sfvalue(sfstdin); if(i != n) terror("Did not read data2\n"); sfsetbuf(sfstdin,NIL(Void_t*),(size_t)SF_UNBOUND); if(sfopen(sfstdout, tstfile(0), "w") != sfstdout) terror("Can't open to write\n"); for(i = 0; i < 32; ++i) { for(k = 0; k < sizeof(bigbuf); ++k) bigbuf[k] = '0' + (k+i)%10; if(sfwrite(sfstdout,bigbuf,sizeof(bigbuf)) != sizeof(bigbuf)) terror("Writing to %s\n", tstfile(0)); } sfclose(sfstdout); if(sfopen(sfstdin, tstfile(0), "r") != sfstdin) terror("Opening to read\n"); sfsetbuf(sfstdin,NIL(Void_t*),8*1024); if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),0)) ) terror("sfreserve failed\n"); for(i = 0; i < 16; ++i) { for(k = 0; k < sizeof(bigbuf); ++k) if(*s++ != ('0' + (k+i)%10)) terror("Wrong data i=%d k=%d\n",i,k); } if((o = sfseek(sfstdin,-15*((Sfoff_t)sizeof(bigbuf)),1)) != sizeof(bigbuf)) terror("sfseek failed o=%lld\n", (Sflong_t)o); if(sfread(sfstdin,bigbuf,sizeof(bigbuf)) != sizeof(bigbuf) ) terror("sfread failed\n"); s = bigbuf; for(i = 1; i < 2; ++i) { for(k = 0; k < sizeof(bigbuf); ++k) if(*s++ != ('0' + (k+i)%10)) terror("Wrong data2 i=%d k=%d\n",i,k); } if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),1)) ) { sfsetbuf(sfstdin,NIL(Void_t*),16*sizeof(bigbuf)); if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),1)) ) terror("sfreserve failed2\n"); }
MAIN() { 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((Sfio_t*)0,tstfile(0),"w"))) terror("Opening to write\n"); if(sfputc(f,'a') != 'a') terror("sfputc\n"); if(sfgetc(f) >= 0) terror("sfgetc\n"); if(!(f = sfopen(f,tstfile(0),"r"))) terror("Opening to read\n"); if(sfgetc(f) != 'a') terror("sfgetc2\n"); if(sfputc(f,'b') >= 0) terror("sfputc2\n"); if(!(f = sfopen(f,tstfile(0),"r+"))) terror("Opening to read/write\n"); if(sfgetc(f) != 'a') terror("sfgetc3\n"); if(sfputc(f,'b') != 'b') terror("sfputc3\n"); if(sfclose(f) < 0) terror("sfclose\n"); if(!(f = sfpopen(NIL(Sfio_t*),sfprints("%s %s", argv[0], tstfile(0)),"r"))) terror("sfpopen\n"); if(sfgetc(f) != 'a') terror("sfgetc4\n"); if(sfgetc(f) != 'b') terror("sfgetc5\n"); if(sfgetc(f) >= 0) terror("sfgetc6\n"); if(!(f = sfopen(f,tstfile(0),"w")) ) terror("sfopen\n"); if(sfputc(f,'a') != 'a') terror("sfputc1\n"); sfsetfd(f,-1); if(sfputc(f,'b') >= 0) terror("sfputc2\n"); if(sfclose(f) < 0) terror("sfclose\n"); if(!(f = sfopen(NIL(Sfio_t*),tstfile(0),"a+")) ) terror("sfopen2\n"); sfset(f,SF_READ,0); if(!sfreserve(f,0,-1) ) terror("Failed on buffer getting\n"); if(sfvalue(f) <= 0) terror("There is no buffer?\n"); TSTEXIT(0); }
void copy(register Archive_t* ap, register int (*copyfile)(Ftw_t*)) { register char* s; register char* t; register char* v; register int c; unsigned long flags; char* mode; char* mtime; if (ap) { deltabase(ap); if (ap->delta && ap->delta->format != ap->expected && ap->expected) error(3, "%s: archive format %s does not match requested format %s", ap->name, ap->delta->format->name, ap->expected->name); if (state.append || state.update) { ap->format = ap->delta->format; if (!(ap->format->flags & APPEND)) error(3, "%s: archive format %s does support append/update", ap->name, ap->format->name); if (state.update) ap->update = ap->delta->tab; ap->delta = 0; ap->parent = 0; ap->volume--; } putprologue(ap, state.append || state.update); } if (state.files) ftwalk((char*)state.files, copyfile, state.ftwflags|FTW_MULTIPLE, state.exact ? (Ftw_cmp_t)0 : cmpftw); else { sfopen(sfstdin, NiL, "rt"); sfset(sfstdin, SF_SHARE, 0); mode = state.mode; mtime = state.mtime; for (;;) { if (s = state.peekfile) { state.peekfile = 0; c = state.peeklen; } else if (!(s = sfgetr(sfstdin, '\n', 1))) break; else c = sfvalue(sfstdin) - 1; sfwrite(state.tmp.lst, s, c); if (!(s = sfstruse(state.tmp.lst))) nospace(); flags = state.ftwflags; if (state.filter.line) { if (!(c = *s++)) continue; state.filter.options = s; if (!(s = skip(s, c))) continue; *s++ = 0; state.filter.command = s; if (!(s = skip(s, c))) continue; *s++ = 0; state.filter.path = s; if (!(s = skip(s, c))) state.filter.name = state.filter.path; else { *s++ = 0; state.filter.name = s; if (s = skip(s, c)) *s = 0; } s = state.filter.options; for (;;) { if (t = strchr(s, ',')) *t = 0; if (v = strchr(s, '=')) { *v++ = 0; c = strtol(v, NiL, 0); } else c = 1; if (s[0] == 'n' && s[1] == 'o') { s += 2; c = !c; } if (streq(s, "logical") || streq(s, "physical")) { if (s[0] == 'p') c = !c; if (c) flags &= ~(FTW_META|FTW_PHYSICAL); else { flags &= ~(FTW_META); flags |= FTW_PHYSICAL; } } else if (streq(s, "mode")) state.mode = v; else if (streq(s, "mtime")) state.mtime = v; if (!t) break; s = t + 1; } s = state.filter.path; state.filter.line = *state.filter.name ? 2 : 1; } c = *s ? ftwalk(s, copyfile, flags, NiL) : 0; state.mode = mode; state.mtime = mtime; if (c) { error(2, "%s: not completely copied", s); break; } } } if (ap) { deltadelete(ap); putepilogue(ap); } }
{ /* don't print error message for stdout for compatibility */ if(fd==1) return(1); errormsg(SH_DICT,ERROR_system(1),msg); } if(!(outfile=shp->sftable[fd])) { sh_onstate(shp,SH_NOTRACK); n = SF_WRITE|((n&IOREAD)?SF_READ:0); shp->sftable[fd] = outfile = sfnew(NIL(Sfio_t*),shp->outbuff,IOBSIZE,fd,n); sh_offstate(shp,SH_NOTRACK); sfpool(outfile,shp->outpool,SF_WRITE); } /* turn off share to guarantee atomic writes for printf */ n = sfset(outfile,SF_SHARE|SF_PUBLIC,0); printv: if(format) { /* printf style print */ Sfio_t *pool; struct printf pdata; memset(&pdata, 0, sizeof(pdata)); pdata.sh = shp; pdata.hdr.version = SFIO_VERSION; pdata.hdr.extf = extend; pdata.nextarg = argv; sh_offstate(shp,SH_STOPOK); pool=sfpool(sfstderr,NIL(Sfio_t*),SF_WRITE); do {
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f, *f2; char buf[1024]; char rbuf[4 * 1024]; off_t o; int i; if (!(f = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Can't open file"); sfset(f, SF_IOCHECK, 1); Disc.exceptf = except; if (!sfdisc(f, &Disc)) terror("Pushing discipline failed"); sfdisc(f, &Disc); if (Type != SF_DPUSH) terror("Did not get push event"); /* this is to test sfraise(NULL,...) */ if (!(f2 = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Can't open file"); sfdisc(f2, &Disc); Sfn = 0; if (sfraise(0, SF_WRITE, 0) < 0) terror("sfraise failed"); if (Sfn != 2) terror("Didn't get right event count"); sfdisc(f, NULL); if (Type != SF_DPOP) terror("Did not get pop event"); sfwrite(f, "123", 3); sfsync(f); if (Type != SF_SYNC) terror("Did not get sync event"); sfwrite(f, "123", 3); sfpurge(f); if (Type != SF_PURGE) terror("Did not get purge event"); sfclose(f); if (Type != SF_CLOSING) terror("Did not get close event"); sfclose(f); if (Type != SF_FINAL) terror("Did not get final event"); if (!(f = sfopen(NULL, tstfile("sf", 0), "r"))) terror("Can't open file"); Disc2.readf = readfunc; Disc2.exceptf = except3; sfdisc(f, &Disc2); if (sfgetc(f) >= 0) terror("There should be no data here"); if (Type != SF_LOCKED) terror("Did not get lock event"); /* test to see if sfclose() preserves seek location */ if (!(f = sftmp(0))) terror("Can't create temp file"); sfsetbuf(f, buf, sizeof(buf)); for (i = 0; i < sizeof(rbuf); ++i) rbuf[i] = i; sfwrite(f, rbuf, sizeof(rbuf)); sfset(f, SF_WRITE, 0); Disc.exceptf = except2; sfdisc(f, &Disc); sfseek(f, (Sfoff_t)0, 0); if (sfread(f, rbuf, 4) != 4) terror("reading 4 bytes"); for (i = 0; i < 4; ++i) { if (rbuf[i] != i) terror("wrong 4 bytes"); } sfsync(f); if ((o = lseek(sffileno(f), (off_t)0, SEEK_CUR)) != 4) { terror("Wrong seek location %lld", (Sfoff_t)o); } if ((i = dup(sffileno(f))) < 0) terror("Can't dup file descriptor"); if ((o = lseek(i, (off_t)0, SEEK_CUR)) != 4) terror("Wrong seek location %lld", (Sfoff_t)o); sfclose(f); if ((o = lseek(i, (off_t)0, SEEK_CUR)) != 4) terror("Wrong seek location %lld", (Sfoff_t)o); texit(0); }
main() { int n, fd; Sfio_t *f; char *s, buf[1024]; int fdv[100]; buf[0] = 0; sfsetbuf(sfstdout,buf,sizeof(buf)); if(!sfstdout->pool) terror("No pool\n"); sfdisc(sfstdout,&Disc); sfset(sfstdout,SF_SHARE,0); sfputr(sfstdout,"123456789",0); if(strcmp(buf,"123456789") != 0) terror("Setting own buffer for stdout\n"); if(sfpurge(sfstdout) < 0) terror("Purging sfstdout\n"); if((fd = creat("xxx",0666)) < 0) terror("Creating xxx\n"); if(write(fd,buf,sizeof(buf)) != sizeof(buf)) terror("Writing to xxx\n"); if(lseek(fd,0L,0) < 0) terror("Seeking back to origin\n"); if(!(f = sfnew((Sfio_t*)0,buf,sizeof(buf),fd,SF_WRITE))) terror("Making stream\n"); if(!(s = sfreserve(f,SF_UNBOUND,1)) || s != buf) terror("sfreserve1 returns the wrong pointer\n"); sfwrite(f,s,0); #define NEXTFD 12 if((fd+NEXTFD) < (sizeof(fdv)/sizeof(fdv[0])) ) { struct stat st; int i; for(i = 0; i < fd+NEXTFD; ++i) fdv[i] = fstat(i,&st); } if((n = sfsetfd(f,fd+NEXTFD)) != fd+NEXTFD) terror("Try to set file descriptor to %d but get %d\n",fd+NEXTFD,n); if((fd+NEXTFD) < (sizeof(fdv)/sizeof(fdv[0])) ) { struct stat st; int i; for(i = 0; i < fd+NEXTFD; ++i) if(i != fd && fdv[i] != fstat(i,&st)) terror("Fd %d changes status after sfsetfd %d->%d\n", i, fd, fd+NEXTFD); } if(!(s = sfreserve(f,SF_UNBOUND,1)) || s != buf) terror("sfreserve2 returns the wrong pointer\n"); sfwrite(f,s,0); if(sfsetbuf(f,NIL(Void_t*),(size_t)SF_UNBOUND) != buf) terror("sfsetbuf didnot returns last buffer\n"); sfsetbuf(f,buf,sizeof(buf)); if(sfreserve(f,SF_UNBOUND,1) != buf || sfvalue(f) != sizeof(buf) ) terror("sfreserve3 returns the wrong value\n"); sfwrite(f,s,0); system("rm xxx >/dev/null 2>&1"); return 0; }
int b_head(int argc, register char** argv, void* context) { static const char header_fmt[] = "\n==> %s <==\n"; register Sfio_t* fp; register char* cp; register off_t keep = 10; register off_t skip = 0; register int delim = '\n'; int header = 1; char* format = (char*)header_fmt+1; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case 'c': delim = -1; /*FALLTHROUGH*/ case 'n': if (opt_info.offset && argv[opt_info.index][opt_info.offset] == 'c') { delim = -1; opt_info.offset++; } if ((keep = opt_info.number) <=0) error(2, "%s: %I*d: positive numeric option argument expected", opt_info.name, sizeof(keep), keep); continue; case 'q': header = argc; continue; case 'v': header = 0; continue; case 's': skip = opt_info.number; continue; case '?': error(ERROR_usage(2), "%s", opt_info.arg); continue; case ':': error(2, "%s", opt_info.arg); continue; } break; } argv += opt_info.index; argc -= opt_info.index; if (error_info.errors) error(ERROR_usage(2), "%s", optusage(NiL)); if (cp = *argv) argv++; do { if (!cp || streq(cp, "-")) { cp = "/dev/stdin"; fp = sfstdin; sfset(fp, SF_SHARE, 1); } else if (!(fp = sfopen(NiL, cp, "r"))) { error(ERROR_system(0), "%s: cannot open", cp); continue; } if (argc > header) sfprintf(sfstdout, format, cp); format = (char*)header_fmt; if (skip > 0) sfmove(fp, NiL, skip, delim); if (sfmove(fp, sfstdout, keep, delim) < 0 && errno != EPIPE) error(ERROR_system(0), "%s: read error", cp); if (fp != sfstdin) sfclose(fp); } while (cp = *argv++); if (sfsync(sfstdout)) error(ERROR_system(0), "write error"); return error_info.errors != 0; }
/* * This routine will turn the sftmp() file into a real /tmp file or pipe */ void sh_subtmpfile(int pflag) { Shell_t *shp = &sh; int fds[2]; Sfoff_t off; register struct checkpt *pp = (struct checkpt*)shp->jmplist; register struct subshell *sp = subshell_data->pipe; if(sfset(sfstdout,0,0)&SF_STRING) { register int fd; /* save file descriptor 1 if open */ if((sp->tmpfd = fd = fcntl(1,F_DUPFD,10)) >= 0) { fcntl(fd,F_SETFD,FD_CLOEXEC); shp->fdstatus[fd] = shp->fdstatus[1]|IOCLEX; close(1); shp->fdstatus[1] = IOCLOSE; } else if(errno!=EBADF) { ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT; shp->toomany = 1; errormsg(SH_DICT,ERROR_system(1),e_toomany); } if(shp->subshare || !pflag) { sfdisc(sfstdout,SF_POPDISC); if((fd=sffileno(sfstdout))>=0) { shp->fdstatus[fd] = IOREAD|IOWRITE; sfsync(sfstdout); if(fd==1) fcntl(1,F_SETFD,0); else { sfsetfd(sfstdout,1); shp->fdstatus[1] = shp->fdstatus[fd]; shp->fdstatus[fd] = IOCLOSE; } goto skip; } } } if(sp && (shp->fdstatus[1]==IOCLOSE || (!shp->subshare && !(shp->fdstatus[1]&IONOSEEK)))) { struct stat statb,statx; int fd; sh_pipe(fds); sp->pipefd = fds[0]; sh_fcntl(sp->pipefd,F_SETFD,FD_CLOEXEC); /* write the data to the pipe */ if(off = sftell(sfstdout)) { write(fds[1],sfsetbuf(sfstdout,(Void_t*)sfstdout,0),(size_t)off); sfpurge(sfstdout); } if((sfset(sfstdout,0,0)&SF_STRING) || fstat(1,&statb)<0) statb.st_ino = 0; sfclose(sfstdout); if((sh_fcntl(fds[1],F_DUPFD, 1)) != 1) errormsg(SH_DICT,ERROR_system(1),e_redirect); sh_close(fds[1]); if(statb.st_ino) for(fd=0; fd < 10; fd++) { if(fd==1 || ((shp->fdstatus[fd]&(IONOSEEK|IOSEEK|IOWRITE))!=(IOSEEK|IOWRITE)) || fstat(fd,&statx)<0) continue; if(statb.st_ino==statx.st_ino && statb.st_dev==statx.st_dev) { sh_close(fd); fcntl(1,F_DUPFD, fd); } } skip: sh_iostream(shp,1); sfset(sfstdout,SF_SHARE|SF_PUBLIC,1); sfpool(sfstdout,shp->outpool,SF_WRITE); if(pp && pp->olist && pp->olist->strm == sfstdout) pp->olist->strm = 0; } }
int sfdcpzip(Sfio_t* sp, const char* path, unsigned long flags, Pzdisc_t* disc) { Sfio_t* io; Sfpzip_t* pz; Pz_t* oz; if (flags & PZ_HANDLE) { oz = (Pz_t*)sp; sp = oz->io; } else oz = 0; if (sfset(sp, 0, 0) & SF_WRITE) { if (flags & PZ_STAT) return -1; } else if (!(flags & PZ_FORCE)) { unsigned char* s; int r; int m1; int m2; if (!(r = sfset(sp, 0, 0) & SF_SHARE)) sfset(sp, SF_SHARE, 1); s = (unsigned char*)sfreserve(sp, PZ_GZ_MAGOFF + 2, 1); if (!r) sfset(sp, SF_SHARE, 0); if (!s) return -1; m1 = s[0]; m2 = s[1]; r = m1 == PZ_MAGIC_1 && m2 == PZ_MAGIC_2 && s[2] > 0 && s[3] < 10 || m1 == GZ_MAGIC_1 && m2 == GZ_MAGIC_2 && s[PZ_GZ_MAGOFF] == PZ_GZ_MAGIC_1 && s[PZ_GZ_MAGOFF+1] == PZ_GZ_MAGIC_2; sfread(sp, s, 0); if (flags & PZ_STAT) return r; if (!r) { if (!(flags & PZ_NOGZIP)) { if (m1 == GZ_MAGIC_1) { if (m2 == GZ_MAGIC_2) r = sfdcgzip(sp, (flags & PZ_CRC) ? 0 : SFGZ_NOCRC); else if (m2 == LZ_MAGIC_2) r = sfdclzw(sp, 0); } else if (m1 == 'B' && m2 == 'Z' && s[2] == 'h' && s[3] >= '1' && s[3] <= '9') r = sfdcbzip(sp, 0); } return r; } sfsync(sp); } if (!(io = sfnew(NiL, NiL, SF_UNBOUND, sffileno(sp), (sfset(sp, 0, 0) & (SF_READ|SF_WRITE))))) return -1; if (!(pz = newof(0, Sfpzip_t, 1, 0))) { io->_file = -1; sfclose(io); return -1; } pz->disc.version = PZ_VERSION; flags &= ~(PZ_READ|PZ_WRITE|PZ_STAT|PZ_STREAM|PZ_INTERNAL); flags |= PZ_PUSHED|PZ_STREAM|((sfset(sp, 0, 0) & SF_READ) ? PZ_READ : PZ_WRITE); if (oz && (oz->flags & PZ_WRITE)) flags |= PZ_DELAY; if (disc) { pz->disc.errorf = disc->errorf; pz->disc.window = disc->window; pz->disc.options = disc->options; pz->disc.partition = disc->partition; if (disc->splitf) flags |= PZ_ACCEPT; } if (!(pz->pz = pzopen(&pz->disc, (char*)io, flags)) || (sp->_file = open("/dev/null", 0)) < 0) { io->_file = -1; sfclose(io); free(pz); return -1; } if (path) pz->pz->path = path; pz->sfdisc.exceptf = sfpzexcept; if (flags & PZ_WRITE) { pz->sfdisc.writef = sfpzwrite; pz->io = io; } else pz->sfdisc.readf = sfpzread; sfset(sp, SF_SHARE|SF_PUBLIC, 0); if (sfdisc(sp, &pz->sfdisc) != &pz->sfdisc) { close(sp->_file); sp->_file = io->_file; sfseek(sp, sftell(io), SEEK_SET); io->_file = -1; pzclose(pz->pz); free(pz); return -1; } if (oz) oz->flags |= pz->pz->flags & PZ_INTERNAL; return 1; }
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f, *f2; char *s; int i, n; char buf[16 * 1024]; if (!(f = sfopen(NULL, tstfile("sf", 0), "w+"))) terror("Can't open file"); if (sfnputc(f, 'a', 1000) != 1000) terror("Writing"); if (sfseek(f, (Sfoff_t)0, 0) != 0) terror("Seeking"); if ((n = (int)sfsize(f)) != 1000) terror("Wrong size %d", n); if (!(f2 = sfnew(NULL, NULL, (size_t)SF_UNBOUND, sffileno(f), SF_WRITE))) { terror("Can't open stream"); } if (sfseek(f2, (Sfoff_t)1999, 0) != (Sfoff_t)1999) terror("Seeking2"); sfputc(f2, 'b'); sfsync(f2); if ((n = (int)sfsize(f2)) != 2000) terror("Wrong size2 %d", n); if ((n = (int)sfsize(f)) != 1000) terror("Wrong size3 %d", n); sfputc(f, 'a'); sfset(f, SF_SHARE, 1); if ((n = (int)sfsize(f)) != 2000) terror("Wrong size4 %d", n); if (!(f = sfopen(f, NULL, "srw"))) terror("Can't open string stream"); sfwrite(f, "0123456789", 10); if (sfsize(f) != 10) terror("String size is wrong1"); sfseek(f, (Sfoff_t)19, 0); if (sfsize(f) != 10) terror("String size is wrong2"); sfputc(f, 'a'); if (sfsize(f) != 20) terror("String size is wrong3"); sfseek(f, (Sfoff_t)0, 0); if (sfsize(f) != 20) terror("String size is wrong4"); sfseek(f, (Sfoff_t)0, 0); if (!(s = sfreserve(f, SF_UNBOUND, SF_LOCKR)) && sfvalue(f) != 20) { terror("String size is wrong5"); } sfread(f, s, 5); if (sfsize(f) != 20) terror("String size is wrong6"); sfwrite(f, "01234567890123456789", 20); if (sfsize(f) != 25) terror("String size is wrong7"); strcpy(buf, "0123456789"); if (!(f = sfopen(f, buf, "s+"))) terror("Can't open string stream2"); if (sfset(f, 0, 0) & SF_MALLOC) terror("SF_MALLOC should not have been set"); if (sfsize(f) != 10) terror("String size is wrong8"); sfread(f, buf, 5); if ((n = (int)sfwrite(f, "0123456789", 10)) != 5) terror("Write wrong amount %d", n); if (sfsize(f) != 10) terror("String size is wrong9"); if (!(f = sfopen(f, tstfile("sf", 0), "w"))) terror("Reopening file1"); for (i = 0; i < 10000; ++i) { if (sfputc(f, '0' + (i % 10)) != '0' + (i % 10)) terror("sfputc failed"); } if (!(f = sfopen(f, tstfile("sf", 0), "r+"))) terror("Reopening file2"); if (sfsize(f) != 10000) terror("Bad size of file1"); sfsetbuf(f, buf, 1024); for (i = 0; i < 20; ++i) { if (!sfreserve(f, 100, 0)) terror("Reserve failed"); } s = buf + 1024; for (i = 0; i < 20; ++i) s[i] = '0' + i % 10; sfseek(f, (Sfoff_t)(10000 - 10), 0); if (sfwrite(f, s, 20) != 20) terror("Write failed"); if (sfsize(f) != 10010) terror("Bad size of file2"); sfseek(f, (Sfoff_t)0, 0); for (i = 0; i < 10; ++i) { if (!(s = sfreserve(f, 1001, 0))) terror("Reserve failed2"); if (s[0] != '0' + i) terror("Bad data1"); } for (n = 0; n < 1001; ++n) { if (s[n] != ((n + i - 1) % 10 + '0')) terror("Bad data"); } /* test to see if a string stream extends ok during writes */ s = malloc(5); f = sfnew(NULL, (void *)s, 5, -1, SF_STRING | SF_READ | SF_WRITE | SF_MALLOC); if (!f) terror("Can't create string stream"); if (sfwrite(f, "01", 2) != 2) terror("Bad write to string stream"); if (sfwrite(f, "2345678", 7) != 7) terror("Bad write to string stream2"); if (sfputc(f, 0) != 0) terror("sfputc failed"); if (sfseek(f, (Sfoff_t)0, 0) != 0) terror("sfseek failed"); if ((n = (int)sfread(f, buf, 100)) != 10) terror("sfread gets wrong amount of data %d", n); if (strcmp(buf, "012345678") != 0) terror("Get wrong data"); texit(0); }