Guest User

Untitled

a guest
Jan 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include<stdio.h>
  2. #define HOUR 3600
  3. #define MINUTE 60
  4. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
  5. #define IS_WINDOWS 1
  6. #else
  7. #define IS_WINDOWS 0
  8. #endif
  9.  
  10. void scanf_int(char *q, int *n);
  11. int shutdown(int h, int m, int s);
  12. int calculate_seconds(int h, int m, int s);
  13.  
  14. int main() {
  15.  
  16. int h = 0;
  17. int m = 0;
  18. int s = 0;
  19.  
  20. printf("Simple Shutdown Timer for Windows.\n");
  21. printf("Written by: Joshua Paylaga\n\n");
  22.  
  23. scanf_int("Hour(s): ", &h);
  24. scanf_int("Minute(s): ", &m);
  25. scanf_int("Seconds(s): ", &s);
  26.  
  27. if ((h >= 0) && (m >= 0 && m < 60) && (s >= 0 && s < 60)) {
  28. if (shutdown(h, m, s) == 1) {
  29. printf("\nYour computer will shutdown in %d:%d:%d.\n\n", h, m, s);
  30. system("pause");
  31. }
  32. else {
  33. printf("\nThis program is intended only for Windows Machine.\n\n");
  34. }
  35. }
  36. else {
  37. printf("\nInvalid time provided.\n\n");
  38. }
  39.  
  40. return 0;
  41. }
  42.  
  43. /**
  44. * Accepts input from user and prevent run-time breakage
  45. * due to NULL input or wrong data type input.
  46. */
  47. void scanf_int(char *q, int *n) {
  48. printf("%s", q);
  49. char buf[40];
  50. while (fgets(buf, sizeof buf, stdin) != NULL) {
  51. if (sscanf(buf, "%d", n) != 1) {
  52. printf("%s", q);
  53. }
  54. else {
  55. break;
  56. }
  57. }
  58. }
  59.  
  60. /**
  61. * Shuts down your computer within the time given.
  62. * It utilizes the windows command shutdown /s /f /t {seconds}
  63. * to initialize shutdown. This will not work if compiled to
  64. * other operating systems.
  65. */
  66. int shutdown(int h, int m, int s) {
  67. int success = 0;
  68. if (IS_WINDOWS == 1) {
  69. int seconds = calculate_seconds(h, m, s);
  70. char command[250];
  71. sprintf(command, "shutdown /s /f /t %d", seconds);
  72. system(command);
  73. success = 1;
  74. }
  75. else {
  76. int seconds = calculate_seconds(h, m, s);
  77. char command[250];
  78. sprintf(command, "shutdown +h %d", seconds);
  79. system(command);
  80. success = 1;
  81. }
  82. return success;
  83. }
  84.  
  85. /**
  86. * Converts h:m:s time to seconds.
  87. */
  88. int calculate_seconds(int h, int m, int s) {
  89. int h_to_s = HOUR * h;
  90. int m_to_s = MINUTE * m;
  91. return (h_to_s + m_to_s + s);
  92. }
Add Comment
Please, Sign In to add comment