/[dtapublic]/projs/trunk/projs/20161106_kicker/c_main.c
ViewVC logotype

Annotation of /projs/trunk/projs/20161106_kicker/c_main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 78 - (hide annotations) (download)
Mon Nov 7 03:39:51 2016 UTC (7 years, 4 months ago) by dashley
File MIME type: text/plain
File size: 6764 byte(s)
Implemented looping and CTRL-C/CTRL-BREAK handling.
1 dashley 77 // $Header$
2     //-------------------------------------------------------------------------------------------------
3     //This file is part of "kicker", a program for spawning threads and/or processes periodically in
4     //order to unstall certain Windows programs and scripts. The cause of the stall is unknown, but
5     //it is known that spawning threads and/or processes unstalls the system.
6     //-------------------------------------------------------------------------------------------------
7     //This source code and any program in which it is compiled/used is provided under the MIT License,
8     //reproduced below.
9     //-------------------------------------------------------------------------------------------------
10     //Permission is hereby granted, free of charge, to any person obtaining a copy of
11     //this software and associated documentation files(the "Software"), to deal in the
12     //Software without restriction, including without limitation the rights to use,
13     //copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the
14     //Software, and to permit persons to whom the Software is furnished to do so,
15     //subject to the following conditions :
16     //
17     //The above copyright notice and this permission notice shall be included in all
18     //copies or substantial portions of the Software.
19     //
20     //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21     //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22     //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23     //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24     //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25     //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26     //SOFTWARE.
27     //-------------------------------------------------------------------------------------------------
28     #include <assert.h>
29     #include <malloc.h>
30     #include <process.h>
31     #include <stdio.h>
32     #include <string.h>
33     #include <time.h>
34 dashley 78 #include <windows.h>
35 dashley 77
36 dashley 78 #define NELEM(A) (sizeof(A)/sizeof(A[0]))
37 dashley 77
38 dashley 78 void emit_hline_std(void)
39     {
40     printf("----------------------------------------------------------------------------------\n");
41     }
42    
43     void emit_mit_license(void)
44     {
45     const char *ref[] =
46     {
47     "Permission is hereby granted, free of charge, to any person obtaining a copy of",
48     "this software and associated documentation files(the \"Software\"), to deal in the",
49     "Software without restriction, including without limitation the rights to use,",
50     "copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the",
51     "Software, and to permit persons to whom the Software is furnished to do so,",
52     "subject to the following conditions :",
53     "",
54     "The above copyright notice and this permission notice shall be included in all",
55     "copies or substantial portions of the Software.",
56     "",
57     "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR",
58     "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",
59     "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE",
60     "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",
61     "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",
62     "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE",
63     "SOFTWARE."
64     };
65    
66     size_t i;
67    
68     for (i=0; i<NELEM(ref); i++)
69     {
70     printf("%s\n", ref[i]);
71     }
72     }
73    
74     const char *kickish_words[] =
75     {
76     "Kick",
77     "Smack",
78     "Slug",
79     "Punch",
80     "Assault",
81     "Hit",
82     "Strike",
83     "Slap",
84     "Smack",
85     "Spank",
86     "Cuff",
87     "Thump",
88     "Swat",
89     "Beat",
90     "Thrash",
91     "Batter",
92     "Pound",
93     "Pummel",
94     "Box",
95     "Whip",
96     "Flog",
97     "Cane",
98     "Wallop",
99     "Bash",
100     "Bop",
101     "Clout",
102     "Clip",
103     "Clobber",
104     "Sock",
105     "Swipe",
106     "Crown",
107     "Belt",
108     "Tan",
109     "Deck",
110     "Floor",
111     "Bump",
112     "Knock",
113     "Bang",
114     "Tap",
115     "Welt"
116     };
117    
118     volatile int ctrl_c_or_break_pressed = 0; //volatile because the Visual C++ environment spawns a thread to handle
119     //CTRL-C or CTRL-BREAK.
120    
121     BOOL CtrlHandler(DWORD fdwCtrlType)
122     {
123     switch (fdwCtrlType)
124     {
125     case CTRL_C_EVENT:
126     case CTRL_BREAK_EVENT:
127     ctrl_c_or_break_pressed = 1;
128     return(TRUE);
129     //There is no default case. There is no break statement. I've assigned a
130     //magic number to a variable. But the function still works fine.
131     //Can you believe it? It must be a miracle!
132     }
133    
134     return(FALSE); //Did I mention the multiple return points? This program should
135     //blow up and wipe out several city blocks!
136     }
137    
138 dashley 77 int c_main(int argc, char* argv[])
139 dashley 78 {
140     size_t selector = NELEM(kickish_words) - 1;
141     size_t ui;
142     int first_time = 1;
143    
144     emit_hline_std();
145     printf("kicker, Copyright 2016 David T. Ashley (dashley@gmail.com)\n");
146     emit_hline_std();
147     printf("This program is provided under the MIT License, reproduced below.\n");
148     emit_hline_std();
149     emit_mit_license();
150     emit_hline_std();
151     printf("This program accepts no command-line arguments and simply spawns a new thread\n");
152     printf("approximately every 1 second. The purpose of the program is to prevent\n");
153     printf("certain stalls that occur under Windows when running some programs/scripts\n");
154     printf("It is known that spawning a new thread periodically unstalls the programs/\n");
155     printf("scripts. The program can be terminated with CTRL-C or CTRL-BREAK.\n");
156     emit_hline_std();
157    
158     SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
159     //Yes, I know I'm supposed to check the return code.
160    
161     while (! ctrl_c_or_break_pressed)
162 dashley 77 {
163 dashley 78 //Backspace over the previous word.
164     if (! first_time)
165     {
166     //Beep(750, 500);
167    
168     for (ui = 0; ui < (strlen(kickish_words[selector]) + 1); ui++)
169     {
170     printf("%c", (char)8);
171     }
172     for (ui = 0; ui < (strlen(kickish_words[selector]) + 1); ui++)
173     {
174     printf(" ");
175     }
176     for (ui = 0; ui < (strlen(kickish_words[selector]) + 1); ui++)
177     {
178     printf("%c", (char)8);
179     }
180     }
181    
182     first_time = 0;
183    
184     //Go to next string.
185     if (selector < (NELEM(kickish_words) - 1))
186     selector++;
187     else
188     selector = 0;
189    
190     //Print the description.
191     printf("%s!", kickish_words[selector]);
192    
193     //Delay. Sleep() is likely the most effective way to not waste CPU capacity.
194     Sleep(1000);
195 dashley 77 }
196    
197 dashley 78 printf("\n");
198     emit_hline_std();
199     printf("Program terminated with CTRL-C or CTRL-BREAK.\n");
200     emit_hline_std();
201    
202     return(0);
203     }
204    
205 dashley 77 // End of c_main.c.

Properties

Name Value
svn:eol-style native
svn:keywords Header

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25