view toys/posix/sleep.c @ 818:264b9da809df

Simplify license text, as mentioned on the mailing list. Reasoning: it was never my intent to require anybody to copy license text into another project if they cut and pasted something out of toybox. The "permission for any purpose" is as close to public domain as you can get in our current screwed up legal system without making people uncomfortable the _other_ way. (Besides, my initial reading of that was "all copies of the source code" but that's not what it says, and somebody pointed out that Android has "show license text" options because paranoid lawyers think that sort of thing applies to the BINARY version, which is nuts.)
author Rob Landley <rob@landley.net>
date Thu, 14 Mar 2013 09:02:37 -0500
parents 786841fdb1e0
children ce0519f6457c
line wrap: on
line source

/* sleep.c - Wait for a number of seconds.
 *
 * Copyright 2007 Rob Landley <rob@landley.net>
 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
 *
 * See http://opengroup.org/onlinepubs/9699919799/utilities/sleep.html

USE_SLEEP(NEWTOY(sleep, "<1", TOYFLAG_BIN))

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

    Wait before exiting.

config SLEEP_FLOAT
  bool
  default y
  depends on SLEEP && TOYBOX_FLOAT
  help
    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);
  }
}