<commit_before>/**
 * @file
 * @brief
 *
 * @date 25.04.2012
 * @author <NAME>
 */

#include <embox/cmd.h>
#include <unistd.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

EMBOX_CMD(exec);

static void print_usage(void) {
	printf("Usage: echo \"STRING\" >> FILE\n");
}

static int exec(int argc, char **argv) {
	int opt;
	FILE *fd;
	const char *mode;

	getopt_init();
	while (-1 != (opt = getopt(argc - 1, argv, "h"))) {
		switch(opt) {
		case 'h':
			print_usage();
			return 0;
		default:
			break;
		}
	}

	if (argc > 3) {
		if (0 == strcmp((const char *) argv[argc - 2], ">>")) {
			mode = "a";
		} else if (0 == strcmp((const char *) argv[argc - 2], ">")) {
			mode = "w";
		}
		else {
			print_usage();
			return 0;
		}

		if (NULL == (fd = fopen((const char *) argv[argc - 1], mode))) {
			return -errno;
		}

		fwrite((const void *) argv[1], strlen((const char *) argv[1]), 1, fd);
		fwrite((const void *) "\n", 1, 1, fd);
		fclose(fd);
		return 0;
	}
	else if (argc == 2) {
		printf("%s \n",argv[argc - 1]);
	}
	else {
		print_usage();
	}
	return 0;
}<commit_msg>echo cmd returns error when no write could be done<commit_after>/**
 * @file
 * @brief
 *
 * @date 25.04.2012
 * @author <NAME>
 */

#include <embox/cmd.h>
#include <unistd.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

EMBOX_CMD(exec);

static void print_usage(void) {
	printf("Usage: echo \"STRING\" >> FILE\n");
}

static int exec(int argc, char **argv) {
	int opt;
	FILE *fd;
	const char *mode;

	getopt_init();
	while (-1 != (opt = getopt(argc - 1, argv, "h"))) {
		switch(opt) {
		case 'h':
			print_usage();
			return 0;
		default:
			break;
		}
	}

	if (argc > 3) {
		size_t write_items_count;

		if (0 == strcmp((const char *) argv[argc - 2], ">>")) {
			mode = "a";
		} else if (0 == strcmp((const char *) argv[argc - 2], ">")) {
			mode = "w";
		}
		else {
			print_usage();
			return 0;
		}

		if (NULL == (fd = fopen((const char *) argv[argc - 1], mode))) {
			return -errno;
		}

		write_items_count = fwrite((const void *) argv[1], strlen((const char *) argv[1]), 1, fd);
		write_items_count += fwrite((const void *) "\n", 1, 1, fd);
		fclose(fd);
		return write_items_count > 0 ? 0 : -EIO;
	}
	else if (argc == 2) {
		printf("%s \n",argv[argc - 1]);
	}
	else {
		print_usage();
	}
	return 0;
}
{'fraction_non_alphanumeric': 0.21087314662273476, 'fraction_numerical': 0.022652388797364087, 'mean_word_length': 3.089225589225589, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2596930', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1005, 'n_words': 260}