Buridans_donkey/asino.c

292 lines
5.3 KiB
C
Raw Permalink Normal View History

2016-12-08 22:10:33 +01:00
/* Asino, order a list of strings randomly
*
* Copyright (C) 2016 Francesco Mecca
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
2016-11-02 21:56:34 +01:00
#include <stdio.h>
#include <math.h>
#include <sys/poll.h>
#include <limits.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <linux/random.h>
#include <stdlib.h>
#include <string.h>
2016-11-07 19:20:01 +01:00
#include <stdbool.h>
2016-11-02 21:56:34 +01:00
#define MAX_L 1000
struct arg {
char *str;
struct arg *next;
struct arg *prev;
int idx;
};
int
check_pipe()
{
struct pollfd fds;
int ret;
fds.fd = STDIN_FILENO; //stdin file descriptor (0)
fds.events = POLLIN;
ret = poll(&fds, 1, 0);
return ret;
}
struct arg*
new_arg(void)
{
struct arg *a;
if(!(a = malloc(sizeof(struct arg)))){
fprintf(stderr, "Allocation error.\n");
exit(EXIT_FAILURE);
}
a->prev = NULL;
a->next = NULL;
return a;
}
struct arg*
push_arg(struct arg* a, struct arg *args)
{
2016-11-03 14:54:39 +01:00
a->prev = args;
args->next = a;
2016-11-02 21:56:34 +01:00
return a;
}
int
load_arguments(struct arg *args, int idx, char **argv, int argc, struct arg **head)
{
// head is passed and stores the head to the pipeArgs struct
char buf[MAX_L];
struct arg *newA;
if (argc == 1 && check_pipe () == 1) {
// load from poll
if (fgets(buf, MAX_L, stdin) == NULL) {
return idx;
}
if(buf[strlen(buf) - 1] == '\n'){
buf[strlen(buf) - 1] = '\0';
}
} else { // load argv
if (argc == idx + 1) {
// got all the arguments
return idx;
}
strcpy (buf, argv[idx + 1]);
}
newA = new_arg();
newA->str = strdup(buf);
if(args == NULL){
args = newA;
*head = args;
args->idx = idx;
} else {
args = push_arg(newA, args);
args->idx = idx; // should pass index on push_arg
}
return load_arguments(args, idx +1, argv, argc, head);
}
int
2016-11-07 19:20:01 +01:00
get_randN(bool randFlag)
2016-11-02 21:56:34 +01:00
{
int seed;
2016-11-07 19:20:01 +01:00
syscall(SYS_getrandom, &seed, sizeof(int), randFlag);
2016-11-02 21:56:34 +01:00
return abs(seed);
}
int
check_alloc(char *a)
{
if(!a){
return 1;
}
return 0;
}
struct arg *
get_element_node (struct arg *a, int idx)
{
while (idx != a->idx) {
2016-11-03 14:54:39 +01:00
a = a->next;
2016-11-02 21:56:34 +01:00
}
return a;
}
void
swap_elements (struct arg **elements, int x, int y)
{
char *strX, *strY;
struct arg *eX, *eY;
/*strX = get_element_str (elements[0], x);*/
/*strY = get_element_str (elements[0], y);*/
eX = get_element_node (elements[0], x);
eY = get_element_node (elements[0], y);
strX = strdup (eX->str);
strY = strdup (eY->str);
// start swap
free (eX->str);
eX->str = strdup (strY);
free (eY->str);
eY->str = strdup (strX);
free (strX);
free (strY);
}
struct arg *
2016-11-07 19:20:01 +01:00
shuffle_elements(struct arg *elements, int nElements, bool randFlag)
2016-11-02 21:56:34 +01:00
{
int i, j;
double randN;
for(i=nElements-1; i>0; i--){
2016-11-07 19:20:01 +01:00
randN = (double) get_randN(randFlag)/(INT_MAX/(i)) ;
2016-11-02 21:56:34 +01:00
j = round(randN);
if(i != j){
swap_elements (&elements, i, j);
}
}
return elements;
}
void
2016-11-07 19:20:01 +01:00
print_elements(struct arg *elements, bool numbFlag)
2016-11-02 21:56:34 +01:00
{
while (elements != NULL) {
2016-11-07 19:20:01 +01:00
if (numbFlag == true) {
printf ("%d. ", elements->idx + 1);
}
printf ("%s\n", elements->str);
2016-11-03 14:54:39 +01:00
elements = elements->next;
2016-11-02 21:56:34 +01:00
}
}
void
2016-11-07 19:20:01 +01:00
free_elements (struct arg *ptr)
2016-11-02 21:56:34 +01:00
{
struct arg *tmp;
while (ptr != NULL) {
tmp = ptr;
2016-11-03 14:54:39 +01:00
ptr = ptr->next; // :^)
2016-11-02 21:56:34 +01:00
free (tmp->str);
free (tmp);
}
}
2016-11-07 19:20:01 +01:00
struct cli_t {
int argc;
char **argv;
bool random;
bool numbers;
};
struct cli_t
parse_args (const int argc, char ** argv)
{
/*
* this function parses the args
* return cli_t struct that contains all the flags used
* parameters:
* --urandom, -u: no arguments, uses /dev/urandom instead of /dev/random
* --no-numbers, -n: no arguments, disable line numbers
*/
int i, pos = 1;
struct cli_t cli;
cli.random = GRND_NONBLOCK;
cli.numbers = true;
cli.argc = argc;
cli.argv = calloc (argc , sizeof (char *));
for (i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'n':
cli.numbers = false;
cli.argc--;
break;
case 'u':
cli.random = GRND_RANDOM;
cli.argc--;
default:
// long options
if (strcmp (argv[i], "--no-numbers") == 0) {
cli.numbers = false;
cli.argc--;
} else if (strcmp (argv[i], "--urandom") == 0) {
cli.random = GRND_RANDOM;
cli.argc--;
}
}
} else {
cli.argv[pos] = strdup (argv[i]);
pos++;
}
}
return cli;
}
void
free_cli (struct cli_t cli)
{
int i;
for (i = 0; i < cli.argc; ++i) {
if (cli.argv[i] != NULL) {
free (cli.argv[i]);
}
}
free (cli.argv);
}
2016-11-02 21:56:34 +01:00
int
main(int argc, char *argv[])
{
int nElements;
struct arg *elements = NULL, *head = NULL;
2016-11-07 19:20:01 +01:00
struct cli_t cli;
2016-11-02 21:56:34 +01:00
2016-11-07 19:20:01 +01:00
cli = parse_args (argc, argv);
2016-11-02 21:56:34 +01:00
//check argv
2016-11-07 19:20:01 +01:00
nElements = load_arguments(elements, 0, cli.argv, cli.argc, &head); // recursive polling
2016-11-02 21:56:34 +01:00
// now head stores the beginning of the list
2016-11-07 19:20:01 +01:00
if((head = shuffle_elements(head, nElements, cli.random))){
print_elements(head, cli.numbers);
free_elements (head);
2016-11-02 21:56:34 +01:00
}
2016-11-07 19:20:01 +01:00
free_cli (cli);
2016-11-02 21:56:34 +01:00
return 0;
}