Ejemplo n.º 1
0
static void read_image_version(void) {
	FILE *fp;
	int length;

	fp = fopen("/etc/tf_image_version", "rb");

	if (fp == NULL) {
		return;
	}

	length = robust_fread(fp, _image_version, sizeof(_image_version) - 1);

	fclose(fp);

	if (length < 0) {
		string_copy(_image_version, sizeof(_image_version), "<unknown>");

		return;
	}

	_image_version[length] = '\0';

	while (length > 0 && strspn(&_image_version[length - 1], " \f\n\r\t\v") > 0) {
		--length;
	}

	_image_version[length] = '\0';

	if (length == 0) {
		string_copy(_image_version, sizeof(_image_version), "<unknown>");

		return;
	}
}
Ejemplo n.º 2
0
int red_extension_read_eeprom_from_fs(uint8_t *buffer, int extension) {
	FILE *fp;
	char file_name[128];
	int length;

	if (robust_snprintf(file_name, sizeof(file_name),
	                    "/tmp/new_eeprom_extension_%d.conf", extension) < 0) {
		return -1;
	}

	fp = fopen(file_name, "rb");

	if (fp == NULL) {
		return -1;
	}

	length = robust_fread(fp, buffer, EEPROM_SIZE);

	if (fclose(fp) < 0) {
		log_warn("Could not close file %s", file_name);
	}

	if (remove(file_name) < 0) {
		log_warn("Could not delete file %s", file_name);
	}

	return length;
}
Ejemplo n.º 3
0
// sets errno on error
int conf_file_read(ConfFile *conf_file, const char *filename,
                   ConfFileReadWarningFunction warning, void *opaque) {
	bool success = false;
	int allocated = 256;
	FILE *fp = NULL;
	char *buffer = NULL;
	int rc;
	char c;
	int length = 0;
	bool skip = false;
	int number = 1;
	char *tmp;
	int i;
	ConfFileLine *line;

	// open file
	fp = fopen(filename, "rb");

	if (fp == NULL) {
		goto cleanup;
	}

	// allocate buffer
	buffer = malloc(allocated);

	if (buffer == NULL) {
		errno = ENOMEM;

		goto cleanup;
	}

	*buffer = '\0';

	// read and parse lines
	for (;;) {
		rc = robust_fread(fp, &c, 1);

		if (rc < 0) {
			goto cleanup;
		}

		if (rc == 0) {
			// use \0 to indicate end-of-file. this also ensures that parsing
			// stops on the first \0 character in the file
			c = '\0';
		}

		if (c == '\0' || c == '\n') {
			// end-of-file or end-of-line found. only use \n as end-of-line
			// marker. don't care about \r-only systems
			if (!skip) {
				// remove trailing \r if line ends with \r\n sequence
				if (length > 0 && buffer[length - 1] == '\r' && c == '\n') {
					buffer[--length] = '\0';
				}

				if (conf_file_parse_line(conf_file, number, buffer,
				                         warning, opaque) < 0) {
					goto cleanup;
				}
			}

			if (c == '\0') {
				// end-of-file reached
				break;
			}

			*buffer = '\0';
			length = 0;
			skip = false;
			++number;
		} else if (!skip) {
			if (length + 2 > allocated) {
				// not enough room for char and NULL-terminator
				if (allocated < 32768) {
					tmp = realloc(buffer, allocated * 2);

					if (tmp == NULL) {
						errno = ENOMEM;

						goto cleanup;
					}

					buffer = tmp;
					allocated *= 2;
				} else {
					// line is too long, skip it
					skip = true;

					if (warning != NULL) {
						// limit printed line length in log messages
						buffer[32] = '\0';

						warning(CONF_FILE_READ_WARNING_LINE_TOO_LONG,
						        number, buffer, opaque);
					}
				}
			}

			if (!skip) {
				buffer[length++] = c;
				buffer[length] = '\0';
			}
		}
	}

	// remove trailing empty lines
	for (i = conf_file->lines.count - 1; i >= 0; --i) {
		line = array_get(&conf_file->lines, i);

		if (line->raw == NULL || *line->raw != '\0') {
			break;
		}

		array_remove(&conf_file->lines, i, conf_file_line_destroy);
	}

	success = true;

cleanup:
	robust_fclose(fp);
	free(buffer);

	return success ? 0 : -1;
}