This patch is to convert inputted string to the integer
when read data from stdin. While entering data flow, the
data between bytes can be separated by either space,
or ',' (or by '.').

V1-V2:
        1. Rebased the patch on the latest sg_write_buffer.c
        2. Added the wrong input checkup, and process
        3. Changed the delimer from ",. " to ",. /n/t", in order
         to skip the spaces at the end of string because of misoperation.
        4. Modified some wrong indents.

Tested on my own UFS platform, used to issue VU command:

./sg_write_buffer -b 0 -i 0 -v -l 0x2c -m 1  -S 7 -r /dev/block/sda 
0x01 0x40 0x20 
tried to read 44 bytes from -, got 3 bytes
pad with 0xff bytes and continue
sending single write buffer, mode=0x1, mpsec=7, id=0, offset=0, len=44
    Write buffer cdb: 3b e1 00 00 00 00 00 00 2c 00 

./sg_read_buffer -l 32 -m 1 -S 6 -v /dev/block/sda 
    Read buffer(10) cdb: 3c c1 00 00 00 00 00 00 20 00 
 00     61 34 64 36 63 38 61 42  65 36 4b 4d 4c 34 4c 30
 10     30 34 44 43 45 32 30 32  57 30 00 00 00 00 00 00

Signed-off-by: Bean Huo <bean...@micron.com>
---
 src/sg_write_buffer.c | 58 ++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 48 insertions(+), 10 deletions(-)

diff --git a/src/sg_write_buffer.c b/src/sg_write_buffer.c
index 9c81e93..115f0af 100644
--- a/src/sg_write_buffer.c
+++ b/src/sg_write_buffer.c
@@ -212,6 +212,7 @@ main(int argc, char * argv[])
     const char * device_name = NULL;
     const char * file_name = NULL;
     uint8_t * dop = NULL;
+    uint8_t * read_buf = NULL;
     uint8_t * free_dop = NULL;
     char * cp;
     const struct mode_s * mp;
@@ -440,16 +441,51 @@ main(int argc, char * argv[])
                     }
                 }
             }
-            res = read(infd, dop, wb_len);
-            if (res < 0) {
-                ret = sg_convert_errno(errno);
-                snprintf(ebuff, EBUFF_SZ, ME "couldn't read from %s",
-                         file_name);
-                perror(ebuff);
-                if (! got_stdin)
-                    close(infd);
-                goto err_out;
-            }
+           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((char*)read_buf, ",. \n\t");
+               while (pch != NULL) {
+                   val = sg_get_num_nomult(pch);
+                   if (val >= 0 && val < 255) {
+                       dop[res] = val;
+                       res++;
+                   } else {
+                       pr2serr("Data read from STDIO is wrong.\n"
+                               "Please input the data by byte, the bytes 
should be seperated\n"
+                               "by either space, or ',' ( or by '.'), and the 
value per byte should\n"
+                               "be between 0~255. Hexadecimal number preceded 
by either '0x' or\n"
+                               "'OX' (or has a trailing 'h' or 'H').\n");
+                       ret = SG_LIB_SYNTAX_ERROR;
+                       goto err_out;
+                   }
+                   pch = strtok(NULL, ",. \n\t");
+               }
+            } else {
+                res = read(infd, dop, wb_len);
+                if (res < 0) {
+                    ret = sg_convert_errno(errno);
+                    snprintf(ebuff, EBUFF_SZ, ME "couldn't read from %s",
+                             file_name);
+                    perror(ebuff);
+                    if (! got_stdin)
+                        close(infd);
+                    goto err_out;
+               }
+           }
             if (res < wb_len) {
                 if (wb_len_given) {
                     pr2serr("tried to read %d bytes from %s, got %d bytes\n",
@@ -537,6 +573,8 @@ main(int argc, char * argv[])
 err_out:
     if (free_dop)
         free(free_dop);
+    if (read_buf)
+       free(read_buf);
     if (sg_fd >= 0) {
         res = sg_cmds_close_device(sg_fd);
         if (res < 0) {
-- 
2.7.4

Reply via email to