aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
Diffstat (limited to 'toys')
-rw-r--r--toys/other/hello.c13
-rw-r--r--toys/other/netcat.c30
-rw-r--r--toys/pending/xzcat.c63
-rw-r--r--toys/posix/paste.c17
-rw-r--r--toys/posix/xargs.c8
5 files changed, 61 insertions, 70 deletions
diff --git a/toys/other/hello.c b/toys/other/hello.c
index b0fc5381..a6bc69eb 100644
--- a/toys/other/hello.c
+++ b/toys/other/hello.c
@@ -5,7 +5,9 @@
* See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
* See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
-USE_HELLO(NEWTOY(hello, "e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN))
+// Accept many different kinds of command line argument:
+
+USE_HELLO(NEWTOY(hello, "(walrus)(blubber):e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN))
config HELLO
bool "hello"
@@ -29,14 +31,18 @@ GLOBALS(
long c_number;
struct arg_list *d_list;
long e_count;
+ char *blubber_string;
int more_globals;
)
+// Parse many different kinds of command line argument:
+
void hello_main(void)
{
printf("Hello world\n");
+ if (toys.optflags) printf("flags=%x\n", toys.optflags);
if (toys.optflags & FLAG_a) printf("Saw a\n");
if (toys.optflags & FLAG_b) printf("b=%s\n", TT.b_string);
if (toys.optflags & FLAG_c) printf("c=%ld\n", TT.c_number);
@@ -44,7 +50,8 @@ void hello_main(void)
printf("d=%s\n", TT.d_list->arg);
TT.d_list = TT.d_list->next;
}
- if (TT.e_count) printf("e was seen %ld times", TT.e_count);
-
+ if (TT.e_count) printf("e was seen %ld times\n", TT.e_count);
while (*toys.optargs) printf("optarg=%s\n", *(toys.optargs++));
+ if (toys.optflags & FLAG_walrus) printf("Saw --walrus\n");
+ if (TT.blubber_string) printf("--blubber=%s\n", TT.blubber_string);
}
diff --git a/toys/other/netcat.c b/toys/other/netcat.c
index 0173e6db..a245d119 100644
--- a/toys/other/netcat.c
+++ b/toys/other/netcat.c
@@ -11,13 +11,13 @@ config NETCAT
bool "netcat"
default y
help
- usage: netcat [-wpq #] [-s addr] {IPADDR PORTNUM|-f FILENAME|-let} [-e COMMAND]
+ usage: netcat [-wpq #] [-s addr] [-u] {IPADDR PORTNUM|-f FILENAME}
- -w SECONDS timeout for connection
+ -f use FILENAME (ala /dev/ttyS0) instead of network
-p local port number
- -s local ipv4 address
-q SECONDS quit this many seconds after EOF on stdin.
- -f use FILENAME (ala /dev/ttyS0) instead of network
+ -s local ipv4 address
+ -w SECONDS timeout for connection
Use "stty 115200 -F /dev/ttyS0 && stty raw -echo -ctlecho" with
netcat -f to connect to a serial port.
@@ -27,13 +27,14 @@ config NETCAT_LISTEN
default y
depends on NETCAT
help
+ usage: netcat [-t] [-lL COMMAND...]
+
-t allocate tty (must come before -l or -L)
-l listen for one incoming connection.
-L listen for multiple incoming connections (server mode).
- Any additional command line arguments after -l or -L are executed
- to handle each incoming connection. If none, the connection is
- forwarded to stdin/stdout.
+ The command line after -l or -L is executed to handle each incoming
+ connection. If none, the connection is forwarded to stdin/stdout.
For a quick-and-dirty server, try something like:
netcat -s 127.0.0.1 -p 1234 -tL /bin/bash -l
@@ -68,7 +69,7 @@ static void lookup_name(char *name, uint32_t *result)
{
struct hostent *hostbyname;
- hostbyname = gethostbyname(name);
+ hostbyname = gethostbyname(name); // getaddrinfo
if (!hostbyname) error_exit("no host '%s'", name);
*result = *(uint32_t *)*hostbyname->h_addr_list;
}
@@ -102,8 +103,7 @@ void netcat_main(void)
struct sockaddr_in address;
// Setup socket
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (-1 == sockfd) perror_exit("socket");
+ sockfd = xsocket(AF_INET, SOCK_STREAM, 0);
fcntl(sockfd, F_SETFD, FD_CLOEXEC);
temp = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(temp));
@@ -140,7 +140,7 @@ void netcat_main(void)
}
// Do we need to return immediately because -l has arguments?
- if ((toys.optflags&FLAG_l) && toys.optc) {
+ if ((toys.optflags & FLAG_l) && toys.optc) {
if (fork()) goto cleanup;
close(0);
close(1);
@@ -170,7 +170,7 @@ void netcat_main(void)
if (!temp) close(sockfd);
dup2(fd, 0);
dup2(fd, 1);
- dup2(fd, 2);
+ if (toys.optflags&FLAG_L) dup2(fd, 2);
if (fd>2) close(fd);
}
}
@@ -186,10 +186,8 @@ void netcat_main(void)
// (Does not play well with -L, but what _should_ that do?)
set_alarm(0);
- if (CFG_NETCAT_LISTEN && (toys.optflags&(FLAG_L|FLAG_l) && toys.optc)) {
- execvp(*toys.optargs, toys.optargs);
- error_exit("Exec failed");
- }
+ if (CFG_NETCAT_LISTEN && (toys.optflags&(FLAG_L|FLAG_l) && toys.optc))
+ xexec(toys.optargs);
// Poll loop copying stdin->socket and socket->stdout.
for (;;) {
diff --git a/toys/pending/xzcat.c b/toys/pending/xzcat.c
index 5d69087c..3bbb8230 100644
--- a/toys/pending/xzcat.c
+++ b/toys/pending/xzcat.c
@@ -249,47 +249,8 @@ uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
return ~crc;
}
-/*
- * This must be called before any other xz_* function (but after crc_init())
- * to initialize the CRC64 lookup table.
- */
static uint64_t xz_crc64_table[256];
-void xz_crc64_init(void)
-{
- const uint64_t poly = 0xC96C5795D7870F42ULL;
-
- uint32_t i;
- uint32_t j;
- uint64_t r;
-
- for (i = 0; i < 256; ++i) {
- r = i;
- for (j = 0; j < 8; ++j)
- r = (r >> 1) ^ (poly & ~((r & 1) - 1));
-
- xz_crc64_table[i] = r;
- }
-
- return;
-}
-
-/*
- * Update CRC64 value using the polynomial from ECMA-182. To start a new
- * calculation, the third argument must be zero. To continue the calculation,
- * the previously returned value is passed as the third argument.
- */
-uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc)
-{
- crc = ~crc;
-
- while (size != 0) {
- crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
- --size;
- }
-
- return ~crc;
-}
// END xz.h
@@ -304,7 +265,19 @@ void xzcat_main(void)
const char *msg;
crc_init(xz_crc32_table, 1);
- xz_crc64_init();
+ const uint64_t poly = 0xC96C5795D7870F42ULL;
+ uint32_t i;
+ uint32_t j;
+ uint64_t r;
+
+ /* initialize CRC64 table*/
+ for (i = 0; i < 256; ++i) {
+ r = i;
+ for (j = 0; j < 8; ++j)
+ r = (r >> 1) ^ (poly & ~((r & 1) - 1));
+
+ xz_crc64_table[i] = r;
+ }
/*
* Support up to 64 MiB dictionary. The actually needed memory
@@ -2767,8 +2740,14 @@ static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)
s->crc = xz_crc32(b->out + s->out_start,
b->out_pos - s->out_start, s->crc);
else if (s->check_type == XZ_CHECK_CRC64)
- s->crc = xz_crc64(b->out + s->out_start,
- b->out_pos - s->out_start, s->crc);
+ s->crc = ~(s->crc);
+ size_t size = b->out_pos - s->out_start;
+ uint8_t *buf = b->out + s->out_start;
+ while (size) {
+ s->crc = xz_crc64_table[*buf++ ^ (s->crc & 0xFF)] ^ (s->crc >> 8);
+ --size;
+ }
+ s->crc=~(s->crc);
if (ret == XZ_STREAM_END) {
if (s->block_header.compressed != VLI_UNKNOWN
diff --git a/toys/posix/paste.c b/toys/posix/paste.c
index 92a52534..0e170cdb 100644
--- a/toys/posix/paste.c
+++ b/toys/posix/paste.c
@@ -28,28 +28,26 @@ GLOBALS(
void paste_main(void)
{
- char *p, *buf = toybuf;
- char **args = toys.optargs;
+ char *p, *buf = toybuf, **args = toys.optargs;
size_t ndelim = 0;
int i, j, c;
// Process delimiter list
// TODO: Handle multibyte characters
if (!(toys.optflags & FLAG_d)) TT.delim = "\t";
- p = TT.delim;
- for (; *p; p++, buf++, ndelim++) {
+ for (p = TT.delim; *p; p++, buf++, ndelim++) {
if (*p == '\\') {
p++;
if (-1 == (i = stridx("nt\\0", *p)))
error_exit("bad delimiter: \\%c", *p);
*buf = "\n\t\\\0"[i];
- }
- else *buf = *p;
+ } else *buf = *p;
}
*buf = 0;
if (toys.optflags & FLAG_s) { // Sequential
FILE *f;
+
for (; *args; args++) {
if ((*args)[0] == '-' && !(*args)[1]) f = stdin;
else if (!(f = fopen(*args, "r"))) perror_exit("%s", *args);
@@ -66,20 +64,21 @@ void paste_main(void)
if (f != stdin) fclose(f);
putchar('\n');
}
- }
- else { // Parallel
+ } else { // Parallel
// Need to be careful not to print an extra line at the end
FILE **files;
int anyopen = 1;
+
files = (FILE**)(buf + 1);
for (; *args; args++, files++) {
if ((*args)[0] == '-' && !(*args)[1]) *files = stdin;
else if (!(*files = fopen(*args, "r"))) perror_exit("%s", *args);
}
- for (; anyopen;) {
+ while (anyopen) {
anyopen = 0;
for (i = 0; i < toys.optc; i++) {
FILE **f = (FILE**)(buf + 1) + i;
+
if (*f) for (;;) {
c = getc(*f);
if (c != EOF) {
diff --git a/toys/posix/xargs.c b/toys/posix/xargs.c
index 18b70f2e..e1611ec3 100644
--- a/toys/posix/xargs.c
+++ b/toys/posix/xargs.c
@@ -25,6 +25,14 @@ config XARGS
#-r Don't run command with empty input
#-L Max number of lines of input per command
-E stop at line matching string
+
+config XARGS_PEDANTIC
+ bool "TODO xargs pedantic posix compatability"
+ default n
+ depends on XARGS
+ help
+ This version supports insane posix whitespace handling rendered obsolete
+ by -0 mode.
*/
#define FOR_xargs