beta-tester for insque.c needed
Hi List,
i rewrote the example from insque.3
the code now uses a zero-size array and has a demo for remque.
i need some feedback if that is working perhaps with other libc version than glibc. (note i tested on arm and x86_64).
cu,
wh
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <search.h>
struct element {
struct element *forward;
struct element *backward;
char payload[];
};static struct element *new_element(char *arg)
{
struct element *e;
e = calloc(sizeof(struct element)+1+strlen(arg),1);
if (e == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
strcpy(e->payload,arg);
return e;
}int main(int argc, char *argv[])
{
struct element *first, *elem, *prev;
int circular, opt, errfnd;
/* The "-c" command-line option can be used to specify that the
list is circular */
errfnd = 0;
circular = 0;
while ((opt = getopt(argc, argv, "c")) != -1) {
switch (opt) {
case 'c':
circular = 1;
break;
default:
errfnd = 1;
break;
}
}
if (errfnd || optind >= argc) {
fprintf(stderr, "Usage: %s [-c] string...\n", argv[0]);
exit(EXIT_FAILURE);
}
/* Create first element and place it in the linked list */
elem = new_element(argv[optind]);
first = elem;
if (circular) {
elem->forward = elem;
elem->backward = elem;
insque(elem, elem);
} else {
insque(elem, NULL);
}
/* Add remaining command-line arguments as list elements */
while (++optind < argc) {
prev = elem;
elem = new_element(argv[optind]);
insque(elem, prev);
}
/* Traverse the list from the start, printing element names */
printf("Traversing completed list:\n");
elem = first;
do {
printf(" %s\n", elem->payload);
elem = elem->forward;
} while (elem != NULL && elem != first);
if (elem == first)
printf("That was a circular list\n");
elem=first;
do
{
struct element *next;
next= elem->forward;
printf("free %s\n", elem->payload);
remque(elem);
free(elem);
if (circular && elem==next )
break;
elem=next;
} while (elem != NULL );
exit(EXIT_SUCCESS);
}