Esempio n. 1
0
int validate(grid board, int possible, int i, int j) {
    int valid_rows = check_row(board, possible, i, j);
    int valid_cols = check_column(board, possible, i, j);
    int valid_square = check_square(board, possible, i, j);

    return valid_rows && valid_cols && valid_square;
}
Esempio n. 2
0
static int circular_buffer_get(lua_State* lua)
{
    circular_buffer* cb = check_circular_buffer(lua, 3);
    int row             = check_row(lua, cb, 2, 0);
    int column          = check_column(lua, cb, 3);

    if (row != -1) {
        lua_pushnumber(lua, cb->m_values[(row * cb->m_columns) + column]);
    } else {
        lua_pushnil(lua);
    }
    return 1;
}
Esempio n. 3
0
void place_queen(int row){

	if(row == 8){
		print_column(++num);
		return;
	}

	for(int i = 0; i < 8; ++i){
		column_row[row] = i;
		if(check_column(row)){
			place_queen(row + 1);
		}
	}
}
Esempio n. 4
0
static int circular_buffer_set(lua_State* lua)
{
    circular_buffer* cb = check_circular_buffer(lua, 4);
    int row             = check_row(lua, cb, 2, 1); // advance the buffer
                                                    // forward if necessary
    int column          = check_column(lua, cb, 3);
    double value        = luaL_checknumber(lua, 4);

    if (row != -1) {
        cb->m_values[(row * cb->m_columns) + column] = value;
        lua_pushnumber(lua, value);
    } else {
        lua_pushnil(lua);
    }
    return 1;
}
Esempio n. 5
0
int game_end(Board board)
{
	for (int x = 0; x < board.dimx; x++)
	{
		if (check_row(board,x))
			return 1;
	}
	for (int y = 0; y < board.dimy; y++)
	{
		if (check_column(board,y))
			return 1;
	}
	if (check_diagonals(board))
		return 1;
		
	if (board.used_spaces == board.dimx * board.dimy)
		return 2;	
		
	return 0;
}
Esempio n. 6
0
static bool is_won(const game_t *game, const int x, const int y)
{
	const int min_win_count = game->grid_size - 1;
	int count;

	count = check_column(game, x, y);
	if (count >= min_win_count)
		return true;

	count = check_row(game, x, y);
	if (count >= min_win_count)
		return true;

	count = check_diagonal_1(game, x, y);
	if (count >= min_win_count)
		return true;

	count = check_diagonal_2(game, x, y);
	if (count >= min_win_count)
		return true;

	return false;
}
Esempio n. 7
0
static int circular_buffer_set_header(lua_State* lua)
{
    circular_buffer* cb = check_circular_buffer(lua, 4);
    int column          = check_column(lua, cb, 2);
    const char* name    = luaL_checkstring(lua, 3);
    const char* type    = luaL_checkstring(lua, 4);

    strncpy(cb->m_headers[column].m_name, name, COLUMN_NAME_SIZE - 1);
    for (int i = 0; i < MAX_TYPE; ++i) {
        if (strcmp(type, column_type_names[i]) == 0) {
            cb->m_headers[column].m_type = i;

            char* n = cb->m_headers[column].m_name;
            for (int j = 0; n[j] != 0; ++j) {
                if (!isalnum(n[j])) {
                    n[j] = '_';
                }
            }
            break;
        }
    }
    lua_pushinteger(lua, column + 1); // return the 1 based Lua column
    return 1;
}
Esempio n. 8
0
static int command_userlog(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct log_data* ldata = (struct log_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	size_t argnum = list_size(cmd->args);
	struct plugin_command_arg_data* arg1 = NULL;
	struct plugin_command_arg_data* arg2 = NULL;
	struct plugin_command_arg_data* arg3 = NULL;
	
	if (argnum == 3)
	{
		arg1 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer);
		arg2 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string);
		arg3 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string);
	}
	else
	{
		if (argnum == 2)
		{
			cbuf_append_format(buf, "*** %s: Missing search pattern.", cmd->prefix);
			plugin->hub.send_message(plugin, user, cbuf_get(buf));
			cbuf_destroy(buf);
			return 0;
		}
		if (argnum == 1)
			arg1 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer);
	}

	int lines = arg1 ? arg1->data.integer : 20;
	char* column = arg2 ? arg2->data.string : "";
	char* search = arg3 ? arg3->data.string : "";
	size_t column_len = strlen(column);
	size_t search_len = strlen(search);
	char query[1024];
	sqlite3_stmt *res;
	int error = 0;
	const char *tail;
	size_t count = 0;

	if (lines > 200)
		lines = 200;

	if (search_len && column_len)
	{
		if(!check_column(column))
		{
			cbuf_append_format(buf, "*** %s: Invalid column. Valid columns are: nick, cid, addr, credentials, useragent, message, all.", cmd->prefix);
			plugin->hub.send_message(plugin, user, cbuf_get(buf));
			cbuf_destroy(buf);
			return 0;
		}

		if (strcmp(column, "all") == 0)
		{
			sprintf(query, "SELECT * FROM userlog WHERE nick LIKE '%%%s%%' OR cid LIKE '%%%s%%' OR credentials LIKE '%%%s%%' OR useragent LIKE '%%%s%%' OR addr LIKE '%%%s%%' OR message LIKE '%%%s%%' ORDER BY time DESC LIMIT %d;", search, search, search, search, search, search, lines);
			cbuf_append_format(buf, "*** %s: Search_ing for \"%s\" in all columns.", cmd->prefix, search);
		}
		else
		{
			sprintf(query, "SELECT * FROM userlog WHERE %s LIKE '%%%s%%' ORDER BY time DESC LIMIT %d;", column, search, lines);
			cbuf_append_format(buf, "*** %s: Searching for \"%s\" in column \"%s\".", cmd->prefix, search, column);
		}
	}
	else
	{
		sprintf(query, "SELECT * FROM userlog ORDER BY time DESC LIMIT %d;", lines);
		cbuf_append_format(buf, "*** %s: ", cmd->prefix);
	}

	error = sqlite3_prepare_v2(ldata->db, query, strlen(query), &res, &tail);
    
	while (sqlite3_step(res) == SQLITE_ROW)
	{
		cbuf_append_format(buf, "\n[%s] %s, %s [%s] [%s] \"%s\" - %s", (char*) sqlite3_column_text(res, 6), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 0), (char*) sqlite3_column_text(res, 3), (char*) sqlite3_column_text(res, 2), (char*) sqlite3_column_text(res, 4), (char*) sqlite3_column_text(res, 5));
		count++;
	}

	if (error || count == 0)
	{
		if (search_len && column_len)
			cbuf_append(buf, "\n");
		cbuf_append(buf, "No log entries found.");
	}
	else
		cbuf_append_format(buf, "\n\n%zd entr%s shown", count, count != 1 ? "ies" : "y");

	sqlite3_finalize(res);
  
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
    
	return 0;
}
Esempio n. 9
0
void
arg_printusage (struct getargs *args,
		size_t num_args,
		const char *progname,
		const char *extra_string)
{
    unsigned int i;
    size_t max_len = 0;
    char buf[128];
    int col = 0, columns;

#ifdef HAVE___PROGNAME
    if (progname == NULL)
	progname = __progname;
#endif
    if (progname == NULL)
	progname = "";

#ifdef GETARGMANDOC
    if(getenv("GETARGMANDOC")){
	mandoc_template(args, num_args, progname, extra_string);
	return;
    }
#endif

    columns = 80; /* Always assume that the window is 80 chars wide */
    col = 0;
    col += fprintf (stderr, "Usage: %s", progname);
    for (i = 0; i < num_args; ++i) {
	size_t len = 0;

	if (args[i].long_name) {
	    buf[0] = '\0';
	    strlcat(buf, "[--", sizeof(buf));
	    len += 2;
	    if(args[i].type == arg_negative_flag) {
		strlcat(buf, "no-", sizeof(buf));
		len += 3;
	    }
	    strlcat(buf, args[i].long_name, sizeof(buf));
	    len += strlen(args[i].long_name);
	    len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf), 
			     0, 1, &args[i]);
	    strlcat(buf, "]", sizeof(buf));
	    if(args[i].type == arg_strings)
		strlcat(buf, "...", sizeof(buf));
	    col = check_column(stderr, col, (int)strlen(buf) + 1, columns);
	    col += fprintf(stderr, " %s", buf);
	}
	if (args[i].short_name) {
	    basestring_snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
	    len += 2;
	    len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf), 
			     0, 0, &args[i]);
	    strlcat(buf, "]", sizeof(buf));
	    if(args[i].type == arg_strings)
		strlcat(buf, "...", sizeof(buf));
	    col = check_column(stderr, col, (int)strlen(buf) + 1, columns);
	    col += fprintf(stderr, " %s", buf);
	}
	if (args[i].long_name && args[i].short_name)
	    len += 2; /* ", " */
	max_len = max(max_len, len);
    }
    if (extra_string) {
	col = check_column(stderr, col, (int)strlen(extra_string) + 1, columns);
	fprintf (stderr, " %s\n", extra_string);
    } else
	fprintf (stderr, "\n");
    for (i = 0; i < num_args; ++i) {
	if (args[i].help) {
	    size_t count = 0;

	    if (args[i].short_name) {
		count += fprintf (stderr, "-%c", args[i].short_name);
		print_arg (buf, sizeof(buf), 0, 0, &args[i]);
		count += fprintf(stderr, "%s", buf);
	    }
	    if (args[i].short_name && args[i].long_name)
		count += fprintf (stderr, ", ");
	    if (args[i].long_name) {
		count += fprintf (stderr, "--");
		if (args[i].type == arg_negative_flag)
		    count += fprintf (stderr, "no-");
		count += fprintf (stderr, "%s", args[i].long_name);
		print_arg (buf, sizeof(buf), 0, 1, &args[i]);
		count += fprintf(stderr, "%s", buf);
	    }
	    while(count++ <= max_len)
		putc (' ', stderr);
	    fprintf (stderr, "%s\n", args[i].help);
	}
    }
}
Esempio n. 10
0
void ROKEN_LIB_FUNCTION
arg_printusage_i18n (struct getargs *args,
		     size_t num_args,
		     const char *usage,
		     const char *progname,
		     const char *extra_string,
		     char *(i18n)(const char *))
{
    int i;
    size_t max_len = 0;
    char buf[128];
    int col = 0, columns;
    struct winsize ws;

    if (progname == NULL)
	progname = getprogname();

    if (i18n == NULL)
	i18n = builtin_i18n;

    if(getenv("GETARGMANDOC")){
	mandoc_template(args, num_args, progname, extra_string, i18n);
	return;
    }
    if(get_window_size(2, &ws) == 0)
	columns = ws.ws_col;
    else
	columns = 80;
    col = 0;
    col += fprintf (stderr, "%s: %s", usage, progname);
    buf[0] = '\0';
    for (i = 0; i < num_args; ++i) {
	if(args[i].short_name && ISFLAG(args[i])) {
	    char s[2];
	    if(buf[0] == '\0')
		strlcpy(buf, "[-", sizeof(buf));
	    s[0] = args[i].short_name;
	    s[1] = '\0';
	    strlcat(buf, s, sizeof(buf));
	}
    }
    if(buf[0] != '\0') {
	strlcat(buf, "]", sizeof(buf));
	col = check_column(stderr, col, strlen(buf) + 1, columns);
	col += fprintf(stderr, " %s", buf);
    }

    for (i = 0; i < num_args; ++i) {
	size_t len = 0;

	if (args[i].long_name) {
	    buf[0] = '\0';
	    strlcat(buf, "[--", sizeof(buf));
	    len += 2;
	    if(args[i].type == arg_negative_flag) {
		strlcat(buf, "no-", sizeof(buf));
		len += 3;
	    }
	    strlcat(buf, args[i].long_name, sizeof(buf));
	    len += strlen(args[i].long_name);
	    len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
			     0, 1, &args[i], i18n);
	    strlcat(buf, "]", sizeof(buf));
	    if(args[i].type == arg_strings)
		strlcat(buf, "...", sizeof(buf));
	    col = check_column(stderr, col, strlen(buf) + 1, columns);
	    col += fprintf(stderr, " %s", buf);
	}
	if (args[i].short_name && !ISFLAG(args[i])) {
	    snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
	    len += 2;
	    len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
			     0, 0, &args[i], i18n);
	    strlcat(buf, "]", sizeof(buf));
	    if(args[i].type == arg_strings)
		strlcat(buf, "...", sizeof(buf));
	    col = check_column(stderr, col, strlen(buf) + 1, columns);
	    col += fprintf(stderr, " %s", buf);
	}
	if (args[i].long_name && args[i].short_name)
	    len += 2; /* ", " */
	max_len = max(max_len, len);
    }
    if (extra_string) {
	check_column(stderr, col, strlen(extra_string) + 1, columns);
	fprintf (stderr, " %s\n", extra_string);
    } else
	fprintf (stderr, "\n");
    for (i = 0; i < num_args; ++i) {
	if (args[i].help) {
	    size_t count = 0;

	    if (args[i].short_name) {
		count += fprintf (stderr, "-%c", args[i].short_name);
		print_arg (buf, sizeof(buf), 0, 0, &args[i], i18n);
		count += fprintf(stderr, "%s", buf);
	    }
	    if (args[i].short_name && args[i].long_name)
		count += fprintf (stderr, ", ");
	    if (args[i].long_name) {
		count += fprintf (stderr, "--");
		if (args[i].type == arg_negative_flag)
		    count += fprintf (stderr, "no-");
		count += fprintf (stderr, "%s", args[i].long_name);
		print_arg (buf, sizeof(buf), 0, 1, &args[i], i18n);
		count += fprintf(stderr, "%s", buf);
	    }
	    while(count++ <= max_len)
		putc (' ', stderr);
	    fprintf (stderr, "%s\n", (*i18n)(args[i].help));
	}
    }
}
Esempio n. 11
0
void test_column(int column[HEIGHT], int expected_value) {
    test( check_column(column) == expected_value ? "Pass" : "Fail" );
}
Esempio n. 12
0
int main(int argc, char *argv[])
{
    //Variable declarations
    FILE *fileptr = NULL;
    char tmpstring[MAXLEN+1];
    time_t rawtime;
    struct tm * timeinfo;
    char namegen[1024];
    int k=0;
    
    int status = 0;
    yyin = fopen(argv[1], "r");
    struct config_params params;
    struct storage_record record_temp;
    struct bigstring str;
	int max_keys = 10;
	char keynames[10][100];
    yyparse(&params, &record_temp, &str, &max_keys, keynames, &status);
    
    printf("port number: %d\n", params.server_port);

    printf("status1: %d\n", status);
    if (status == -1)
    {
    	printf("Error in configration files\n");
    	errno = ERR_INVALID_PARAM;
    	exit(EXIT_FAILURE);
    }
    
    printf("status2: %d\n", status);
    
    status = check_column(&params);
    
    printf("status3: %d\n", status);
    
    if (status == -1)
    {
    	printf("Error in configration files\n");
    	errno = ERR_INVALID_PARAM;
    	exit(EXIT_FAILURE);
    }    
     
     
     
    struct city **headlist=(struct city**)malloc(sizeof(struct city*) * MAX_TABLES);
    for(k=0;k<100;k++){
	headlist[k]=(struct city*)malloc(sizeof(struct city));
    }
    //End of variable declarations
    
    if(flag!=1&&LOGGING==2){
    	time ( &rawtime );
    	timeinfo = localtime ( &rawtime );
    	sprintf(namegen,"Server-%.4d-%.2d-%.2d-%.2d-%.2d-%.2d.log",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
    	fileptr=fopen(namegen,"w");
    	flag=1;
    }
    
    // Process command line arguments.
    //This program expects exactly one argument: the config file name.
    assert(argc > 0);
    if (argc != 2){
	if(LOGGING==2){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    sprintf(tmpstring,"Usage %s <config_file>\n",argv[0]);
	    logger(fileptr,namegen);//Timestamp
	    logger(fileptr,tmpstring);
	}
	else if(LOGGING==1){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    printf("%s",namegen);//Timestamp
	}
	
	printf("Usage %s <config_file>\n", argv[0]);
	exit(EXIT_FAILURE);
    }
    
    if (status != 0) {
	if(LOGGING==2){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    sprintf(tmpstring,"Error processing config file.\n");
	    logger(fileptr,namegen);//Timestamp
	    logger(fileptr,tmpstring);
	}
	else if(LOGGING==1){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    printf("%s",namegen);//Timestamp
	}
	
	printf("Error processing config file.\n");
	exit(EXIT_FAILURE);
    }
    
    if(LOGGING==2){
	sprintf(tmpstring,"Server on %s:%d\n",params.server_host, params.server_port);
	time(&rawtime);
	timeinfo=localtime(&rawtime);
	sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	logger(fileptr,namegen);//Timestamp
	logger(fileptr,tmpstring);
    }
    else if(LOGGING==1){
	time(&rawtime);
	timeinfo=localtime(&rawtime);
	sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	printf("%s",namegen);//Timestamp
	printf("Server on %s:%d\n",params.server_host,params.server_port);
    }
    
    
    
    // Create a socket.
    int listensock = socket(PF_INET, SOCK_STREAM, 0);
    
    if (listensock < 0) {
	if(LOGGING==2){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    sprintf(tmpstring,"Error creating socket.\n");
	    logger(fileptr,namegen);//Timestamp
	    logger(fileptr,tmpstring);
	}
	else if(LOGGING==1){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    printf("%s",namegen);//Timestamp
	}
	
	printf("Error creating socket.\n");
	exit(EXIT_FAILURE);
    }
    
    
    
    // Allow listening port to be reused if defunct.
    int yes = 1;
    status = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
    
    if (status != 0) {
	
	if(LOGGING==2){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    sprintf(tmpstring,"Error configuring socket.\n");
	    logger(fileptr,namegen);//Timestamp
	    logger(fileptr,tmpstring);
	}
	else if(LOGGING==1){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    printf("%s",namegen);//Timestamp
	}
	
	printf("Error configuring socket.\n");
	exit(EXIT_FAILURE);
    }
    
    
    
    // Bind it to the listening port.
    struct sockaddr_in listenaddr;
    memset(&listenaddr, 0, sizeof listenaddr);
    listenaddr.sin_family = AF_INET;
    listenaddr.sin_port = htons(params.server_port);
    inet_pton(AF_INET, params.server_host, &(listenaddr.sin_addr)); // bind to local IP address
    status = bind(listensock, (struct sockaddr*) &listenaddr, sizeof listenaddr);
    if (status != 0) {
	
	if(LOGGING==2){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    sprintf(tmpstring,"Error binding socket.\n");
	    logger(fileptr,namegen);//Timestamp
	    logger(fileptr,tmpstring);
	}
	else if(LOGGING==1){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    printf("%s",namegen);//Timestamp
	}
	
	printf("Error binding socket.\n");
	exit(EXIT_FAILURE);
    }
    
    
    
    // Listen for connections.
    status = listen(listensock, MAX_LISTENQUEUELEN);
    if (status != 0) {
	
	if(LOGGING==2){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    sprintf(tmpstring,"Error listening on socket.\n");
	    logger(fileptr,namegen);//Timestamp
	    logger(fileptr,tmpstring);
	}
	else if(LOGGING==1){
	    time(&rawtime);
	    timeinfo=localtime(&rawtime);
	    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	    printf("%s",namegen);//Timestamp
	}
	
	printf("Error listening on socket.\n");
	exit(EXIT_FAILURE);
    }
    
    
    
    if (params.option == 0)
	{
	    // Listen loop.
	    int wait_for_connections = 1;
	    
	    while (wait_for_connections) {
		// Wait for a connection.
		struct sockaddr_in clientaddr;
		socklen_t clientaddrlen = sizeof clientaddr;
		int clientsock = accept(listensock, (struct sockaddr*)&clientaddr, &clientaddrlen);
		
		
		if (clientsock < 0) {	    
		    if(LOGGING==2){
			time(&rawtime);
			timeinfo=localtime(&rawtime);
			sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
			sprintf(tmpstring,"Error accepting a connection.\n");
			logger(fileptr,namegen);//Timestamp
			logger(fileptr,tmpstring);
		    }
		    else if(LOGGING==1){
			time(&rawtime);
			timeinfo=localtime(&rawtime);
			sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
			printf("%s",namegen);//Timestamp
		    }
		    
		    printf("Error accepting a connection.\n");
			exit(EXIT_FAILURE);
		}
		

		if(LOGGING==2){
		    sprintf(tmpstring,"Got a connection from %s:%d\n",inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
		    time(&rawtime);
		    timeinfo=localtime(&rawtime);
		    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
		    logger(fileptr,namegen);//Timestamp
		    logger(fileptr,tmpstring);
		}
		else if(LOGGING==1){
		    time(&rawtime);
		    timeinfo=localtime(&rawtime);
		    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
		    printf("%s",namegen);//Timestamp
		    printf("Got a connection from %s:%d\n",inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
		}
		
		int auth_success = 0;
		
		// Get commands from client.
		int wait_for_commands = 1;
		do {
		    // Read a line from the client.
		    char cmd[MAX_CMD_LEN];
		    int status = recvline(clientsock, cmd, MAX_CMD_LEN);
		    
		    if (status != 0) {
			// Either an error occurred or the client closed the connection.
			wait_for_commands = 0;
		    }
			else {
			    // Handle the command from the client.
			    //printf("OUTSIDE %p", headlist);
			    int status = handle_command(clientsock, cmd, fileptr, &params, headlist, &auth_success);
			    
			    if (status != 0)
				wait_for_commands = 0; // Oops.  An error occured.
			    
			}
		    
		} while (wait_for_commands);
		
		// Close the connection with the client.
		close(clientsock);
		//LOG(("Closed connection from %s:%d.\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port));
		if(LOGGING==2){
		    sprintf(tmpstring, "Closed connection from %s:%d\n",inet_ntoa(clientaddr.sin_addr),clientaddr.sin_port);
		    time(&rawtime);
		    timeinfo=localtime(&rawtime);
		    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
		    logger(fileptr,namegen);//Timestamp
		    logger(fileptr,tmpstring);
		}
		else if(LOGGING==1){
		    time(&rawtime);
			timeinfo=localtime(&rawtime);
			sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
			printf("%s",namegen);//Timestamp
			printf("Closed connection from %s:%d\n",inet_ntoa(clientaddr.sin_addr),clientaddr.sin_port);
		}
		
		auth_success = 0;
		
	    }
	    // Stop listening for connections.
	    close(listensock);
	    //free(tmpstring);
	    return EXIT_SUCCESS;    	
	}
    else if (params.option == 1)
	{
	    // Allocate threads pool
	    int i;
	    for (i = 0; i!=MAX_CONNECTIONS; ++i)
		{
		    runtimeThreads[i] = malloc( sizeof( struct _ThreadInfo ) );    
		}
	    
	    // Listen loop.
	    int wait_for_connections = 1;
	    
	    while (wait_for_connections) {
		// Wait for a connection.
		ThreadInfo tiInfo = getThreadInfo(); 
		tiInfo->clientaddrlen = sizeof(struct sockaddr_in); 		
		tiInfo->clientsock = accept(listensock, (struct sockaddr*)&tiInfo->clientaddr, &tiInfo->clientaddrlen);
		
		tiInfo->fileptr = fileptr;
		tiInfo->params = &params;
		tiInfo->headlist = headlist;
		tiInfo->auth_success = 0;
		
		
		if (tiInfo->clientsock < 0) {	    
		    if(LOGGING==2){
			time(&rawtime);
			timeinfo=localtime(&rawtime);
			sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
			sprintf(tmpstring,"Error accepting a connection.\n");
			logger(fileptr,namegen);//Timestamp
			logger(fileptr,tmpstring);
		    }
		    else if(LOGGING==1){
			time(&rawtime);
			timeinfo=localtime(&rawtime);
			sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
			printf("%s",namegen);//Timestamp
		    }
		    
		    printf("Error accepting a connection.\n");
		    exit(EXIT_FAILURE);
		}
		else
		    {
			if(LOGGING==2){
			    sprintf(tmpstring,"Got a connection from %s:%d\n",inet_ntoa(tiInfo->clientaddr.sin_addr), tiInfo->clientaddr.sin_port);
			    time(&rawtime);
			    timeinfo=localtime(&rawtime);
			    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
			    logger(fileptr,namegen);//Timestamp
			    logger(fileptr,tmpstring);
			}
			else if(LOGGING==1){
			    time(&rawtime);
			    timeinfo=localtime(&rawtime);
			    sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
			    printf("%s",namegen);//Timestamp
			    printf("Got a connection from %s:%d\n",inet_ntoa(tiInfo->clientaddr.sin_addr), tiInfo->clientaddr.sin_port);
			}		
			
			pthread_create( &tiInfo->theThread, NULL, threadCallFunction, tiInfo ); 
		    }
	    }	
	    
	    
	    /* At the end, wait until all connections close */ 
	    for (i=topRT; i!=botRT; i = (i+1)%MAX_CONNECTIONS)
		pthread_join(runtimeThreads[i]->theThread, 0 ); 
	    
	    /* Deallocate all the resources */ 
	    for (i=0; i!=MAX_CONNECTIONS; i++)
		free( runtimeThreads[i] );  	
	    
	    close(listensock);
	    return EXIT_SUCCESS;      
	}
    
    return EXIT_SUCCESS; 
}
Esempio n. 13
0
void
arg_printusage (struct getargs *args,
                size_t num_args,
                const char *progname,
                const char *extra_string)
{
    int i;
    size_t max_len = 0;
    char buf[128];
    int col = 0, columns;
    struct winsize ws;

    columns = 80;
    col = 0;
    col += fprintf (stderr, "Usage: %s", progname);
    for (i = 0; i < num_args; ++i) {
        size_t len = 0;

        if (args[i].long_name) {
            buf[0] = '\0';
            strncat(buf, "[--", sizeof(buf));
            len += 2;
            if(args[i].type == arg_negative_flag) {
                strncat(buf, "no-", sizeof(buf));
                len += 3;
            }
            strncat(buf, args[i].long_name, sizeof(buf));
            len += strlen(args[i].long_name);
            len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
                             0, 1, &args[i]);
            strncat(buf, "]", sizeof(buf));
            if(args[i].type == arg_strings)
                strncat(buf, "...", sizeof(buf));
            col = check_column(stderr, col, strlen(buf) + 1, columns);
            col += fprintf(stderr, " %s", buf);
        }
        if (args[i].short_name) {
            snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
            len += 2;
            len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
                             0, 0, &args[i]);
            strncat(buf, "]", sizeof(buf));
            if(args[i].type == arg_strings)
                strncat(buf, "...", sizeof(buf));
            col = check_column(stderr, col, strlen(buf) + 1, columns);
            col += fprintf(stderr, " %s", buf);
        }
        if (args[i].long_name && args[i].short_name)
            len += 2; /* ", " */
        max_len = max(max_len, len);
    }
    if (extra_string) {
        col = check_column(stderr, col, strlen(extra_string) + 1, columns);
        fprintf (stderr, " %s\n", extra_string);
    } else
        fprintf (stderr, "\n");
    for (i = 0; i < num_args; ++i) {
        if (args[i].help) {
            size_t count = 0;

            if (args[i].short_name) {
                count += fprintf (stderr, "-%c", args[i].short_name);
                print_arg (buf, sizeof(buf), 0, 0, &args[i]);
                count += fprintf(stderr, "%s", buf);
            }
            if (args[i].short_name && args[i].long_name)
                count += fprintf (stderr, ", ");
            if (args[i].long_name) {
                count += fprintf (stderr, "--");
                if (args[i].type == arg_negative_flag)
                    count += fprintf (stderr, "no-");
                count += fprintf (stderr, "%s", args[i].long_name);
                print_arg (buf, sizeof(buf), 0, 1, &args[i]);
                count += fprintf(stderr, "%s", buf);
            }
            while(count++ <= max_len)
                putc (' ', stderr);
            fprintf (stderr, "%s\n", args[i].help);
        }
    }
}