return ret;
}
+
+int isp_file_to_buff(char* data, unsigned int len, char* filename)
+{
+ int in_fd = -1;
+ unsigned int bytes_read = 0;
+
+ /* Open file for reading if input is not stdin */
+ if (strncmp(filename, "-", strlen(filename)) == 0) {
+ /* FIXME : set as non blocking ? */
+ in_fd = STDIN_FILENO;
+ } else {
+ in_fd = open(filename, O_RDONLY | O_NONBLOCK);
+ if (in_fd <= 0) {
+ perror("Unable to open file for reading");
+ printf("Tried to open \"%s\".\n", filename);
+ return -13;
+ }
+ }
+
+ /* Read file content */
+ do {
+ int nb = read(in_fd, &data[bytes_read], (len - bytes_read));
+ if (nb < 0) {
+ if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
+ usleep( 50 );
+ continue;
+ }
+ perror("Input file read error");
+ if (in_fd != STDIN_FILENO) {
+ close(in_fd);
+ }
+ return -12;
+ } else if (nb == 0) {
+ /* End of file */
+ break;
+ }
+ bytes_read += nb;
+ } while (bytes_read < len);
+
+ if (in_fd != STDIN_FILENO) {
+ close(in_fd);
+ }
+
+ return bytes_read;
+}
+
/* ---- File utility functions ----------------------------------------------*/
int isp_buff_to_file(char* data, unsigned int len, char* filename);
+int isp_file_to_buff(char* data, unsigned int len, char* filename);
+
#endif /* ISP_UTILS_H */
/* Arguments */
unsigned long int addr = 0;
char* in_file_name = NULL;
- int in_fd = -1;
char file_buff[RAM_MAX_SIZE];
unsigned int bytes_read = 0;
int ret = 0;
}
in_file_name = args[1];
- /* Open file for reading if input is not stdin */
- if (strncmp(in_file_name, "-", strlen(in_file_name)) == 0) {
- /* FIXME : set as non blocking ? */
- in_fd = STDIN_FILENO;
- } else {
- in_fd = open(in_file_name, O_RDONLY | O_NONBLOCK);
- if (in_fd <= 0) {
- perror("Unable to open file for reading");
- printf("Tried to open \"%s\".\n", in_file_name);
- return -13;
- }
- }
- /* Read file content */
- do {
- int nb = read(in_fd, &file_buff[bytes_read], (RAM_MAX_SIZE - bytes_read));
- if (nb < 0) {
- if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
- usleep( 50 );
- continue;
- }
- perror("Input file read error");
- if (in_fd != STDIN_FILENO) {
- close(in_fd);
- }
- return -12;
- } else if (nb == 0) {
- /* End of file */
- break;
- }
- bytes_read += nb;
- } while (bytes_read < RAM_MAX_SIZE);
- if (in_fd != STDIN_FILENO) {
- close(in_fd);
+ /* Read data */
+ bytes_read = isp_file_to_buff(file_buff, RAM_MAX_SIZE, in_file_name);
+ if (bytes_read <= 0){
+ return -13;
}
+
if (trace_on) {
printf("Read %d octet(s) from input file\n", bytes_read);
}