[net.unix-wizards] question about ctime

gds@Mit-Csr.ARPA (03/30/84)

time(2) in unix v6 says it takes as argument a long, and returns suitable
information for ctime(3) to return a character pointer.  However, I'm
getting strange results when I try to do a couple of things.

executing
main()
{
          long i;
          char *t;

          t = ctime(time(&i));
}
results in a core dump.  The sources to date(1) show time(2) as taking
int [2] rather than long.  So when I tried the following, I got a bus
error core dump.

main()
{
          int i[2];
          char *t;

          t = ctime(time(i));
          puts(t);
}

when substituting t[80] for *t, it did not compile, claiming that the
line which the assignment to t is on needed an lvalue (i presume it is
the "t" that doesn't have one).

Am I doing something wrong, or is unix?  Should I be loading some
special library?
-------

gwyn@Brl-Vld.ARPA (03/30/84)

From:      Doug Gwyn (VLD/VMB) <gwyn@Brl-Vld.ARPA>

The reason for int tval[2] instead of long tval was that the original
6th Edition UNIX C or its precursors did not know about the "long" data
type, so two (short) ints were used to hold long data.

I suspect your real problem however is that you are not declaring
the type of the time() function to be a long.  If you fail to declare
a function, C will assume that it is int-valued.

You should also look at the actual source for the time() function.  I
have seen at least three versions of this on 6th Edition UNIXes.  Maybe
yours does not return a long value but rather a 0-or-1 success
indicator (in which case, call time() first then call ctime() later
with the address of the storage that ctime() filled).

jab@uokvax.UUCP (04/03/84)

#R:sri-arpa:-44200:uokvax:6200023:000:1326
uokvax!jab    Apr  3 11:01:00 1984

/***** uokvax:net.unix-wizar / sri-arpa!ARPA / 12:00 am  Apr  2, 1984 */
time(2) in unix v6 says it takes as argument a long, and returns suitable
information for ctime(3) to return a character pointer.  However, I'm
getting strange results when I try to do a couple of things.

executing
main()
{
          long i;
          char *t;

          t = ctime(time(&i));
}
results in a core dump.  The sources to date(1) show time(2) as taking
int [2] rather than long.  So when I tried the following, I got a bus
error core dump.

main()
{
          int i[2];
          char *t;

          t = ctime(time(i));
          puts(t);
}

when substituting t[80] for *t, it did not compile, claiming that the
line which the assignment to t is on needed an lvalue (i presume it is
the "t" that doesn't have one).

Am I doing something wrong, or is unix?  Should I be loading some
special library?
-------
/* ---------- */

No, no, no!

time(2) returns a long. Fine. Both time(2) and ctime(3) take pointers
to longs. On the older versions of the pdp-11, longs were passed as
a pointer to two integers, and so something like the above worked, sort of.

If you run lint(1) on the above program, it will howl and bitch until
doomsday.

(For those of you who use "time_t", fine, just "s/long/time_t/g" in the
above.)

	Jeff Bowles
	Lisle, IL

johnl@haddock.UUCP (04/04/84)

#R:sri-arpa:-44200:haddock:16800010:000:346
haddock!johnl    Apr  3 11:10:00 1984

In this code:

	main()
	{
		  long i;
		  char *t;

		  t = ctime(time(&i));
	}

the problem is simple.  The argument to ctime() is a pointer to a long,
not the long itself.  This will work on all of the Unix systems I know:

		  time(&i);
		  t = ctime(&i);

More careful reading of your manual would have revealed this.

John Levine, ima!johnl

dixon@ihuxl.UUCP (D. A. Dixon) (04/16/84)

Two points regarding the program 

main()
{
	long i;
	char *t;

	t = ctime(time(&i));
}


1.) time() returns a long where as ctime() requires the address of
    a long;

2.) on a PDP 11 you must declare time() to be a function returning
    a long, otherwise it will default to a function returning an
    int (which is a 16 bit quantity on the PDP 11). Also, as a matter
    of form you should declare ctime() to be a function returning a
    pointer.



D A Dixon
ihnp4!ihuxl!dixon