1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 int
main(argc,argv)5 main(argc, argv)
6 char **argv;
7 {
8     register int c;
9     int xp;
10     int width;
11 
12     if ( argc != 2 ) {
13 	fprintf(stderr, "usage: %s width\n", argv[0]);
14 	exit(1);
15     }
16     else if ( (width=atoi(argv[1])) < 1 ) {
17 	fprintf(stderr, "%s: please set width to > 0\n", argv[0]);
18 	exit(1);
19     }
20 
21 
22     for ( xp = 1; (c = getchar()) != EOF; xp++ ) {
23 	while ( c & 0xC0 ) {
24 	    /* assume that (1) the output device understands utf-8, and
25 	     *             (2) the only c & 0x80 input is utf-8.
26 	     */
27 	    do {
28 		if ( xp <= width )
29 		    putchar(c);
30 	    } while ( (c = getchar()) != EOF && (c & 0x80) && !(c & 0x40) );
31 	    ++xp;
32 	}
33 	if ( c == '\n' )
34 	    xp = 0;
35 	if ( xp <= width )
36 	    putchar(c);
37     }
38     exit(0);
39 }
40