1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1992-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * rev [-l] [file ...]
1N/A *
1N/A * reverse the characters or lines of one or more files
1N/A *
1N/A * David Korn
1N/A * AT&T Laboratories
1N/A * dgk@research.att.com
1N/A *
1N/A */
1N/A
1N/Astatic const char usage[] =
1N/A"[-?\n@(#)$Id: rev (AT&T Research) 2007-11-29 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?rev - reverse the characters or lines of one or more files]"
1N/A"[+DESCRIPTION?\brev\b copies one or more files to standard output "
1N/A "reversing the order of characters on every line of the file "
1N/A "or reversing the order of lines of the file if \b-l\b is specified.]"
1N/A"[+?If no \afile\a is given, or if the \afile\a is \b-\b, \brev\b "
1N/A "copies from standard input starting at the current offset.]"
1N/A"[l:line?Reverse the lines of the file.]"
1N/A
1N/A"\n"
1N/A"\n[file ...]\n"
1N/A"\n"
1N/A"[+EXIT STATUS?]{"
1N/A "[+0?All files copied successfully.]"
1N/A "[+>0?One or more files did not copy.]"
1N/A"}"
1N/A"[+SEE ALSO?\bcat\b(1), \btail\b(1)]"
1N/A;
1N/A
1N/A#include <cmd.h>
1N/A#include <rev.h>
1N/A
1N/A/*
1N/A * reverse the characters within a line
1N/A */
1N/Astatic int rev_char(Sfio_t *in, Sfio_t *out)
1N/A{
1N/A register int c;
1N/A register char *ep, *bp, *cp;
1N/A register wchar_t *wp, *xp;
1N/A register size_t n;
1N/A register size_t w;
1N/A if (mbwide())
1N/A {
1N/A wp = 0;
1N/A w = 0;
1N/A while(cp = bp = sfgetr(in,'\n',0))
1N/A {
1N/A ep = bp + (n=sfvalue(in)) - 1;
1N/A if (n > w)
1N/A {
1N/A w = roundof(n + 1, 1024);
1N/A if (!(wp = newof(wp, wchar_t, w, 0)))
1N/A {
1N/A error(ERROR_SYSTEM|2, "out of space");
1N/A return 0;
1N/A }
1N/A }
1N/A xp = wp;
1N/A while (cp < ep)
1N/A *xp++ = mbchar(cp);
1N/A cp = bp;
1N/A while (xp > wp)
1N/A cp += mbconv(cp, *--xp);
1N/A *cp++ = '\n';
1N/A if (sfwrite(out, bp, cp - bp) < 0)
1N/A return -1;
1N/A }
1N/A if (wp)
1N/A free(wp);
1N/A }
1N/A else
1N/A while(cp = bp = sfgetr(in,'\n',0))
1N/A {
1N/A ep = bp + (n=sfvalue(in)) -1;
1N/A while(ep > bp)
1N/A {
1N/A c = *--ep;
1N/A *ep = *bp;
1N/A *bp++ = c;
1N/A }
1N/A if(sfwrite(out,cp,n)<0)
1N/A return(-1);
1N/A }
1N/A return(0);
1N/A}
1N/A
1N/Aint
1N/Ab_rev(int argc, register char** argv, void* context)
1N/A{
1N/A register Sfio_t *fp;
1N/A register char *cp;
1N/A register int n, line=0;
1N/A NOT_USED(argc);
1N/A
1N/A cmdinit(argc, argv, context, ERROR_CATALOG, 0);
1N/A for (;;)
1N/A {
1N/A switch (optget(argv, usage))
1N/A {
1N/A case 'l':
1N/A line=1;
1N/A continue;
1N/A case ':':
1N/A error(2, "%s", opt_info.arg);
1N/A break;
1N/A case '?':
1N/A error(ERROR_usage(2), "%s", opt_info.arg);
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A argv += opt_info.index;
1N/A if(error_info.errors)
1N/A error(ERROR_usage(2),"%s",optusage((char*)0));
1N/A n=0;
1N/A if(cp = *argv)
1N/A argv++;
1N/A do
1N/A {
1N/A if(!cp || streq(cp,"-"))
1N/A fp = sfstdin;
1N/A else if(!(fp = sfopen((Sfio_t*)0,cp,"r")))
1N/A {
1N/A error(ERROR_system(0),"%s: cannot open",cp);
1N/A n=1;
1N/A continue;
1N/A }
1N/A if(line)
1N/A line = rev_line(fp,sfstdout,sftell(fp));
1N/A else
1N/A line = rev_char(fp,sfstdout);
1N/A if(fp!=sfstdin)
1N/A sfclose(fp);
1N/A if(line < 0)
1N/A error(ERROR_system(1),"write failed");
1N/A }
1N/A while(cp= *argv++);
1N/A return(n);
1N/A}