Esempio n. 1
0
static int parent(int *pipefd, int cpid)
{
	char *input;
	unsigned int tx_buf;

	close(pipefd[0]);
	while (1) {
		printf("> ");
		input = foo_gets();
		if (input == NULL) {
			printf("ERROR: Error while reading from stdin.\n");
			return -1;
		}
		if (!strcmp(input, "exit\n")) {
			free(input);
			close(pipefd[1]);
			kill(cpid, SIGKILL);
			return 0;
		}
		errno = 0;
		tx_buf = strtol(input, NULL, 0);
		if (errno == ERANGE)
			printf("WARNING: strtol conversion out of range.\n");
		free(input);
		if (write(pipefd[1], &tx_buf, 4) != 4)
			printf("WARNING: Not all data was written.\n");
	}
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
    int num, ref;
    char *num_str;

    if (argc > 1) {
        num_str = argv[1];
    } else {
        size_t len;

        num_str = foo_gets();
        len = strlen(num_str);
        if (num_str[len - 1] == '\n')
            num_str[len - 1] = '\0';
    }

    num = my_atoi(num_str);
    ref = atoi(num_str);
    if (num != ref)
        fprintf(stderr, "ERROR: mismatch: in:%s, out:%d, ref:%d\n",
                num_str, num, ref);
    else
        printf("in:%s, out:%d, ref:%d\n", num_str, num, ref);

    return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
	char *string;

	if (argc > 1) {
		string = argv[1];
	} else {
		printf("Enter string: ");
		string = foo_gets();
		if (string[strlen(string) - 1] == '\n')
			string[strlen(string) - 1] = '\0';
	}

	char *rev = revstr_new(string);

	printf("in:   %s\n", string);
	printf("out1: %s\n", rev);
	revstr_inplace(string);

	if (strcmp(string, rev))
		fprintf(stderr, "ERROR: results differ\n");
	printf("out2: %s\n", string);

	return 0;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
	int i, j, summe;
	unsigned int upperbound = 1000;
	unsigned int lowerbound = 1;
	int divcount = 0;
	int divs[100] = { 0 };
	char *rline;
	clock_t start = 0;
	clock_t stop = 0;

	printf("Searching for perfect numbers:\n");

	printf("Enter lower bound for search (%u): ", lowerbound);
	rline = foo_gets();
	if (rline == NULL) {
		printf("ERROR: Error reading from stdin");
	} else {
		if (strcmp(rline, "\n"))
			lowerbound = strtol(rline, NULL, 0);
		free(rline);
	}

	printf("Enter upper bound for search (%u): ", upperbound);
	rline = foo_gets();
	if (!rline) {
		printf("ERROR: Error reading from stdin");
	} else {
		if (strcmp(rline, "\n"))
			upperbound = strtol(rline, NULL, 0);
		free(rline);
	}
	printf("Search range: %u..%u\n", lowerbound, upperbound);

	start = clock();

	for (i = lowerbound; i <= upperbound; i++) {
		summe = 0;
		divcount = 0;
		for (j = (i >> 1); j > 0; j--) {
			if ((i % j) == 0) {
				summe += j;
				divs[divcount++] = j;
			}
			if (summe > i)
				break;
		}
		if (summe == i) {
			printf("%i\t\tDivs: ", summe);
			for (j = 0; j < divcount; j++)
				printf("%u ", divs[j]);
			printf("\n");
		}
	}

	stop = clock();

	printf("Computation time: %f\n",
	       (double)(stop - start) / (double)CLOCKS_PER_SEC);
	return 0;
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
	char *rline;
	clock_t start, stop;
	uintptr_t lowerbound = 2;
	uintptr_t upperbound = 100;

	printf("Searching for primes:\n");

	printf("Enter lower bound for search (%" PRIuPTR "): ", lowerbound);
	rline = foo_gets();
	if (rline == NULL) {
		printf("ERROR: Error reading from stdin");
	} else {
		if (strcmp(rline, "\n"))
			lowerbound = strtol(rline, NULL, 0);
		free(rline);
	}

	printf("Enter upper bound for search (%" PRIuPTR "): ", upperbound);
	rline = foo_gets();
	if (!rline) {
		printf("ERROR: Error reading from stdin");
	} else {
		if (strcmp(rline, "\n"))
			upperbound = strtol(rline, NULL, 0);
		free(rline);
	}

	printf("Search range: %" PRIuPTR "..%" PRIuPTR "\n",
	       lowerbound, upperbound);

	/* catch 2 as a prime */
	if (lowerbound <= 2) {
		printf("2\n");
	}

	/* only odd numbers can be prime (ignoring 2) */
	lowerbound |= 1;

	start = clock();

	for (uintptr_t i = lowerbound; i <= upperbound; i += 2) {
		bool is_prime = true;

		for (uintptr_t j = 2; j <= sqrt(i); j++) {
			if (i % j == 0) {
				is_prime = false;
				break;
			}
		}

		if (!is_prime)
			continue;

		printf("%" PRIdPTR "\n", i);
	}

	stop = clock();

	printf("Computation time: %f\n",
	       (double)(stop - start) / (double)CLOCKS_PER_SEC);

	return 0;
}
Esempio n. 6
0
int main(void)
{
	int ret = 0;
	int socket_fd;
	struct sockaddr_un address;
	int bytes_received, bytes_sent;
	char *buf;
	size_t buf_size;

	if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		perror("ERROR: unable to create socket");
		return socket_fd;
	}

	memset(&address, 0, sizeof(address));
	address.sun_family = AF_UNIX;
	strcpy(address.sun_path, SERVER_SOCK_FILE);

	if (connect(socket_fd, (const struct sockaddr *)&address,
				sizeof(address))) {
		perror("ERROR: unable to connect");
		ret = -1;
		goto err_close;
	}

	while (1) {
		printf("Enter message to transmit: ");
		buf = foo_gets();
		if (!buf) {
			printf("ERROR: unable to create message\n");
			ret = -1;
			goto err_close;
		}
		buf_size = strlen(buf);

		if (!strncmp("quit", buf, 4) || !strncmp("exit", buf, 4)) {
			printf("bye\n");
			ret = 0;
			free(buf);
			goto err_close;
		}

		bytes_sent = write(socket_fd, buf, buf_size - 1);
		if (bytes_sent < 0) {
			perror("ERROR: unable to send data");
			ret = bytes_sent;
			goto err_close;
		} else {
			printf("CLIENT: sent %d bytes\n", bytes_sent);
		}

		bytes_received = read(socket_fd, buf, buf_size - 1);
		if (bytes_received < 0) {
			perror("ERROR: unable to receive data");
			ret = bytes_received;
			goto err_close;
		} else {
			printf("CLIENT: received %d bytes\n", bytes_received);
		}

		buf[bytes_received] = '\0';
		printf("Received message: %s\n", buf);
		free(buf);
	}

err_close:
	close(socket_fd);

	return ret;
}