From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andreas Schweigstill Date: Wed, 13 Feb 2008 15:20:56 +0100 Subject: [U-Boot-Users] [RFC/PATCH] Add expr command In-Reply-To: References: Message-ID: <47B2FCC8.3030003@schweigstill.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Hello! Kumar Gala schrieb: > Add a simple expr command that will set an env variable as the result > of the command. Good idea! I have been missing this for a long time but I also was too lazy to implement it. > + /* Validate arguments */ > + if ((argc != 5)) { > + printf("Usage:\n%s\n", cmdtp->usage); > + return 1; > + } It should also be checked if the operator has a length of exactly one character in order to prevent typos. + /* Validate arguments */ + if ((argc != 5) || (strlen(argv[3]) != 1)) { + printf("Usage:\n%s\n", cmdtp->usage); + return 1; + } May we rely on the compiler optimizing the above expression so strlen() != 1 will only be evaluated if argc == 5 ? Probably we have to write + ulong a, b; + char buf[16]; + int valid = 0; + + /* Validate arguments */ + if (argc == 5) { + if (strlen(argv[3]) == 1) { + valid = 1; + } + } + if (!valid) { + printf("Usage:\n%s\n", cmdtp->usage); + return 1; + } Regards Andreas Schweigstill -- Dipl.-Phys. Andreas Schweigstill Schweigstill IT | Embedded Systems Schauenburgerstra?e 116, D-24118 Kiel, Germany Phone: (+49) 431 5606-435, Fax: (+49) 431 5606-436 Mobile: (+49) 171 6921973, Web: http://www.schweigstill.de/