xref: /titanic_51/usr/src/lib/libcmd/common/rev.c (revision bc37da3aa8455efcf567c456746e3fb9d7f0a189)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1992-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23  * rev [-l] [file ...]
24  *
25  * reverse the characters or lines of one or more files
26  *
27  *   David Korn
28  *   AT&T Laboratories
29  *   dgk@research.att.com
30  *
31  */
32 
33 static const char usage[] =
34 "[-?\n@(#)$Id: rev (AT&T Research) 1999-04-10 $\n]"
35 USAGE_LICENSE
36 "[+NAME?rev - reverse the characters or lines of one or more files]"
37 "[+DESCRIPTION?\brev\b copies one or more files to standard output "
38 	"reversing the order of characters on every line of the file "
39 	"or reversing the order of lines of the file if \b-l\b is specified.]"
40 "[+?If no \afile\a is given, or if the \afile\a is \b-\b, \brev\b "
41         "copies from standard input starting at the current offset.]"
42 "[l:line?Reverse the lines of the file.]"
43 
44 "\n"
45 "\n[file ...]\n"
46 "\n"
47 "[+EXIT STATUS?]{"
48         "[+0?All files copied successfully.]"
49         "[+>0?One or more files did not copy.]"
50 "}"
51 "[+SEE ALSO?\bcat\b(1), \btail\b(1)]"
52 ;
53 
54 #include	<cmd.h>
55 #include	<rev.h>
56 
57 /*
58  * reverse the characters within a line
59  */
60 static int rev_char(Sfio_t *in, Sfio_t *out)
61 {
62 	register int c;
63 	register char *ep, *bp, *cp;
64 	register int n;
65 	while(cp = bp = sfgetr(in,'\n',0))
66 	{
67 		ep = bp + (n=sfvalue(in)) -1;
68 		while(ep > bp)
69 		{
70 			c = *--ep;
71 			*ep = *bp;
72 			*bp++ = c;
73 		}
74 		if(sfwrite(out,cp,n)<0)
75 			return(-1);
76 	}
77 	return(0);
78 }
79 
80 int
81 b_rev(int argc, register char** argv, void* context)
82 {
83 	register Sfio_t *fp;
84 	register char *cp;
85 	register int n, line=0;
86 	NOT_USED(argc);
87 
88 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
89 	while (n = optget(argv, usage)) switch (n)
90 	{
91 	    case 'l':
92 		line=1;
93 		break;
94 	    case ':':
95 		error(2, "%s", opt_info.arg);
96 		break;
97 	    case '?':
98 		error(ERROR_usage(2), "%s", opt_info.arg);
99 		break;
100 	}
101 	argv += opt_info.index;
102 	if(error_info.errors)
103 		error(ERROR_usage(2),"%s",optusage((char*)0));
104 	n=0;
105 	if(cp = *argv)
106 		argv++;
107 	do
108 	{
109 		if(!cp || streq(cp,"-"))
110 			fp = sfstdin;
111 		else if(!(fp = sfopen((Sfio_t*)0,cp,"r")))
112 		{
113 			error(ERROR_system(0),"%s: cannot open",cp);
114 			n=1;
115 			continue;
116 		}
117 		if(line)
118 			line = rev_line(fp,sfstdout,sftell(fp));
119 		else
120 			line = rev_char(fp,sfstdout);
121 		if(fp!=sfstdin)
122 			sfclose(fp);
123 		if(line < 0)
124 			error(ERROR_system(1),"write failed");
125 	}
126 	while(cp= *argv++);
127 	return(n);
128 }
129