Incorporate review feedback from OpenOCD team

Remove unused file handle, and rename the only remaining one to make
more sense.
Close file descriptor if initialization fails.

Change-Id: I383567aaadb1aa59d86f814eba8bc65f24e91928
This commit is contained in:
Tim Newsome 2018-01-19 13:13:25 -08:00
parent cb543e804d
commit fb2291421b
1 changed files with 9 additions and 17 deletions

View File

@ -40,8 +40,7 @@
static char *remote_bitbang_host; static char *remote_bitbang_host;
static char *remote_bitbang_port; static char *remote_bitbang_port;
static FILE *remote_bitbang_in; static FILE *remote_bitbang_file;
static FILE *remote_bitbang_out;
static int remote_bitbang_fd; static int remote_bitbang_fd;
/* Circular buffer. When start == end, the buffer is empty. */ /* Circular buffer. When start == end, the buffer is empty. */
@ -93,25 +92,25 @@ static void remote_bitbang_fill_buf(void)
static void remote_bitbang_putc(int c) static void remote_bitbang_putc(int c)
{ {
if (EOF == fputc(c, remote_bitbang_out)) if (EOF == fputc(c, remote_bitbang_file))
REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno)); REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno));
} }
static int remote_bitbang_quit(void) static int remote_bitbang_quit(void)
{ {
if (EOF == fputc('Q', remote_bitbang_out)) { if (EOF == fputc('Q', remote_bitbang_file)) {
LOG_ERROR("fputs: %s", strerror(errno)); LOG_ERROR("fputs: %s", strerror(errno));
return ERROR_FAIL; return ERROR_FAIL;
} }
if (EOF == fflush(remote_bitbang_out)) { if (EOF == fflush(remote_bitbang_file)) {
LOG_ERROR("fflush: %s", strerror(errno)); LOG_ERROR("fflush: %s", strerror(errno));
return ERROR_FAIL; return ERROR_FAIL;
} }
/* We only need to close one of the FILE*s, because they both use the same */ /* We only need to close one of the FILE*s, because they both use the same */
/* underlying file descriptor. */ /* underlying file descriptor. */
if (EOF == fclose(remote_bitbang_out)) { if (EOF == fclose(remote_bitbang_file)) {
LOG_ERROR("fclose: %s", strerror(errno)); LOG_ERROR("fclose: %s", strerror(errno));
return ERROR_FAIL; return ERROR_FAIL;
} }
@ -140,7 +139,7 @@ static int char_to_int(int c)
/* Get the next read response. */ /* Get the next read response. */
static int remote_bitbang_rread(void) static int remote_bitbang_rread(void)
{ {
if (EOF == fflush(remote_bitbang_out)) { if (EOF == fflush(remote_bitbang_file)) {
remote_bitbang_quit(); remote_bitbang_quit();
REMOTE_BITBANG_RAISE_ERROR("fflush: %s", strerror(errno)); REMOTE_BITBANG_RAISE_ERROR("fflush: %s", strerror(errno));
} }
@ -289,17 +288,10 @@ static int remote_bitbang_init(void)
if (remote_bitbang_fd < 0) if (remote_bitbang_fd < 0)
return remote_bitbang_fd; return remote_bitbang_fd;
remote_bitbang_in = fdopen(remote_bitbang_fd, "r"); remote_bitbang_file = fdopen(remote_bitbang_fd, "w");
if (remote_bitbang_in == NULL) { if (remote_bitbang_file == NULL) {
LOG_ERROR("fdopen: failed to open read stream");
close(remote_bitbang_fd);
return ERROR_FAIL;
}
remote_bitbang_out = fdopen(remote_bitbang_fd, "w");
if (remote_bitbang_out == NULL) {
LOG_ERROR("fdopen: failed to open write stream"); LOG_ERROR("fdopen: failed to open write stream");
fclose(remote_bitbang_in); close(remote_bitbang_fd);
return ERROR_FAIL; return ERROR_FAIL;
} }