[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [BusyBox] tail patch
From:       "Allen Soard" <esp-software () mail ! hypermart ! net>
Date:       2000-07-21 21:00:12
[Download RAW message or body]

> THIS IS A MESSAGE IN 'MIME' FORMAT.  Your mail reader does not support MIME.
> Please read the first section, which is plain text, and ignore the rest.

The attached patch adds the +X option to tail.
It works by using the existing wc code to count
the lines and converting the +X option to a -n
option. so there are modifications to both tail.c
and wc.c. total binary cost 190 bytes.

-E.Allen Soard

Bookmark the HyperMart Small Business Center. All the tools you need to
succeed!
http://www.hypermart.net/center/
["busybox.tail+.patch" (application/octet-stream)]

diff -urN busybox.virgin/tail.c busybox/tail.c
--- busybox.virgin/tail.c	Fri Jul 21 10:26:53 2000
+++ busybox/tail.c	Fri Jul 21 13:05:24 2000
@@ -50,6 +50,7 @@
 #define BB_DECLARE_EXTERN
 #define bb_need_help
 #include "messages.c"
+#include "wc.h"
 
 
 #define XWRITE(fd, buffer, n_bytes)					\
@@ -334,6 +335,7 @@
 {
 	int exit_status = 0;
 	int n_units = DEFAULT_N_LINES;
+	int n_skip = 0;
 	int n_tmp, i;
 	char opt;
 
@@ -364,6 +366,9 @@
 					usage(tail_usage);
 				}
 			}
+		}
+		else if (argv[i][0] == '+') {
+			sscanf(argv[1],"+%d",&n_skip);
 		} else {
 			break;
 		}
@@ -380,8 +385,10 @@
 	if (i >= argc) {
 		exit_status |= tail_file("-", n_units);
 	} else {
-		for (; i < argc; i++)
+		for (; i < argc; i++){
+			n_units=wc_lines(argv[i])-(n_skip-2);
 			exit_status |= tail_file(argv[i], n_units);
+		}
 	}
 
 	return(exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
diff -urN busybox.virgin/usage.c busybox/usage.c
--- busybox.virgin/usage.c	Fri Jul 21 10:26:53 2000
+++ busybox/usage.c	Fri Jul 21 13:47:18 2000
@@ -1014,6 +1014,7 @@
 	"file name. With no FILE, or when FILE is -, read standard input.\n\n"
 	"Options:\n"
 	"\t-n NUM\t\tPrint last NUM lines instead of first 10\n"
+//	"\t+NUM\t\tSkip over the first NUM lines. overrides -n\n"
 
 	"\t-f\t\tOutput data as the file grows.  This version\n"
 	"\t\t\tof 'tail -f' supports only one file at a time.\n"
diff -urN busybox.virgin/wc.c busybox/wc.c
--- busybox.virgin/wc.c	Fri Jul 21 10:26:53 2000
+++ busybox/wc.c	Fri Jul 21 12:30:45 2000
@@ -21,14 +21,16 @@
  */
 
 #include "internal.h"
+#include "wc.h"
 #include <stdio.h>
 #include <getopt.h>
 
 static int total_lines, total_words, total_chars, max_length;
 static int print_lines, print_words, print_chars, print_length;
+int lines, words, chars, length;
 
 void print_counts(int lines, int words, int chars, int length,
-				  const char *name)
+				  const char *name) 
 {
 	char const *space = "";
 
@@ -53,7 +55,7 @@
 
 static void wc_file(FILE * file, const char *name)
 {
-	int lines, words, chars, length;
+//	int lines, words, chars, length;
 	int in_word = 0, linepos = 0;
 	int c;
 
@@ -91,7 +93,7 @@
 		length = linepos;
 	if (in_word)
 		words++;
-	print_counts(lines, words, chars, length, name);
+//	print_counts(lines, words, chars, length, name);
 	total_lines += lines;
 	total_words += words;
 	total_chars += chars;
@@ -100,7 +102,16 @@
 	fclose(file);
 	fflush(stdout);
 }
-
+int wc_lines(const char *name)
+{
+	FILE *file;
+	file = fopen(name, "r");
+	if (file == NULL) {
+		fatalError(name);
+	}
+	wc_file(file, name);
+	return lines;
+}
 int wc_main(int argc, char **argv)
 {
 	FILE *file;
@@ -142,6 +153,7 @@
 				fatalError(argv[optind]);
 			}
 			wc_file(file, argv[optind]);
+			print_counts(lines, words, chars, length, argv[optind]);
 			num_files_counted++;
 			optind++;
 		}
diff -urN busybox.virgin/wc.h busybox/wc.h
--- busybox.virgin/wc.h	Wed Dec 31 16:00:00 1969
+++ busybox/wc.h	Fri Jul 21 12:27:45 2000
@@ -0,0 +1,23 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini wc implementation for busybox
+ *
+ * by Edward Betts <edward@debian.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+int wc_lines(const char *name);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic