Exemple #1
0
StringLinkedList *
string_linked_list_init(void)
{
    StringLinkedList *sll = malloc(sizeof(StringLinkedList));
    if (sll == NULL) {
        DIE("ERROR: Not enough memory to allocate StringLinkedList")
    }
    sll->head = NULL;
    sll->tail = NULL;
    sll->len = 0;
    
    return sll;
}
static void
parse_version_word(char *word, StringLinkedList *sll)
{
    if (expr == NULL) {
        setup_version_regex();
    }
    
    int ovecsize = 0;
    if (pcre_fullinfo(expr, NULL, PCRE_INFO_CAPTURECOUNT, &ovecsize) != 0) {
        DIE("ERROR: Problem calling pcre_fullinfo")
    }
    ovecsize = (ovecsize + 1) * 3;
    
    int *ovector = calloc(ovecsize, sizeof(int));
    if (ovector == NULL) {
        DIE("ERROR: Not enough memory to allocate ovector")
    }

    int offset = 0;
    int flags = 0;
    char *part;

    while (0 < pcre_exec(expr, 0, word, strlen(word), offset, flags, ovector, ovecsize)) {
        
        part = malloc((WORD_MAX_LEN+1) * sizeof(char));
        if (part == NULL) {
            DIE("ERROR: Not enough memory to allocate word")
        }

        snprintf(part, WORD_MAX_LEN+1, "%.*s", ovector[1]-ovector[0], word+ovector[0]);

        string_linked_list_append(sll, part);
        
        offset = ovector[1];
        flags |= PCRE_NOTBOL;
    }
    
    free(ovector);
}
Exemple #3
0
/*
	Compute reduced heap size by reserving area and rounding down to
	nearest page.
*/
double
computeReserve(double rate, int phases)
{
	assert(rate > 1.0);
	if (phases == 0)	/* not concurrent */
		return 0.0;
	else if (phases == 1) /* concurrent */
		return rate / (rate + 1.0);
	else if (phases == 2) /* concurrent - non-committing, committing */
		return (rate + 1.0) / (rate * rate + rate + 1.0);
	DIE("invalid phases");
	return 0.0;	/* NOTREACHED */
}
Exemple #4
0
int WaitChild(pid_t pid, const char *name) {
  int err, status;

  do {
    err = waitpid(pid, &status, 0);
  } while (err == -1 && errno == EINTR);

  if (err == -1) {
    DIE("wait on %s (pid %d) failed\n", name, pid);
  }

  return status;
}
Exemple #5
0
void add_to_common_env(char *key, char *value)
{
#ifndef EXCLUDE_CGI
    common_cgi_env = realloc(common_cgi_env, (common_cgi_env_count + 2) * (sizeof(char *)));
    if (common_cgi_env== NULL) {
        DIE("Unable to allocate memory for common CGI environment variable.");
    }
    common_cgi_env[common_cgi_env_count] = env_gen_extra(key,value, 0);
    if (common_cgi_env[common_cgi_env_count] == NULL) {
        /* errors already reported */
        DIE("memory allocation failure in add_to_common_env");
    }
    common_cgi_env[++common_cgi_env_count] = NULL;
    /* I find it hard to believe that somebody would actually
     * make 90+ *common* CGI variables, but it's always better
     * to be safe.
     */
    if (common_cgi_env_count > CGI_ENV_MAX) {
        DIE("far too many common CGI environment variables added.");
    }
#endif // !EXCLUDE_CGI
}
Exemple #6
0
static void loadRom()
{
	const char * file = S9xGetFilename(FILE_ROM);

	printf("ROM: %s\n", file);

	if (!Memory.LoadROM(file))
		DIE("Loading ROM failed");
	
	file = S9xGetFilename(FILE_SRAM);
	printf("SRAM: %s\n", file);
	Memory.LoadSRAM(file); 
}
Exemple #7
0
Window::Window(Game* parent)
{
  width = height = 600;
  game = parent;
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  windowHandle = glfwCreateWindow(width, height, "Grass", NULL, NULL);
  glfwSetWindowPos(windowHandle,800,30);
  if (!windowHandle)
    DIE("Failed to create window");
  glfwMakeContextCurrent(windowHandle);
}
Exemple #8
0
int recv_c(int socket, byte *recvbuf, int bufsize) {
    LOG_DEBUG(TAG_CMD, "Receiving command");
    int bytes_total = 0;
    int bytes_received = 0;
    int bytes_missing = CMD_HEADER_LEN;
    uint16_t pl_len = 0;

    // Receive header
    while (bytes_missing > 0) {
        bytes_received = recv(socket, recvbuf+bytes_total, bytes_missing, 0);
        if (bytes_received == 0) {
            return 0;
        } else if (bytes_total > bufsize) {
            DIE(TAG_CMD, "Recvbuf overflow");
        }
        bytes_total += bytes_received;
        bytes_missing -= bytes_received;
    }

    // Check length of the packet and receive more data if needed
    memcpy(&pl_len, recvbuf+CMD_PL_LEN_OFFSET, sizeof(uint16_t));
    pl_len = ntohs(pl_len);
    bytes_received = 0;
    bytes_missing = pl_len;
    while (bytes_missing > 0) {
        bytes_received = recv(socket, recvbuf+bytes_total, bytes_missing, 0);
        if (bytes_received == 0) {
            LOG_WARN(TAG_CMD, "GUI disconnected");
            return 0;
        } else if (bytes_total > bufsize) {
            DIE(TAG_CMD, "Recvbuf overflow");
        }
        bytes_total += bytes_received;
        bytes_missing -= bytes_received;
    }
    
    LOG_DEBUG(TAG_CMD, "Received %d bytes", bytes_total);
    return bytes_total;
}
Exemple #9
0
static void close_files(void)
{
	size_t n_files = sizeof(files) / sizeof(files[0]);
	size_t i;
	int rc;

	for (i = 0; i < n_files; i++) {
		rc = close(fds[i]);
		DIE(rc < 0, "close");
	}

	free(fds);
}
Exemple #10
0
/* based on show_diff_time from eachfile.c */
void show_time(FILE *out, char T, time_t val)
{
    char	buf[ELC_TIMEBUFSIZ];

    putc(T, out);
    putc('(', out);

    if (! strftime(buf, ELC_TIMEBUFSIZ, "%Y%m%d-%H%M%S", localtime(&val)) )
      DIE("strftime");
    fputs(buf, out);

    fputs(") ", out);
}
Exemple #11
0
	static void
flytec_pbrtr(flytec_t *flytec, track_t *track, void (*callback)(void *, const char *), void *data)
{
	char buf[9];
	if (snprintf(buf, sizeof buf, "PBRTR,%02d", track->index) != 8)
		DIE("sprintf", 0);
	flytec_puts_nmea(flytec, buf);
	flytec_expectc(flytec, XOFF);
	char line[1024];
	while (flytec_gets(flytec, line, sizeof line))
		callback(data, line);
	flytec_expectc(flytec, XON);
}
Exemple #12
0
	static void
flytec_puts_nmea(flytec_t *flytec, char *s)
{
	int checksum = 0;
	char *p;
	for (p = s; *p; ++p)
		checksum ^= (unsigned char) *p;
	int len = strlen(s) + 7;
	char *buf = xmalloc(len);
	memset(buf, 0, len);
	if (snprintf(buf, len, "$%s*%02X\r\n", s, checksum) != len - 1)
		DIE("snprintf", 0);
	if (flytec->logfile)
		fprintf(flytec->logfile, "> %s", buf);
	int rc;
	do {
		rc = write(flytec->fd, buf, len);
	} while (rc == -1 && errno == EINTR);
	if (rc == -1)
		DIE("write", errno);
	free(buf);
}
Exemple #13
0
static void create_iocp_accept(void)
{
	BOOL bRet;

	memset(&ac, 0, sizeof(ac));

	/* Create simple socket for acceptance */
	ac.sockfd = socket(PF_INET, SOCK_STREAM, 0);
	DIE(ac.sockfd == INVALID_SOCKET, "socket");

	/* Launch overlapped connection accept through AcceptEx. */
	bRet = AcceptEx(
			listenfd,
			ac.sockfd,
			ac.buffer,
			0,
			128,
			128,
			&ac.len,
			&ac.ov);
	DIE(bRet == FALSE && WSAGetLastError() != ERROR_IO_PENDING, "AcceptEx");
}
Exemple #14
0
// PDO error handler for the statement context.
bool pdo_sqlsrv_handle_stmt_error( sqlsrv_context& ctx, unsigned int sqlsrv_error_code, bool warning TSRMLS_DC,
                                   va_list* print_args )
{
    pdo_stmt_t* pdo_stmt = reinterpret_cast<pdo_stmt_t*>( ctx.driver());
    SQLSRV_ASSERT( pdo_stmt != NULL && pdo_stmt->dbh != NULL, "pdo_sqlsrv_handle_stmt_error: Null statement or dbh passed" );

    sqlsrv_error_auto_ptr error;

    if( sqlsrv_error_code != SQLSRV_ERROR_ODBC ) {
        core_sqlsrv_format_driver_error( ctx, get_error_message( sqlsrv_error_code ), error, SEV_ERROR TSRMLS_CC, print_args );
    }
    else {
        bool err = core_sqlsrv_get_odbc_error( ctx, 1, error, SEV_ERROR TSRMLS_CC );
        SQLSRV_ASSERT( err == true, "No ODBC error was found" );
    }

    SQLSRV_STATIC_ASSERT( sizeof( error->sqlstate ) <= sizeof( pdo_stmt->error_code ));
    strcpy_s( pdo_stmt->error_code, sizeof( pdo_stmt->error_code ), reinterpret_cast<const char*>( error->sqlstate ));

    switch( pdo_stmt->dbh->error_mode ) {
        case PDO_ERRMODE_EXCEPTION:
            if( !warning ) {

                pdo_sqlsrv_throw_exception( error TSRMLS_CC );
            }
            ctx.set_last_error( error );
            break;
        case PDO_ERRMODE_WARNING:
            if( !warning ) {
                unsigned int msg_len = strlen( reinterpret_cast<const char*>( error->native_message )) + SQL_SQLSTATE_BUFSIZE 
                    + MAX_DIGITS + 1;
                sqlsrv_malloc_auto_ptr<char> msg;
                msg = static_cast<char*>( sqlsrv_malloc( msg_len ));
                core_sqlsrv_format_message( msg, msg_len, WARNING_TEMPLATE, error->sqlstate, error->native_code, 
                                            error->native_message );
                php_error( E_WARNING, msg );
                sqlsrv_free( msg );
            }
            ctx.set_last_error( error );
            break;
        case PDO_ERRMODE_SILENT:
            ctx.set_last_error( error );
            break;
        default:
            DIE( "Unknown error mode. %1!d!", pdo_stmt->dbh->error_mode );
            break;
    }

    // return error ignored = true for warnings.
    return ( warning ? true : false );
}
void print(elem *hashtable, int size, char *file_name)
{
	int i;
	FILE *fd;

	if(file_name != NULL)
	{
		fd = fopen(file_name, "a");
		DIE(fd == NULL, "[print] Error opening file.");
	}

	for(i = 0; i < size; i++)
	{
		if(hashtable[i].next != NULL)
		{
			elem *temp = hashtable[i].next;
			
			if(file_name != NULL)
			{
				while(temp != NULL)
				{
					if(temp->next == NULL)
						fprintf(fd, "%s", temp->value);				/* Print without the last space */
					else
						fprintf(fd, "%s ", temp->value);

					temp = temp->next;
				}
			}else
			{
				while(temp != NULL)
				{
					if(temp->next == NULL)
						printf("%s", temp->value);
					else
						printf("%s ", temp->value);

					temp = temp->next;
				}
			}

			if(file_name != NULL)
				fprintf(fd, "\n");
			else
				printf("\n");
		}
	}

	if(file_name != NULL)
		fclose(fd);
}
void CNPC::DoWCMilitiaAI() {
   if (DIE(1000) <= 4) {
	switch (this->GetVnum()) {
	   case 13003:
		this->AddCommand("emote works on his backstab.");
		break;
	   case 13005:
		this->AddCommand("emote takes a shot at a target and hits it's mark.");
		this->AddCommand("smile");
		break;
	   case 13006:
		this->AddCommand("emote takes a shot at a target and misses wildly.");
		this->AddCommand("frown");
		break;
	   case 13007:
		this->AddCommand("emote works hard on his next invention, scribbling on a piece of paper.");
		break;
	   case 13009:
		switch (DIE(3)) {
		   case 1:
			this->AddCommand("emote sits and eats quietly.");
			break;
		   case 2:
			this->AddCommand("say Man, working out really builds up the appetite.");
			break;
		   case 3:
			this->AddCommand("say I liked Mom's cooking much better.");
			break;
		}
		break;
	   case 13002:
		this->AddCommand("emote sings... I wanna make love to you woman... I wanna lay you down by the fire..");
		break;
	   default:
		break;
	}
   }
}
Exemple #17
0
mem_t
my_mmap(int size, int prot)
{
	caddr_t start = NULL;
	static int fd = -1;
	int flags;
	mem_t mapped;
#ifdef sparc
	{
		if (fd == -1)
			if ((fd = open("/dev/zero", O_RDWR)) == -1) {
				perror("/dev/zero");
				DIE("out of memory");
			}
		flags = MAP_PRIVATE;
	}
#else
	flags = MAP_ANONYMOUS;
#endif
#ifdef sparc
	/*
		We'd like to use on-demand allocation if the OS supports it.
		Currently, MAP_NORESERVE is not POSIX.  If other OSs do not
		support it, then we should think about implementing a scheme
		where Heap_t structures are reallocated to allow for growth.
	*/
	flags |= MAP_NORESERVE;
#endif
	mapped = (mem_t) mmap(start, size, prot, flags, fd, 0);
	if (mapped == (mem_t) -1) {
		fprintf(stderr,"mmap %d failed: %s\n", size, strerror(errno));
		DIE("out of memory");
	}
	if (diag)
		fprintf(stderr,"mmap %d succeeded with mapped = %lx, prot = %d\n",
			size, (long)mapped, prot);
	return mapped;
}
Exemple #18
0
int
cmd_crop(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection *conn)
{
	struct mpd_status *status = getStatus( conn );
	int length = mpd_status_get_queue_length(status) - 1;

	if (length < 0) {

		mpd_status_free(status);
		DIE( "A playlist longer than 1 song in length is required to crop.\n" );

	} else if (mpd_status_get_state(status) == MPD_STATE_PLAY ||
		   mpd_status_get_state(status) == MPD_STATE_PAUSE) {
		if (!mpd_command_list_begin(conn, false))
			printErrorAndExit(conn);

		while( length >= 0 )
		{
			if (length != mpd_status_get_song_pos(status)) {
				mpd_send_delete(conn, length);
			}
			length--;
		}

		mpd_status_free(status);

		if (!mpd_command_list_end(conn) || !mpd_response_finish(conn))
			printErrorAndExit(conn);

		return ( 0 );

	} else {

		mpd_status_free(status);
		DIE( "You need to be playing to crop the playlist\n" );

	}
}
int
cmd_crop(mpd_unused int argc, mpd_unused char **argv, mpd_Connection *conn)
{
	mpd_Status *status = getStatus( conn );
	int length = ( status->playlistLength - 1 );

	if( status->playlistLength == 0 ) {

		mpd_freeStatus(status);
		DIE( "You have to have a playlist longer than 1 song in length to crop" );

	} else if( status->state == 3 || status->state == 2 ) { /* If playing or paused */

		mpd_sendCommandListBegin( conn );
		printErrorAndExit( conn );

		while( length >= 0 )
		{
			if( length != status->song )
			{
				mpd_sendDeleteCommand( conn, length );
				printErrorAndExit( conn );
			}
			length--;
		}

		mpd_sendCommandListEnd( conn );
		my_finishCommand( conn );
		mpd_freeStatus( status );
		return ( 0 );

	} else {

		mpd_freeStatus(status);
		DIE( "You need to be playing to crop the playlist\n" );

	}
}
Exemple #20
0
static void handle_new_connection(OVERLAPPED *ovp)
{
	struct connection *conn;
	char abuffer[64];
	HANDLE hRet;
	int rc;

	rc = setsockopt(
			ac.sockfd,
			SOL_SOCKET,
			SO_UPDATE_ACCEPT_CONTEXT,
			(char *) &listenfd,
			sizeof(listenfd)
		  );
	DIE(rc < 0, "setsockopt");

	rc = get_peer_address(ac.sockfd, abuffer, 64);
	if (rc < 0) {
		ERR("get_peer_address");
		return;
	}

	dlog(LOG_DEBUG, "Accepted connection from %s\n", abuffer);

	/* Instantiate new connection handler. */
	conn = connection_create(ac.sockfd);

	/* Add socket to IoCompletionPort. */
	hRet = w_iocp_add_key(iocp, (HANDLE) conn->sockfd, (ULONG_PTR) conn);
	DIE(hRet != iocp, "w_iocp_add_key");

	/* Schedule receive operation. */
	connection_schedule_socket_receive(conn);

	/* Use AcceptEx to schedule new connection acceptance. */
	create_iocp_accept();

}
Exemple #21
0
int main(int argc, char **argv) {
	FILE *in ;
	MATRIX m ;
	int j, i ;

	if(argc != 2 ) 
		DIE("Missing parameter", 1) ;

	in = fopen(argv[1], "r") ;
	if(!in)
		DIE("Could not open file", 2) ;
	read_file(in, &m) ;
	fclose(in) ;

	print_matrix(&m) ;
	printf("========\n") ;
	matrix_to_entity(&m)  ;
	print_matrix(&m) ;

	free_matrix(&m) ;
	
	return 0 ;
}
Exemple #22
0
LPVOID MapFile(HANDLE hFile, DWORD size) {
	HANDLE hFileMap;
	LPVOID p;

	hFileMap = CreateFileMapping(
			hFile,
			NULL,
			PAGE_READWRITE,
			0,
			size,
			NULL);
	DIE(hFileMap == NULL, "CreateFileMapping");

	p = MapViewOfFile(
			hFileMap,
			FILE_MAP_ALL_ACCESS,
			0,
			0,
			0);
	DIE(p == NULL, "MapViewOfFile");

	return p;
}
Exemple #23
0
static void
do_block(const void *in, size_t len, size_t buf_len, size_t offset,
         size_t fp_len, int action, int options) {
  switch (action) {
  case BMZ_A_PACK:
    do_pack(in, len, buf_len, offset, fp_len, options);
    break;
  case BMZ_A_UNPACK:
    do_unpack(in, len, buf_len);
    break;
  default:
    DIE("unknown action: %d", action);
  }
}
int
cmd_disable(mpd_unused int argc, char **argv, mpd_Connection *conn)
{
	int arg;

        if( ! parse_int( argv[0], &arg ) || arg <= 0 ) {
		DIE( "Not a positive integer\n" );
	} else {
		mpd_sendDisableOutputCommand( conn, ( arg - 1 ) );
	}

	mpd_finishCommand( conn );
	return cmd_outputs(0, NULL, conn);
}
int cmd_list ( int argc, char ** argv, mpd_Connection * conn )
{
	Constraint *constraints;
	int numconstraints = 0;
	int type;
	int i;
	char *tag;

	type = get_search_type(argv[0]);
	if (type < 0)
		return -1;

	argc -= 1;
	argv += 1;

	if (argc > 0) {
		if (argc % 2 != 0) {
			DIE("arguments must be a tag type and "
			    "optional pairs of search types and queries\n");
		}

		numconstraints = get_constraints(argc, argv, &constraints);
		if (numconstraints < 0)
			return -1;
	}

	mpd_startFieldSearch(conn, type);

	if (argc > 0) {
		for (i = 0; i < numconstraints; i++) {
			mpd_addConstraintSearch(conn, constraints[i].type,
						charset_to_utf8(constraints[i].query));
		}

		free(constraints);
	}

	mpd_commitSearch(conn);
	printErrorAndExit(conn);

	while ((tag = mpd_getNextTag(conn, type))) {
		printErrorAndExit(conn);
		printf("%s\n", charset_from_utf8(tag));
		free(tag);
	}

	my_finishCommand(conn);

	return 0;
}
Exemple #26
0
int getIntegerParameter(const char* paramName, int defaultValue, int allowDefault){
	char paramValue[VALUE_LENGTH];
	char defaultString[VALUE_LENGTH];
	sprintf(defaultString, "%d", defaultValue);
	int error = getMaskedParameter(paramValue, paramName, defaultString, allowDefault);
	int result = atoi(paramValue);
	if(result == 0 && strcmp(paramValue, "0") != 0){
		DIE("ERROR: Wrong value of %s in a configuration file ('%s'). Should be integer.", paramName, paramValue);
	}
	if(error != 0){
		return 0;
	}
	return result;
}
Exemple #27
0
void
more_begin(void)
{
  static struct termios tty;

  tty = stored_tty;
  tty.c_lflag &= (~ECHO);
  tty.c_lflag &= (~ICANON);

  if (tcsetattr (0, TCSANOW, &tty) < 0)
    DIE("tcsetattr");

  more_active = 1;
}
Exemple #28
0
static void start_task_monitor(void)
{
	int rc;

	setup_cpumask();
	DEBUG("Starting task life cycle monitor on CPUs %s\n", nl_cpumask);

	rc = send_cmd(nl_fd, nl_id,
		      getpid(),
		      TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_REGISTER_CPUMASK,
		      nl_cpumask, strlen(nl_cpumask) + 1);
	if (rc < 0)
		DIE("send cmd failed with error %d\n", rc);
}
Exemple #29
0
static void sendto_ok(void) 
{
	int sockfd;
	struct sockaddr_sw local_addr;
	struct sockaddr_sw remote_addr;
	ssize_t bytes_sent;
	char buffer[BUFSIZ];
	int rc;

	fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");

	sockfd = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
	DIE(sockfd < 0, "sw_socket");

	rc = sw_bind(sockfd, (struct sockaddr *) &local_addr, sizeof(local_addr));
	DIE(rc < 0, "sw_bind");

	bytes_sent = sw_sendto(sockfd, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
	
	perror("sw_sendto");
	test( bytes_sent >= 0 );

}
Exemple #30
0
int
cmd_volume(int argc, char **argv, struct mpd_connection *conn)
{
	struct int_value_change ch;

	if (argc == 1) {
		if (!parse_int_value_change(argv[0], &ch))
			DIE("\"%s\" is not an integer\n", argv[0]);
	} else {
		struct mpd_status *status = getStatus(conn);

		if (mpd_status_get_volume(status) >= 0)
			printf("volume:%3i%c\n",
			       mpd_status_get_volume(status), '%');
		else
			printf("volume: n/a\n");

		mpd_status_free(status);
		return 0;
	}

	if (ch.is_relative) {
#if LIBMPDCLIENT_CHECK_VERSION(2,9,0)
		if (mpd_connection_cmp_server_version(conn, 0, 18, 0) >= 0) {
			/* MPD 0.18 knows the "volume" command for
			   relative changes */
			if (!mpd_run_change_volume(conn, ch.value))
				printErrorAndExit(conn);
			return 1;
		}
#endif

		struct mpd_status *status = getStatus(conn);
		int old_volume = mpd_status_get_volume(status);
		mpd_status_free(status);

		ch.value += old_volume;
		if (ch.value < 0)
			ch.value = 0;
		else if (ch.value > 100)
			ch.value = 100;

		if (ch.value == old_volume)
			return 1;
	}

	if (!mpd_run_set_volume(conn, ch.value))
		printErrorAndExit(conn);
	return 1;
}