Beispiel #1
0
void test_llist_concatenate(void) {
  Llist list1 = NULL;
  Llist list2 = NULL;
  Node node;
  node_alloc(&node);

  for (int i = 0; i < 4; ++i) {
    llist_add(node, &list1);
    node.score += 10;
    llist_add(node, &list2);
    node.score -= 9;
  }

  /* printf("LIST 1 \n\n"); */

  /* int i = 1; */
  /* Element *tmp = list1; */
  /* while (tmp != NULL) { */
  /*   printf("#n = %d\n", i++); */
  /*   node_print(tmp->value); */
  /*   tmp = tmp->next; */
  /* } */

  /* printf("LIST 2 \n\n"); */

  /* i = 1; */
  /* tmp = list2; */
  /* while (tmp != NULL) { */
  /*   printf("#n = %d\n", i++); */
  /*   node_print(tmp->value); */
  /*   tmp = tmp->next; */
  /* } */

  /* llist_concatenate(&list1, list2); */

  /* printf("LIST 1+2\n\n"); */

  /* i = 1; */
  /* tmp = list1; */
  /* while (tmp != NULL) { */
  /*   printf("#n = %d\n", i++); */
  /*   node_print(tmp->value); */
  /*   tmp = tmp->next; */
  /* } */

  llist_free(&list1);
  llist_free(&list2);
  node_free(&node);
}
Beispiel #2
0
void test_llist_shorten(void) {
  Node node;
  Llist list = NULL;
  node_alloc(&node);
  node.score = 0;
  for (int i = 0; i < 50; ++i) {
    llist_add(node, &list);
    node.score++;
  }
  node_free(&node);

  /* int i = 1; */
  /* Element *tmp = list; */
  /* while (tmp != NULL) { */
  /*   printf("#n = %d\n", i++); */
  /*   node_print(tmp->value); */
  /*   tmp = tmp->next; */
  /* } */

  /* printf("%d", llist_shorten(&list, 60)); */
  /* printf("%d", llist_shorten(&list, 30)); */

  /* i = 1; */
  /* tmp = list; */
  /* while (tmp != NULL) { */
  /*   printf("#n = %d\n", i++); */
  /*   node_print(tmp->value); */
  /*   tmp = tmp->next; */
  /* } */
  llist_free(&list);
}
Beispiel #3
0
static void sed_free_and_close_stuff(void)
{
	sed_cmd_t *sed_cmd = G.sed_cmd_head.next;

	llist_free(G.append_head, free);

	while (sed_cmd) {
		sed_cmd_t *sed_cmd_next = sed_cmd->next;

		if (sed_cmd->sw_file)
			xprint_and_close_file(sed_cmd->sw_file);

		if (sed_cmd->beg_match) {
			regfree(sed_cmd->beg_match);
			free(sed_cmd->beg_match);
		}
		if (sed_cmd->end_match) {
			regfree(sed_cmd->end_match);
			free(sed_cmd->end_match);
		}
		if (sed_cmd->sub_match) {
			regfree(sed_cmd->sub_match);
			free(sed_cmd->sub_match);
		}
		free(sed_cmd->string);
		free(sed_cmd);
		sed_cmd = sed_cmd_next;
	}

	free(G.hold_space);

	while (G.current_input_file < G.input_file_count)
		fclose(G.input_file_list[G.current_input_file++]);
}
Beispiel #4
0
static void sed_free_and_close_stuff(void)
{
	sed_cmd_t *sed_cmd = G.sed_cmd_head;

	llist_free(G.append_head, free);

	while (sed_cmd) {
		sed_cmd_t *sed_cmd_next = sed_cmd->next;

		if (sed_cmd->sw_file)
			fclose(sed_cmd->sw_file);

		if (sed_cmd->beg_match) {
			regfree(sed_cmd->beg_match);
			free(sed_cmd->beg_match);
		}
		if (sed_cmd->end_match) {
			regfree(sed_cmd->end_match);
			free(sed_cmd->end_match);
		}
		if (sed_cmd->sub_match) {
			regfree(sed_cmd->sub_match);
			free(sed_cmd->sub_match);
		}
		free(sed_cmd->string);
		free(sed_cmd);
		sed_cmd = sed_cmd_next;
	}

	free(G.hold_space);

	if (G.current_fp)
		fclose(G.current_fp);
}
Beispiel #5
0
static int is_superset(struct pack_list *pl, struct llist *list)
{
	struct llist *diff;

	diff = llist_copy(list);

	while (pl) {
		llist_sorted_difference_inplace(diff, pl->all_objects);
		if (diff->size == 0) { /* we're done */
			llist_free(diff);
			return 1;
		}
		pl = pl->next;
	}
	llist_free(diff);
	return 0;
}
Beispiel #6
0
static void quadtree_node_free(quadtree_node *qn) {
	if (!qn) {
		return;
	}

	llist_free(qn->points);
	free(qn->children);
	free(qn);
}
Beispiel #7
0
void test_llist_add(void) {
  Llist list = NULL;
  Node node;
  node_alloc(&node);
  node.score = 150;
  mpz_set_str(*(node.data), "1234", 10);

  /* list should be NULL */
  CU_ASSERT_PTR_NULL(list);

  /* Add the node to the list */
  llist_add(node, &list);

  /* Look if the data contained in the list are the same than the node added */
  CU_ASSERT_TRUE(node_is_equal(list->value, node));

  /* int i = 1; */
  /* Element *tmp = list; */
  /* while (tmp != NULL) { */
  /*   printf("#n = %d\n", i++); */
  /*   node_print(tmp->value); */
  /*   tmp = tmp->next; */
  /* } */

  /* Add a new node */
  node.score++;
  mpz_set_str(*(node.data), "5678", 10);
  llist_add(node, &list);

  /* Look if the node has been added BEFORE the first one
     (because his score is higher) */
  CU_ASSERT_TRUE(node_is_equal(list->value, node));

  /* Add another node */
  node.score -= 2;
  llist_add(node, &list);

  /* Look if the node at the top has not changed
     (because his score is lower) */
  node.score += 2;
  CU_ASSERT_TRUE(node_is_equal(list->value, node));

  /* i = 0; */
  /* tmp = list; */
  /* while (tmp != NULL) { */
  /*   printf("#n = %d\n", i++); */
  /*   node_print(tmp->value); */
  /*   tmp = tmp->next; */
  /* } */

  llist_free(&list);
  node_free(&node);
}
Beispiel #8
0
int main(void) 
{

	bash ls;
	struct llist *ls_list;

	bash_exec(&ls, "cat /var/log/syslog");
	ls_list = bash_tolist(&ls);
	llist_foreach(ls_list, printstring);
	llist_free(ls_list);
	bash_close(&ls);
	return 0;
}
Beispiel #9
0
void test_llist_length(void) {
  Node node;
  Llist list = NULL;
  node_alloc(&node);
  node.score = 0;
  for (int i = 0; i < 50; ++i) {
    llist_add(node, &list);
    node.score++;
  }
  node_free(&node);

  CU_ASSERT_EQUAL(llist_length(list), 50);

  llist_free(&list);
}
int main(int argc, char* argv[]) {

	int ary[] = {0,1,2,3,4,5,6};

	llist* l = ary2llist( ary , 7 );
	
	int k = 2;
	if ( argc > 1) {
		k = atoi(argv[1]);
	}
	
	printf( "%i -> %i\n", k, index_from_end(l, k)->val  );
	printf( "%i -> %i\n", k, index_from_end_iter(l, k)->val  );

	llist_free(l);

	exit(0);

}
Beispiel #11
0
void test_llist_free(void) {
  Llist list = NULL;
  Node arc1;
  Node arc2;
  node_alloc(&arc1);
  node_alloc(&arc2);
  arc1.score = 150;
  arc2.score = 151;

  /* Add something in order to suppress it later */
  llist_add(arc1, &list);
  llist_add(arc2, &list);

  /* Free the list */
  llist_free(&list);

  /* Test if llist_free returns a NULL pointing list */
  CU_ASSERT_PTR_NULL(list);

  node_free(&arc1);
  node_free(&arc2);
}
Beispiel #12
0
// Note, contents aren't freed
void list_free(list *lst){
  llist_free(lst->head);
  free(lst); lst = NULL;
}
Beispiel #13
0
// Note: contents aren't freed
void llist_free(llist *lst) {
  if(lst==NULL) return;
  llist *next = lst->next;
  free(lst); lst = NULL;
  llist_free(next);
}	
Beispiel #14
0
int main(int argc, char **argv)
{
    llist_t *paths,*src,*todo;
    set_t *incl;
    map_t *deps;

    if (argc < 2) {
        fprintf(stderr,"FastDep v%s for LAMMPS\n"
                "Usage: %s [-I <path> ...] -- <src1> [<src2> ...]\n",
                version,argv[0]);
        fprintf(stderr,"Supported extensions: %s, %s, %s\n",
                extensions[0], extensions[1], extensions[2]);
        return 1;
    }

    /* hash tables for all known included files and dependencies
     * we guesstimate a little over 2x as many entries as sources. */
    incl = set_init(2*argc);
    deps = map_init(2*argc);

    /* list of include search paths. prefixed by "." and "..". */
    paths = llist_init();
    llist_append(paths,".");
    llist_append(paths,"..");

    while (++argv, --argc > 0) {
        if (strncmp(*argv, "-I", 2) == 0) {
            if ((*argv)[2] != '\0') {
                llist_append(paths,trim_path(*argv+2));
            } else {
                ++argv;
                --argc;
                if (argc > 0) {
                    if (strcmp(*argv,"--") == 0) {
                        break;
                    } else {
                        llist_append(paths,trim_path(*argv));
                    }
                }
            }
        } else if (strcmp(*argv,"--") == 0) {
            break;
        } /* ignore all unrecognized arguments before '--'. */
    }

    src = llist_init();
    while (++argv, --argc > 0) {
        llist_append(src,*argv);
    }

    /* process files to look for includes */
    todo = llist_init();
    find_includes(src->head,todo,paths,incl,deps);
    find_includes(todo->head,todo,paths,incl,deps);
    llist_free(todo);

    fprintf(stdout,"# FastDep v%s for LAMMPS\n",version);
    fputs("# Search path: ",stdout);
    llist_print(paths);
    fprintf(stdout,"# % 5d sources\n# % 5d includes\n# % 5d depfiles\n",
            llist_size(src),set_size(incl),map_size(deps));

    set_free(incl);
    do_depend(src->head,deps);

    llist_free(src);
    llist_free(paths);
    map_free(deps);
    return 0;
}
Beispiel #15
0
static void delete_block_backed_filesystems(void)
{
	llist_free(fslist, free);
}
Beispiel #16
0
void hash_map_free(hash_map *hm) {
    for(int i=0; i<hm->hashLength; i++) 
        llist_free(hm->boxes[i]);
    free(hm->boxes);
}
Beispiel #17
0
int main() {
    running = 1;
    signal(SIGINT, interrupt_handler);
    printf("Starting server\n");

    if(!init_server_socket(&sock, PORT))
        return -1;

    //A linked list might not be the most efficient for this
    llist client_sessions;
    llist_init(&client_sessions);

    queue message_queue;
    queue_init(&message_queue);

    //TODO: initialise thread pool
    const int num_workers = 4;
    pthread_t workers[num_workers];
    for(int i=0; i<num_workers; i++) {
        pthread_create(&workers[i], NULL, worker_run, (void*)&message_queue);
    }
    
    //select stuff
    fd_set readsocketset;
    fd_set writesocketset;
    fd_set errorsocketset;
    struct timeval timeout;

    while(running) {
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
        
        printf("1\n");
        build_socket_list(&client_sessions, &readsocketset);
        FD_ZERO(&readsocketset);
        FD_SET(sock, &readsocketset);

        int s = select(client_sessions.len, &readsocketset, 0, 0, &timeout);
        printf("2\n");
        if(s < 0) {
            printf("ERROR: Select error\n");
            exit(1);
        }
        //if we have a new connection create a new session
        if(FD_ISSET(sock, &readsocketset)) {
            int csock = check_for_connections(sock);
            session clientSession;
            session_init(&clientSession, csock);
            llist_append(&client_sessions, (void*)&clientSession);
        }

        printf("2\n");
        //check if each session exists in the read socket thingE
        llist_node *cur = client_sessions.head;
        while(cur != NULL) {
            int sock = ((session*)cur->data)->sock;
            //check readsocketset
            if(FD_ISSET(sock, &readsocketset)) 
                client_read_data((session*)cur->data);
            //check writesocketset
            //check errorset
            cur = cur->next;
        }

        //TODO:
        //parse the messages
        //add parsed message to the queue

        //send messages on the queue (Should this be done here?)
    }
    printf("Exiting..\n");
    
    //free memory
    llist_node *cur = client_sessions.head;
    while(cur != NULL) {
        session *sess = (session*)cur->data;
        session_end(sess);
        free(sess);
        cur = cur->next;
    }
    llist_free(&client_sessions);
    close(sock);

    pthread_exit(NULL);
}
Beispiel #18
0
void fslist_cleanup(fslist_p fsl_p)
{
	llist_free(fsl_p->list, fs_free);
}
Beispiel #19
0
int last_main(int argc UNUSED_PARAM, char **argv)
{
	struct utmpx ut;
	const char *filename = _PATH_WTMP;
	llist_t *zlist;
	off_t pos;
	time_t start_time;
	time_t boot_time;
	time_t down_time;
	int file;
	smallint going_down;
	smallint boot_down;

	/*opt =*/ getopt32(argv, "Wf:" /* "H" */, &filename);
#ifdef BUT_UTIL_LINUX_LAST_HAS_NO_SUCH_OPT
	if (opt & LAST_OPT_H) {
		/* Print header line */
		if (opt & LAST_OPT_W) {
			printf(HEADER_FORMAT, HEADER_LINE_WIDE);
		} else {
			printf(HEADER_FORMAT, HEADER_LINE);
		}
	}
#endif

	file = xopen(filename, O_RDONLY);
	{
		/* in case the file is empty... */
		struct stat st;
		fstat(file, &st);
		start_time = st.st_ctime;
	}

	time(&down_time);
	going_down = 0;
	boot_down = NORMAL; /* 0 */
	zlist = NULL;
	boot_time = 0;
	/* get file size, rounding down to last full record */
	pos = xlseek(file, 0, SEEK_END) / sizeof(ut) * sizeof(ut);
	for (;;) {
		pos -= (off_t)sizeof(ut);
		if (pos < 0) {
			/* Beyond the beginning of the file boundary =>
			 * the whole file has been read. */
			break;
		}
		xlseek(file, pos, SEEK_SET);
		xread(file, &ut, sizeof(ut));
		/* rewritten by each record, eventially will have
		 * first record's ut_tv.tv_sec: */
		start_time = ut.ut_tv.tv_sec;

		switch (get_ut_type(&ut)) {
		case SHUTDOWN_TIME:
			down_time = ut.ut_tv.tv_sec;
			boot_down = DOWN;
			going_down = 1;
			break;
		case RUN_LVL:
			if (is_runlevel_shutdown(&ut)) {
				down_time = ut.ut_tv.tv_sec;
				going_down = 1;
				boot_down = DOWN;
			}
			break;
		case BOOT_TIME:
			strcpy(ut.ut_line, "system boot");
			show_entry(&ut, REBOOT, down_time);
			boot_down = CRASH;
			going_down = 1;
			break;
		case DEAD_PROCESS:
			if (!ut.ut_line[0]) {
				break;
			}
			/* add_entry */
			llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
			break;
		case USER_PROCESS: {
			int show;

			if (!ut.ut_line[0]) {
				break;
			}
			/* find_entry */
			show = 1;
			{
				llist_t *el, *next;
				for (el = zlist; el; el = next) {
					struct utmpx *up = (struct utmpx *)el->data;
					next = el->link;
					if (strncmp(up->ut_line, ut.ut_line, __UT_LINESIZE) == 0) {
						if (show) {
							show_entry(&ut, NORMAL, up->ut_tv.tv_sec);
							show = 0;
						}
						llist_unlink(&zlist, el);
						free(el->data);
						free(el);
					}
				}
			}

			if (show) {
				int state = boot_down;

				if (boot_time == 0) {
					state = LOGGED;
					/* Check if the process is alive */
					if ((ut.ut_pid > 0)
					 && (kill(ut.ut_pid, 0) != 0)
					 && (errno == ESRCH)) {
						state = GONE;
					}
				}
				show_entry(&ut, state, boot_time);
			}
			/* add_entry */
			llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
			break;
		}
		}

		if (going_down) {
			boot_time = ut.ut_tv.tv_sec;
			llist_free(zlist, free);
			zlist = NULL;
			going_down = 0;
		}
	}

	if (ENABLE_FEATURE_CLEAN_UP) {
		llist_free(zlist, free);
	}

	printf("\nwtmp begins %s", ctime(&start_time));

	if (ENABLE_FEATURE_CLEAN_UP)
		close(file);
	fflush_stdout_and_exit(EXIT_SUCCESS);
}
Beispiel #20
0
void list_empty(struct list *list) {
  if (list->first) llist_free(list->first);
  list->size = 0;
}
Beispiel #21
0
int pidof_main(int argc UNUSED_PARAM, char **argv)
{
	unsigned first = 1;
	unsigned opt;
#if ENABLE_FEATURE_PIDOF_OMIT
	llist_t *omits = NULL; /* list of pids to omit */
	opt_complementary = "o::";
#endif

	/* do unconditional option parsing */
	opt = getopt32(argv, ""
			IF_FEATURE_PIDOF_SINGLE ("s")
			IF_FEATURE_PIDOF_OMIT("o:", &omits));

#if ENABLE_FEATURE_PIDOF_OMIT
	/* fill omit list.  */
	{
		llist_t *omits_p = omits;
		while (1) {
			omits_p = llist_find_str(omits_p, "%PPID");
			if (!omits_p)
				break;
			/* are we asked to exclude the parent's process ID?  */
			omits_p->data = utoa((unsigned)getppid());
		}
	}
#endif
	/* Looks like everything is set to go.  */
	argv += optind;
	while (*argv) {
		pid_t *pidList;
		pid_t *pl;

		/* reverse the pidlist like GNU pidof does.  */
		pidList = pidlist_reverse(find_pid_by_name(*argv));
		for (pl = pidList; *pl; pl++) {
#if ENABLE_FEATURE_PIDOF_OMIT
			if (opt & OPT_OMIT) {
				llist_t *omits_p = omits;
				while (omits_p) {
					if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
						goto omitting;
					}
					omits_p = omits_p->link;
				}
			}
#endif
			printf(" %u" + first, (unsigned)*pl);
			first = 0;
			if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
				break;
#if ENABLE_FEATURE_PIDOF_OMIT
 omitting: ;
#endif
		}
		free(pidList);
		argv++;
	}
	if (!first)
		bb_putchar('\n');

#if ENABLE_FEATURE_PIDOF_OMIT
	if (ENABLE_FEATURE_CLEAN_UP)
		llist_free(omits, NULL);
#endif
	return first; /* 1 (failure) - no processes found */
}