fix format, fix free()

This commit is contained in:
2025-11-05 21:27:38 +01:00
parent 3e2d233ddc
commit 36dff3796d

View File

@@ -37,15 +37,13 @@ int usage() {
"-y --symbols Replace space with -, add non-letters\n" "-y --symbols Replace space with -, add non-letters\n"
"-d --debug Enable debug output\n" "-d --debug Enable debug output\n"
"-v --version Print program version\n" "-v --version Print program version\n"
"-h -? --help Print this help screen\n" "-h -? --help Print this help screen\n");
);
return 1; return 1;
} }
int WMIN, WMAX, humantoss, verbose, dontjump, symbols; int WMIN, WMAX, humantoss, verbose, dontjump, symbols;
int main(int argc, char **argv) {
int main (int argc, char **argv) {
int count = 4; int count = 4;
char *dictfile = NULL; char *dictfile = NULL;
int opt; int opt;
@@ -55,79 +53,80 @@ int main (int argc, char **argv) {
humantoss = verbose = dontjump = symbols = 0; humantoss = verbose = dontjump = symbols = 0;
static struct option longopts[] = { static struct option longopts[] = {
{ "wordcount", required_argument, NULL, 'c' }, {"wordcount", required_argument, NULL, 'c'},
{ "minlen", required_argument, NULL, 'l' }, {"minlen", required_argument, NULL, 'l'},
{ "maxlen", required_argument, NULL, 'm' }, {"maxlen", required_argument, NULL, 'm'},
{ "humantoss", required_argument, NULL, 't' }, {"humantoss", required_argument, NULL, 't'},
{ "dictfile", required_argument, NULL, 'f' }, {"dictfile", required_argument, NULL, 'f'},
{ "dontjump", no_argument, NULL, 'n' }, {"dontjump", no_argument, NULL, 'n'},
{ "symbols", no_argument, NULL, 'y' }, {"symbols", no_argument, NULL, 'y'},
{ "version", no_argument, NULL, 'v' }, {"version", no_argument, NULL, 'v'},
{ "help", no_argument, NULL, 'h' }, {"help", no_argument, NULL, 'h'},
{ "debug", no_argument, NULL, 'd' }, {"debug", no_argument, NULL, 'd'},
}; };
while ((opt = getopt_long(argc, argv, "l:m:tf:c:vh?dny", longopts, NULL)) != -1) { while ((opt = getopt_long(argc, argv, "l:m:tf:c:vh?dny", longopts, NULL)) !=
switch (opt) { -1) {
case 'v': switch (opt) {
fprintf(stderr, "This is %s version %s\n", argv[0], VERSION); case 'v':
return 1; fprintf(stderr, "This is %s version %s\n", argv[0], VERSION);
break; return 1;
case 'h': break;
case '?': case 'h':
return usage(); case '?':
break;
case 'c':
count = atoi(optarg);
break;
case 'l':
WMIN = atoi(optarg);
break;
case 'm':
WMAX = atoi(optarg);
break;
case 't':
humantoss = 1;
break;
case 'y':
symbols = 1;
break;
case 'd':
verbose++;
break;
case 'n':
dontjump = 1;
break;
case 'f':
dictfile = malloc(strlen(optarg));
strncpy(dictfile, optarg, strlen(optarg));
break;
default:
return usage(); return usage();
break; break;
} case 'c':
} count = atoi(optarg);
break;
case 'l':
WMIN = atoi(optarg);
break;
case 'm':
WMAX = atoi(optarg);
break;
case 't':
humantoss = 1;
break;
case 'y':
symbols = 1;
break;
case 'd':
verbose++;
break;
case 'n':
dontjump = 1;
break;
case 'f':
dictfile = malloc(strlen(optarg));
strncpy(dictfile, optarg, strlen(optarg));
break;
default:
return usage();
break;
}
}
if(dictfile == NULL) { if (dictfile == NULL) {
dictfile = STRINGIZE_VALUE_OF(DICTFILE); dictfile = STRINGIZE_VALUE_OF(DICTFILE);
} }
if(dontjump) { if (dontjump) {
WMIN = 0; WMIN = 0;
WMAX = 128; WMAX = 128;
} }
debug(" using dictfile: %s", dictfile); debug(" using dictfile: %s", dictfile);
debug("minimum word length: %d", WMIN); debug("minimum word length: %d", WMIN);
debug("maximum word length: %d", WMAX); debug("maximum word length: %d", WMAX);
if(humantoss) if (humantoss)
debug("user rolls dices"); debug("user rolls dices");
else else
debug("program rolls dices"); debug("program rolls dices");
getwords(dictfile, count); getwords(dictfile, count);
return 0; return 0;
} }
void getwords(char *dictfile, int count) { void getwords(char *dictfile, int count) {
@@ -145,14 +144,14 @@ void getwords(char *dictfile, int count) {
tossed = malloc(count * sizeof(int)); tossed = malloc(count * sizeof(int));
for(i=0; i<count; i++) { for (i = 0; i < count; i++) {
tosses = toss(5, i); tosses = toss(5, i);
one = tosses[0] * 10000; one = tosses[0] * 10000;
two = tosses[1] * 1000; two = tosses[1] * 1000;
three = tosses[2] * 100; three = tosses[2] * 100;
four = tosses[3] * 10; four = tosses[3] * 10;
five = tosses[4]; five = tosses[4];
pos = one + two + three + four + five; pos = one + two + three + four + five;
@@ -161,23 +160,23 @@ void getwords(char *dictfile, int count) {
free(tosses); free(tosses);
} }
if(symbols) if (symbols)
sep = '-'; sep = '-';
for(i=0; i<count-1; i++) { for (i = 0; i < count - 1; i++) {
fprintf(stdout, "%s%c", words[tossed[i]], sep); fprintf(stdout, "%s%c", words[tossed[i]], sep);
} }
fprintf(stdout, "%s", words[tossed[count-1]]); fprintf(stdout, "%s", words[tossed[count - 1]]);
if(symbols) if (symbols)
fprintf(stdout, "%%8"); fprintf(stdout, "%%8");
fprintf(stdout, "\n"); fprintf(stdout, "\n");
free(tossed); free(tossed);
for(i=0; i<6666; i++) for (i = 0; i < 66666; i++)
if(words[i] != NULL) if (words[i] != NULL)
free(words[i]); free(words[i]);
free(words); free(words);