On 2018-11-09 7:18 p.m., Bean Huo (beanhuo) wrote:
This patch is to convert inputted string to the integer when read data from stdin. While entering data, the data between bytes can be separated by space, or by ',' or by '.'.
Could you send me this patch against sg_write_buffer.c as found in the recently released version 1.44 . In there the version string for sg_write_buffer.c is: "1.28 20180628"
Signed-off-by: Bean Huo <bean...@micron.com> --- src/sg_write_buffer.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/sg_write_buffer.c b/src/sg_write_buffer.c index 7560e7e..902bc5d 100644 --- a/src/sg_write_buffer.c +++ b/src/sg_write_buffer.c @@ -195,6 +195,7 @@ main(int argc, char * argv[]) const char * device_name = NULL; const char * file_name = NULL; unsigned char * dop = NULL; + unsigned char * read_buf= NULL;
For example the above 2 lines now use uint8_t
char * cp; const struct mode_s * mp; char ebuff[EBUFF_SZ]; @@ -394,6 +395,33 @@ main(int argc, char * argv[]) } } } + if (infd == STDIN_FILENO) { + if (NULL == (read_buf = (unsigned char *)malloc(DEF_XFER_LEN))) { + pr2serr(ME "out of memory\n"); + ret = SG_LIB_SYNTAX_ERROR; + goto err_out; + } + res = read(infd, read_buf, DEF_XFER_LEN); + if (res < 0) { + snprintf(ebuff, EBUFF_SZ, ME "couldn't read from STDIN"); + perror(ebuff); + ret = SG_LIB_FILE_ERROR; + goto err_out; + } + char * pch; + int val = 0; + res = 0; + pch = strtok(read_buf, ",. "); + while (pch != NULL) { + printf("read %s ", pch); + val = sg_get_num_nomult(pch); + if (val > 0 && val < 255) {
Hmm, perhaps: if (val >= 0 && val < 256) .... else <error message>
+ dop[res] = val; + res++; + } + pch = strtok(NULL, ",. "); + } + } else { res = read(infd, dop, wb_len); if (res < 0) { snprintf(ebuff, EBUFF_SZ, ME "couldn't read from %s", @@ -404,6 +432,7 @@ main(int argc, char * argv[]) ret = SG_LIB_FILE_ERROR; goto err_out; } + }
Tabbing looks a little off here. Thanks Doug Gilbert
if (res < wb_len) { if (wb_len_given) { pr2serr("tried to read %d bytes from %s, got %d bytes\n", @@ -472,6 +501,8 @@ main(int argc, char * argv[]) err_out: if (dop) free(dop); + if (read_buf) + free(read_buf); res = sg_cmds_close_device(sg_fd); if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res));