annotate toys/other/rev.c @ 753:7d258f3c13dc

Adding implementation of rev including tests
author Elie De Brauwer <eliedebrauwer@gmail.com>
date Sun, 16 Dec 2012 12:31:15 +0100
parents
children 06c32155cb95
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
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
13 Output lines reversed, when no files are given stdin is used.
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 (;;) {
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 int len;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
24 int i;
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;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
27 i = 0;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
28 while ( i <= len/2)
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
29 {
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
30 char tmp;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
31 tmp = c[i];
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
32 c[i] = c[len-i];
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
33 c[len-i] = tmp;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
34 i++;
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
35 }
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
36 xputs(c);
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
37 free(c);
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 }
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 void rev_main(void)
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
42 {
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
43 loopfiles(toys.optargs, do_rev);
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
44 }