From patchwork Fri Nov 9 18:18:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 10676405 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7FDC915E9 for ; Fri, 9 Nov 2018 18:18:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 720AE2E0A6 for ; Fri, 9 Nov 2018 18:18:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 659F72EA9F; Fri, 9 Nov 2018 18:18:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB8222E0A6 for ; Fri, 9 Nov 2018 18:18:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728638AbeKJEAd (ORCPT ); Fri, 9 Nov 2018 23:00:33 -0500 Received: from mailout.micron.com ([137.201.242.129]:59338 "EHLO mailout.micron.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728108AbeKJEAd (ORCPT ); Fri, 9 Nov 2018 23:00:33 -0500 Received: from mail.micron.com (bowex17h.micron.com [137.201.20.32]) by mailout.micron.com (8.14.4/8.14.6) with ESMTP id wA9IIinC016374; Fri, 9 Nov 2018 11:18:44 -0700 Received: from bowex17e.micron.com (137.201.21.213) by BOWEX17H.micron.com (137.201.20.32) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Fri, 9 Nov 2018 11:18:44 -0700 Received: from NAM02-CY1-obe.outbound.protection.outlook.com (137.201.20.7) by bowex17e.micron.com (137.201.21.213) with Microsoft SMTP Server (TLS) id 15.0.1347.2 via Frontend Transport; Fri, 9 Nov 2018 11:18:44 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=MicronCorp.onmicrosoft.com; s=selector1-micron-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Wx8gvpHnzZv3EwDRuN+zsa2HI7GPRK3o0MTJZL3A6nM=; b=LwKUU2texOH3CFOwdgLM25UxCNZ5LvSbEoQgf+7XLHIWNRJQ4VjntNGKtEt9YSL9QXNADCRRWKdVx0REfozclp8bXcWwUaKenEGdx4rfMfzyJTnt7oyA4MysZAldxEA0TreSU9HRW2+Dvz5Tb0QBd21mGYDB1CCNEfN3cr9tN1I= Received: from BYAPR08MB4533.namprd08.prod.outlook.com (52.135.234.15) by BYAPR08MB4565.namprd08.prod.outlook.com (52.135.234.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1294.21; Fri, 9 Nov 2018 18:18:42 +0000 Received: from BYAPR08MB4533.namprd08.prod.outlook.com ([fe80::79a1:21fc:771b:7e20]) by BYAPR08MB4533.namprd08.prod.outlook.com ([fe80::79a1:21fc:771b:7e20%6]) with mapi id 15.20.1294.034; Fri, 9 Nov 2018 18:18:42 +0000 From: "Bean Huo (beanhuo)" To: "Bean Huo (beanhuo)" , "linux-scsi@vger.kernel.org" , "dgilbert@interlog.com" CC: Hannes Reinecke , Bart Van Assche Subject: [PATCH v1] sg3_utils: sg_write_buffer: convert string to integer while reading from stdio Thread-Topic: [PATCH v1] sg3_utils: sg_write_buffer: convert string to integer while reading from stdio Thread-Index: AdR4WIMvxtbV6j8AR4ieWNS8vvmlfA== Date: Fri, 9 Nov 2018 18:18:42 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [165.225.81.44] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;BYAPR08MB4565;6:3knoO84wM9BnxUiPOV7g74ThTtfdwv+vhekcIGXPQPGDU3m96FjqNWuKnQvz7LgFHU/Z97WKZhoe3exVb1l1wGpHjTxdVM4e8kWXnXC0FfZCYvexZEf1P9K7w0TptOKhmC0nJvQizJWWK+CwYb65RuRHsi+IaLdO7WkdIRcdc8EIxrqTIC/T9+5VYGdSNWGPp8dzMZKdk8eZKpdC7rHehczBpqF1W5OgzPlPcF4rAljsHavgf2exyMX0r6zMr/6rYaAXXIQE3n/WMTHrU0unuLSijtFep9giWQ6fCB1MKMi+IhPQpBFxXBQpYRQsvdXMXuB7xW8MYzN6erbCLnvb2uiIKmTdEbaqetCZQsVG1BZyASCXPW5lrmpLTPrJPrc1efcv7UDUEB1zLWxK7bHh8/0CjcDNBeVb3P54iIZLq4SLXs7ataSVGOXDjfObdOjcfdvNAqlsZtQAzJR+yXPBdQ==;5:hvZx9gDWKVhtMqckZHbSLYtFGnpKMpWZbE61GU8/IrafzG+RGwyAse6lUTnAL8g6M/1bZivdfTm4v274vfqAWMoPQ0gzwyKGgfiQLgv74PFZdQx3bpirfSrpDbv4WobFfnTHJl966w+ZeJJ9fqYtWnFgkhUE/4AWomAd4SAilKA=;7:vPhoGui+Ipw6kItDwpvrtwGr1Rslk8lJ7v6ajEJo7h0P/GNaOC2A+Ed6X9+7iQJJRjj5vVt4lML2JiDWe8t0dNF96SgDc3LhrFWj6vIRLIeSx4f0V7m1Mk5Kdvn0ho1XqeeRGRmjqsVglZIq5QIWiw== x-ms-exchange-antispam-srfa-diagnostics: SOS;SOR; x-ms-office365-filtering-correlation-id: 78719ab2-a2c2-4313-d1be-08d6466fc7de x-microsoft-antispam: BCL:0;PCL:0;RULEID:(7020095)(4652040)(8989299)(5600074)(711020)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020);SRVR:BYAPR08MB4565; x-ms-traffictypediagnostic: BYAPR08MB4565: authentication-results: spf=none (sender IP is ) smtp.mailfrom=beanhuo@micron.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(34377916053724); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(3002001)(3231382)(944501410)(52105095)(148016)(149066)(150057)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123560045)(20161123562045)(20161123564045)(201708071742011)(7699051)(76991095);SRVR:BYAPR08MB4565;BCL:0;PCL:0;RULEID:;SRVR:BYAPR08MB4565; x-forefront-prvs: 08512C5403 x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(366004)(136003)(396003)(376002)(39860400002)(346002)(189003)(199004)(9686003)(106356001)(6436002)(8936002)(6506007)(71200400001)(71190400001)(55016002)(256004)(3846002)(4326008)(5660300001)(6116002)(486006)(97736004)(14444005)(476003)(68736007)(105586002)(316002)(2900100001)(7696005)(81166006)(81156014)(14454004)(186003)(86362001)(2201001)(2501003)(53936002)(110136005)(54906003)(2906002)(33656002)(8676002)(478600001)(305945005)(55236004)(26005)(74316002)(66066001)(7736002)(25786009)(102836004)(99286004);DIR:OUT;SFP:1101;SCL:1;SRVR:BYAPR08MB4565;H:BYAPR08MB4533.namprd08.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: micron.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: W+3iP83K39mScTP7hPJyUPwMmdr2DezJ4/eds1qqFWw9uJk56oSIB5rhOLa2Gg51fkIIn+7q6QE5df8n37nGvGN/XzK4ixtMIIJbAAWPG0lRs+qVfMBTkgAMvhJdETDZMm17euFm8nb2cNbQsMvaxQelAMk3u975w0Zlurvbx8tSB1fyUkBK8itO8JIOL5UdkvpnfYHHkIBqOwcGvxBknbBMDJKi52/mau+eozz0VfzhKX8FjSIvrqWs2k5KWXLRvN9EsTwOTVqC0alNfZSEBKAvqQi9FgNKgG9/zgNzaQTpXS4icg3iT5nYI86ceQvTbcF7Rphis9B+wqHAXgkxiPmkP52SxSMz4ur5WTV4fak= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 78719ab2-a2c2-4313-d1be-08d6466fc7de X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Nov 2018 18:18:42.6145 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f38a5ecd-2813-4862-b11b-ac1d563c806f X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR08MB4565 X-OriginatorOrg: micron.com X-TM-AS-Product-Ver: SMEX-12.0.0.1782-8.200.1013-24212.005 X-TM-AS-Result: No--0.663600-0.000000-31 X-TM-AS-MatchedID: 709251-703788-707321-851106-700398-188019-704852-708712-7 00324-703529-851458-701339-700059-700008-188199-105630-137717-701163-148004 -148007-148020-148050-42000-42003-63 X-TM-AS-User-Approved-Sender: Yes X-TM-AS-User-Blocked-Sender: No X-MT-CheckInternalSenderRule: True X-Scanned-By: MIMEDefang 2.78 on 137.201.82.98 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 '.'. Signed-off-by: Bean Huo --- 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; 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) { + 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; } + } 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));