+/*
+ This accepts a pointer to a nil terminated string, and ensures that there is a
+ newline as the last character. If there is not, a new buffer is allocated and
+ the newline is added. If a new buffer is allocated, the buffer passed in is
+ freed. The function returns a pointer which the caller should use, and must
+ free. In the event of an error, a nil pointer is returned.
+*/
+static char* ensure_nlterm( char* buf ) {
+ char* nb = NULL;
+ int len = 1;
+
+
+ nb = buf;
+ if( buf == NULL || (len = strlen( buf )) < 2 ) {
+ if( (nb = (char *) malloc( sizeof( char ) * 2 )) != NULL ) {
+ *nb = '\n';
+ *(nb+1) = 0;
+ }
+ } else {
+ if( buf[len-1] != '\n' ) {
+ fprintf( stderr, "[WRN] rmr buf_check: input buffer was not newline terminated (file missing final \\n?)\n" );
+ if( (nb = (char *) malloc( sizeof( char ) * (len + 2) )) != NULL ) {
+ memcpy( nb, buf, len );
+ *(nb+len) = '\n'; // insert \n and nil into the two extra bytes we allocated
+ *(nb+len+1) = 0;
+ }
+
+ free( buf );
+ }
+ }
+
+ return nb;
+}