Decoded: sleep (coreutils)

[Back to Project Main Page]

Note: This page explores the design of command-line utilities. It is not a user guide.
[GNU Manual] [POSIX requirement] [Linux man] [FreeBSD man]

Logical flow of sleep command (coreutils)

Summary

sleep - delay for a specified time

[Source] [Code Walkthrough]

Lines of code: 155
Principal syscall: nanosleep()
Support syscalls: None
Options: 2 (help and version)

Spirtually linked to the sleep utility from Version 3 (4?) UNIX (1973)
Added to Shellutils in November 1992 [First version]
Number of revisions: 131 [Code Evolution]

Helpers:
  • apply_suffix() - Converts the input value to seconds from the given suffix basis
External non-standard helpers:
  • die() - Exit with mandatory non-zero error and message to stderr
  • error() - Outputs error message to standard error with possible process termination
  • xnanosleep() - gnulib wrapper for the nanosleep() syscall

Setup

sleep only uses the default parsing options, which require no initialization

main() initializes the following:

  • ok - The status of parsing
  • seconds - The number of seconds to sleep

Parsing

Parsing sleep means interpreting the interval argument. We separate the number from the suffix and then multiply the number based on the suffix to find the number of seconds.

Parsing failures

This failure case is explicitly checked:

  • No argument is provided
  • An unknown time interval is used

This failure result in a short error message followed by the usage instructions.


Execution

Execution is merely passing the calculated seconds to xnanosleep().

Failure occurs if xnanosleep() fails.


[Back to Project Main Page]