Skip to main content

Regarding environment variable behaviour differences between C and COBOL

  • February 15, 2013
  • 0 replies
  • 0 views

Problem:

For ACCEPT FROM ENVIRONMENT-VALUE the COBOL Language reference states:

If no previous DISPLAY with a mnemonic-name associated with an ENVIRONMENT-NAME has been performed, or if the specified environment variable does not exist, the imperative statement associated with an ON EXCEPTION phrase is executed. The value in identifier is undefined in this case.

*****************

But, what happens when an environment variable has a value of NULL?

In Server Express 4.0 the ON EXCEPTION will be executed if the environment variable in not set or if it is set to a NULL value.

In OCDS 4.1 for UNIX, the ON EXCEPTION is only executed when an environment variable does not exist.

Also, in C the getenv() function can distinguish between an environment which isn't set, an environment variable set to NULL and an environment variable that is set.

The following C program shows the possible results:

$ cat envtest.c

#include <stdio.h>

#include <stdlib.h>

main() {

char    *p;

if((p = getenv("ENV_VAR")))

{

   if(p[0] == '\\0')

     printf("NULL string\\n");

   else

     printf("%s\\n", p);

}

else

   printf("getenv() returned NULL\\n");

}

In this case the string "getenv() returned NULL" is displayed when the environment variable is unset, "NULL string is displayed when ENV_VAR is a NULL string or the value of the environment variable will be displayed when it is set to a value.

Resolution:

Environment variables have three states.  The first two states are obvious:

1.  Set - has a value

2.  Unset - does not exist, undefined

The third state is in-between the first two.  In this state the environment variable exists but has a  NULL value, i.e. has been set using ENV_VAR=

The behaviour in Server Express is correct and complies with the COBOL standards.  The behaviour therefore in OCDS is incorrect.  The behaviour in C is irrelevant other than a way of showing the three states!

Basically, the phrase "the specified environment variable does not exist" is the key here.  In Server Express this is taken to mean "if the environment variable does not exist or if it exists but has a NULL value" because they are logically identical, the only difference is one of semantics.

Old KB# 4495