--- ha_logger.c.orig	2010-06-04 22:30:35.330481510 +0200
+++ ha_logger.c	2010-06-04 23:52:27.023105705 +0200
@@ -17,6 +17,9 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
+ *
+ * revised by Andreas Mock (andreas.mock@web.de), 2010-06-04
+ *
  */
 #include <lha_internal.h>
 #include <glib.h>
@@ -38,129 +41,105 @@
 #define EXIT_FAIL	1
 
 int LogToDaemon(int priority, const char * buf, int bstrlen, gboolean use_pri_str);
-void            cl_log(int priority, const char * fmt, ...) G_GNUC_PRINTF(2,3);
-static void
-usage(void)
-{
-	printf("usage: "
-	       "ha_logger [-t tag] [-D <ha-log/ha-debug>] [message]\n");
+void cl_log(int priority, const char * fmt, ...) G_GNUC_PRINTF(2,3);
+
+
+static void usage(void) {
+	printf("usage: ha_logger [-t tag] [-D <ha-log/ha-debug>] [message]\n");
 	return;
 }
-int
-main(int argc, char** argv)
-{
-	int	priority; 
-	char*	entity = NULL;
+
+
+int main(int argc, char* argv[]) {
+	int	priority;
+	char* entity = NULL;
 	int	c;
-	char	buf[1024];
+	char buf[1025];
 	const char* logtype = "ha-log";
 
 	
-	while (( c =getopt(argc, argv,"t:D:h")) != -1){
-		switch(c){
-			
-		case 't':
-			entity = optarg;
-			break;
-		case 'D':
-			logtype=optarg;
-			break;
-		case 'h':
-			usage();
-			exit(1);		
-		default:
-			usage();
-			exit(1);
+	while((c = getopt(argc, argv, "t:D:h")) != -1) {
+		switch(c) {
+            case 't':
+                entity = optarg;
+                break;
+            case 'D':
+                logtype=optarg;
+                break;
+            case 'h':
+                usage();
+                exit(EXIT_FAIL);		
+            default:
+                usage();
+                exit(EXIT_FAIL);
 		}
-		
 	}
 
-	if(!cl_log_test_logd()){
+	if(!cl_log_test_logd()) {
 		fprintf(stderr, "logd is not running");
 		return EXIT_FAIL;
 	}
-	
-	argc -=optind;
-	argv += optind;
-		
-	if (entity != NULL){
+
+	/* reuse c as index counter in argv array */
+	int c = optind;
+
+    /* setting logging tag */
+	if(entity != NULL) {
 		cl_log_set_entity(entity);		
 	}
-	
-	if (strcmp(logtype, "ha-log") == 0){
+
+    /* setting logging level */
+	if(strncmp(logtype, "ha-log", 6) == 0) {
 		priority = LOG_INFO;
-	} else if (strcmp(logtype, "ha-debug") == 0){
+	}
+    else if(strncmp(logtype, "ha-debug", 8) == 0) {
 		priority = LOG_DEBUG;
-	}else{
-		goto err_exit;
+	}
+    else {
+        usage();
+        return(EXIT_FAIL);
 	}	
-	
-	if (argc > 0){
-		
-		register char *p, *endp;
-		int len;
-		
-		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
-			len = strlen(*argv);
-			if (p + len > endp && p > buf) {
-				if (LogToDaemon(priority,buf,
-						strnlen(buf, 1024),FALSE) ==HA_OK){
+
+    /* is message given on the command line? */
+	if(c < argc) {
+        register char* start;
+		int index;
+        size_t length;
+        for(index = optind; index < argc; index++) {
+            start = argv[index];
+            length = strlen(start);
+            while(length > 0) {
+                strncpy(buf, start, 1024);
+                buf[1024] = (char) 0;
+                length = strlen(buf);
+				if(LogToDaemon(priority, buf, length, FALSE) == HA_OK) {
+                    start = start + length;
+                    length = strlen(start);
 					continue;
-				}else{
-					return EXIT_FAIL;
 				}
-				/* NOTREACHED */
-				/* p = buf; */
-			}
-			if (len > sizeof(buf) - 1) {
-				if (LogToDaemon(priority,*argv,
-						strnlen(*argv, 1024),FALSE) ==HA_OK){
-					argv++;
-					continue;
-				}else{
+                else {
 					return EXIT_FAIL;
 				}
-				
-			} else {
-				if (p != buf){
-					*p++ = ' ';
-				}
-				memcpy(p, *argv++, len);
-				*(p += len) = '\0';
-			}
-		}
-		if (p != buf) {
-			if (LogToDaemon(priority,buf,
-					strnlen(buf, 1024),FALSE) ==HA_OK){
-				return EXIT_OK;
-			}else{
-				return EXIT_FAIL;
-			}
-		}
-		
-
-	}else {
-		while (fgets(buf, sizeof(buf), stdin) != NULL) {
-			/* glibc is buggy and adds an additional newline,
-			   so we have to remove it here until glibc is fixed */
-			int len = strlen(buf);
-			
-			if (len > 0 && buf[len - 1] == '\n')
-				buf[len - 1] = '\0';
-			
-			if (LogToDaemon(priority, buf,strlen(buf), FALSE) == HA_OK){
+            }
+        }
+	}
+    /* input from stdin */
+    else {
+		while(fgets(buf, sizeof(buf), stdin) != NULL) {
+			size_t length = strlen(buf);
+            /* strip off LF at the end */
+			if(length > 0 && buf[length - 1] == '\n') {
+				buf[length - 1] = '\0';
+            }
+			if(LogToDaemon(priority, buf, strlen(buf), FALSE) == HA_OK) {
 				continue;
-			}else {
-				return EXIT_FAIL;
+			}
+            else {
+                return EXIT_FAIL;
 			}
 		}
-		
-		return EXIT_OK;
 	}
-	
- err_exit:
-	usage();
-	return(1);
 
+    return EXIT_OK;
 }
 
