Decoded: yes (coreutils)

[Back to Project Main Page]

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

Logical flow of yes command (coreutils)

Summary

yes - print a string until interrupted

[Source] [Code Walkthrough]

Lines of code: 134
Principal syscall: write() -- wrapped by full_write()
Support syscalls: None
Options: 2 (help and version)

Descended from yes in 4BSD (1980)
Added to Coreutils in November 1992 [First version]
Number of revisions: 101 [Code Evolution]

Helpers:
  • None
External non-standard helpers:
  • bad_cast() - Ensures character pointer is not const
  • error() - Outputs error message to standard error with possible process termination

Setup

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

The work is performed in main() using the following:

  • operands - The pointer to the beginning of operands
  • operand_lim - The pointer to the end of operands
  • operand_len - The length of the operand (presumably one word of the output message)
  • operandp - Iterator to the current operand (some word of the output message)
  • buf - The output message buffer
  • bufused - Index to the first unused buffer byte

Parsing

Parsing yes involves two checks:

  • Check for any long options
  • Check for anything else

Parsing failures

This failure case is explicitly checked:

  • An unknown option is passed

Execution

Basic execution looks like this:

  • Loop through all arguments (words of the output) to count the buffer size needed
  • If the message is short, ensure that a buffer capable of multiple copies is used. Size will be BUFSIZE, a page-aligned value
  • Fill the buffer once including a newline and save the size
  • Fill the remainder of the buffer with complete copies
  • Repeatedly write the buffer to the output. Note that the one full write is typically many lines rather than one write per line

The utility will never terminate. Practically speaking, the user will close STDOUT at some point and the queued write will fail, exiting with the failure condition.


[Back to Project Main Page]