home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume10 / revcat.db < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  2.7 KB

  1. From decwrl!elroy.jpl.nasa.gov!jarthur!uunet!allbery Sun Feb 18 00:49:44 PST 1990
  2. Article 1332 of comp.sources.misc:
  3. Path: decwrl!elroy.jpl.nasa.gov!jarthur!uunet!allbery
  4. From: dennis@virtech.UUCP (Dennis P. Bednar)
  5. Newsgroups: comp.sources.misc
  6. Subject: v10i061: Backwards cat
  7. Message-ID: <78920@uunet.UU.NET>
  8. Date: 14 Feb 90 01:58:26 GMT
  9. References: <6488@lindy.Stanford.EDU> <1989Dec15.195258.13668@twwells.com>
  10. Sender: allbery@uunet.UU.NET
  11. Organization: Virtual Technologies Inc.
  12. Lines: 87
  13. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  14.  
  15. Posting-number: Volume 10, Issue 61
  16. Submitted-by: dennis@virtech.UUCP (Dennis P. Bednar)
  17. Archive-name: revcat_db
  18.  
  19. #! /bin/sh
  20. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  21. # Contents:  revcat.c
  22. echo extracting 'revcat.c'
  23. if test -f 'revcat.c' -a -z "$1"; then echo Not overwriting 'revcat.c'; else
  24. sed 's/^X//' << \EOF > 'revcat.c'
  25. X/*
  26. X * rev.c
  27. X * dennis bednar
  28. X * Cat a file backwards (last line first, first line last).
  29. X * Done by a recursive procedure that minimizes chewing up
  30. X * the stack space.
  31. X */
  32. X
  33. X#include <stdio.h>
  34. X
  35. Xmain( argc, argv )
  36. X    char    **argv;
  37. X{
  38. X    FILE    *ifp;
  39. X    int    i;
  40. X
  41. X    if (argc == 1)            /* no arguments            */
  42. X        rev_stream(stdin);    /* so reverse stdin        */
  43. X    else for (i = 1; i < argc; ++i)    /* loop on file name args    */
  44. X    {
  45. X        ifp = fopen( argv[i], "r");
  46. X        rev_stream( ifp );    /* reverse one file        */
  47. X        fclose( ifp );
  48. X    }
  49. X    exit(0);
  50. X}
  51. X
  52. X/* this is outside of rev_stream() function to avoid eating up stack space */
  53. Xstatic    char    linebuf[ BUFSIZ ];    /* one line from file        */
  54. Xstatic    FILE    *rev_ifp;        /* handle for file        */
  55. X
  56. X/* Note that we want to minimize the stack space used by the recursive
  57. X * function.  So we use global static variables wherever possible.
  58. X */ 
  59. Xrev_stream( ifp )
  60. X    FILE    *ifp;
  61. X{
  62. X    rev_ifp = ifp;            /* save in global for recursion    */
  63. X    rev_stream_recursive();        /* do the work            */
  64. X}
  65. X
  66. X/*
  67. X * recursively function to reverse lines of a stream 'rev_ifp'.
  68. X */
  69. Xrev_stream_recursive( )
  70. X{
  71. X    char    *cp;
  72. X    char    *strdup();
  73. X
  74. X    /* read one line then recurse to read the rest of the file.
  75. X     */
  76. X    if ( fgets(linebuf, sizeof(linebuf), rev_ifp ) )    /* !EOF */
  77. X    {
  78. X        cp = strdup( linebuf );    /* duplicate this line        */
  79. X        rev_stream_recursive();    /* reverse the rest of file    */
  80. X        fputs( cp, stdout );    /* now print this line        */
  81. X    }
  82. X}
  83. X
  84. Xchar    *strdup( old )
  85. X    char    *old;
  86. X{
  87. X    char    *new;
  88. X    extern    char    *malloc();
  89. X    
  90. X    new = malloc( strlen(old) + 1 );    /* room for null */
  91. X    strcpy( new, old );
  92. X    return new;
  93. X}
  94. EOF
  95. chars=`wc -c < 'revcat.c'`
  96. if test $chars !=     1549; then echo 'revcat.c' is $chars characters, should be     1549 characters!; fi
  97. fi
  98. exit 0
  99. -- 
  100. Dennis Bednar    uunet!virtech!dennis    (703)760-3357(w)   (703)437-4384(h)
  101. Cable & Wireless,    Tysons Corner VA
  102.  
  103.  
  104.