Mercurial > hg > toybox
annotate toys/pending/more.c @ 1294:88abd5b68a17 draft
dhcp client had a segfault, when DHCP message contained 'pad' option.
The parsing logic kept checking for other options beyond __pad__ option, without
checking if it was __end__ option after that or not.
author | Ashwini Sharma <ak.ashwini1981@gmail.com> |
---|---|
date | Wed, 21 May 2014 05:12:38 -0500 |
parents | 0ecfaa7022e8 |
children | e1dd7fb3920c |
rev | line source |
---|---|
1163
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* more.c - View FILE (or stdin) one screenful at a time. |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * Copyright 2013 Bilal Qureshi <bilal.jmi@gmail.com> |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * No Standard |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 USE_MORE(NEWTOY(more, NULL, TOYFLAG_USR|TOYFLAG_BIN)) |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 config MORE |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 bool "more" |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 default n |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 help |
1265
0ecfaa7022e8
usage: is lower case (the help generator looks for that, might as well be consistent).
Rob Landley <rob@landley.net>
parents:
1163
diff
changeset
|
13 usage: more [FILE]... |
1163
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 View FILE (or stdin) one screenful at a time. |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 */ |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 #define FOR_more |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 #include "toys.h" |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 #include <signal.h> |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 GLOBALS( |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 struct termios inf; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 int cin_fd; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 ) |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 static void signal_handler(int sig) |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 tcsetattr(TT.cin_fd, TCSANOW, &TT.inf); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 xputc('\n'); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 signal(sig, SIG_DFL); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 raise(sig); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 _exit(sig | 128); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 static void do_cat_operation(int fd, char *name) |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 char *buf = NULL; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 if(toys.optc > 1) printf(":::::::::::::::::::::::\n" |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 "%s\n:::::::::::::::::::::::\n",name); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 for (; (buf = get_line(fd)); free(buf)) printf("%s\n", buf); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 void more_main() |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 int ch, lines, input_key = 0, disp_more, more_msg_len; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 unsigned rows = 24, cols = 80; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 struct stat st; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 struct termios newf; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 FILE *fp, *cin; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 if (!isatty(STDOUT_FILENO) || !(cin = fopen("/dev/tty", "r"))) { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 loopfiles(toys.optargs, do_cat_operation); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 toys.exitval = 0; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 return; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 TT.cin_fd = fileno(cin); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 tcgetattr(TT.cin_fd,&TT.inf); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 //Prepare terminal for input |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 memcpy(&newf, &TT.inf, sizeof(struct termios)); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 newf.c_lflag &= ~(ICANON | ECHO); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 newf.c_cc[VMIN] = 1; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 newf.c_cc[VTIME] = 0; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 tcsetattr(TT.cin_fd, TCSANOW, &newf); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 sigatexit(signal_handler); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 do { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 fp = stdin; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 if (*toys.optargs && !(fp = fopen(*toys.optargs, "r"))) { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 perror_msg("'%s'", *toys.optargs); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 continue; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 st.st_size = disp_more = more_msg_len = lines = 0; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 fstat(fileno(fp), &st); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 terminal_size(&cols, &rows); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 rows--; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 if(toys.optc > 1) { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 printf(":::::::::::::::::::::::\n" |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 "%s\n:::::::::::::::::::::::\n",*toys.optargs); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 rows -= 3; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 while ((ch = getc(fp)) != EOF) { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 if (input_key != 'r' && disp_more) { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 more_msg_len = printf("--More-- "); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 if (st.st_size) |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 more_msg_len += printf("(%d%% of %lld bytes)", |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 (int) (100 * ( (double) ftell(fp) / (double) st.st_size)), |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 st.st_size); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 fflush(NULL); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 while (1) { |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 input_key = getc(cin); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 input_key = tolower(input_key); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 printf("\r%*s\r", more_msg_len, ""); // Remove previous msg |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 if (input_key == ' ' || input_key == '\n' || input_key == 'q' |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 || input_key == 'r') break; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 more_msg_len = printf("(Enter:Next line Space:Next page Q:Quit R:Show the rest)"); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 more_msg_len = lines = disp_more = 0; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 if (input_key == 'q') goto stop; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 terminal_size(&cols, &rows); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 rows--; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 if (ch == '\n') |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 if (++lines >= rows || input_key == '\n') disp_more = 1; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 putchar(ch); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 } |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 fclose(fp); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 fflush(NULL); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 } while (*toys.optargs && *++toys.optargs); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 stop: |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 tcsetattr(TT.cin_fd, TCSANOW, &TT.inf); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 fclose(cin); |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 toys.exitval = 0; |
972d4fc0c8a2
Two more commands (last and more) submitted by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 } |