[BusyBox] [PATCH] less size reduction patch

Tito farmatito at tiscali.it
Wed May 18 14:36:28 UTC 2005


Hi,
This patch is not intended to be applied as  at the moment i still
don't understand fully the less applet code.
It is just a suggestion how things could be tweaked 
for size optimization and should be rewiewed by the
author.
It is tested only a little bit and somewhat hazardous ;-) .

BTW it removes the warnings at compile time so we
can work on the real ones.

One liners changed are just commented out,
blocks changed are ripped out as they distracted
the eye to much.

Ciao and good hacking,
Tito

PS.: there are a few formatting and trivial changes, sorry for that,
       they slipped in while hacking and i was to lazy to fix them.

-------------- next part --------------
--- miscutils/less_orig.c	2005-05-16 13:33:12.000000000 +0200
+++ miscutils/less.c	2005-05-18 16:18:59.000000000 +0200
@@ -68,45 +68,47 @@
 #define MAXLINES 10000
 
 /* Function prototypes */
-static void tty_width_height();
-static void set_tty_cooked();
-static void set_tty_raw();
+
+/* Get height and width of terminal */
+#define tty_width_height()            get_terminal_width_height(0, &height, &width)
+static void set_tty_cooked(void);
+static void set_tty_raw(void);
 static void tless_exit(int code);
-int tless_getch();
+int tless_getch(void);
 static void move_cursor(int x, int y);
-static void clear_line();
-static void data_readlines();
-static void free_flines();
-int calc_percent();
+static void clear_line(void);
+static void data_readlines(void);
+static void free_flines(void);
+int calc_percent(void);
 int reverse_percent(int percentage);
-static void equals_status();
-static void m_status_print();
-static void medium_status_print();
-static void status_print();
-static void buffer_print();
-static void buffer_init();
+/*static void equals_status(void);*/
+static void m_status_print(void);
+static void medium_status_print(void);
+static void status_print(void);
+static void buffer_print(void);
+static void buffer_init(void);
 static void buffer_down(int nlines);
 static void buffer_up(int nlines);
 static void buffer_line(int linenum);
 static void keypress_process(int keypress);
-static void colon_process();
+static void colon_process(void);
 static void number_process(int first_digit);
-static void flag_change();
-static void examine_file();
-static void next_file();
-static void previous_file();
-static void first_file();
-static void remove_current_file();
-static void full_repaint();
-static void add_linenumbers();
-static void regex_process();
+static void flag_change(void);
+static void examine_file(void);
+static void next_file(void);
+static void previous_file(void);
+static void first_file(void);
+static void remove_current_file(void);
+static void full_repaint(void);
+static void add_linenumbers(void);
+static void regex_process(void);
 char *process_regex_on_line(char *line, regex_t *pattern);
 int less_main(int argc, char *argv[]);
 
 static int height;
 static int width;
-static char files[10][256];
-static char filename[256];
+static char **files;
+static char *filename;
 static char buffer[100][256];
 static char *flines[MAXLINES];
 static int current_file = 1;
@@ -125,11 +127,6 @@
 /* Needed termios structures */
 static struct termios term_orig, term_vi;
 
-/* Get height and width of terminal */
-static void tty_width_height() {
-	get_terminal_width_height(0, &height, &width);
-}
-
 /* Reset terminal input to normal */
 static void set_tty_cooked() {
         fflush(stdout);
@@ -156,7 +153,8 @@
 		 "ti" and "te" termcap commands; can this be done with
 		 only termios.h? */
 	
-	printf("\n");
+	/*printf("\n");*/
+	putchar('\n');
 	exit(code);
 }
 
@@ -215,24 +213,13 @@
 	char current_line[256];
 	FILE *fp;
 	
-	fp = fopen(filename, "rt");
-	
-	if (!fp) {
-		sprintf(current_line, "Error opening file %s\n", filename);
-		perror(current_line);
-		exit(1);
-	}
+	fp = bb_xfopen(filename, "rt");
 	
 	for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) {
 		strcpy(current_line, "");
 		fgets(current_line, 256, fp);
-		if (ferror(fp)) {
-			sprintf(current_line, "Error reading from file %s\n", filename);
-			perror(current_line);
-			exit(1);
-		}
-		flines[i] = (char *) malloc((strlen(current_line) + 1) * sizeof(char));
-		strcpy(flines[i], current_line);
+		bb_xferror(fp, filename);
+		flines[i] = (char *) bb_xstrndup (current_line, (strlen(current_line) + 1) * sizeof(char));
 	}
 	num_flines = i - 2;
 	line_pos = 0;
@@ -272,7 +259,8 @@
 	if (!past_eof) {
 		if (!line_pos) {
 			if (num_files > 1)
-				printf("%s%s %s%i%s%i%s%i-%i/%i ", HIGHLIGHT, filename, "(file ", current_file, " of ", num_files, ") lines ", line_pos + 1, line_pos + height - 1, num_flines + 1);
+				printf("%s%s %s%i%s%i%s%i-%i/%i ", HIGHLIGHT, filename, "(file ", current_file, " of ", num_files, ") lines ",
+													line_pos + 1, line_pos + height - 1, num_flines + 1);
 			else {
 				printf("%s%s lines %i-%i/%i ", HIGHLIGHT, filename, line_pos + 1, line_pos + height - 1, num_flines + 1);
 			}
@@ -474,10 +462,11 @@
 			if (linenum + i < num_flines + 2)
 				strcpy(buffer[i], flines[linenum + i]);
 			else
-				if (TILDE_FLAG)
+				/*if (TILDE_FLAG)
 					strcpy(buffer[i], "\n");
 				else	
-					strcpy(buffer[i], "~\n");
+					strcpy(buffer[i], "~\n");*/
+				strcpy(buffer[i], (TILDE_FLAG) ? "\n" : "~\n");
 		}
 		line_pos = linenum;
 		/* Set past_eof so buffer_down and buffer_up act differently */
@@ -663,9 +652,11 @@
 static void examine_file() {
 	clear_line();
 	printf("Examine: ");
+	/*FIXME looks hazardous!!! */
 	scanf("%s", filename);
 	current_file = ++num_files;
-	strcpy(files[current_file - 1], filename);
+	/*strcpy(files[current_file - 1], filename);*/
+	files[current_file - 1] = filename;
 	free_flines();
 	data_readlines();
 	buffer_init();
@@ -675,7 +666,8 @@
 static void next_file() {
 	if (current_file != num_files) {
 		current_file++;
-		strcpy(filename, files[current_file - 1]);
+		/*strcpy(filename, files[current_file - 1]);*/
+		filename = files[current_file - 1];
 		free_flines();
 		data_readlines();
 		buffer_init();
@@ -690,7 +682,8 @@
 static void previous_file() {
 	if (current_file != 1) {
 		current_file--;
-		strcpy(filename, files[current_file - 1]);
+		/*strcpy(filename, files[current_file - 1]);*/
+		filename = files[current_file - 1];
 		free_flines();
 		data_readlines();
 		buffer_init();
@@ -705,7 +698,8 @@
 static void first_file() {
 	if (current_file != 1) {
 		current_file = 1;
-		strcpy(filename, files[current_file - 1]);
+		/*strcpy(filename, files[current_file - 1]);*/
+		filename = files[current_file - 1];
 		free_flines();
 		data_readlines();
 		buffer_init();
@@ -720,14 +714,16 @@
 	if (current_file != 1) {
 		previous_file();
 		for (i = 3; i <= num_files; i++)
-			strcpy(files[i - 2], files[i - 1]);
+			/*strcpy(files[i - 2], files[i - 1]);*/
+			files[i - 2] = files[i - 1];
 		num_files--;
 		buffer_print();
 	}
 	else {
 		next_file();
 		for (i = 2; i <= num_files; i++)
-			strcpy(files[i - 2], files[i - 1]);
+			/*strcpy(files[i - 2], files[i - 1]);*/
+			files[i - 2] = files[i - 1];
 		num_files--;
 		current_file--;
 		buffer_print();
@@ -747,17 +743,12 @@
 static void add_linenumbers() {
 
 	char current_line[256];
-	char line_number[300];
 	int i;
 	
 	for (i = 0; i <= num_flines; i++) {
-		strcpy(current_line, flines[i]);
-		sprintf(line_number, "%d ", i+1);
-		strcat(line_number, " ");
-		strcat(line_number, current_line);
-		free(flines[i]);
-		flines[i] = (char *) malloc(sizeof(char) * (strlen(line_number) + 1));
-		strcpy(flines[i], line_number);
+		safe_strncpy(current_line, flines[i], 256);
+		flines[i] = xrealloc(flines[i], strlen(current_line) +  /* MAXLINES = 10000 */ /* + 5 + 1 + 1 */ + 7 );
+		sprintf(flines[i],"%5d %s", i+1, current_line);
 	}
 }
 
@@ -769,7 +760,6 @@
 	
 	char current_line[256];
 	char uncomp_regex[100];
-	int err_no;
 	int i;
 	regex_t *pattern;
 	
@@ -782,18 +772,8 @@
 	fgets(uncomp_regex, 100, stdin);
 
 	/* Compile the regex and check for errors */
-	if ((err_no=regcomp(pattern, uncomp_regex, 0)) != 0) {
-		size_t length_r;
-		char *err_buf;
-		length_r = regerror(err_no, pattern, NULL, 0);
-		err_buf = malloc(length_r);
-		regerror(err_no, pattern, err_buf, length_r);
-		fprintf(stderr, "%s\n", err_buf);
-		free(err_buf);
-		regfree(pattern);
-		exit(1);
-	}
-	
+	xregcomp(pattern, uncomp_regex, 0);
+
 	/* Run the regex on each line of the current file here */
 	for (i = 0; i < num_flines; i++) {
 		
@@ -807,44 +787,33 @@
 	   should be returned. */
 }
 
-int less_main(int argc, char *argv[]) {
+int less_main(int argc, char **argv) {
 	
+	unsigned long flags;
 	int keypress;
-	int c;
-	int i;
-	int j = 0;
-	
-	opterr = 0;
-	
-	while ((c = getopt(argc, argv, "EMNm~")) != -1)
-		switch (c) {
-			case 'E': E_FLAG = 1;
-				  break;
-			case 'M': M_FLAG = 1;
-				  break;
-			case 'N': N_FLAG = 1;
-				  break;
-			case 'm': m_FLAG = 1;
-				  break;
-			case '~': TILDE_FLAG = 1;
-				  break;
-			default:
-				  break;
-		}
-	
-	for (i = optind; i < argc; i++) {
-		strcpy(files[j], argv[i]);
-		j++;
-	}
+
+	flags =  bb_getopt_ulflags(argc, argv, "EMNm~");
+	E_FLAG = (flags & 1);
+	M_FLAG = (flags & 2);
+	N_FLAG = (flags & 4);
+	TILDE_FLAG = (flags & 8);
 	
-	num_files = i - optind;
+	argc -= optind;
+	argv += optind;
+
+	files = argv;
+	num_files = argc;
 
 	if (!num_files) {
-		fputs("Missing filename (\"less -?\" for help)\n", stderr);
-		exit(1);
+		/* TODO Read stdin here instead ? */
+		/*fputs("Missing filename (\"less -?\" for help)\n", stderr);
+		exit(1);*/
+		bb_error_msg("Missing filename");
+		bb_show_usage();
 	}
 	
-	strcpy(filename, files[0]);
+	/*strcpy(filename, files[0]);*/
+	filename = files[0];
 	tty_width_height();
 	data_readlines();
 	buffer_init();


More information about the busybox mailing list