| 1 | /*- | 
|---|
| 2 | * Copyright (c) 1987, 1992, 1993 | 
|---|
| 3 | *      The Regents of the University of California.  All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright | 
|---|
| 9 | *    notice, this list of conditions and the following disclaimer. | 
|---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | 
|---|
| 11 | *    notice, this list of conditions and the following disclaimer in the | 
|---|
| 12 | *    documentation and/or other materials provided with the distribution. | 
|---|
| 13 | * 3. All advertising materials mentioning features or use of this software | 
|---|
| 14 | *    must display the following acknowledgement: | 
|---|
| 15 | *      This product includes software developed by the University of | 
|---|
| 16 | *      California, Berkeley and its contributors. | 
|---|
| 17 | * 4. Neither the name of the University nor the names of its contributors | 
|---|
| 18 | *    may be used to endorse or promote products derived from this software | 
|---|
| 19 | *    without specific prior written permission. | 
|---|
| 20 | * | 
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 24 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|---|
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 31 | * SUCH DAMAGE. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #ifndef lint | 
|---|
| 35 | static const char copyright[] = | 
|---|
| 36 | "@(#) Copyright (c) 1987, 1992, 1993\n\ | 
|---|
| 37 | The Regents of the University of California.  All rights reserved.\n"; | 
|---|
| 38 | #endif /* not lint */ | 
|---|
| 39 |  | 
|---|
| 40 | #if 0 | 
|---|
| 41 | #ifndef lint | 
|---|
| 42 | static char sccsid[] = "@(#)rev.c       8.3 (Berkeley) 5/4/95"; | 
|---|
| 43 | #endif /* not lint */ | 
|---|
| 44 | #endif | 
|---|
| 45 |  | 
|---|
| 46 | #include <sys/cdefs.h> | 
|---|
| 47 | __FBSDID("$FreeBSD: src/usr.bin/rev/rev.c,v 1.8 2004/12/15 06:24:57 ssouhlal Exp $"); | 
|---|
| 48 |  | 
|---|
| 49 | #include <sys/types.h> | 
|---|
| 50 |  | 
|---|
| 51 | #include <err.h> | 
|---|
| 52 | #include <errno.h> | 
|---|
| 53 | #include <locale.h> | 
|---|
| 54 | #include <stdio.h> | 
|---|
| 55 | #include <stdlib.h> | 
|---|
| 56 | #include <string.h> | 
|---|
| 57 | #include <unistd.h> | 
|---|
| 58 | #include <wchar.h> | 
|---|
| 59 |  | 
|---|
| 60 | void usage(void); | 
|---|
| 61 |  | 
|---|
| 62 | int | 
|---|
| 63 | main(int argc, char *argv[]) | 
|---|
| 64 | { | 
|---|
| 65 | const char *filename; | 
|---|
| 66 | #ifdef __OS2__ /* Currently missing putwchar and fgetwln, too bad the the DBCS guys... */ | 
|---|
| 67 | static char line[48*1024]; | 
|---|
| 68 | char *p, *t; | 
|---|
| 69 | #else | 
|---|
| 70 | wchar_t *p, *t; | 
|---|
| 71 | #endif | 
|---|
| 72 | FILE *fp; | 
|---|
| 73 | size_t len; | 
|---|
| 74 | int ch, rval; | 
|---|
| 75 |  | 
|---|
| 76 | setlocale(LC_ALL, ""); | 
|---|
| 77 |  | 
|---|
| 78 | while ((ch = getopt(argc, argv, "")) != -1) | 
|---|
| 79 | switch(ch) { | 
|---|
| 80 | case '?': | 
|---|
| 81 | default: | 
|---|
| 82 | usage(); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | argc -= optind; | 
|---|
| 86 | argv += optind; | 
|---|
| 87 |  | 
|---|
| 88 | fp = stdin; | 
|---|
| 89 | filename = "stdin"; | 
|---|
| 90 | rval = 0; | 
|---|
| 91 | do { | 
|---|
| 92 | if (*argv) { | 
|---|
| 93 | if ((fp = fopen(*argv, "r")) == NULL) { | 
|---|
| 94 | warn("%s", *argv); | 
|---|
| 95 | rval = 1; | 
|---|
| 96 | ++argv; | 
|---|
| 97 | continue; | 
|---|
| 98 | } | 
|---|
| 99 | filename = *argv++; | 
|---|
| 100 | } | 
|---|
| 101 | #ifdef __OS2__ | 
|---|
| 102 | while ((p = fgets(line, sizeof(line), fp)) != NULL) { | 
|---|
| 103 | len = strlen(line); | 
|---|
| 104 | if (p[len - 1] == '\n') | 
|---|
| 105 | --len; | 
|---|
| 106 | t = p + len - 1; | 
|---|
| 107 | for (t = p + len - 1; t >= p; --t) | 
|---|
| 108 | putchar(*t); | 
|---|
| 109 | putchar('\n'); | 
|---|
| 110 | } | 
|---|
| 111 | #else | 
|---|
| 112 | while ((p = fgetwln(fp, &len)) != NULL) { | 
|---|
| 113 | if (p[len - 1] == '\n') | 
|---|
| 114 | --len; | 
|---|
| 115 | t = p + len - 1; | 
|---|
| 116 | for (t = p + len - 1; t >= p; --t) | 
|---|
| 117 | putwchar(*t); | 
|---|
| 118 | putwchar('\n'); | 
|---|
| 119 | } | 
|---|
| 120 | #endif | 
|---|
| 121 | if (ferror(fp)) { | 
|---|
| 122 | warn("%s", filename); | 
|---|
| 123 | clearerr(fp); | 
|---|
| 124 | rval = 1; | 
|---|
| 125 | } | 
|---|
| 126 | (void)fclose(fp); | 
|---|
| 127 | } while(*argv); | 
|---|
| 128 | exit(rval); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | void | 
|---|
| 132 | usage(void) | 
|---|
| 133 | { | 
|---|
| 134 | (void)fprintf(stderr, "usage: rev [file ...]\n"); | 
|---|
| 135 | exit(1); | 
|---|
| 136 | } | 
|---|