sleep.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Return after fixed period of delay. Copyright 1984 (C) Richard Stallman
  2. Permission is granted to anyone to make or distribute
  3. verbatim copies of this program
  4. provided that the copyright notice and this permission notice are preserved;
  5. and provided that the recipient is not asked to waive or limit his right to
  6. redistribute copies as permitted by this permission notice;
  7. and provided that anyone possessing a machine-executable copy
  8. is granted access to copy the source code, in machine-readable form,
  9. in some reasonable manner.
  10. Permission is granted to distribute derived works or enhanced versions of
  11. this program under the above conditions with the additional condition
  12. that the entire derivative or enhanced work
  13. must be covered by a permission notice identical to this one.
  14. Anything distributed as part of a package containing portions derived
  15. from this program, which cannot in current practice perform its function
  16. usefully in the absence of what was derived directly from this program,
  17. is to be considered as forming, together with the latter,
  18. a single work derived from this program,
  19. which must be entirely covered by a permission notice identical to this one
  20. in order for distribution of the package to be permitted.
  21. In other words, you are welcome to use, share and improve this program.
  22. You are forbidden to forbid anyone else to use, share and improve
  23. what you give them. Help stamp out software-hoarding! */
  24. long argdecode ();
  25. main (argc, argv)
  26. int argc;
  27. char **argv;
  28. {
  29. int i;
  30. long time = 0;
  31. for (i = 1; i < argc; i++)
  32. {
  33. time += argdecode (argv[i]);
  34. }
  35. sleep (time);
  36. }
  37. long
  38. argdecode (s)
  39. char *s;
  40. {
  41. long value;
  42. int radix = 10;
  43. char *p = s;
  44. int c;
  45. if (*p != '0')
  46. radix = 10;
  47. else if (*++p == 'x')
  48. {
  49. radix = 16;
  50. p++;
  51. }
  52. else
  53. radix = 8;
  54. value = 0;
  55. while (((c = *p++) >= '0' && c <= '9')
  56. || (radix == 16 && (c & ~40) >= 'A' && (c & ~40) <= 'Z'))
  57. {
  58. value *= radix;
  59. if (c >= '0' && c <= '9')
  60. value += c - '0';
  61. else
  62. value += (c & ~40) - 'A';
  63. }
  64. if (c == 's')
  65. ;
  66. else if (c == 'm')
  67. value *= 60;
  68. else if (c == 'h')
  69. value *= 60 * 60;
  70. else if (c == 'd')
  71. value *= 60 * 60 * 24;
  72. else
  73. p--;
  74. if (*p)
  75. fatal ("invalid time interval %s", s);
  76. return value;
  77. }
  78. /* Print error message and exit. */
  79. fatal (s1, s2)
  80. char *s1, *s2;
  81. {
  82. error (s1, s2);
  83. exit (1);
  84. }
  85. /* Print error message. `s1' is printf control string, `s2' is arg for it. */
  86. error (s1, s2)
  87. char *s1, *s2;
  88. {
  89. printf ("sleep: ");
  90. printf (s1, s2);
  91. printf ("\n");
  92. }