[U-Boot-Users] [RFC/PATCH] Add expr command

Andreas Schweigstill andreas at schweigstill.de
Wed Feb 13 15:20:56 CET 2008


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/




More information about the U-Boot mailing list