Decoded: link (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 link command (coreutils)

Summary

link - make a hard link to a file

[Source] [Code Walkthrough]

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

Added to Coreutils in April 2002 [First version]
Number of revisions: 50 [Code Evolution]

Helpers:
  • None
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
  • quoteaf_n() - Outputs a printable argument

Setup

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

main() initializes no additional variables


Parsing

Parsing for link involves two checks:

  • Check for any long options
  • Check for anything else

Parsing failures

These failure cases are explicitly checked:

  • If any non-long option is passed
  • If there are any missing operands (target files, etc)
  • If there are any extra operands

All failures result in a short error message followed by the usage instructions.


Execution

This wrapper utility passes user arguments directly to the link() syscall.

Utility usage...

link filename linkname

...matches link() syscall prototype

int link(const char *path1, const char *path2);

The success or failure of the syscall determines the return value of the utility

Extra comments

Here is how the link() syscall is handled in a modern linux kernel (v5.0):

(Declaration), (Execution)


[Back to Project Main Page]