/* Last edited on 2024-12-25 10:14:55 by stolfi */ /****************************************************************************/ /* (C) Copyright 1992 Universidade Estadual de Campinas (UNICAMP) */ /* Campinas, SP, Brazil */ /* */ /* Authors: */ /* */ /* Jorge Stolfi - CS Dept, UNICAMP */ /* */ /* This file can be freely distributed, modified, and used for any */ /* non-commercial purpose, provided that this copyright and authorship */ /* notice be included in any copy or derived version of this file. */ /* */ /* DISCLAIMER: This software is offered ``as is'', without any guarantee */ /* as to fitness for any particular purpose. Neither the copyright */ /* holder nor the authors or their employers can be held responsible for */ /* any damages that may result from its use. */ /****************************************************************************/ #include #include #include #include #include typedef unsigned char byte; typedef int bool_t; #define TRUE 1 #define FALSE 0 #ifdef MSDOS #include #else #endif /* INTERNAL PROTOTYPES */ int main(int argc, char *argv[]); bool_t ReadLine(FILE *rd, byte **L, long *N, long *A); /* Returns TRUE if "rd" (whose filename is "name") was at end-of-file. Otherwise reads the next line of "rd" into "*L", and stores its length into "*N". The final newline is stored if present. Assumes the buffer "*L" is "*A" bytes long; reallocates a new one if necessary. */ void WriteLine(byte L[], long N); /* Writes the line "L[0..N-1]" to stdout, reversed. If the line ends with a newline, that byte is written last. Assumes "N" is not zero. */ void GetOptions(int argc, char *argv[]); /* Parses the command line options (which are none). */ void ErrorParams(char *msg); /* Print error message and exit with status=1. */ /* PROCEDURES */ char *progName; int main(int argc, char *argv[]) { /* Command line options: */ /* Internal variables: */ byte *L; /* Current input line */ long N; /* Used size of "L[i]". */ long A; /* Allocated size of "L[i]" */ /* Command line parsing: */ progName = argv[0]; GetOptions(argc, argv); /* Allocate line buffer */ A = 2001; L = (byte *)malloc(A*sizeof(byte)); /* Main loop: */ while(! ReadLine(stdin, &L, &N, &A)) WriteLine(L, N); /* Close files and prit report: */ fclose(stdout); return(0); } bool_t ReadLine(FILE *rd, byte **L, long *N, long *A) { int c; byte *curL = (*L); long curA = (*A); long curN = 0; c = fgetc(rd); if (c == EOF) { (*N) = 0; return(TRUE); } while (c != EOF) { if (curN >= curA) { long newA = 2*curA; byte *newL = (byte *)malloc(newA*sizeof(byte)); long i; for (i=0; i= 0) && (L[i] == '\n')) { i--; while (i >= 0) { putchar(L[i]); i--; } putchar('\n'); } else { while (i >= 0) { putchar(L[i]); i--; } } } void GetOptions(int argc, char *argv[]) { if (argc != 1) { ErrorParams("wrong number of arguments"); } } void ErrorParams(char *msg) { fprintf(stderr, "%s: %s\n", progName, msg); fprintf(stderr, "usage: %s < infile > outfile\n", progName); fflush(stderr); exit(1); }