annotate toys/other/rev.c @ 754:06c32155cb95

Minor cleanups.
author Rob Landley <rob@landley.net>
date Wed, 19 Dec 2012 09:16:27 -0600
parents 7d258f3c13dc
children c42f64ea297d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
753
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
1 /* rev.c - reverse lines of a set of given input files
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
2 *
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
4
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
5 USE_REV(NEWTOY(rev, NULL, TOYFLAG_USR|TOYFLAG_BIN))
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
6
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
7 config REV
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
8 bool "rev"
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
9 default y
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
10 help
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
11 usage: rev [FILE...]
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
12
754
06c32155cb95 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 753
diff changeset
13 Output each line reversed, when no files are given stdin is used.
753
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
14 */
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
15
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
16 #include "toys.h"
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
17
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
18 void do_rev(int fd, char *name)
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
19 {
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
20 char *c;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22 for (;;) {
754
06c32155cb95 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 753
diff changeset
23 int len, i;
06c32155cb95 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 753
diff changeset
24
753
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
25 if (!(c = get_line(fd))) break;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
26 len = strlen(c) - 1;
754
06c32155cb95 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 753
diff changeset
27 for (i = 0; i <= len/2; i++) {
06c32155cb95 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 753
diff changeset
28 char tmp = c[i];
06c32155cb95 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 753
diff changeset
29
753
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
30 c[i] = c[len-i];
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
31 c[len-i] = tmp;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
32 i++;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
33 }
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
34 xputs(c);
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
35 free(c);
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
36 }
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
37 }
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
38
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
39 void rev_main(void)
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
40 {
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
41 loopfiles(toys.optargs, do_rev);
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
42 }