home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / unix_c / utils / reverse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  4.2 KB  |  174 lines

  1. 10-Sep-85 11:17:20-MDT,4446;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Tue 10 Sep 85 11:17:09-MDT
  4. Received: from usenet by TGR.BRL.ARPA id a025785; 10 Sep 85 5:50 EDT
  5. From: K A Gluck <kurt@pyuxhh.uucp>
  6. Newsgroups: net.sources
  7. Subject: program to totaly reverse input
  8. Message-ID: <873@pyuxhh.UUCP>
  9. Date: 9 Sep 85 20:48:55 GMT
  10. To:       unix-sources@BRL-TGR.ARPA
  11.  
  12. rellik gub
  13. # Program to super reverse input
  14. # this program will completly reverse its input.
  15. # (with one exception - the output will have an extra line feed
  16. # at its beginning, and the final trainling line feed will be deleted)
  17. # for example, the folowing was a copy of the previous run through 
  18. # this program
  19. #
  20. #-------------------------------------------------------------------------
  21. # margorp siht
  22. #  hguorht nur suoiverp eht fo ypoc a saw gniwolof eht ,elpmaxe rof
  23. # )deteled eb lliw deef enil gnilniart lanif eht dna ,gninnigeb sti ta
  24. # deef enil artxe na evah lliw tuptuo eht - noitpecxe eno htiw(
  25. # .tupni sti esrever yltelpmoc lliw margorp siht
  26. #
  27. # tupni esrever repus ot margorP
  28. #
  29. #-------------------------------------------------------------------------
  30. # This is a shell archive.  Remove anything before this line, then
  31. # unpack it by saving it in a file and typing "sh file".  (Files
  32. # unpacked will be owned by you and have default permissions.)
  33. #
  34. # This archive contains:
  35. # reverse.c reverse.man
  36.  
  37. echo x - reverse.c
  38. cat > "reverse.c" << '//E*O*F reverse.c//'
  39. #include <stdio.h>
  40. #include <fcntl.h>
  41. #define strlen 256
  42.  
  43. main(argc,argv)  /* rev- lists file. */
  44.         /* argc - number of args*/
  45.         /* argv - pointer to array of char strings of args*/
  46. int argc;
  47. char *argv[];
  48. {
  49.  int fp, open(), creat() , strcmp() ;
  50.  char template[30], *mktemp() , *strcpy() ;
  51.  char *tmp;
  52. char *tmpname() ;
  53.  long pos;
  54.  int n;
  55.  
  56.  if( argc == 1 ) /* no args - halt */
  57.     { printf("rev- no argument given\n");
  58.       printf("usage-   rev file1 [{filen}]\n");
  59.     printf(" alt:    rev -    reads from stdin (pipes etc...) \n");
  60.       exit(1); }
  61.  else
  62.     while( --argc > 0 )
  63.         if( (*++argv)[0] == '-' )
  64.         {
  65.             if( ( fp = open(( tmp = ( mktemp("/tmp/revXXXXXX\0")) ),O_RDWR | O_CREAT , 0755)) == -1 )
  66.             {    printf("rev- cant open %s <<<<\n",*template);
  67.             } else {
  68.                 filecopy(0,fp);
  69.                 filerev(fp) ;
  70.                 close(fp);
  71.                 unlink(tmp);
  72.             }
  73.         } else {
  74.             if (( fp = open(*argv,0)) == -1 ) {
  75.                 printf("rev- cant open %s ###################\n",*argv);
  76.             } else {
  77.                 filerev(fp) ;
  78.                 close(fp);
  79.             }
  80.         }
  81.  }
  82.  filecopy(fpi,fpo)    /* list a file            */
  83.  int fpi , fpo;        /* the file            */
  84. { char c ;
  85.  while ( ( read(fpi,&c,1) ) > 0 ) 
  86.      write(fpo,&c,1) ; 
  87.  }
  88.  filerev(fp)    /* list a file            */
  89.  int fp;        /* the file            */
  90. { char c ;
  91.   long p ;
  92.     int flag;
  93.  long lseek() ;
  94.  int whence;
  95.  p=0L ;
  96.  whence=2;
  97.  flag=1;
  98.   while ( ( lseek(fp,p,whence ) ) != -1L )  {
  99.     p=0L-2L;
  100.     whence=1;
  101.      read(fp,&c,1) ; 
  102.     if( flag==0 )     write(1,&c,1) ; 
  103.     flag=0;
  104.     }
  105.     printf("\n");
  106.  }
  107. //E*O*F reverse.c//
  108.  
  109. echo x - reverse.man
  110. cat > "reverse.man" << '//E*O*F reverse.man//'
  111. .tr ~
  112. .de Na
  113. .br
  114. \\$1
  115. ..
  116. .de Ex
  117. .IP "" 5
  118. .B \\$1 \\$2 \$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
  119. .P
  120. ..
  121. .de Es
  122. .IP "\\$1" 5
  123. .B \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9 
  124. ..
  125. .de Ee
  126. .br
  127. .B \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9 
  128. ..
  129. .de Ez
  130. .P
  131. ..
  132. .tr ~
  133. .TH reverse lman "Pics System Test"
  134. .SH NAME
  135. reverse - c program to reverse files.
  136. .SH SYNOPSIS
  137. .Na "reverse files"
  138. .SH DESCRIPTION
  139. .B reverse
  140. is a filter to reverse files.
  141. It is used in a manner similar to cat (see cat(1)).
  142. .B reverse
  143. will process each of its arguments in turn, reading the files backwards.
  144. If any file has 
  145. .B -
  146. for its name, 
  147. .B reverse
  148. will create a temporary file, read standard input into that temporary file
  149. and then reverse the temporary file.
  150. .SH EXAMPLES
  151. .Es "To reverse input from terminal" reverse -
  152. .Ez
  153. .Es "To create a confusing listing" ps -ef |reverse -
  154. .Ez
  155. .Es  "To reverse the date" date |reverse -
  156. .Ez
  157. .Es "To reverse a file" who >file
  158. .Ee reverse file
  159. .Ez
  160. .SH "SEE ALSO"
  161. cat(1)
  162. .SH AUTHOR
  163. Kurt Gluck, SPL 1c273a x2023
  164. //E*O*F reverse.man//
  165.  
  166. exit 0
  167. -- 
  168. Kurt Gluck                              (201)-561-7100 x2023
  169. SPL 1c273a                              ihnp4!rruxg!kurt
  170. Bell Communications Research Inc        ihnp4!pyuxhh!kurt
  171. 50 Cragwood Road
  172. South Plainfield, NJ 07080              
  173.