Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933633AbbKRWcE (ORCPT ); Wed, 18 Nov 2015 17:32:04 -0500 Received: from mail-pa0-f43.google.com ([209.85.220.43]:35238 "EHLO mail-pa0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933422AbbKRWb7 (ORCPT ); Wed, 18 Nov 2015 17:31:59 -0500 From: Joshua Clayton To: Mark Brown , linux-spi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Anton Bondarenko , Joshua Clayton Subject: [PATCH v2 2/6] spi: spidev_test: transfer_escaped_string function Date: Wed, 18 Nov 2015 14:30:38 -0800 Message-Id: X-Mailer: git-send-email 2.5.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1876 Lines: 76 Move the input_tx code into its own small function. This cleans up some variables from main() that are used only here. While we are at it, check malloc calls instead of assuming they succeed. Signed-off-by: Joshua Clayton --- tools/spi/spidev_test.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 135b3f5..f9d2957 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -249,13 +249,30 @@ static void parse_opts(int argc, char *argv[]) } } +static void transfer_escaped_string(int fd, char *str) +{ + size_t size = strlen(str + 1); + uint8_t *tx; + uint8_t *rx; + + tx = malloc(size); + if (!tx) + pabort("can't allocate tx buffer"); + + rx = malloc(size); + if (!rx) + pabort("can't allocate rx buffer"); + + size = unescape((char *)tx, str, size); + transfer(fd, tx, rx, size); + free(rx); + free(tx); +} + int main(int argc, char *argv[]) { int ret = 0; int fd; - uint8_t *tx; - uint8_t *rx; - int size; parse_opts(argc, argv); @@ -300,17 +317,10 @@ int main(int argc, char *argv[]) printf("bits per word: %d\n", bits); printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); - if (input_tx) { - size = strlen(input_tx+1); - tx = malloc(size); - rx = malloc(size); - size = unescape((char *)tx, input_tx, size); - transfer(fd, tx, rx, size); - free(rx); - free(tx); - } else { + if (input_tx) + transfer_escaped_string(fd, input_tx); + else transfer(fd, default_tx, default_rx, sizeof(default_tx)); - } close(fd); -- 2.5.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/