From patchwork Wed Jul 24 06:01:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 814128 Delivered-To: patch@linaro.org Received: by 2002:adf:f288:0:b0:367:895a:4699 with SMTP id k8csp2617646wro; Tue, 23 Jul 2024 23:04:01 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV12tMaBP69l7G1KGg9menTM3VnZNj3Nt69+2d5Mri8EiDjEK+ffGcprhRZZIEnzhs7sVx7PZfcmbcFRH2dEpR1 X-Google-Smtp-Source: AGHT+IGtAHeM1+9HwaSlbzQg/Z05KSnx/rpVzArkuftUxUe4vWw4kf2Wle2PQFZuKii9/NnWE6Sq X-Received: by 2002:a5d:63c5:0:b0:363:b3ea:7290 with SMTP id ffacd0b85a97d-369f66f02c2mr483759f8f.20.1721801041670; Tue, 23 Jul 2024 23:04:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721801041; cv=none; d=google.com; s=arc-20160816; b=f1N75Rr5bMJJNtiJuxpDAmjGHRrgOuEGxnBL7T3hvTFDHEcGV+KKCK7cyGIWbtvUT4 ziJd4FweczdmFY7SmvdbtUSgrTNatohRPyQP4lu4MoNXrKGu6VseDz6k4MasbZOu70JW MihULfqgxoR5Hg1+9XvYu0c7WYvvz3W62KCSFNOT7V1QHXTyZat6sODggJbpdzYcutCI zwSmFJsFO10/JLFE3jfwjRXMpX1Bb/EjiiQ3xDlY1XhTMYpfnq2ZVzREWANCRz1kTYsL 24/UnjOSZBUODGWGEfdfVMcUmB/mNVxvvYDhFrVj3z5eILwni1Qt6/xzgLGTz2InKWw5 XahQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=1J4pjz8GuX61Zr+PNukJJ9QJ8qUtMVVVbxj8XkTT2Oc=; fh=eimiGswTlodK8yi2DcDfSicmKwvq5bxKQKpD+SkkTNY=; b=jSShuSreq33OTf8baCpha5i0E6k5Juga8Fxfr88o5zxEzUfFdo30cXOPoODR8SmgD0 Bh00dbuOmFUo30YwY10BZX5LpSn7e4CaZoKFLuRZm+Ny6HcWTv0TZPVY7RpP0EK9GUJE pp3CTBHHxq0nErXYHq1kV/mTqcBz4QBB4XM58Kqz/d0N6TIGNEIUiTw6/QVWEvdGBJmz 9VE0AAJpm7Pnti98FbWOaHUBTh1xHreGmf8O6tdKtBUwypEQm6FF7x/KwWkIXTUNERKz dRc65lwM472kBQrJI2B7BZWmwtlgIyVWx2CXywqGdPI+DU6HcRPBaZJOIOy8vBvW8QDl f16Q==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id 5b1f17b1804b1-427f937e9ebsi3636035e9.59.2024.07.23.23.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 23:04:01 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 308068888E; Wed, 24 Jul 2024 08:03:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 716DD88005; Wed, 24 Jul 2024 08:03:08 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 2139C8881B for ; Wed, 24 Jul 2024 08:03:04 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0C23F106F; Tue, 23 Jul 2024 23:03:29 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id ECE803F766; Tue, 23 Jul 2024 23:02:58 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Heinrich Schuchardt , Simon Glass , Marek Vasut , Mark Kettenis , Michal Simek , Patrick DELAUNAY , Patrice CHOTARD , Huan Wang , Angelo Dureghello , Daniel Schwierzeck , Thomas Chou , Rick Chen , Max Filippov , Sughosh Ganu Subject: [PATCH 04/40] lib: Convert str_to_list() to use alist Date: Wed, 24 Jul 2024 11:31:48 +0530 Message-Id: <20240724060224.3071065-5-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240724060224.3071065-1-sughosh.ganu@linaro.org> References: <20240724060224.3071065-1-sughosh.ganu@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean From: Simon Glass Use this new data structure in the utility function. Signed-off-by: Simon Glass Signed-off-by: Sughosh Ganu --- lib/strto.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/strto.c b/lib/strto.c index f83ac67c66..277e83b20f 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -9,6 +9,7 @@ * Wirzenius wrote this portably, Torvalds fucked it up :-) */ +#include #include #include #include @@ -226,37 +227,39 @@ void str_to_upper(const char *in, char *out, size_t len) const char **str_to_list(const char *instr) { - const char **ptr; - char *str, *p; - int count, i; + struct alist alist; + char *str, *p, *start; /* don't allocate if the string is empty */ str = *instr ? strdup(instr) : (char *)instr; if (!str) return NULL; - /* count the number of space-separated strings */ - for (count = 0, p = str; *p; p++) { + alist_init_struct(&alist, char *); + + if (*str) + alist_add(&alist, str, char *); + for (start = str, p = str; *p; p++) { if (*p == ' ') { - count++; *p = '\0'; + start = p + 1; + if (*start) + alist_add(&alist, start, char *); } } - if (p != str && p[-1]) - count++; - /* allocate the pointer array, allowing for a NULL terminator */ - ptr = calloc(count + 1, sizeof(char *)); - if (!ptr) { - if (*str) + /* terminate list */ + p = NULL; + alist_add(&alist, p, char *); + if (alist_err(&alist)) { + alist_uninit(&alist); + + if (*instr) free(str); return NULL; } - for (i = 0, p = str; i < count; p += strlen(p) + 1, i++) - ptr[i] = p; - - return ptr; + return alist_uninit_move(&alist, NULL, const char *); } void str_free_list(const char **ptr)