[svn-commits] tilghman: branch tilghman/issue16461 r271337 - /team/tilghman/issue16461/funcs/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jun 18 13:39:57 CDT 2010


Author: tilghman
Date: Fri Jun 18 13:39:54 2010
New Revision: 271337

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=271337
Log:
Fixes before lunch

Modified:
    team/tilghman/issue16461/funcs/func_env.c

Modified: team/tilghman/issue16461/funcs/func_env.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/issue16461/funcs/func_env.c?view=diff&rev=271337&r1=271336&r2=271337
==============================================================================
--- team/tilghman/issue16461/funcs/func_env.c (original)
+++ team/tilghman/issue16461/funcs/func_env.c Fri Jun 18 13:39:54 2010
@@ -498,17 +498,20 @@
 			offset = ftello(ff);
 		}
 		if (length < 0) {
-			fseek(ff, length, SEEK_END);
-			if ((length = ftello(ff) - offset) < 0) {
+			fseeko(ff, length, SEEK_END);
+			if ((length = ftello(ff)) - offset < 0) {
 				/* Eliminates all results */
 				return -1;
 			}
+		} else if (length == LLONG_MAX) {
+			fseeko(ff, 0, SEEK_END);
+			length = ftello(ff);
 		}
 
 		ast_str_reset(*buf);
 
 		fseeko(ff, offset, SEEK_SET);
-		for (off_i = ftello(ff); off_i < flength && off_i <= offset + length; off_i += sizeof(fbuf)) {
+		for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) {
 			/* Calculate if we need to retrieve just a portion of the file in memory */
 			size_t toappend = sizeof(fbuf);
 
@@ -573,9 +576,11 @@
 			if (fseeko(ff, i, SEEK_SET)) {
 				ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
 			}
-			end = fread(fbuf, 1, sizeof(fbuf), ff);
-			fbuf[end] = '\0';
-			for (pos = fbuf + sizeof(buf) - 1; pos > fbuf - 1; pos--) {
+			if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
+				/* Not really necessary... */
+				fbuf[end] = '\0';
+			}
+			for (pos = end < sizeof(fbuf) ? fbuf + end : fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
 				LINE_COUNTER(pos, format, count);
 
 				if (length < 0 && count * -1 == length) {
@@ -739,14 +744,22 @@
 			}
 		}
 
-		if (offset < 0) {
-			fseeko(ff, offset, SEEK_END);
-		}
+		fseeko(ff, offset, offset < 0 ? SEEK_END : SEEK_SET);
 
 		if (length == vlength) {
 			/* Simplest case, a straight replace */
 			if (fwrite(value, 1, vlength, ff) < vlength) {
 				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
+		} else if (length == LLONG_MAX) {
+			/* Simple truncation */
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fflush(ff);
+			if (ftruncate(fileno(ff), ftello(ff))) {
+				ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
 			}
 			fclose(ff);
 		} else if (length > vlength) {
@@ -757,7 +770,7 @@
 			}
 			while (ftello(ff) < flength) {
 				fseeko(ff, length - vlength, SEEK_CUR);
-				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
 					ast_log(LOG_ERROR, "Short read?!!\n");
 				}
 				fseeko(ff, vlength - length, SEEK_CUR);
@@ -765,11 +778,10 @@
 					ast_log(LOG_ERROR, "Short write?!!\n");
 				}
 			}
-			fflush(ff);
-			if (ftruncate(fileno(ff), flength - (length - vlength))) {
+			fclose(ff);
+			if (truncate(args.filename, flength - (length - vlength))) {
 				ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
 			}
-			fclose(ff);
 		} else {
 			/* Most complex -- need to open a gap */
 			char fbuf[4096];




More information about the svn-commits mailing list