1 |
On Tuesday 25 July 2006 16:33, Alan McKinnon wrote: |
2 |
|
3 |
> Um, no. Read my post again. The command 'test' and the command '[' |
4 |
> have *different* syntax so cannot possible be links to each other and |
5 |
> still have it work. The command does behave differently depending on |
6 |
> the name it is called with, but this does not change the syntax used |
7 |
> on the command line that invokes it. |
8 |
|
9 |
There are two cases: |
10 |
|
11 |
[1] syntax checking is done by the shell, or |
12 |
[2] syntax checking is done by the program. |
13 |
|
14 |
The interesting case is [2]. Let's make an oversimplified example. |
15 |
Suppose you want the commands "commandA" and "commandB". |
16 |
The syntax for commandA is |
17 |
|
18 |
commandA <arg1> <arg2> |
19 |
|
20 |
but commandB takes a third argument, so you invoke it with |
21 |
|
22 |
commandB <arg1> <arg2> <arg3> |
23 |
|
24 |
This, to me, qualifies as "*different* syntax". |
25 |
|
26 |
Here is how those checks can be implemented _using the same program_. |
27 |
|
28 |
|
29 |
#include <stdio.h> |
30 |
#include <stdlib.h> |
31 |
#include <string.h> |
32 |
|
33 |
int main(int argc, char *argv[]) { |
34 |
|
35 |
char *progname; |
36 |
|
37 |
/* the following should actually extract the basename of |
38 |
the program from argv[0], but to keep things simple |
39 |
let's assume that the shell does this for us */ |
40 |
progname = argv[0]; |
41 |
|
42 |
if (strcmp(progname, "commandA") == 0) { |
43 |
|
44 |
/* check that we have TWO arguments */ |
45 |
if (argc == 3) { |
46 |
printf("Ok, running as commandA with two arguments\n"); |
47 |
exit(0); |
48 |
} else { |
49 |
printf("Running as commandA, but with the wrong number of |
50 |
arguments\n"); |
51 |
exit(1); |
52 |
} |
53 |
|
54 |
} else if (strcmp(progname, "commandB") == 0) { |
55 |
/* check that we have THREE arguments */ |
56 |
if (argc == 4) { |
57 |
printf("Ok, running as commandB with three arguments\n"); |
58 |
exit(0); |
59 |
} else { |
60 |
printf("Running as commandB, but with the wrong number of |
61 |
arguments\n"); |
62 |
exit(1); |
63 |
} |
64 |
} else { |
65 |
printf("Bad program name!\n"); |
66 |
exit(1); |
67 |
} |
68 |
} |
69 |
|
70 |
In fact, as I said in a previous post, [ and test are built from the same |
71 |
source file, and the "[" vs. "test" difference is used only to check for |
72 |
proper syntax. |
73 |
-- |
74 |
gentoo-user@g.o mailing list |