benhoyt / goawk

A POSIX-compliant AWK interpreter written in Go, with CSV support
https://benhoyt.com/writings/goawk/
MIT License
1.94k stars 84 forks source link

srand() initialization without an argument. #231

Closed JohnEarnest closed 1 month ago

JohnEarnest commented 6 months ago

The POSIX spec for Awk states:

srand([expr]): Set the seed value for rand to expr or use the time of day if expr is omitted. The previous seed value shall be returned.

This makes the expression srand(srand()) a portable (if rather bizarre) idiom for obtaining the current unix epoch in pure Awk. gawk, mawk, and the "one-true" awk support this idiom, but goawk does not appear to:

% gawk 'BEGIN{print srand(srand())}'
1714957797
% goawk 'BEGIN{print srand(srand())}'
1
% goawk --version
v1.27.0

I think you would be within your rights to declare supporting this idiom undesirable, but I'm not aware of any alternatives that are fully POSIX-compliant. Shelling out to date +%s, for example, might seem reasonable, but %s as a date format character does not appear to be guaranteed as of POSIX 1003.1-2017. Thoughts?

benhoyt commented 6 months ago

Interesting, and thanks for the report! I'll dig into this in the next week or so.

ko1nksm commented 5 months ago

This behavior is standardized in POSIX.1-2024.

https://www.austingroupbugs.net/view.php?id=983

FYI, date +%s is also standardized in POSIX.1-2024.

JohnEarnest commented 1 month ago

Excellent! Thanks for this fix, Ben!