view toys/posix/sleep.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 6df4ccc0acbe
children ce0519f6457c
line wrap: on
line source

/* sleep.c - Wait for a number of seconds.
 * Copyright 2007 Rob Landley <>
 * Copyright 2012 Georgi Chorbadzhiyski <>
 * See


config SLEEP
  bool "sleep"
  default y
    usage: sleep SECONDS

    Wait before exiting.

  default y
  depends on SLEEP && TOYBOX_FLOAT
    The delay can be a decimal fraction. An optional suffix can be "m"
    (minutes), "h" (hours), "d" (days), or "s" (seconds, the default).

#include "toys.h"

void sleep_main(void)

  if (!CFG_TOYBOX_FLOAT) toys.exitval = sleep(atol(*toys.optargs));
  else {
    char *arg;
    double d = strtod(*toys.optargs, &arg);
    struct timespec tv;

    // Parse suffix
    if (*arg) {
      int ismhd[]={1,60,3600,86400};
      char *smhd = "smhd", *c = strchr(smhd, *arg);
      if (!c) error_exit("Unknown suffix '%c'", *arg);
      d *= ismhd[c-smhd];

    tv.tv_nsec=1000000000*(d-(tv.tv_sec = (unsigned long)d));
    toys.exitval = !!nanosleep(&tv, NULL);