annotate toys/other/rev.c @ 944:b4faf2ae39e8

This inlines CRC64, and nothing more. The functions involved were called only once.
author Isaac Dunham <idunham@lavabit.com>
date Sat, 06 Jul 2013 11:26:15 -0500
parents c42f64ea297d
children
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 }
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
33 xputs(c);
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
34 free(c);
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 }
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 void rev_main(void)
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 loopfiles(toys.optargs, do_rev);
7d258f3c13dc Adding implementation of rev including tests
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
41 }