Exemple #1
0
int main(int argc, char *argv[]){
	int listenfd;
//	daemon_init(argv[0], 0);

	g_GroupTable = tableCreate(D2MCED_GROUP_NUMBER, sizeof(struct group));

	if(read_config()<0){
        perror("Error: read_config()");
		return -1;
	}
	//UDP_init(unsigned int ip,unsigned short int port,struct sockaddr_in * sockaddr)
	listenfd = UDP_bind(INADDR_ANY, g_system_conf.demon_port);
    demon_receiver(listenfd);
	tableDestroy(g_GroupTable);
//	closelog();
    return 1;
}
Exemple #2
0
int main()
{	
	char* line;
	line = getNextLine();
	Hashtable table = tableCreate();
	char* author;
	author = findAuthor(line);
	tableInsert(table, author, line);
	free(author);
	printf("%s\n",line);
	
	char* line2;
	while(line2 = getNextLine())
	{
		if(strcmp(line,line2) == 0)		//if line2 = prev line..print ibid
		{
			printf("%s\n","ibid.");
		}
		else
		{
			author = findAuthor(line2);
			char* authorLine;
			authorLine = (char*)tableSearch(table, author);
			if(authorLine != NULL && !strcmp(authorLine, line2))
			{
				printf("%s%s\n","op. cit. ",author);
			}
			else
			{
				tableInsert(table, author, line2);
				printf("%s\n",line2);
			}
			free(author);
		}
		free(line);
		line = strdup(line2);
		free(line2);
	}
	free(line);
	tableDestroy(table);
	return 0;
}
Exemple #3
0
int main_imanager_req(){
	int i, j;
	int index;
	void *buf;
	unsigned int offset;
	unsigned int users_count;
	unsigned int src_node;
	unsigned int seq_number;
	unsigned int node_id;
	struct sm_header *find;
	char *sm_name;
	if(g_group.coordinator.main_id == g_group.node_id)
		return 1;
	//max 33 mutex
	buf = mem_malloc(MAIN_IMAIN_SIZE);
	((struct request_header*)buf)->msg_type = MSG_IMAIN_MANAGER;
	sendRecv(g_group.coordinator.main_id, buf, sizeof(struct request_header),
			buf, MSG_IMAIN_MANAGER);
	if(g_group.sm_table == NULL){
		g_group.sm_table = hashTableCreate(SM_HASH_SIZE);
	}
	
	src_node = ((struct imain_reply*)buf)->req.src_node;
	seq_number = ((struct imain_reply*)buf)->req.src_seq_number;
	offset = sizeof(struct imain_reply);
	//copy share memory data
	for(i=0; i<((struct imain_reply*)buf)->sm_num; i++){
		//mutex name
		sm_name = malloc(((struct main_sm_info*)(buf+offset))->name_len);
		memcpy(sm_name, buf+offset+sizeof(struct main_sm_info), ((struct main_sm_info*)(buf+offset))->name_len);
		sm_name[((struct main_sm_info*)(buf+offset))->name_len] = 0;
		printf("%s", sm_name);
		//search mutex
		find = hashTableSearch(g_group.sm_table, ((struct main_sm_info*)(buf+offset))->hash_id, sm_name);
		if(find == NULL){
			//create new
			find = createSM(((struct main_sm_info*)(buf+offset))->hash_id, sm_name, 
						((struct main_sm_info*)(buf+offset))->home_node, ((struct main_sm_info*)(buf+offset))->size);
			hashTableInsert(g_group.sm_table, (struct hashheader*)find);
		}
		//copy 
		find->count = ((struct main_sm_info*)(buf+offset))->count;
		//copy queue info
		users_count = ((struct main_sm_info*)(buf+offset))->users_count;
		offset += sizeof(struct main_sm_info)+strlen(sm_name);
		if(find->users == NULL)
			find->users = tableCreate(MAX_NODE_NUM, sizeof(unsigned int));
		for(j=0; j< users_count; j++){
			node_id = *((unsigned int*)(buf+offset));
			if(searchNode(find->users, src_node)==-1){
				index = tableGetEmpty(find->users);
				if(index == -1)
					return -1;
				tableAdd(find->users, (void*)&src_node, index);
			}
			offset += sizeof(unsigned int);
		}
	}	
	g_group.coordinator.main_id = g_group.node_id;
	((struct request_header*)buf)->msg_type = MSG_IMAIN_READY;
	((struct request_header*)buf)->seq_number = seq_number;
	sendTo(src_node ,buf, sizeof(struct request_header));
	mem_free(buf);
	return 1;
}
/* opens the provided txtfile, and compares each letter with a table of letters from the base string. 
The function keeps track of whether or not the word is formable. Then when a tokenizer is found the 
word counter updates, and so does the formable word counter, depernding on its status */
void processTokensFromFile(char** base_array, const char** fname_array, const unsigned int num_inputs, const unsigned int* length_array, \
    const unsigned int max_length, const unsigned char silence, const unsigned char tare_setup, const size_t buckets)
 {
    unsigned int char_count, word_count, formable_count, buff_index;
    FILE *input_file;
    char *c_buff=NULL;
    int c;                                             //character returned from fgetc
    Table *base_table, *comparison_table;
    unsigned char is_formable;                 
    unsigned int* index;

    c_buff= calloc(max_length+1, sizeof(char));     //used to build word tokens as read from the provided file
    if (c_buff==NULL){
        printf("Memory allocation failed: char pointer c_buff\n");
        exit(0);
    }

    base_table=tableCreate(TABLE_SIZE);
    comparison_table=tableCreate(TABLE_SIZE);

    fillTable(base_table, base_array[0]);
    index=generateIndex(base_table);                         //used to rebuild only the necessary parts of the table
    copyTableFromIndex(comparison_table, base_table, index);

    
    if (!tare_setup) {
        for(unsigned int i=0; i<num_inputs; ++i) {
            char_count=0, word_count=0, formable_count=0, buff_index=0;
            is_formable=1;                                //assume token is formable until checkChar returns otherwise  
            
            input_file= fopen(fname_array[i], "r");                 
            if(input_file==NULL) {                          //Prevents seg fault crash if there is a problem with the provided file
                printf("Improper file name: %s\n",fname_array[i]);
                continue;
            }

            if(!silence) {
                displayIntro(i+1,fname_array[i]);
            }

            do{
                c = fgetc(input_file);                       //gets next character
                if(isTokenizer(c) || c==EOF) {          
                    if(buff_index != 0){                        //non-negative index + reaching a tokenizer means c_buff contains a token
                        ++word_count;
                        char_count += buff_index+1;         // +1 to account for the tokenizing character causing termination of token reading
                        if(is_formable) {            
                            ++formable_count;
                            if (!silence) {printf("\t%s\n",c_buff);}
                        } 
                        //reset params for next loop and next token
                        buff_index=0, is_formable=1;           
                        copyTableFromIndex(comparison_table, base_table,index);    
                        memset(&c_buff[0],0,(buff_index < max_length) ? buff_index : max_length);        
                    }
                    else {
                        ++char_count;       // A tokenizer char has been found, but no token is being formed
                    }          
                }
                else
                {
                    if(is_formable) { 
                        if(checkChar(c,comparison_table)) {  
                            c_buff[buff_index]=c; 
                        } else {
                            is_formable=0;
                        }    
                    } 
                    ++buff_index;
                }

            } while(c!=EOF);
            --char_count;       //correct for EOF being read for a char
            fclose(input_file);
            reportResults(length_array[0],char_count, word_count, formable_count);
        }
    }    

    tableDestroy(base_table);
    tableDestroy(comparison_table);
    indexDestroy(index);
    free(c_buff);

}
Exemple #5
0
int cli_scanrtf(cli_ctx *ctx)
{
	char* tempname;
	const unsigned char* ptr;
	const unsigned char* ptr_end;
	int ret = CL_CLEAN;
	struct rtf_state state;
	struct stack stack;
	size_t bread;
	table_t* actiontable;
	uint8_t main_symbols[256];
	size_t offset = 0;

	cli_dbgmsg("in cli_scanrtf()\n");

	memset(main_symbols, 0, 256);
	main_symbols['{']=1;
	main_symbols['}']=1;
	main_symbols['\\']=1;

	stack.stack_cnt = 0;
	stack.stack_size = 16;
	stack.elements = 0;
	stack.warned = 0;
	stack.states = cli_malloc(stack.stack_size*sizeof(*stack.states));

	if(!stack.states) {
        cli_errmsg("ScanRTF: Unable to allocate memory for stack states\n");
		return CL_EMEM;
    }

	if(!(tempname = cli_gentemp(ctx->engine->tmpdir)))
	    return CL_EMEM;

	if(mkdir(tempname, 0700)) {
	    	cli_dbgmsg("ScanRTF -> Can't create temporary directory %s\n", tempname);
		free(stack.states);
		free(tempname);
		return CL_ETMPDIR;
	}

	actiontable = tableCreate();
	if((ret = load_actions(actiontable))) {
		cli_dbgmsg("RTF: Unable to load rtf action table\n");
		free(stack.states);
		if(!ctx->engine->keeptmp)
			cli_rmdirs(tempname);
		free(tempname);
		tableDestroy(actiontable);
		return ret;
	}

	init_rtf_state(&state);

	for (offset = 0; (ptr = fmap_need_off_once_len(*ctx->fmap, offset, BUFF_SIZE, &bread)) && bread; offset += bread) {
	    ptr_end = ptr + bread;
	    while(ptr < ptr_end) {
			switch(state.parse_state) {
				case PARSE_MAIN: 
					switch(*ptr++) {
						case '{':
							if(( ret = push_state(&stack,&state) )) {
								cli_dbgmsg("RTF:Push failure!\n");
								SCAN_CLEANUP;
								return ret;
							}
							break;
						case '}':
							if(state.cb_data && state.cb_end)
								if(( ret = state.cb_end(&state, ctx) )) {
									SCAN_CLEANUP;
									return ret;
								}
							if(( ret = pop_state(&stack,&state) )) {
								cli_dbgmsg("RTF:pop failure!\n");
								SCAN_CLEANUP;
								return ret;
							}
							break;
						case '\\':
							state.parse_state = PARSE_CONTROL_;
							break;
						default:
							ptr--;
							{
								size_t i;
								size_t left = ptr_end - ptr;
								size_t use = left;
								for(i = 1;i < left; i++)
									if(main_symbols[ptr[i]]) {
										use = i;
										break;
									}
								if(state.cb_begin) {
									if(!state.cb_data)
										 if(( ret = state.cb_begin(&state, ctx,tempname) )) {
											 SCAN_CLEANUP;
											 return ret;
										}
									if(( ret = state.cb_process(&state, ptr, use) )) {
										if(state.cb_end) {
											state.cb_end(&state,ctx);
										}
										SCAN_CLEANUP;
										return ret;
									}
								}
								ptr += use;
							}
					}
					break;
				case PARSE_CONTROL_:
					if(isalpha(*ptr))  {
						state.parse_state = PARSE_CONTROL_WORD;
						state.controlword_cnt = 0;
					}
					else
						state.parse_state = PARSE_CONTROL_SYMBOL;
					break;
				case PARSE_CONTROL_SYMBOL:
					ptr++;	/* Do nothing */
					state.parse_state = PARSE_MAIN;
					break;
				case PARSE_CONTROL_WORD:
					if(state.controlword_cnt == 32) {
						cli_dbgmsg("Invalid control word: maximum size exceeded:%s\n",state.controlword);
						state.parse_state = PARSE_MAIN;
					}
					else if(isalpha(*ptr))
						state.controlword[state.controlword_cnt++] = *ptr++;
					else {
						if(isspace(*ptr)) {
							state.controlword[state.controlword_cnt++] = *ptr++;
							state.parse_state = PARSE_INTERPRET_CONTROLWORD;
						}
						else if (isdigit(*ptr)) {
							state.parse_state = PARSE_CONTROL_WORD_PARAM;
							state.controlword_param = 0;
							state.controlword_param_sign = 1;
						}
						else if(*ptr == '-') {
							ptr++;
							state.parse_state = PARSE_CONTROL_WORD_PARAM;
							state.controlword_param = 0;
							state.controlword_param_sign = -1;
						}
						else {
							state.parse_state = PARSE_INTERPRET_CONTROLWORD;
						}
					}
					break;
				case PARSE_CONTROL_WORD_PARAM:
					if(isdigit(*ptr)) {
						state.controlword_param = state.controlword_param*10 + *ptr++ - '0';
					}
					else if(isalpha(*ptr)) {
						ptr++;
					}
					else {
						if(state.controlword_param_sign < 0)
							state.controlword_param = -state.controlword_param;
						state.parse_state = PARSE_INTERPRET_CONTROLWORD;
					}
					break;
				case PARSE_INTERPRET_CONTROLWORD:
					{
						int action;

						state.controlword[state.controlword_cnt] = '\0';
						action = tableFind(actiontable, state.controlword);
						if(action != -1) {
							if(state.cb_data && state.cb_end) {/* premature end of previous block */
								state.cb_end(&state,ctx);
								state.cb_begin = NULL;
								state.cb_end = NULL;
								state.cb_data = NULL;
							}
							rtf_action(&state,action);
						}
						state.parse_state = PARSE_MAIN;
						break;
					}
			}
		}
	}

	SCAN_CLEANUP;
	return ret;
}