-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
145 lines (126 loc) · 3.73 KB
/
main.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
08/10/2018
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <argp.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
static void print_numbers(uintmax_t, uintmax_t, uintmax_t);
static uintmax_t cnum(char *);
static void fill_strs(const char *);
static void print_usage(void);
static void generate_table(uintmax_t *, uintmax_t, uintmax_t);
static const char doc[] = "Write a random permutation of the input lines to standard output.\vMandatory arguments to long options are mandatory for short options too.\n";
const char *argp_program_version = "shufi 1.0.0";
static struct argp_option options[] =
{
{ .doc = "" },
{ .name = "input-range", .key = 'i', .arg="LO-HI, e.g: 1-100", .doc = "treat each number LO through HI as an input line" },
{ .name = "head-count", .key = 'n', .arg="COUNT", .doc = "output at most COUNT lines" },
{ .doc = NULL }
};
static char str1[256] = {""};
static char str2[256] = {""};
static uintmax_t res_iters = 10U;
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
(void)state;
switch(key) {
case 'i': fill_strs(arg); break;
case 'n': res_iters = cnum(arg); break;
default: return ARGP_ERR_UNKNOWN;
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
struct argp arg_parser = {
.doc = doc,
.options = options,
.parser = parse_opt
};
argp_parse(&arg_parser, argc, argv, 0, NULL, NULL);
if (argc < 2) {
print_usage();
return EXIT_FAILURE;
}
print_numbers(cnum(str1), cnum(str2), res_iters);
return EXIT_SUCCESS;
}
static void print_numbers(uintmax_t start, uintmax_t iters, uintmax_t restrict_iters) {
time_t t;
uintmax_t arr[10000U] = {0};
uintmax_t z = 0U;
uintmax_t w = 0U;
if (0U == iters) {
iters = 10U;
}
if (-1 == (t = time(NULL))) {
puts("time(NULL) failed");
return;
}
if ((9999U < iters) || (start >= iters)) {
print_usage();
return;
}
srandom((unsigned int)t ^ ((unsigned int)getpid()));
/* Fill the x-to-z range starting
* from 0 to z */
generate_table(arr, iters, start);
/* index the newly created array
and make sure that we dont print
any "0" in here */
for (z = 0U; z < restrict_iters; z++) {
while (0U == (w = arr[(uintmax_t)random() % iters])) {
continue;
}
printf("%" PRIuMAX "\n", w);
}
}
static void generate_table(uintmax_t *table, uintmax_t iters, uintmax_t start) {
uintmax_t x = 0U;
for (; start < iters; start++, x++) {
table[x] = start;
}
}
static void fill_strs(const char *s) {
char *s1 = str1;
char *s2 = str2;
if (255 < (strlen(s) - 1)) {
snprintf(str1, 255, "%s", "1");
snprintf(str2, 255, "%s", "10");
return;
}
for (; *s && *s != '-'; s++) {
*s1++ = *s;
}
*s1 = '\0';
if (*s) {
s++;
}
for (; *s; s++) {
*s2++ = *s;
}
*s2 = '\0';
}
static uintmax_t cnum(char *str) {
return strtoumax(str ? str : "1000", (char **)NULL, 10);
}
static void print_usage(void) {
puts("usage: shufi -i 1-100 -n 10");
}