[dovecot-cvs] dovecot/src/lib var-expand.c,1.11,1.11.2.1

cras at dovecot.org cras at dovecot.org
Thu Aug 10 23:22:42 EEST 2006


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv21201/src/lib

Modified Files:
      Tag: branch_1_0
	var-expand.c 
Log Message:
Negative offsets count from the end of the string. Patch by Johannes Berg.



Index: var-expand.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/var-expand.c,v
retrieving revision 1.11
retrieving revision 1.11.2.1
diff -u -d -r1.11 -r1.11.2.1
--- var-expand.c	12 Apr 2006 07:39:32 -0000	1.11
+++ var-expand.c	10 Aug 2006 20:22:40 -0000	1.11.2.1
@@ -11,7 +11,8 @@
 #include <stdlib.h>
 
 struct var_expand_context {
-	unsigned int offset, width;
+	int offset;
+	unsigned int width;
 };
 
 struct var_expand_modifier {
@@ -71,7 +72,7 @@
 	}
 
 	str_printfa(hash, "%x", value);
-	while (str_len(hash) < ctx->offset)
+	while ((int)str_len(hash) < ctx->offset)
 		str_insert(hash, 0, "0");
         ctx->offset = 0;
 
@@ -109,6 +110,7 @@
 	const char *(*modifier[MAX_MODIFIER_COUNT])
 		(const char *, struct var_expand_context *);
 	unsigned int i, modifier_count;
+	int sign = 1;
 	bool zero_padding = FALSE;
 
 	memset(&ctx, 0, sizeof(ctx));
@@ -120,6 +122,10 @@
 
 			/* [<offset>.]<width>[<modifiers>]<variable> */
 			ctx.width = 0;
+			if (*str == '-') {
+				sign = -1;
+				str++;
+			}
 			if (*str == '0') {
 				zero_padding = TRUE;
 				str++;
@@ -132,7 +138,7 @@
 			if (*str != '.')
 				ctx.offset = 0;
 			else {
-				ctx.offset = ctx.width;
+				ctx.offset = sign * (int)ctx.width;
 				ctx.width = 0;
 				str++;
 				while (*str >= '0' && *str <= '9') {
@@ -178,9 +184,19 @@
 			if (var != NULL) {
 				for (i = 0; i < modifier_count; i++)
 					var = modifier[i](var, &ctx);
-				while (*var != '\0' && ctx.offset > 0) {
-					ctx.offset--;
-					var++;
+
+				if (ctx.offset < 0) {
+					/* if offset is < 0 then we want to
+					   start at the end */
+					size_t len = strlen(var);
+
+					if (len > (size_t)-ctx.offset)
+						var += len + ctx.offset;
+				} else {
+					while (*var != '\0' && ctx.offset > 0) {
+						ctx.offset--;
+						var++;
+					}
 				}
 				if (ctx.width == 0)
 					str_append(dest, var);
@@ -205,7 +221,7 @@
 	const struct var_expand_modifier *m;
 
 	/* [<offset>.]<width>[<modifiers>]<variable> */
-	while (*str >= '0' && *str <= '9')
+	while ((*str >= '0' && *str <= '9') || *str == '-')
 		str++;
 
 	if (*str == '.') {



More information about the dovecot-cvs mailing list