annotate toys/pending/test.c @ 1276:d48bdc1cb017 draft

Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
author Rob Landley <rob@landley.net>
date Tue, 06 May 2014 06:31:28 -0500
parents 9927d1d1e7e2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
832
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* test.c - evaluate expression
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2013 Rob Landley <rob@landley.net>
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
6
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_TEST(NEWTOY(test, NULL, TOYFLAG_USR|TOYFLAG_BIN))
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
8
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config TEST
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
10 bool "test"
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default n
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
13 usage: test [-bcdefghLPrSsuwx PATH] [-nz STRING] [-t FD] [X ?? Y]
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
14
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
15 Return true or false by performing tests. (With no arguments return false.)
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
16
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
17 --- Tests with a single argument (after the option):
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
18 PATH is/has:
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -b block device -f regular file -p fifo -u setuid bit
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -c char device -g setgid -r read bit -w write bit
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
21 -d directory -h symlink -S socket -x execute bit
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
22 -e exists -L symlink -s nonzero size
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
23 STRING is:
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
24 -n nonzero size -z zero size (STRING by itself implies -n)
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
25 FD (integer file descriptor) is:
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
26 -t a TTY
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
27
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
28 --- Tests with one argument on each side of an operator:
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
29 Two strings:
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
30 = are identical != differ
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
31 Two integers:
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
32 -eq equal -gt first > second -lt first < second
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
33 -ne not equal -ge first >= second -le first <= second
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
34
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
35 --- Modify or combine tests:
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
36 ! EXPR not (swap true/false) EXPR -a EXPR and (are both true)
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
37 ( EXPR ) evaluate this first EXPR -o EXPR or (is either true)
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
38 */
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
39
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
40 #include "toys.h"
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
41
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
42 void test_main(void)
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
43 {
964
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
44 int id, not;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
45 char *s, *err_fmt = "Bad flag '%s'";
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
46
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
47 toys.exitval = 2;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
48 if (!strcmp("[", toys.which->name))
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
49 if (!strcmp("]", toys.optargs[--toys.optc])) error_exit("Missing ']'");
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
50 if (!strcmp("!", toys.optargs[0])) {
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
51 not = 1;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
52 toys.optargs++;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
53 toys.optc--;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
54 }
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
55 if (!toys.optc) toys.exitval = 0;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
56 else if (toys.optargs[0][0] == '-') {
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
57 id = stridx("bcdefghLpSsurwxznt", toys.optargs[0][1]);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
58 if (id == -1 || toys.optargs[0][2]) error_exit(err_fmt, toys.optargs[0]);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
59 if (id < 12) {
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
60 struct stat st;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
61 int nolink;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
62
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
63 toys.exitval = 1;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
64 if (lstat(toys.optargs[1], &st) == -1) return;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
65 nolink = !S_ISLNK(st.st_mode);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
66 if (!nolink && (stat(toys.optargs[1], &st) == -1)) return;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
67
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
68 if (id == 0) toys.exitval = !S_ISBLK(st.st_mode); // b
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
69 else if (id == 1) toys.exitval = !S_ISCHR(st.st_mode); // c
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
70 else if (id == 2) toys.exitval = !S_ISDIR(st.st_mode); // d
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
71 else if (id == 3) toys.exitval = 0; // e
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
72 else if (id == 4) toys.exitval = !S_ISREG(st.st_mode); // f
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
73 else if (id == 5) toys.exitval = !(st.st_mode & S_ISGID); // g
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
74 else if ((id == 6) || (id == 7)) toys.exitval = nolink; // hL
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
75 else if (id == 8) toys.exitval = !S_ISFIFO(st.st_mode); // p
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
76 else if (id == 9) toys.exitval = !S_ISSOCK(st.st_mode); // S
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
77 else if (id == 10) toys.exitval = st.st_size == 0; // s
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
78 else toys.exitval = !(st.st_mode & S_ISUID); // u
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
79 }
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
80 else if (id < 15) // rwx
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
81 toys.exitval = access(toys.optargs[1], 1 << (id - 12)) == -1;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
82 else if (id < 17) // zn
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
83 toys.exitval = toys.optargs[1] && !*toys.optargs[1] ^ (id - 15);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
84 else { // t
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
85 struct termios termios;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
86 toys.exitval = tcgetattr(atoi(toys.optargs[1]), &termios) == -1;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
87 }
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
88 }
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
89 else if (toys.optc == 1) toys.exitval = *toys.optargs[0] == 0;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
90 else if (toys.optc == 3) {
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
91 if (*toys.optargs[1] == '-') {
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
92 long a = atol(toys.optargs[0]), b = atol(toys.optargs[2]);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
93
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
94 s = toys.optargs[1] + 1;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
95 if (!strcmp("eq", s)) toys.exitval = a != b;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
96 else if (!strcmp("ne", s)) toys.exitval = a == b;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
97 else if (!strcmp("gt", s)) toys.exitval = a < b;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
98 else if (!strcmp("ge", s)) toys.exitval = a <= b;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
99 else if (!strcmp("lt", s)) toys.exitval = a > b;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
100 else if (!strcmp("le", s)) toys.exitval = a >= b;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
101 else error_exit(err_fmt, toys.optargs[1]);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
102 }
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
103 else {
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
104 int result = strcmp(toys.optargs[0], toys.optargs[2]);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
105
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
106 s = toys.optargs[1];
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
107 if (!strcmp("=", s)) toys.exitval = !!result;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
108 else if (!strcmp("!=", s)) toys.exitval = !result;
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
109 else error_exit(err_fmt, toys.optargs[1]);
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
110 }
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
111 }
9927d1d1e7e2 Implement test
Felix Janda <felix.janda@posteo.de>
parents: 832
diff changeset
112 toys.exitval ^= not;
832
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
113 return;
75526930cdd7 Add help text for pending test command. (No actual implementation yet.)
Rob Landley <rob@landley.net>
parents:
diff changeset
114 }