home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 8 Other
/
08-Other.zip
/
OS2MNX1.ZIP
/
REV.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-26
|
3KB
|
100 lines
/* rev - reverse an ASCII line Authors: Paul Polderman & Michiel Huisjes */
/* $Header: D:/RCS/RCS/rev.c 1.1 89/12/10 06:47:29 RCA Exp $
* $Log: rev.c $
* Revision 1.1 89/12/10 06:47:29 RCA
* Initial revision
*
*/
#include <sys/types.h>
#include <fcntl.h>
/* #include <blocksize.h> */
#define BLOCK_SIZE 80 /* RCA */
#ifndef EOF
#define EOF ((char) -1)
#endif
#define std_err printf /* RCA */
int fd; /* File descriptor from file being read */
main(argc, argv)
int argc;
char *argv[];
{
register unsigned short i;
/* if (argc == 1) { \\ If no arguments given, use stdin as input \\
* fd = 0;
* rev();
* exit(0);
* }
*/
if (argv[1][0] == '-') /* RCA Use stdin */
{
fd = 0;
rev();
exit(0);
}
if (argc == 1)
{
printf("█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█\n");
printf("█ REV (reverse ASCII input) $Author: RCA $ █\n");
printf("█ $Date: 89/12/10 06:47:29 $ $Revision: 1.1 $ █\n");
printf("█ Usage: REV [inputfiles] [-] █\n");
printf("█ Purpose: Reverse the spelling of input file. Uses: █\n");
printf("█ Projection TV? Hebrew? █\n");
printf("█ Options: [inputfiles] Reverse the contents of file(s). █\n");
printf("█ [-] Use standard input (^Z ends input). █\n");
printf("█ OS: DOS or OS/2 █\n");
printf("█ Credits: Paul Polderman and Michiel Huisjes █\n");
printf("█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█\n");
}
for (i = 1; i < argc; i++) { /* Reverse each line in arguments */
if ((fd = open(argv[i], O_RDONLY)) < 0) {
std_err("Cannot open ");
std_err(argv[i]);
std_err("\n");
continue;
}
rev();
close(fd);
}
exit(0);
}
rev()
{
char output[BLOCK_SIZE]; /* Contains a reversed line */
register unsigned short i; /* Index in output array */
do {
i = BLOCK_SIZE - 1;
while ((output[i] = nextchar()) != '\n' && output[i] != EOF) i--;
write(1, &output[i + 1], BLOCK_SIZE - 1 - i); /* write reversed line */
if (output[i] == '\n') /* and write a '\n' */
write(1, "\n", 1);
} while (output[i] != EOF);
}
char buf[BLOCK_SIZE];
nextchar()
{ /* Does a sort of buffered I/O */
static int n = 0; /* Read count */
static int i; /* Index in input buffer to next character */
if (--n <= 0) { /* We've had this block. Read in next block */
n = read(fd, buf, BLOCK_SIZE);
i = 0; /* Reset index in array */
}
return((n <= 0) ? EOF : buf[i++]); /* Return -1 on EOF */
}