On Tuesday 25 July 2006 16:33, Alan McKinnon wrote:

> Um, no. Read my post again. The command 'test' and the command '['
> have *different* syntax so cannot possible be links to each other and
> still have it work. The command does behave differently depending on
> the name it is called with, but this does not change the syntax used
> on the command line that invokes it.

There are two cases:

[1] syntax checking is done by the shell, or
[2] syntax checking is done by the program.

The interesting case is [2]. Let's make an oversimplified example. 
Suppose you want the commands "commandA" and "commandB".
The syntax for commandA is 

commandA <arg1> <arg2>

but commandB takes a third argument, so you invoke it with

commandB <arg1> <arg2> <arg3>

This, to me, qualifies as "*different* syntax".

Here is how those checks can be implemented _using the same program_.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {

  char *progname;

  /* the following should actually extract the basename of
     the program from argv[0], but to keep things simple 
     let's assume that the shell does this for us */
  progname = argv[0];

  if (strcmp(progname, "commandA") == 0) {

    /* check that we have TWO arguments */
    if (argc == 3) {
      printf("Ok, running as commandA with two arguments\n");
      exit(0);
    } else {
      printf("Running as commandA, but with the wrong number of 
arguments\n");
      exit(1);
    }

  } else if (strcmp(progname, "commandB") == 0) {
    /* check that we have THREE arguments */
    if (argc == 4) {
      printf("Ok, running as commandB with three arguments\n");
      exit(0);
    } else {
      printf("Running as commandB, but with the wrong number of 
arguments\n");
      exit(1);
    }
  } else {
    printf("Bad program name!\n");
    exit(1);
  }
}

In fact, as I said in a previous post, [ and test are built from the same 
source file, and the "[" vs. "test" difference is used only to check for 
proper syntax.
-- 
gentoo-user@gentoo.org mailing list

Reply via email to