home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume10 / revcat_db < prev    next >
Text File  |  1990-02-13  |  3KB  |  95 lines

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