aboutsummaryrefslogtreecommitdiff
path: root/tests/chargen.c
blob: 13ef8f5474e6d0da8c278de441afd4e65c0947e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
  NAME
     chargen - generate a stream of characters
     
  SYNOPSIS
     chargen [-c C] [-l LEN] [-p] [-s N]

  DESCRIPTION
     Produces on standard output a stream of characters.  The stream consists
     of all 256 characters repeated cyclically until total number of characters
     reaches 4096 (or LEN).
     
  OPTIONS
     -c C  Start from ASCII character C

     -l LEN
           Stop when LEN characters have been generated.
     
     -p    Produce only printable characters

     -s N  Start from character with ordinal number N
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>

int
main(int argc, char **argv)
{
    int c;
    int start = 0;
    int mod = UCHAR_MAX;
    size_t i, len = 4096;
    int printable = 0;
    char *p;
    
    while ((c = getopt(argc, argv, "cl:ps:")) != EOF) {
	switch (c) {
	case 'c':
	    start = optarg[0];
	    break;
	case 'p':
	    printable = 1;
	    break;
	case 's':
	    start = atoi(optarg) % UCHAR_MAX;
	    break;
	case 'l':
	    errno = 0;
	    len = strtoul(optarg, &p, 10);
	    if (*p) {
		fprintf(stderr, "bad length (near %s)", p);
		exit(1);
	    } else if (errno) {
		perror("bad length");
		exit(1);
	    }
	    break;
	default:
	    exit(2);
	}
    }

    if (printable && !isprint(start))
	do {
	    start = (start + 1) % mod;
	} while (printable && !isprint(start));
	
    for (i = 0; i < len; i++) {
	putchar(start);
	do {
	    start = (start + 1) % mod;
	} while (printable && !isprint(start));
    }
    return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.