home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PSION / COMMS / PSIONMAI / PMFULLSO / SUNMAIL / SHORTENL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-04  |  1.2 KB  |  60 lines

  1. /* this program reads from std in and truncates lines to the first space
  2.    after the line limit -10. If the line goes beyond the line limit it is
  3.    immediatly truncated to the limit and output continues on the next line
  4.    NOTE files are assumed to have the last line ending in a newline */
  5.  
  6. #include <stdio.h>
  7. int linelen = 100 ;
  8. int lookspace ;
  9. main(argc, argv)
  10. int argc ;
  11. char * argv[] ;
  12. {
  13.     int ch ;
  14.     int i ;
  15.     if (argc == 2)
  16.         linelen = atoi(argv[1]) ;
  17.     /* minor sanity check incase the argument was bad */
  18.     if (linelen == 0)
  19.     {
  20.         linelen = 100 ;
  21.     }
  22.     /* where should we start looking for spaces */
  23.     if (linelen > 50)
  24.         lookspace = linelen - 10 ;
  25.     else 
  26.         lookspace = linelen -5 ;
  27.     if(lookspace <5)
  28.         lookspace == linelen ;
  29.  
  30.     i = 0 ;
  31.     while ((ch = getchar()) != EOF)
  32.     {
  33.         /* we have a newline */
  34.         if (ch == '\n')
  35.         {
  36.             putchar(ch) ;    
  37.             i = 0 ;
  38.         }
  39.         /* we have reached the limit, force a newline */
  40.         else if (i == linelen)
  41.         {
  42.             putchar('\n') ;
  43.             i = 0 ;
  44.             putchar(ch) ;
  45.         }
  46.         else if ((i > lookspace) && (ch == ' '))
  47.         {
  48.             putchar('\n') ;
  49.             i = 0 ;
  50.             /* we dont put the space here */
  51.         }
  52.         else
  53.         {
  54.             /* the line is OK and we dont have a newline */
  55.             i ++ ;
  56.             putchar(ch) ;
  57.         }
  58.     }
  59. }
  60.