oga's tools
修訂 | 4131f5e37e0079bc94c398eb8c3a7ecf03f26286 (tree) |
---|---|
時間 | 2013-12-30 17:52:39 |
作者 | oga <oga@mxg....> |
Commiter | oga |
add tool for win
@@ -0,0 +1,252 @@ | ||
1 | +/* | |
2 | + * clip.cpp 指定された引数をクリップボードにコピーする。 | |
3 | + * SendToに入れると便利 | |
4 | + * | |
5 | + * 1999/06/28 V1.00 by oga. | |
6 | + * 2003/06/02 V1.01 漢字判定バグ対策 | |
7 | + * 2007/01/07 V1.02 support -cyg & -unc & bug fix | |
8 | + */ | |
9 | +#include <windows.h> | |
10 | +#include <stdio.h> | |
11 | + | |
12 | +#define ISKANJI(c) (((unsigned char)(c) >= 0x80 && (unsigned char)(c) < 0xa0) || ((unsigned char)(c) >= 0xe0 && (unsigned char)(c) < 0xff)) | |
13 | + | |
14 | + | |
15 | +int vf = 0; /* verbose */ | |
16 | +int cygf = 0; /* -cyg V1.02-A */ | |
17 | +int uncf = 0; /* -unc V1.02-A */ | |
18 | + | |
19 | +/* | |
20 | + * get_onepath() | |
21 | + * | |
22 | + * IN pt : 文字列 例 abcd\def\hij | |
23 | + * buf: 1個のパス要素を取得するバッファ | |
24 | + * OUT buf: ptの先頭から1個のパス要素を返す | |
25 | + * ret: 取得したパス要素の次のパス要素の先頭 | |
26 | + */ | |
27 | +char *get_onepath(char *pt, char *buf) | |
28 | +{ | |
29 | + int i = 0; | |
30 | + int kanjif = 0; | |
31 | + | |
32 | + if (*pt == '\0') { | |
33 | + return NULL; | |
34 | + } | |
35 | + while (*pt != '\0' && !(!kanjif && *pt == '\\')) { | |
36 | + buf[i] = *pt; | |
37 | + if (kanjif == 0 && ISKANJI(*pt)) { | |
38 | + kanjif = 1; | |
39 | + } else { | |
40 | + kanjif = 0; | |
41 | + } | |
42 | + ++pt; | |
43 | + ++i; | |
44 | + if (vf >= 2) printf("kf:%d 0x%02x\n", kanjif, *pt); | |
45 | + } | |
46 | + buf[i] = '\0'; | |
47 | + if (*pt == '\\') { | |
48 | + ++pt; /* \の次に移動 */ | |
49 | + } | |
50 | + return pt; | |
51 | +} | |
52 | + | |
53 | +/* | |
54 | + * UNCパスに変換 | |
55 | + * | |
56 | + * IN : drv ドライブ文字 | |
57 | + * IN : size uncpathバッファサイズ | |
58 | + * OUT: uncpath 変換されたUNCパス | |
59 | + * OUT: ret 0:成功 -1:失敗(uncpathにはエラー要因を格納) | |
60 | + */ | |
61 | +int GetUNCPath(char *drv, char *uncpath, DWORD *psize) | |
62 | +{ | |
63 | + int st; | |
64 | + | |
65 | + /* UNCパス取得 */ | |
66 | + st = WNetGetConnection(drv, uncpath, psize); | |
67 | + if (st != NO_ERROR) { | |
68 | + /* エラーの場合はuncpathにエラー要因を入れる */ | |
69 | + switch(st) { | |
70 | + case ERROR_BAD_DEVICE: | |
71 | + strcpy(uncpath, "BAD_DEVICE"); | |
72 | + break; | |
73 | + case ERROR_NOT_CONNECTED: | |
74 | + strcpy(uncpath, "NOT_CONNECTED"); | |
75 | + break; | |
76 | + case ERROR_CONNECTION_UNAVAIL: | |
77 | + strcpy(uncpath, "CONNECTION_UNAVAIL"); | |
78 | + break; | |
79 | + case ERROR_NO_NETWORK: | |
80 | + strcpy(uncpath, "NO_NETWORK"); | |
81 | + break; | |
82 | + case ERROR_NO_NET_OR_BAD_PATH: | |
83 | + strcpy(uncpath, "NO_NET_OR_BAD_PATH"); | |
84 | + break; | |
85 | + default: | |
86 | + sprintf(uncpath, "Error %d", st); | |
87 | + break; | |
88 | + } | |
89 | + return -1; /* Fail */ | |
90 | + } | |
91 | + return 0; /* Success */ | |
92 | +} | |
93 | + | |
94 | +void usage() | |
95 | +{ | |
96 | + printf("usage: clip [-v] [-cyg] [-unc] <str>\n"); | |
97 | + printf(" -cyg: cygwin format path\n"); | |
98 | + printf(" -unc: UNC format path\n"); | |
99 | + exit(1); | |
100 | +} | |
101 | + | |
102 | +int main(int a, char *b[]) | |
103 | +{ | |
104 | + int i; | |
105 | + char *path = NULL; | |
106 | + | |
107 | + for (i = 1; i < a; i++) { | |
108 | + if (!strcmp(b[i], "-h")) { | |
109 | + usage(); | |
110 | + } | |
111 | + if (!strcmp(b[i], "-cyg")) { /* V1.02-A */ | |
112 | + cygf = 1; | |
113 | + continue; | |
114 | + } | |
115 | + if (!strcmp(b[i], "-unc")) { /* V1.02-A */ | |
116 | + uncf = 1; | |
117 | + continue; | |
118 | + } | |
119 | + if (!strcmp(b[i], "-v")) { | |
120 | + ++vf; | |
121 | + continue; | |
122 | + } | |
123 | + path = b[i]; | |
124 | + } | |
125 | + | |
126 | + if (path == NULL) { | |
127 | + usage(); | |
128 | + } | |
129 | + | |
130 | + if (OpenClipboard(NULL) == FALSE) { | |
131 | + printf("OpenClipboard Error.(%d)\n",GetLastError()); | |
132 | + return 1; | |
133 | + } | |
134 | + | |
135 | + if (EmptyClipboard() == FALSE) { | |
136 | + printf("EmptyClipboard Error.(%d)\n",GetLastError()); | |
137 | + return 1; | |
138 | + } | |
139 | + | |
140 | + HGLOBAL hgp; | |
141 | + | |
142 | + /* グローバル領域取得 */ | |
143 | + if ((hgp = GlobalAlloc(GMEM_MOVEABLE| | |
144 | + GMEM_DDESHARE| | |
145 | + GMEM_ZEROINIT, | |
146 | + 1024)) == NULL) { | |
147 | + printf("GlobalAlloc Error.(%d)\n",GetLastError()); | |
148 | + return 1; | |
149 | + } | |
150 | + | |
151 | + char *str; /* ClipBoard出力用パス */ | |
152 | + char buf[1024]; | |
153 | + char shstr[1024]; /* short path */ | |
154 | + char wkshstr[1024] = ""; /* FindFirstFile用 work */ | |
155 | + | |
156 | + /* strにグローバルな領域を設定 */ | |
157 | + if ((str = (char *)GlobalLock(hgp)) == NULL) { | |
158 | + printf("GlobalLock Error.(%d)\n",GetLastError()); | |
159 | + return 1; | |
160 | + } | |
161 | + | |
162 | + strcpy(shstr, path); /* short path */ | |
163 | + | |
164 | + /* Change to Long Path */ | |
165 | + WIN32_FIND_DATA d; | |
166 | + HANDLE h; | |
167 | + char *pt = shstr; | |
168 | + char *newpt; | |
169 | + char *delm = "\\"; /* V1.02-A */ | |
170 | + | |
171 | + /* V1.02-A start */ | |
172 | + if (cygf) { | |
173 | + delm = "/"; | |
174 | + } | |
175 | + /* V1.02-A end */ | |
176 | + | |
177 | + while ((newpt = get_onepath(pt, buf)) != NULL) { | |
178 | + if (strlen(wkshstr)) strcat(wkshstr, "\\"); /* FindFirst用なので"\" */ | |
179 | + strcat(wkshstr, buf); | |
180 | + if (vf) printf("DEBUG1: wkshstr:[%s]\n", wkshstr); | |
181 | + | |
182 | + /* V1.02-A start */ | |
183 | + /* 取り出したパス要素がドライブ文字の場合 */ | |
184 | + if (strlen(buf) == 2 && buf[1] == ':') { | |
185 | + if (strlen(str)) strcat(str, delm); | |
186 | + | |
187 | + if (vf) printf("DEBUG0: drive:[%s]\n", buf); | |
188 | + | |
189 | + /* buf(ドライブ文字)をUNCに変換 */ | |
190 | + if (uncf) { | |
191 | + char uncpath[1024]; | |
192 | + DWORD size = sizeof(uncpath); | |
193 | + if (GetUNCPath(buf, uncpath, &size) == 0) { | |
194 | + /* ドライブ文字をUNCパスに置き換える */ | |
195 | + strcpy(buf, uncpath); | |
196 | + } else { | |
197 | + /* 変換エラー */ | |
198 | + if (vf) printf("GetUNCPath Error: %s\n", uncpath); | |
199 | + } | |
200 | + } | |
201 | + | |
202 | + if (cygf) { | |
203 | + if (uncf) { | |
204 | + strcat(str, buf); /* UNCをコピー */ | |
205 | + } else { | |
206 | + /* ドライブ名をコピー */ | |
207 | + sprintf(str, "/cygdrive/%s", buf); | |
208 | + /* cygwin pathの場合 ":"を消す */ | |
209 | + str[strlen(str)-1] = '\0'; | |
210 | + /* ドライブ名を小文字化 */ | |
211 | + str[strlen(str)-1] = tolower(str[strlen(str)-1]); | |
212 | + } | |
213 | + } else { | |
214 | + /* ドライブ文字にFindFirstすると現在パスが返ってくるため */ | |
215 | + /* ドライブ文字を直接コピーする */ | |
216 | + strcat(str, buf); | |
217 | + } | |
218 | + pt = newpt; | |
219 | + continue; | |
220 | + } | |
221 | + /* V1.02-A end */ | |
222 | + | |
223 | + /* ExplorerからはShortパスで渡されてくるためLongパス変換する */ | |
224 | + h = FindFirstFile(wkshstr, &d); | |
225 | + if (h != INVALID_HANDLE_VALUE) { | |
226 | + if (strlen(str)) strcat(str, delm); | |
227 | + strcat(str, d.cFileName); | |
228 | + if (vf) printf("DEBUG2: Long:[%s]\n", d.cFileName); | |
229 | + FindClose(h); | |
230 | + } else { | |
231 | + if (vf) printf("DEBUG3: short:[%s]\n", buf); | |
232 | + if (strlen(str)) strcat(str, delm); | |
233 | + strcat(str, buf); | |
234 | + } | |
235 | + pt = newpt; | |
236 | + } | |
237 | + | |
238 | + if (vf) printf("str=[%s]\n", str); | |
239 | + | |
240 | + GlobalUnlock(hgp); | |
241 | + | |
242 | + /* クリップボードにコピー */ | |
243 | + if (SetClipboardData(CF_TEXT, hgp) == NULL) { | |
244 | + printf("SetClipboardData Error.(%d)\n",GetLastError()); | |
245 | + return 1; | |
246 | + } | |
247 | + if (CloseClipboard() == FALSE) { | |
248 | + printf("CloseClipboard Error.(%d)\n",GetLastError()); | |
249 | + return 1; | |
250 | + } | |
251 | + return 0; | |
252 | +} |
@@ -0,0 +1,354 @@ | ||
1 | +/* | |
2 | + * du : ディレクトリの単位のディスク使用量の表示 | |
3 | + * | |
4 | + * 表示形式 | |
5 | + * ブロックベースディスク使用量KB [(ファイルサイズベース使用量KB)] Dir名 | |
6 | + * | |
7 | + * 97/09/27 V1.00 by oga. | |
8 | + * 00/06/12 V1.10 -o support | |
9 | + * 00/07/17 V1.11 continue not when not accessible dir | |
10 | + * 02/10/02 V1.12 -d (depth) support | |
11 | + * 05/08/31 V1.13 support large size dir | |
12 | + * 08/06/15 V1.14 support over 4gb file | |
13 | + */ | |
14 | +#include <windows.h> | |
15 | +#include <stdio.h> | |
16 | +#include <stdlib.h> | |
17 | +#include <string.h> | |
18 | +#include <sys/types.h> | |
19 | + | |
20 | +/* macros */ | |
21 | +#define VER "1.14" | |
22 | +#define IS_DOT(str) (!strcmp(str,".") || !strcmp(str,"..")) | |
23 | + | |
24 | +#define dprintf if (vf) printf | |
25 | +#define dprintf2 if (vf >= 2) printf | |
26 | + | |
27 | +/* funcs */ | |
28 | +void GetBlkSize(char *path, int *blkp); | |
29 | +void Du(char *path); | |
30 | + | |
31 | +/* globals */ | |
32 | +int df = 0; /* -d 指定フラグ */ | |
33 | +int dlevel = 2; /* -d ディレクトリの深さ */ | |
34 | +int sf = 0; /* -s 指定フラグ */ | |
35 | +int depth = 0; /* -s 用ディレクトリ深さ */ | |
36 | +int af = 0; /* -a 指定フラグ */ | |
37 | +int of = 0; /* -o 指定フラグ */ | |
38 | +int vf = 0; /* -v 指定フラグ */ | |
39 | +int blk; /* ブロックサイズ(byte) */ | |
40 | +unsigned int total = 0; /* fileサイズで計算したサイズ */ | |
41 | +unsigned int totalb = 0; /* block単位で計算したサイズ */ | |
42 | + | |
43 | +typedef struct u_long64 { | |
44 | + u_int high; | |
45 | + u_int low; | |
46 | +} u_long64; | |
47 | + | |
48 | +u_long64 total64; /* fileサイズで計算したサイズ(64bit) */ | |
49 | +u_long64 totalb64; /* block単位で計算したサイズ(64bit) */ | |
50 | + | |
51 | +/* x(64) <= x(64) + y(32) */ | |
52 | +void add64(u_long64 *x, u_int y) | |
53 | +{ | |
54 | + u_int sv; | |
55 | + | |
56 | + sv = x->low; | |
57 | + x->low += y; | |
58 | + if (x->low < sv) { | |
59 | + /* lowが小さくなったら桁上がり発生 */ | |
60 | + ++(x->high); | |
61 | + } | |
62 | +} | |
63 | + | |
64 | +/* V1.14-A start */ | |
65 | +/* 上位32ビットに値を足す */ | |
66 | +/* x(64) <= x(64) + y(32)*2^32 */ | |
67 | +void add64high(u_long64 *x, u_int y) | |
68 | +{ | |
69 | + x->high += y; | |
70 | +} | |
71 | +/* V1.14-A end */ | |
72 | + | |
73 | +/* x(64) <= x(64) - y(64) (x > yであること) */ | |
74 | +void sub64(u_long64 *x, u_long64 *y) | |
75 | +{ | |
76 | + if (x->low < y->low) { | |
77 | + /* y.lowが大きければ桁下がり発生 */ | |
78 | + --x->high; | |
79 | + } | |
80 | + x->low = x->low - y->low; | |
81 | + x->high = x->high - y->high; | |
82 | +} | |
83 | + | |
84 | +/* | |
85 | + * x は0x3ff ffffffff までのみ対応(商がu_intに収まる範囲まで) | |
86 | + * | |
87 | + */ | |
88 | +void dev1024(u_long64 *x, u_int *ans) | |
89 | +{ | |
90 | + /* 1024で割る場合 */ | |
91 | + u_long64 wk; | |
92 | + | |
93 | + wk.high = x->high; | |
94 | + wk.low = x->low; | |
95 | + | |
96 | + wk.low = wk.low >> 10; | |
97 | + wk.high = wk.high << 22; | |
98 | + *ans = wk.high | wk.low; | |
99 | +} | |
100 | + | |
101 | +void Usage() | |
102 | +{ | |
103 | + printf("du Verson %s by oga.\n",VER); | |
104 | + printf("usage: du [-a] [-s] [-d <depth>] [-o <size(MB)>] [dirnames]\n"); | |
105 | + printf(" -a: ファイルサイズの合計も表示\n"); | |
106 | + printf(" -s: カレントディレクトリの情報のみ出力\n"); | |
107 | + printf(" -d: 指定した深さまで出力(1以上)\n"); | |
108 | + printf(" -o: 指定サイズ以上の情報のみ出力\n"); | |
109 | +} | |
110 | + | |
111 | +int main(a,b) | |
112 | +int a; | |
113 | +char *b[]; | |
114 | +{ | |
115 | + char *fname[1024]; | |
116 | + int i; | |
117 | + int fn = 0; | |
118 | + | |
119 | + memset(&total64, 0, sizeof(u_long64)); | |
120 | + memset(&totalb64, 0, sizeof(u_long64)); | |
121 | + | |
122 | + for (i=1;i<a;i++) { | |
123 | + if (!strncmp(b[i],"-a",2)) { | |
124 | + af = 1; | |
125 | + continue; | |
126 | + } | |
127 | + if (!strncmp(b[i],"-s",2)) { | |
128 | + sf = 1; | |
129 | + continue; | |
130 | + } | |
131 | + if (!strncmp(b[i],"-v",2)) { | |
132 | + ++vf; | |
133 | + continue; | |
134 | + } | |
135 | + if (!strcmp(b[i],"-d")) { /* V1.12-A */ | |
136 | + if (++i < a) { | |
137 | + df = 1; | |
138 | + dlevel = atoi(b[i]); | |
139 | + } else { | |
140 | + Usage(); | |
141 | + exit(1); | |
142 | + } | |
143 | + if (dlevel < 1) { | |
144 | + Usage(); | |
145 | + exit(1); | |
146 | + } | |
147 | + continue; | |
148 | + } | |
149 | + if (!strcmp(b[i],"-o")) { | |
150 | + if (++i < a) { | |
151 | + of = atoi(b[i]); | |
152 | + } else { | |
153 | + Usage(); | |
154 | + exit(1); | |
155 | + } | |
156 | + continue; | |
157 | + } | |
158 | + if (!strncmp(b[i],"-",1)) { | |
159 | + Usage(); | |
160 | + exit(1); | |
161 | + } | |
162 | + fname[fn++] = b[i]; | |
163 | + } | |
164 | + if (!fn) { | |
165 | + fname[fn++] = ""; /* デフォルトはカレントディレクトリ */ | |
166 | + } | |
167 | + | |
168 | + for (i = 0; i<fn; i++) { | |
169 | + GetBlkSize(fname[i],&blk); | |
170 | + printf("%d bytes per cluster.\n",blk); | |
171 | + Du(fname[i]); | |
172 | + } | |
173 | + return 0; | |
174 | +} | |
175 | + | |
176 | +/* | |
177 | + * GetBlkSize() : 指定パスのクラスタサイズを求める | |
178 | + * | |
179 | + * IN path : パス名 | |
180 | + * | |
181 | + * OUT *blkp : クラスタサイズ(byte) | |
182 | + * パス名が不正な場合は1を返す | |
183 | + */ | |
184 | +void GetBlkSize(char *path,int *blkp) | |
185 | +{ | |
186 | + int sect,bytes,free,totalsz; | |
187 | + char wk[MAX_PATH]; | |
188 | + char *pp = wk; | |
189 | + | |
190 | + strcpy(wk,path); | |
191 | + if (wk[1] == ':') { | |
192 | + wk[2] = '/'; | |
193 | + wk[3] = '\0'; | |
194 | + } else { | |
195 | + pp = NULL; /* current drive */ | |
196 | + } | |
197 | + if (GetDiskFreeSpace(pp,§,&bytes,&free,&totalsz) != TRUE) { | |
198 | + printf("%s is unavailable\n",path); | |
199 | + *blkp = 1; | |
200 | + return; | |
201 | + } | |
202 | + *blkp = sect * bytes; /* calc bytes/Cluster */ | |
203 | +} | |
204 | + | |
205 | +/* | |
206 | + * Du() : 指定パス以下のファイルサイズ合計を求める | |
207 | + * | |
208 | + * IN path : パス名 | |
209 | + * | |
210 | + * OUT sz : ファイルサイズ合計 | |
211 | + * szb : ブロックベースのファイルサイズ合計 | |
212 | + * | |
213 | + */ | |
214 | +void Du(char *path) | |
215 | +{ | |
216 | + HANDLE fh; | |
217 | + WIN32_FIND_DATA wfd; | |
218 | + unsigned int subblk; | |
219 | + char wk[MAX_PATH]; | |
220 | + unsigned int tsv; | |
221 | + unsigned int tsvb; | |
222 | + | |
223 | + u_long64 tsv64; /* V1.13-A */ | |
224 | + u_long64 tsvb64; /* V1.13-A */ | |
225 | + u_long64 work64; /* V1.13-A */ | |
226 | + unsigned int ans; /* V1.13-A */ | |
227 | + | |
228 | + int i; | |
229 | + int ast = 0; /* 1:アスタリスク付き */ | |
230 | + | |
231 | + depth++; | |
232 | + tsv = total; | |
233 | + tsvb = totalb; | |
234 | + | |
235 | + memcpy(&tsv64, &total64, sizeof(u_long64)); /* V1.13-A */ | |
236 | + memcpy(&tsvb64, &totalb64, sizeof(u_long64)); /* V1.13-A */ | |
237 | + | |
238 | + strcpy(wk,path); /* copy path */ | |
239 | + if (strlen(path) && path[strlen(wk)-1] != '/') { | |
240 | + if (path[strlen(path)-1] == '*') { | |
241 | + ast = 1; | |
242 | + for (i = strlen(path)-1; i >= 0; i--) { | |
243 | + if (path[i] == '/' || path[i] == '\\') { | |
244 | + if (i) path[i] = '\0'; | |
245 | + break; | |
246 | + } | |
247 | + path[i] = '\0'; | |
248 | + } | |
249 | + dprintf("xxx path=%s\n",path); | |
250 | + /* wk = "/dir*" */ | |
251 | + /* path = "" */ | |
252 | + } else { | |
253 | + strcat(wk,"/*"); /* add /* */ | |
254 | + /* wk = "dir/*" */ | |
255 | + } | |
256 | + } else { | |
257 | + strcat(wk,"*"); /* add * */ | |
258 | + /* wk = "*" */ | |
259 | + } | |
260 | + | |
261 | + dprintf("Du: Enter path=[%s] search=[%s]\n",path,wk); | |
262 | + | |
263 | + fh = FindFirstFile(wk,&wfd); | |
264 | + if (fh == INVALID_HANDLE_VALUE) { | |
265 | + printf("du: FindFirstFile(%s) Error code=%d\n",wk,GetLastError()); | |
266 | + /* exit(1); */ | |
267 | + depth--; /* V1.11-A */ | |
268 | + return; /* V1.11-C */ | |
269 | + } | |
270 | + if (!IS_DOT(wfd.cFileName)) { | |
271 | + total += wfd.nFileSizeLow; | |
272 | + totalb += (((wfd.nFileSizeLow + blk - 1)/blk)* blk); | |
273 | + | |
274 | + add64(&total64, wfd.nFileSizeLow); /*V1.13-A*/ | |
275 | + add64high(&total64, wfd.nFileSizeHigh); /*V1.14-A*/ | |
276 | + add64(&totalb64,(((wfd.nFileSizeLow + blk - 1)/blk)* blk)); /*V1.13-A*/ | |
277 | + add64high(&totalb64, wfd.nFileSizeHigh); /*V1.14-A*/ | |
278 | + | |
279 | + dprintf2("First: file = %-16s size hi=%u lo=%u (hi=%u lo=%u)\n",wfd.cFileName, | |
280 | + wfd.nFileSizeHigh, wfd.nFileSizeLow, | |
281 | + wfd.nFileSizeHigh, ((wfd.nFileSizeLow + blk - 1)/blk)* blk); | |
282 | + if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | |
283 | + dprintf("[%s] is DIR\n",wfd.cFileName); | |
284 | + strcpy(wk,path); /* restore base path */ | |
285 | + if (strlen(wk) && wk[strlen(wk)-1] != '/') strcat(wk,"/"); | |
286 | + strcat(wk,wfd.cFileName); /* make new path */ | |
287 | + Du(wk); | |
288 | + } | |
289 | + } | |
290 | + while(FindNextFile(fh,&wfd)) { | |
291 | + if (IS_DOT(wfd.cFileName)) { | |
292 | + continue; | |
293 | + } | |
294 | + total += wfd.nFileSizeLow; | |
295 | + totalb += (((wfd.nFileSizeLow + blk - 1)/blk)* blk); | |
296 | + | |
297 | + add64(&total64, wfd.nFileSizeLow); /*V1.13-A*/ | |
298 | + add64high(&total64, wfd.nFileSizeHigh); /*V1.14-A*/ | |
299 | + add64(&totalb64,(((wfd.nFileSizeLow + blk - 1)/blk)* blk)); /*V1.13-A*/ | |
300 | + add64high(&totalb64, wfd.nFileSizeHigh); /*V1.14-A*/ | |
301 | + | |
302 | + dprintf2("First: file = %-16s size hi=%u lo=%u (hi=%u lo=%u)\n",wfd.cFileName, | |
303 | + wfd.nFileSizeHigh, wfd.nFileSizeLow, | |
304 | + wfd.nFileSizeHigh, ((wfd.nFileSizeLow + blk - 1)/blk)* blk); | |
305 | + if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | |
306 | + dprintf("[%s] is DIR\n",wfd.cFileName); | |
307 | + strcpy(wk,path); /* restore base path */ | |
308 | + if (strlen(wk) && wk[strlen(wk)-1] != '/') strcat(wk,"/"); | |
309 | + strcat(wk,wfd.cFileName); /* make new path */ | |
310 | + Du(wk); | |
311 | + } | |
312 | + } | |
313 | + FindClose(fh); | |
314 | + depth--; | |
315 | + if (sf && depth > 1) { | |
316 | + /* -s 指定で深さ2以上なら表示しない */ | |
317 | + return; | |
318 | + } | |
319 | + /* V1.12-A start */ | |
320 | + if (df && depth > dlevel) { | |
321 | + /* -d 指定の深さを超えたものは表示しない */ | |
322 | + return; | |
323 | + } | |
324 | + /* V1.12-A end */ | |
325 | + if (!ast) { | |
326 | + memcpy(&work64, &totalb64, sizeof(u_long64)); /* V1.13-A */ | |
327 | + sub64(&work64, &tsvb64); /* V1.13-A */ | |
328 | + dev1024(&work64, &ans); /* V1.13-A */ | |
329 | + /* if (!of || ((totalb-tsvb)/1024/1024) >= of) { }*/ | |
330 | + if (!of || (ans/1024) >= of) { /* V1.13-C */ | |
331 | + if (df) { | |
332 | + printf("%2d: ", depth); | |
333 | + } | |
334 | +#ifdef OLD | |
335 | + printf("%7d",(totalb-tsvb)/1024); | |
336 | +#else | |
337 | + printf("%7d", ans); /* V1.13-C */ | |
338 | +#endif | |
339 | + if (af) { | |
340 | +#ifdef OLD | |
341 | + printf(" (%7d)",(total-tsv)/1024); | |
342 | +#else | |
343 | + memcpy(&work64, &total64, sizeof(u_long64)); /* V1.13-A */ | |
344 | + sub64(&work64, &tsv64); /* V1.13-A */ | |
345 | + dev1024(&work64, &ans); /* V1.13-A */ | |
346 | + printf(" (%7d)", ans); /* V1.13-C */ | |
347 | +#endif | |
348 | + } | |
349 | + printf(" %s\n",path); | |
350 | + } | |
351 | + } | |
352 | + | |
353 | +} /* End Du() */ | |
354 | + |
@@ -0,0 +1,56 @@ | ||
1 | +#include <windows.h> | |
2 | + | |
3 | +#define DWORD_PTR DWORD* | |
4 | +#define LONG_PTR LONG* | |
5 | + | |
6 | +#include <dshow.h> | |
7 | + | |
8 | +#define MOVIE_FILE "V:\\www\\media\\letsgyo.mpg" | |
9 | + | |
10 | +void main(int a, char *b[]) | |
11 | +{ | |
12 | + int i; | |
13 | + char *fname = MOVIE_FILE; | |
14 | + WCHAR wPath[MAX_PATH]; | |
15 | + | |
16 | + for (i = 1; i<a; i++) { | |
17 | + if (!strcmp(b[i], "-h")) { | |
18 | + printf("usage: dxshow <filename>\n"); | |
19 | + exit(1); | |
20 | + } | |
21 | + fname = b[i]; | |
22 | + } | |
23 | + | |
24 | + MultiByteToWideChar(CP_ACP, 0, fname, -1, wPath, MAX_PATH); | |
25 | + | |
26 | + IGraphBuilder *pGraph; | |
27 | + IMediaControl *pMediaControl; | |
28 | + IMediaEvent *pEvent; | |
29 | + | |
30 | + CoInitialize(NULL); | |
31 | + | |
32 | + // フィルタグラフマネージャを作成し、インターフェイスをクエリする。 | |
33 | + CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, | |
34 | + IID_IGraphBuilder, (void **)&pGraph); | |
35 | + pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl); | |
36 | + pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); | |
37 | + | |
38 | + // グラフを作成。重要: 使用システムのファイル文字列に変更すること。 | |
39 | + //pGraph->RenderFile(L"C:\\Hello_World.avi", NULL); | |
40 | + pGraph->RenderFile(wPath, NULL); | |
41 | + | |
42 | + // グラフの実行。 | |
43 | + pMediaControl->Run(); | |
44 | + | |
45 | + // 終了を待つ。 | |
46 | + long evCode; | |
47 | + pEvent->WaitForCompletion(INFINITE, &evCode); | |
48 | + | |
49 | + // クリーン アップ。 | |
50 | + pMediaControl->Release(); | |
51 | + pEvent->Release(); | |
52 | + pGraph->Release(); | |
53 | + CoUninitialize(); | |
54 | +} | |
55 | + | |
56 | + |
@@ -0,0 +1,55 @@ | ||
1 | +/* | |
2 | + * expr2 | |
3 | + * exprの割り算結果だけ小数点対応版 | |
4 | + * | |
5 | + * 2002/06/02 V0.10 by oga. | |
6 | + */ | |
7 | +#include <stdio.h> | |
8 | +#include <stdlib.h> | |
9 | +#include <string.h> | |
10 | + | |
11 | +void usage() | |
12 | +{ | |
13 | + printf("usage: expr2 <val1> <op> <val2>\n"); | |
14 | + exit(1); | |
15 | +} | |
16 | + | |
17 | +int main(int a, char *b[]) | |
18 | +{ | |
19 | + int val1, val2, result; | |
20 | + float resultf = 0; | |
21 | + | |
22 | + if (a < 4) { | |
23 | + usage(); | |
24 | + } | |
25 | + val1 = atoi(b[1]); | |
26 | + val2 = atoi(b[3]); | |
27 | + switch (b[2][0]) { | |
28 | + case '+': | |
29 | + result = val1 + val2; | |
30 | + break; | |
31 | + case '-': | |
32 | + result = val1 - val2; | |
33 | + break; | |
34 | + case '*': | |
35 | + case 'x': | |
36 | + result = val1 * val2; | |
37 | + break; | |
38 | + case '/': | |
39 | + result = val1 / val2; | |
40 | + if (result * val2 != val1) { | |
41 | + resultf = (float)val1/(float)val2; | |
42 | + } | |
43 | + break; | |
44 | + default: | |
45 | + usage(); | |
46 | + break; | |
47 | + } | |
48 | + if (resultf) { | |
49 | + printf("%.5f\n", resultf); | |
50 | + } else { | |
51 | + printf("%d\n", result); | |
52 | + } | |
53 | + return 0; | |
54 | +} | |
55 | + |
@@ -0,0 +1,135 @@ | ||
1 | +/* | |
2 | + * findwin : Windowタイトルからコントロールの値(text)を取り出す | |
3 | + * | |
4 | + * 2001/01/15 V0.10 by oga. | |
5 | + */ | |
6 | +#include <windows.h> | |
7 | + | |
8 | +char *WinTitle = "素数べんち"; | |
9 | +int af = 0; /* -a flag */ | |
10 | +int evf = 0; /* -ev flag */ | |
11 | +int evid = 0; /* -ev event id */ | |
12 | + | |
13 | +int SendMsg(HWND hWin, int evid) | |
14 | +{ | |
15 | + switch (evid) { | |
16 | + case WM_KEYDOWN: | |
17 | + SendMessage(hWin, evid, 'a', 0); | |
18 | + SendMessage(hWin, WM_KEYUP, 'a', 0); | |
19 | + SendMessage(hWin, WM_SETTEXT, 4, (long)"abcd"); // タイトルバーに設定されてしまう | |
20 | + break; | |
21 | + | |
22 | + default: | |
23 | + SendMessage(hWin, evid, 0, 0); | |
24 | + break; | |
25 | + } | |
26 | + return 0; | |
27 | +} | |
28 | + | |
29 | +int GetControlList(HWND hWin, int level) | |
30 | +{ | |
31 | + HWND hCntl = NULL; | |
32 | + char classname[4096]; | |
33 | + char ctrltext[4096]; | |
34 | + char spc[1024]; | |
35 | + int ret; | |
36 | + | |
37 | + ++level; | |
38 | + strcpy(spc, " "); | |
39 | + spc[level-1] = '\0'; | |
40 | +#if 0 | |
41 | + if (level > 1) { | |
42 | + spc[level-1] = '+'; | |
43 | + } | |
44 | +#endif | |
45 | + | |
46 | + while (1) { | |
47 | + hCntl = FindWindowEx(hWin, hCntl, NULL, NULL); | |
48 | + if (!hCntl) { | |
49 | + break; | |
50 | + } | |
51 | + GetClassName(hCntl, classname, sizeof(classname)); | |
52 | + ctrltext[0] = '\0'; | |
53 | + ret = GetWindowText(hCntl, ctrltext, sizeof(ctrltext)); | |
54 | + if (ret == 0) { | |
55 | + ret = GetLastError(); | |
56 | + if (ret != ERROR_SUCCESS) { | |
57 | + sprintf(ctrltext, "GetWindowText Error(%d)", ret); | |
58 | + } | |
59 | + } | |
60 | + printf("%sClass:%-20s Text:[%s]\n", spc, classname, ctrltext); | |
61 | + if (evf && strstr(ctrltext, WinTitle) && strncmp(ctrltext, "コマンド", 8)) { | |
62 | + hWin = FindWindow(NULL, ctrltext); /* hWin取り直し */ | |
63 | + SetForegroundWindow(hWin); | |
64 | + printf("## Send Message(%d) to %s.\n", evid, ctrltext); | |
65 | + SendMsg(hWin, evid); | |
66 | + exit(1); | |
67 | + } | |
68 | +#if 0 | |
69 | + /* replace other window text */ | |
70 | + if (!strcmp(ctrltext, "0.391")) { | |
71 | + SetWindowText(hCntl, "0.222"); | |
72 | + printf("Set 0.222!!\n"); | |
73 | + /* Send Redraw Message to Message Queue */ | |
74 | + SendMessage(hWin, WM_PAINT, 0, 0); | |
75 | + } | |
76 | +#endif | |
77 | + GetControlList(hCntl, level); | |
78 | + } | |
79 | + return 0; | |
80 | +} | |
81 | + | |
82 | +int main(int a, char *b[]) | |
83 | +{ | |
84 | + HWND hWin = NULL; | |
85 | + int i; | |
86 | + | |
87 | + for (i = 1; i<a; i++) { | |
88 | + if (!strcmp(b[i], "-a")) { | |
89 | + af = 1; | |
90 | + continue; | |
91 | + } | |
92 | + if (i+1 < a && !strcmp(b[i], "-ev")) { | |
93 | + evf = 1; | |
94 | + evid = atoi(b[++i]); | |
95 | + continue; | |
96 | + } | |
97 | + if (!strcmp(b[i], "-close")) { | |
98 | + evf = 1; | |
99 | + evid = WM_CLOSE; | |
100 | + continue; | |
101 | + } | |
102 | + if (!strcmp(b[i], "-key")) { | |
103 | + evf = 1; | |
104 | + evid = WM_KEYDOWN; | |
105 | + continue; | |
106 | + } | |
107 | + if (!strcmp(b[i], "-h")) { | |
108 | + printf("usage: findwin { <win_title> [-close] | -a }\n"); | |
109 | + printf(" -close : send close msg to specific window\n"); | |
110 | + printf(" -a : find all window\n"); | |
111 | + //printf(" -ev <event_id> : send specific event\n"); | |
112 | + exit(1); | |
113 | + } | |
114 | + WinTitle = b[1]; | |
115 | + } | |
116 | + | |
117 | + if (af) { | |
118 | + /* find all window */ | |
119 | + GetControlList(NULL, 0); | |
120 | + } else { | |
121 | + /* find specific window */ | |
122 | + hWin = FindWindow(NULL, WinTitle); | |
123 | + if (hWin) { | |
124 | + printf("[%s] Window Handle = 0x%08x\n", WinTitle, hWin); | |
125 | + GetControlList(hWin, 0); | |
126 | + } else { | |
127 | + printf("Not found %s.\n", WinTitle); | |
128 | + } | |
129 | + if (evf) { | |
130 | + printf("Send Message(%d) to %s.\n", evid, WinTitle); | |
131 | + SendMsg(hWin, evid); | |
132 | + } | |
133 | + } | |
134 | + return 0; | |
135 | +} |
@@ -0,0 +1,77 @@ | ||
1 | +/* | |
2 | + * SJIS対応fold | |
3 | + * | |
4 | + * 08/01/17 V0.10 by oga. | |
5 | + */ | |
6 | +#include <stdio.h> | |
7 | +#include <stdlib.h> | |
8 | +#include <string.h> | |
9 | + | |
10 | +#define ISKANJI(c) (((unsigned char)(c) >= 0x80 && (unsigned char)(c) < 0xa0) || \ | |
11 | + ((unsigned char)(c) >= 0xe0 && (unsigned char)(c) < 0xff)) | |
12 | + | |
13 | +int main(int a, char* b[]) | |
14 | +{ | |
15 | + int i; | |
16 | + int cnt; | |
17 | + int width = 80; | |
18 | + int kf = 0; | |
19 | + int vf = 0; | |
20 | + char *fname = NULL; | |
21 | + unsigned char buf[100000]; | |
22 | + FILE *fp; | |
23 | + | |
24 | + for (i = 0; i < a; i++) { | |
25 | + if (!strcmp(b[i], "-h")) { | |
26 | + printf("usage: fold [-w <width>] [<filename>]\n"); | |
27 | + exit(1); | |
28 | + } | |
29 | + if (!strcmp(b[i], "-v")) { | |
30 | + vf = 1; | |
31 | + continue; | |
32 | + } | |
33 | + if (i+1 < a && !strcmp(b[i], "-w")) { | |
34 | + width = atoi(b[++i]); | |
35 | + continue; | |
36 | + } | |
37 | + fname = b[i]; | |
38 | + } | |
39 | + | |
40 | + if (fname != NULL) { | |
41 | + if ((fp = fopen(fname, "r")) == NULL) { | |
42 | + perror(fname); | |
43 | + exit(1); | |
44 | + } | |
45 | + } else { | |
46 | + fp = stdin; | |
47 | + } | |
48 | + | |
49 | + while (fgets(buf, sizeof(buf), fp)) { | |
50 | + cnt = 0; | |
51 | + kf = 0; | |
52 | + for (i = 0; i < strlen(buf); i++) { | |
53 | + if (buf[i] == '\t') { | |
54 | + cnt += (8-(i & 8)); | |
55 | + } | |
56 | + if (kf == 0 && ISKANJI(buf[i])) { | |
57 | + kf = 1; | |
58 | + } else { | |
59 | + kf = 0; | |
60 | + } | |
61 | + if (!vf) putchar(buf[i]); | |
62 | + if (vf) printf("kf:%d cnt=%d >= %d\n", kf, cnt, width - 1 - (ISKANJI(buf[i+1])?1:0) ); | |
63 | + if (kf == 0 && (cnt >= width - 1 - (ISKANJI(buf[i+1])?1:0))) { | |
64 | + if (buf[i] != '\n') putchar('\n'); | |
65 | + cnt = 0; | |
66 | + continue; | |
67 | + } | |
68 | + ++cnt; | |
69 | + } | |
70 | + } | |
71 | + | |
72 | + if (fp != stdin) fclose(fp); | |
73 | + | |
74 | +} | |
75 | + | |
76 | +/* vim:ts=8:sw=4: | |
77 | + */ |
@@ -0,0 +1,27 @@ | ||
1 | +/* | |
2 | + * 指定したパスのフルパスを求める | |
3 | + * | |
4 | + */ | |
5 | +#include <windows.h> | |
6 | +#include <stdio.h> | |
7 | +#include <stdlib.h> | |
8 | +#include <string.h> | |
9 | + | |
10 | +main(int a, char *b[]) | |
11 | +{ | |
12 | + char *pathp = NULL; /* 結果のファイル名部分へのポインタが格納される */ | |
13 | + char buf[1024]; /* 完全パス格納領域 */ | |
14 | + int ret; | |
15 | + | |
16 | + if (a < 2) { | |
17 | + printf("usage: fullpath <filename>\n"); | |
18 | + exit(1); | |
19 | + } | |
20 | + | |
21 | + ret = GetFullPathName(b[1], sizeof(buf), buf, &pathp); | |
22 | + if (ret == 0) { | |
23 | + printf("GetFullPathName() failed. code=%d\n", GetLastError()); | |
24 | + } | |
25 | + printf("fullpath=[%s], basename=[%s]\n",buf, pathp); | |
26 | + printf("buftop=0x%08x filetop=0x%08x %s\n",buf, pathp, b[1]); | |
27 | +} |
@@ -0,0 +1,35 @@ | ||
1 | +/* | |
2 | + * Get my exe path sample | |
3 | + * | |
4 | + * 2001/01/18 by oga. | |
5 | + * | |
6 | + */ | |
7 | +#include <windows.h> | |
8 | +#include <stdio.h> | |
9 | +#include <stdlib.h> | |
10 | +#include <string.h> | |
11 | + | |
12 | +int main(int a, char *b[]) | |
13 | +{ | |
14 | + | |
15 | + char path[1024]; | |
16 | + HMODULE hMod = NULL; | |
17 | + char *pt = NULL; | |
18 | + | |
19 | + /* 以下を関数化すると何故かドライブ名が返ってきますご注意 */ | |
20 | + hMod = GetModuleHandle(NULL); | |
21 | + if (hMod) { | |
22 | + GetModuleFileName(hMod, path, sizeof(path)); | |
23 | + } else { | |
24 | + printf("GetModuleHandle() error.(%d)\n",GetLastError()); | |
25 | + return -1; | |
26 | + } | |
27 | + printf(" I am [%s]\n", path); | |
28 | + | |
29 | + if (pt = strrchr(path, '\\')) { | |
30 | + *pt = '\0'; | |
31 | + } | |
32 | + printf("My path is [%s]\n", path); | |
33 | + | |
34 | + return 0; | |
35 | +} |
@@ -0,0 +1,204 @@ | ||
1 | +#include <windows.h> | |
2 | +#include <stdio.h> | |
3 | + | |
4 | +typedef struct _smval_t { | |
5 | + int prop; | |
6 | + char *propname; | |
7 | + char level; // 0:default表示 | |
8 | +} smval_t; | |
9 | + | |
10 | +smval_t smvalue[] = { | |
11 | + SM_CXSCREEN, "SM_CXSCREEN", 1, | |
12 | + SM_CYSCREEN, "SM_CYSCREEN", 1, | |
13 | + SM_CXVSCROLL, "SM_CXVSCROLL", 1, | |
14 | + SM_CYHSCROLL, "SM_CYHSCROLL", 1, | |
15 | + SM_CYCAPTION, "SM_CYCAPTION", 0, | |
16 | + SM_CXBORDER, "SM_CXBORDER", 0, | |
17 | + SM_CYBORDER, "SM_CYBORDER", 0, | |
18 | + SM_CXDLGFRAME, "SM_CXDLGFRAME", 0, | |
19 | + SM_CYDLGFRAME, "SM_CYDLGFRAME", 0, | |
20 | + SM_CYVTHUMB, "SM_CYVTHUMB", 1, | |
21 | + SM_CXHTHUMB, "SM_CXHTHUMB", 1, | |
22 | + SM_CXICON, "SM_CXICON", 1, | |
23 | + SM_CYICON, "SM_CYICON", 1, | |
24 | + SM_CXCURSOR, "SM_CXCURSOR", 1, | |
25 | + SM_CYCURSOR, "SM_CYCURSOR", 1, | |
26 | + SM_CYMENU, "SM_CYMENU", 0, | |
27 | + SM_CXFULLSCREEN, "SM_CXFULLSCREEN", 1, | |
28 | + SM_CYFULLSCREEN, "SM_CYFULLSCREEN", 1, | |
29 | + SM_CYKANJIWINDOW, "SM_CYKANJIWINDOW", 1, | |
30 | + SM_MOUSEPRESENT, "SM_MOUSEPRESENT", 1, | |
31 | + SM_CYVSCROLL, "SM_CYVSCROLL", 1, | |
32 | + SM_CXHSCROLL, "SM_CXHSCROLL", 1, | |
33 | + SM_DEBUG, "SM_DEBUG", 1, | |
34 | + SM_SWAPBUTTON, "SM_SWAPBUTTON", 1, | |
35 | + SM_RESERVED1, "SM_RESERVED1", 1, | |
36 | + SM_RESERVED2, "SM_RESERVED2", 1, | |
37 | + SM_RESERVED3, "SM_RESERVED3", 1, | |
38 | + SM_RESERVED4, "SM_RESERVED4", 1, | |
39 | + SM_CXMIN, "SM_CXMIN", 1, | |
40 | + SM_CYMIN, "SM_CYMIN", 1, | |
41 | + SM_CXSIZE, "SM_CXSIZE", 1, | |
42 | + SM_CYSIZE, "SM_CYSIZE", 1, | |
43 | + SM_CXFRAME, "SM_CXFRAME", 1, | |
44 | + SM_CYFRAME, "SM_CYFRAME", 1, | |
45 | + SM_CXMINTRACK, "SM_CXMINTRACK", 1, | |
46 | + SM_CYMINTRACK, "SM_CYMINTRACK", 1, | |
47 | + SM_CXDOUBLECLK, "SM_CXDOUBLECLK", 1, | |
48 | + SM_CYDOUBLECLK, "SM_CYDOUBLECLK", 1, | |
49 | + SM_CXICONSPACING, "SM_CXICONSPACING", 1, | |
50 | + SM_CYICONSPACING, "SM_CYICONSPACING", 1, | |
51 | + SM_MENUDROPALIGNMENT, "SM_MENUDROPALIGNMENT", 1, | |
52 | + SM_PENWINDOWS, "SM_PENWINDOWS", 1, | |
53 | + SM_DBCSENABLED, "SM_DBCSENABLED", 1, | |
54 | + SM_CMOUSEBUTTONS, "SM_CMOUSEBUTTONS", 1, | |
55 | + SM_SECURE, "SM_SECURE", 1, | |
56 | + SM_CXEDGE, "SM_CXEDGE", 1, | |
57 | + SM_CYEDGE, "SM_CYEDGE", 1, | |
58 | + SM_CXMINSPACING, "SM_CXMINSPACING", 1, | |
59 | + SM_CYMINSPACING, "SM_CYMINSPACING", 1, | |
60 | + SM_CXSMICON, "SM_CXSMICON", 1, | |
61 | + SM_CYSMICON, "SM_CYSMICON", 1, | |
62 | + SM_CYSMCAPTION, "SM_CYSMCAPTION", 1, | |
63 | + SM_CXSMSIZE, "SM_CXSMSIZE", 1, | |
64 | + SM_CYSMSIZE, "SM_CYSMSIZE", 1, | |
65 | + SM_CXMENUSIZE, "SM_CXMENUSIZE", 1, | |
66 | + SM_CYMENUSIZE, "SM_CYMENUSIZE", 1, | |
67 | + SM_ARRANGE, "SM_ARRANGE", 1, | |
68 | + SM_CXMINIMIZED, "SM_CXMINIMIZED", 1, | |
69 | + SM_CYMINIMIZED, "SM_CYMINIMIZED", 1, | |
70 | + SM_CXMAXTRACK, "SM_CXMAXTRACK", 1, | |
71 | + SM_CYMAXTRACK, "SM_CYMAXTRACK", 1, | |
72 | + SM_CXMAXIMIZED, "SM_CXMAXIMIZED", 1, | |
73 | + SM_CYMAXIMIZED, "SM_CYMAXIMIZED", 1, | |
74 | + SM_NETWORK, "SM_NETWORK", 1, | |
75 | + SM_CLEANBOOT, "SM_CLEANBOOT", 1, | |
76 | + SM_CXDRAG, "SM_CXDRAG", 1, | |
77 | + SM_CYDRAG, "SM_CYDRAG", 1, | |
78 | + SM_SHOWSOUNDS, "SM_SHOWSOUNDS", 1, | |
79 | + SM_CXMENUCHECK, "SM_CXMENUCHECK", 1, | |
80 | + SM_CYMENUCHECK, "SM_CYMENUCHECK", 1, | |
81 | + SM_SLOWMACHINE, "SM_SLOWMACHINE", 1, | |
82 | + SM_MIDEASTENABLED, "SM_MIDEASTENABLED", 1, | |
83 | +// SM_MOUSEWHEELPRESENT, "SM_MOUSEWHEELPRESENT", 1, | |
84 | +// SM_XVIRTUALSCREEN, "SM_XVIRTUALSCREEN", 1, | |
85 | +// SM_YVIRTUALSCREEN, "SM_YVIRTUALSCREEN", 1, | |
86 | +// SM_CXVIRTUALSCREEN, "SM_CXVIRTUALSCREEN", 1, | |
87 | +// SM_CYVIRTUALSCREEN, "SM_CYVIRTUALSCREEN", 1, | |
88 | +// SM_CMONITORS, "SM_CMONITORS", 1, | |
89 | +// SM_SAMEDISPLAYFORMAT, "SM_SAMEDISPLAYFORMAT", 1, | |
90 | +// SM_CMETRICS, "SM_CMETRICS", 1, | |
91 | +// SM_CMETRICS, "SM_CMETRICS", 1, | |
92 | +}; | |
93 | + | |
94 | +int allf = 0; /* -all */ | |
95 | + | |
96 | + | |
97 | +usage() | |
98 | +{ | |
99 | + printf("usage: getsysm [-a]\n"); | |
100 | +} | |
101 | + | |
102 | +int main(int a, char *b[]) | |
103 | +{ | |
104 | + int i; | |
105 | + | |
106 | + for (i = 1; i<a; i++) { | |
107 | + if (!strcmp(b[i], "-h")) { | |
108 | + usage(); | |
109 | + } | |
110 | + if (!strcmp(b[i], "-a")) { | |
111 | + allf = 1; | |
112 | + } | |
113 | + } | |
114 | + | |
115 | + for (i = 0; i<sizeof(smvalue)/sizeof(smval_t); i++) { | |
116 | + if (allf || smvalue[i].level == 0) { | |
117 | + printf("%-20s : %d\n", smvalue[i].propname, GetSystemMetrics(smvalue[i].prop)); | |
118 | + } | |
119 | + } | |
120 | +} | |
121 | + | |
122 | + | |
123 | +/* | |
124 | +#define SM_CXSCREEN 0 | |
125 | +#define SM_CYSCREEN 1 | |
126 | +#define SM_CXVSCROLL 2 | |
127 | +#define SM_CYHSCROLL 3 | |
128 | +#define SM_CYCAPTION 4 | |
129 | +#define SM_CXBORDER 5 | |
130 | +#define SM_CYBORDER 6 | |
131 | +#define SM_CXDLGFRAME 7 | |
132 | +#define SM_CYDLGFRAME 8 | |
133 | +#define SM_CYVTHUMB 9 | |
134 | +#define SM_CXHTHUMB 10 | |
135 | +#define SM_CXICON 11 | |
136 | +#define SM_CYICON 12 | |
137 | +#define SM_CXCURSOR 13 | |
138 | +#define SM_CYCURSOR 14 | |
139 | +#define SM_CYMENU 15 | |
140 | +#define SM_CXFULLSCREEN 16 | |
141 | +#define SM_CYFULLSCREEN 17 | |
142 | +#define SM_CYKANJIWINDOW 18 | |
143 | +#define SM_MOUSEPRESENT 19 | |
144 | +#define SM_CYVSCROLL 20 | |
145 | +#define SM_CXHSCROLL 21 | |
146 | +#define SM_DEBUG 22 | |
147 | +#define SM_SWAPBUTTON 23 | |
148 | +#define SM_RESERVED1 24 | |
149 | +#define SM_RESERVED2 25 | |
150 | +#define SM_RESERVED3 26 | |
151 | +#define SM_RESERVED4 27 | |
152 | +#define SM_CXMIN 28 | |
153 | +#define SM_CYMIN 29 | |
154 | +#define SM_CXSIZE 30 | |
155 | +#define SM_CYSIZE 31 | |
156 | +#define SM_CXFRAME 32 | |
157 | +#define SM_CYFRAME 33 | |
158 | +#define SM_CXMINTRACK 34 | |
159 | +#define SM_CYMINTRACK 35 | |
160 | +#define SM_CXDOUBLECLK 36 | |
161 | +#define SM_CYDOUBLECLK 37 | |
162 | +#define SM_CXICONSPACING 38 | |
163 | +#define SM_CYICONSPACING 39 | |
164 | +#define SM_MENUDROPALIGNMENT 40 | |
165 | +#define SM_PENWINDOWS 41 | |
166 | +#define SM_DBCSENABLED 42 | |
167 | +#define SM_CMOUSEBUTTONS 43 | |
168 | +#define SM_SECURE 44 | |
169 | +#define SM_CXEDGE 45 | |
170 | +#define SM_CYEDGE 46 | |
171 | +#define SM_CXMINSPACING 47 | |
172 | +#define SM_CYMINSPACING 48 | |
173 | +#define SM_CXSMICON 49 | |
174 | +#define SM_CYSMICON 50 | |
175 | +#define SM_CYSMCAPTION 51 | |
176 | +#define SM_CXSMSIZE 52 | |
177 | +#define SM_CYSMSIZE 53 | |
178 | +#define SM_CXMENUSIZE 54 | |
179 | +#define SM_CYMENUSIZE 55 | |
180 | +#define SM_ARRANGE 56 | |
181 | +#define SM_CXMINIMIZED 57 | |
182 | +#define SM_CYMINIMIZED 58 | |
183 | +#define SM_CXMAXTRACK 59 | |
184 | +#define SM_CYMAXTRACK 60 | |
185 | +#define SM_CXMAXIMIZED 61 | |
186 | +#define SM_CYMAXIMIZED 62 | |
187 | +#define SM_NETWORK 63 | |
188 | +#define SM_CLEANBOOT 67 | |
189 | +#define SM_CXDRAG 68 | |
190 | +#define SM_CYDRAG 69 | |
191 | +#define SM_SHOWSOUNDS 70 | |
192 | +#define SM_CXMENUCHECK 71 | |
193 | +#define SM_CYMENUCHECK 72 | |
194 | +#define SM_SLOWMACHINE 73 | |
195 | +#define SM_MIDEASTENABLED 74 | |
196 | +#define SM_MOUSEWHEELPRESENT 75 | |
197 | +#define SM_XVIRTUALSCREEN 76 | |
198 | +#define SM_YVIRTUALSCREEN 77 | |
199 | +#define SM_CXVIRTUALSCREEN 78 | |
200 | +#define SM_CYVIRTUALSCREEN 79 | |
201 | +#define SM_CMONITORS 80 | |
202 | +#define SM_SAMEDISPLAYFORMAT 81 | |
203 | +#define SM_CMETRICS 83 | |
204 | +*/ |
@@ -0,0 +1,201 @@ | ||
1 | +// | |
2 | +// getver : get version info | |
3 | +// | |
4 | +// V1.01 2001/10/02 by oga. | |
5 | +// | |
6 | +// | |
7 | +#include <stdio.h> | |
8 | +#include <windows.h> | |
9 | + | |
10 | +int csvf = 0; | |
11 | + | |
12 | +// | |
13 | +// IN block : block pointer from GetFileVersionInfo() | |
14 | +// IN bhead : Block Header Name (ex.) "FileDescription" | |
15 | +// OUT str : resource Value | |
16 | +// OUT <ret> : 0:succuess 1:failed | |
17 | +// | |
18 | +int GetRcValue(char *block, char *bhead, char **str) | |
19 | +{ | |
20 | + char wk[2048]; | |
21 | + unsigned int size; // dummy | |
22 | + | |
23 | + sprintf(wk,"\\StringFileInfo\\041104b0\\%s",bhead); | |
24 | + if (VerQueryValue(block, | |
25 | + TEXT(wk), | |
26 | + (LPVOID *)str, &size) == FALSE) { | |
27 | + if (csvf) { | |
28 | + printf(",????", bhead); | |
29 | + } else { | |
30 | + printf("Block Header(%s) not found.\n", bhead); | |
31 | + } | |
32 | + return 1; | |
33 | + } | |
34 | + return 0; | |
35 | +} | |
36 | + | |
37 | +// | |
38 | +// expand_wild() | |
39 | +// | |
40 | +// IN int argc | |
41 | +// IN char *argv[] | |
42 | +// IN int filename start point of argv | |
43 | +// OUT char *filelist[] | |
44 | +// | |
45 | +void expand_wild(int argc, char **argv, int pos, char **filelist) | |
46 | +{ | |
47 | + int i; | |
48 | + int j = 0; | |
49 | + int firstf = 1; | |
50 | + for (i = pos; i<argc; i++) { | |
51 | + //printf("file=%s i=%d/argc=%d\n", argv[i], i, argc); | |
52 | + if (strchr(argv[i], '*') || strchr(argv[i], '?')) { | |
53 | + // include wild char | |
54 | + HANDLE hdir; | |
55 | + WIN32_FIND_DATA wfd; | |
56 | + char *path = argv[i]; | |
57 | + int status; | |
58 | + while (1) { | |
59 | + if (firstf) { | |
60 | + hdir = FindFirstFile(path, &wfd); | |
61 | + if (hdir == INVALID_HANDLE_VALUE) { | |
62 | + // no matching file | |
63 | + //printf("expand_wild: FindFirstFile() error\n"); | |
64 | + break; | |
65 | + } | |
66 | + firstf = 0; | |
67 | + } else { | |
68 | + status = FindNextFile(hdir, &wfd); | |
69 | + if (status == FALSE) { | |
70 | + break; | |
71 | + } | |
72 | + } | |
73 | + filelist[j] = (char *) malloc(strlen(wfd.cFileName)+1); | |
74 | + strcpy(filelist[j], wfd.cFileName); | |
75 | + j++; | |
76 | + } | |
77 | + FindClose(hdir); | |
78 | + } else { | |
79 | + // not wild | |
80 | + filelist[j] = (char *) malloc(strlen(argv[i])+1); | |
81 | + strcpy(filelist[j], argv[i]); | |
82 | + j++; | |
83 | + } | |
84 | + } | |
85 | + //printf("return expand_wild()\n"); | |
86 | +} | |
87 | + | |
88 | +int main(int a, char *b[]) | |
89 | +{ | |
90 | + char buf[65536]; // resource data | |
91 | + char *str; | |
92 | + char *filename; | |
93 | + unsigned int size = 0; | |
94 | + int i = 0; | |
95 | + int k = 0; | |
96 | + char *filelist[2048]; // max 2048 files/dir | |
97 | + VS_FIXEDFILEINFO *vfinfo = NULL; | |
98 | + char *header[] = { | |
99 | + "CompanyName", | |
100 | + "FileDescription", | |
101 | + "FileVersion", | |
102 | + "InternalName", | |
103 | + "LegalCopyright", | |
104 | + "OriginalFilename", | |
105 | + "ProductName", | |
106 | + "ProductVersion", | |
107 | + NULL | |
108 | + }; | |
109 | + | |
110 | + memset(filelist, 0, sizeof(filelist)); | |
111 | + | |
112 | + if (a < 2 || !strcmp(b[1], "-h")) { | |
113 | + printf("usage: getver [-csv] <file.exe> [<file.exe> ...]\n"); | |
114 | + exit(1); | |
115 | + } | |
116 | + i = 1; | |
117 | + if (!strcmp(b[1], "-csv")) { | |
118 | + csvf = 1; | |
119 | + ++i; | |
120 | + } | |
121 | + | |
122 | + // expand wildcard filename | |
123 | + expand_wild(a, b, i, filelist); | |
124 | + | |
125 | + if (csvf) { | |
126 | + // write csv header | |
127 | + printf("FileName,FileVersion,ProductVersion"); | |
128 | + i = 0; | |
129 | + while (header[i] != NULL) { | |
130 | + printf(",%s", header[i]); | |
131 | + i++; | |
132 | + } | |
133 | + printf("\n"); | |
134 | + } | |
135 | + | |
136 | + k = 0; | |
137 | + while (filelist[k] != NULL) { | |
138 | + filename = filelist[k++]; | |
139 | + if (csvf) { | |
140 | + printf("%s",filename); | |
141 | + } else { | |
142 | + printf("===== %s =====\n",filename); | |
143 | + } | |
144 | + if (GetFileVersionInfo(filename, 0, sizeof(buf), buf)) { | |
145 | + // printf("GetFileVersionInfo() success.\n"); | |
146 | + if (VerQueryValue(buf, | |
147 | + TEXT("\\"), | |
148 | + (LPVOID *)&vfinfo, &size)) { | |
149 | + // printf("VerQueryValue() success (size=%d).\n", size); | |
150 | + | |
151 | + if (csvf) { | |
152 | + // FileVersion | |
153 | + printf(",\"%d,%d,%d,%d\"", | |
154 | + vfinfo->dwFileVersionMS >> 16, | |
155 | + vfinfo->dwFileVersionMS & 0xffff, | |
156 | + vfinfo->dwFileVersionLS >> 16, | |
157 | + vfinfo->dwFileVersionLS & 0xffff ); | |
158 | + // ProductVersion | |
159 | + printf(",\"%d,%d,%d,%d\"", | |
160 | + vfinfo->dwProductVersionMS >> 16, | |
161 | + vfinfo->dwProductVersionMS & 0xffff, | |
162 | + vfinfo->dwProductVersionLS >> 16, | |
163 | + vfinfo->dwProductVersionLS & 0xffff ); | |
164 | + } else { | |
165 | + printf("FileVersion : %d,%d,%d,%d\n", | |
166 | + vfinfo->dwFileVersionMS >> 16, | |
167 | + vfinfo->dwFileVersionMS & 0xffff, | |
168 | + vfinfo->dwFileVersionLS >> 16, | |
169 | + vfinfo->dwFileVersionLS & 0xffff ); | |
170 | + printf("ProductVersion : %d,%d,%d,%d\n", | |
171 | + vfinfo->dwProductVersionMS >> 16, | |
172 | + vfinfo->dwProductVersionMS & 0xffff, | |
173 | + vfinfo->dwProductVersionLS >> 16, | |
174 | + vfinfo->dwProductVersionLS & 0xffff ); | |
175 | + } | |
176 | + } else { | |
177 | + printf("GetFileVersionInfo() error.\n"); | |
178 | + } | |
179 | + i = 0; | |
180 | + while (header[i] != NULL) { | |
181 | + if (!GetRcValue(buf, header[i], &str)) { | |
182 | + if (csvf) { | |
183 | + printf(",\"%s\"", str); | |
184 | + } else { | |
185 | + printf("%-16s : [%s]\n", header[i], str); | |
186 | + } | |
187 | + } | |
188 | + ++i; | |
189 | + } | |
190 | + printf("\n"); | |
191 | + } else { | |
192 | + if (csvf) { | |
193 | + printf(",no version info.\n"); | |
194 | + } else { | |
195 | + printf("no version info.\n"); | |
196 | + } | |
197 | + } | |
198 | + } | |
199 | + return 0; | |
200 | +} | |
201 | + |
@@ -0,0 +1,48 @@ | ||
1 | +#include <stdio.h> | |
2 | + | |
3 | +#include <winsock2.h> | |
4 | +#include <iphlpapi.h> | |
5 | +#include <icmpapi.h> | |
6 | + | |
7 | +int | |
8 | +main() | |
9 | +{ | |
10 | + HANDLE hIcmp; | |
11 | + char *SendData = "ICMP SEND DATA"; | |
12 | + LPVOID ReplyBuffer; | |
13 | + DWORD dwRetVal; | |
14 | + DWORD buflen; | |
15 | + PICMP_ECHO_REPLY pIcmpEchoReply; | |
16 | + | |
17 | + hIcmp = IcmpCreateFile(); | |
18 | + | |
19 | + buflen = sizeof(ICMP_ECHO_REPLY) + strlen(SendData) + 1; | |
20 | + ReplyBuffer = (VOID*) malloc(buflen); | |
21 | + if (ReplyBuffer == NULL) { | |
22 | + return 1; | |
23 | + } | |
24 | + memset(ReplyBuffer, 0, buflen); | |
25 | + pIcmpEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer; | |
26 | + | |
27 | + dwRetVal = IcmpSendEcho(hIcmp, | |
28 | + inet_addr("127.0.0.1"), | |
29 | + SendData, strlen(SendData), | |
30 | + NULL, ReplyBuffer, | |
31 | + buflen, | |
32 | + 1000); | |
33 | + if (dwRetVal != 0) { | |
34 | + printf("Received %ld messages.\n", dwRetVal); | |
35 | + printf("\n"); | |
36 | + printf("RTT: %d\n", pIcmpEchoReply->RoundTripTime); | |
37 | + printf("Data Size: %d\n", pIcmpEchoReply->DataSize); | |
38 | + printf("Message: %s\n", pIcmpEchoReply->Data); | |
39 | + } else { | |
40 | + printf("Call to IcmpSendEcho() failed.\n"); | |
41 | + printf("Error: %ld\n", GetLastError()); | |
42 | + } | |
43 | + | |
44 | + IcmpCloseHandle(hIcmp); | |
45 | + | |
46 | + return 0; | |
47 | +} | |
48 | + |
@@ -0,0 +1,51 @@ | ||
1 | +#include <windows.h> | |
2 | +#include <stdio.h> | |
3 | + | |
4 | +//#define GET_USER_NAME_EX 1 | |
5 | + | |
6 | +void usage() | |
7 | +{ | |
8 | +#ifdef GET_USER_NAME_EX | |
9 | + printf("usage: id [{1|2|...|10}]\n"); | |
10 | +#else | |
11 | + printf("usage: id\n"); | |
12 | +#endif | |
13 | + exit(1); | |
14 | +} | |
15 | + | |
16 | +int main(int a, char *b[]) | |
17 | +{ | |
18 | + char userid[2048]; | |
19 | + int size = sizeof(userid); | |
20 | + int name_fmt; | |
21 | + | |
22 | + if (a > 1 && !strcmp(b[1], "-h")) { | |
23 | + usage(); | |
24 | + } | |
25 | + | |
26 | +#ifdef GET_USER_NAME_EX | |
27 | + if (a > 1) { | |
28 | + name_fmt = atoi(b[1]); | |
29 | + if (name_fmt < 1 || name_fmt > 10) { | |
30 | + usage(); | |
31 | + } | |
32 | + if (GetUserNameEx(name_fmt, userid, &size) == TRUE) { | |
33 | + printf("%s\n", userid); | |
34 | + } else { | |
35 | + printf("GetUserName: Error code = %d\n", GetLastError()); | |
36 | + return 1; | |
37 | + } | |
38 | + } else { | |
39 | +#endif | |
40 | + if (GetUserName(userid, &size) == TRUE) { | |
41 | + printf("%s\n", userid); | |
42 | + } else { | |
43 | + printf("GetUserName: Error code = %d\n", GetLastError()); | |
44 | + return 1; | |
45 | + } | |
46 | +#ifdef GET_USER_NAME_EX | |
47 | + } | |
48 | +#endif | |
49 | + return 0; | |
50 | +} | |
51 | + |
@@ -0,0 +1,196 @@ | ||
1 | +/* | |
2 | + * Joystickテスト (DirectInput) | |
3 | + * | |
4 | + * 08/08/17 V0.10 by oga. | |
5 | + */ | |
6 | +#include <windows.h> | |
7 | +#include <stdio.h> | |
8 | +#include "dinput.h" | |
9 | + | |
10 | +#define RELEASE(x) {if(x) { x->Release(); x = NULL;}} | |
11 | + | |
12 | +#define JOY_BUFSIZE 50 | |
13 | + | |
14 | + | |
15 | +LPDIRECTINPUT pDInput; | |
16 | +LPDIRECTINPUTDEVICE2 pDInputDevice; | |
17 | +int joy_ready = 0; | |
18 | + | |
19 | +/* | |
20 | + * ジョイスティックを取得 | |
21 | + */ | |
22 | +BOOL CALLBACK GetJoystickCallback(LPDIDEVICEINSTANCE lpddi,LPVOID pvRef) | |
23 | +{ | |
24 | + HRESULT ret; | |
25 | + LPDIRECTINPUTDEVICE pDev; | |
26 | + | |
27 | + printf("Start GetJoystickCallback.\n"); | |
28 | + // ジョイスティック用デバイスオブジェクトの作成 | |
29 | + ret = pDInput->CreateDevice(lpddi->guidInstance, &pDev, NULL); | |
30 | + if(ret != DI_OK){ | |
31 | + printf("End GetJoystickCallback. (CONTINUE)\n"); | |
32 | + return DIENUM_CONTINUE; | |
33 | + } | |
34 | + | |
35 | + pDev->QueryInterface(IID_IDirectInputDevice2, (LPVOID *)&pDInputDevice); | |
36 | + | |
37 | + printf("End GetJoystickCallback. (STOP)\n"); | |
38 | + return DIENUM_STOP; | |
39 | +} | |
40 | + | |
41 | +/* | |
42 | + * Direct Input 初期化 | |
43 | + */ | |
44 | +int InitDInput(void) | |
45 | +{ | |
46 | + HRESULT ret; | |
47 | + | |
48 | + //ret = DirectInputCreate(hInstApp,DIRECTINPUT_VERSION,&pDInput,NULL); | |
49 | + ret = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &pDInput,NULL); | |
50 | + if(ret != DI_OK){ | |
51 | + printf("DirectInputCreate Failed\n"); | |
52 | + return FALSE; | |
53 | + } | |
54 | + | |
55 | + // ジョイスティックを探す | |
56 | + pDInputDevice = NULL; | |
57 | + pDInput->EnumDevices(DIDEVTYPE_JOYSTICK,(LPDIENUMDEVICESCALLBACK)GetJoystickCallback,NULL,DIEDFL_ATTACHEDONLY); | |
58 | + if(pDInputDevice == NULL){ | |
59 | + printf("EnumDevices Failed. (no available joystick)\n"); | |
60 | + RELEASE(pDInput); | |
61 | + return TRUE; | |
62 | + } | |
63 | + | |
64 | + // データフォーマットを設定 | |
65 | + ret = pDInputDevice->SetDataFormat(&c_dfDIJoystick); | |
66 | + if(ret != DI_OK){ | |
67 | + printf("SetDataFormat Failed\n"); | |
68 | + RELEASE(pDInputDevice); | |
69 | + RELEASE(pDInput); | |
70 | + return FALSE; | |
71 | + } | |
72 | + | |
73 | +#if 0 | |
74 | + // モードを設定 | |
75 | + //ret = pDInputDevice->SetCooperativeLevel(hwnd,DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); | |
76 | + ret = pDInputDevice->SetCooperativeLevel(hwnd,DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); | |
77 | + | |
78 | + if(ret != DI_OK){ | |
79 | + printf("SetCooperativeLevel Failed\n"); | |
80 | + RELEASE(pDInputDevice); | |
81 | + RELEASE(pDInput); | |
82 | + return FALSE; | |
83 | + } | |
84 | +#endif | |
85 | + | |
86 | + DIPROPRANGE diprg; | |
87 | + | |
88 | + // 軸の値の範囲を設定 | |
89 | + diprg.diph.dwSize = sizeof(diprg); | |
90 | + diprg.diph.dwHeaderSize = sizeof(diprg.diph); | |
91 | + diprg.diph.dwObj = DIJOFS_X; | |
92 | + diprg.diph.dwHow = DIPH_BYOFFSET; | |
93 | + diprg.lMin = -1000; | |
94 | + diprg.lMax = +1000; | |
95 | + ret = pDInputDevice->SetProperty(DIPROP_RANGE, &diprg.diph); | |
96 | + if(ret != DI_OK){ | |
97 | + printf("SetProperty(DIPROP_RANGE X) Failed\n"); | |
98 | + } | |
99 | + | |
100 | + diprg.diph.dwObj = DIJOFS_Y; | |
101 | + ret = pDInputDevice->SetProperty(DIPROP_RANGE, &diprg.diph); | |
102 | + if(ret != DI_OK){ | |
103 | + printf("SetProperty(DIPROP_RANGE Y) Failed\n"); | |
104 | + } | |
105 | + | |
106 | + // 入力バッファの指定 (取りこぼし防止のためバッファを使用する場合) | |
107 | + DIPROPDWORD diwd; | |
108 | + | |
109 | + diwd.diph.dwSize = sizeof(diwd); | |
110 | + diwd.diph.dwHeaderSize = sizeof(diwd.diph); | |
111 | + diwd.diph.dwObj = 0; | |
112 | + diwd.diph.dwHow = DIPH_DEVICE; | |
113 | + diwd.dwData = JOY_BUFSIZE; // バッファサイズ | |
114 | + ret = pDInputDevice->SetProperty(DIPROP_BUFFERSIZE, &diwd.diph); | |
115 | + if(ret != DI_OK){ | |
116 | + printf("SetProperty(DIPROP_BUFFERSIZE) Failed\n"); | |
117 | + } | |
118 | + | |
119 | + // アクセス権を取得 | |
120 | + ret = pDInputDevice->Acquire(); | |
121 | + if(ret != DI_OK){ | |
122 | + printf("Acquire Failed\n"); | |
123 | + RELEASE(pDInputDevice); | |
124 | + RELEASE(pDInput); | |
125 | + return FALSE; | |
126 | + } | |
127 | + | |
128 | + joy_ready = 1; | |
129 | + return TRUE; | |
130 | +} | |
131 | + | |
132 | + | |
133 | +void ReleaseDInput() | |
134 | +{ | |
135 | + //if (pDInputDevice) pDInputDevice->Unacquire(); | |
136 | + RELEASE(pDInputDevice); | |
137 | + RELEASE(pDInput); | |
138 | +} | |
139 | + | |
140 | +int main(int a, char *b[]) | |
141 | +{ | |
142 | + DIJOYSTATE dijs; | |
143 | + int ret; | |
144 | + int i; | |
145 | + DWORD itemno; | |
146 | + DIDEVICEOBJECTDATA diobjdat[JOY_BUFSIZE]; | |
147 | + | |
148 | + InitDInput(); | |
149 | + | |
150 | + while (1) { | |
151 | + // 現在の状態を見る場合 | |
152 | + pDInputDevice->Poll(); | |
153 | + ret = pDInputDevice->GetDeviceState(sizeof(DIJOYSTATE), &dijs); | |
154 | + if (ret == DI_OK) { | |
155 | + printf("dijs.lX:%d lY:%d lRx:%d lRy:%d button:%d %d %d %d\n", | |
156 | + dijs.lX, dijs.lY, | |
157 | + dijs.lRx, dijs.lRy, | |
158 | + dijs.rgbButtons[0], dijs.rgbButtons[1], | |
159 | + dijs.rgbButtons[2], dijs.rgbButtons[3]); | |
160 | + if (dijs.rgbButtons[3]) { | |
161 | + printf("button4: quit!!\n"); | |
162 | + break; | |
163 | + } | |
164 | + } | |
165 | + | |
166 | + // バッファを見る場合 | |
167 | + itemno = JOY_BUFSIZE; | |
168 | + ret = pDInputDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), | |
169 | + &diobjdat[0], // 格納先バッファ | |
170 | + &itemno, // I:用意したバッファ数 / O:取得されたバッファ数 | |
171 | + 0); | |
172 | + if (ret == DI_OK) { | |
173 | + printf("itemno:%d\n", itemno); | |
174 | + for (i = 0; i < itemno; i++) { | |
175 | + if (diobjdat[i].dwOfs == DIJOFS_BUTTON0) { | |
176 | + printf("button0: %s %d\n", diobjdat[i].dwData?"ON":"OFF", diobjdat[i].dwData); | |
177 | + } else if (diobjdat[i].dwOfs == DIJOFS_BUTTON1) { | |
178 | + printf("button1: %s %d\n", diobjdat[i].dwData?"ON":"OFF", diobjdat[i].dwData); | |
179 | + } else if (diobjdat[i].dwOfs == DIJOFS_BUTTON2) { | |
180 | + printf("button2: %s %d\n", diobjdat[i].dwData?"ON":"OFF", diobjdat[i].dwData); | |
181 | + } else if (diobjdat[i].dwOfs == DIJOFS_BUTTON3) { | |
182 | + printf("button3: %s %d\n", diobjdat[i].dwData?"ON":"OFF", diobjdat[i].dwData); | |
183 | + } | |
184 | + } | |
185 | + } | |
186 | + Sleep(500); | |
187 | + } | |
188 | + | |
189 | + ReleaseDInput(); | |
190 | + return 0; | |
191 | +} | |
192 | + | |
193 | + | |
194 | +/* vim:ts=4:sw=4: | |
195 | + */ | |
196 | + |
@@ -0,0 +1,112 @@ | ||
1 | +// | |
2 | +// load.c CPU使用率を取得する。 (for NT) | |
3 | +// | |
4 | +// 98/12/24 by oga. | |
5 | +// | |
6 | + | |
7 | +#include <windows.h> | |
8 | +#include <stdio.h> | |
9 | + | |
10 | +#define dprintf printf | |
11 | + | |
12 | +//#define SYSBUF_SIZE 192 | |
13 | +#define SYSENT_SIZE 32 // sysentの数 | |
14 | +#define CPU_MAX 64 // 最大サポートCPU数 | |
15 | + | |
16 | +typedef struct _sysent { | |
17 | + hyper user; | |
18 | + hyper kernel; | |
19 | + hyper base; | |
20 | + hyper unknown[3]; | |
21 | +} sysent; | |
22 | + | |
23 | +hyper Base[CPU_MAX]; | |
24 | +hyper User[CPU_MAX]; | |
25 | +hyper Kernel[CPU_MAX]; | |
26 | + | |
27 | +BOOL first = TRUE; | |
28 | + | |
29 | +int (*NtQuerySystemInformation)(int, hyper [], int, int); | |
30 | + | |
31 | +//hyper sysbuf[SYSBUF_SIZE]; | |
32 | +sysent sysbuf[SYSENT_SIZE]; | |
33 | + | |
34 | +int Init() | |
35 | +{ | |
36 | + int i; | |
37 | + HMODULE hMod = GetModuleHandle("NTDLL.DLL"); | |
38 | + | |
39 | + memset(sysbuf, 0, sizeof(sysbuf)); | |
40 | + dprintf("size of sysbuf=%d\n",sizeof(sysbuf)); | |
41 | + | |
42 | + if (hMod) { | |
43 | + NtQuerySystemInformation = | |
44 | + (int (*)(int, hyper[], int, int))GetProcAddress(hMod, "NtQuerySystemInformation"); | |
45 | + for (i=0; i<CPU_MAX; i++) { | |
46 | + Base[i] = User[i] = Kernel[i] = (hyper)0; | |
47 | + printf("User = %d\n",User[i]); | |
48 | + } | |
49 | + return 0; | |
50 | + } else { | |
51 | + return -1; | |
52 | + } | |
53 | +} | |
54 | + | |
55 | +// | |
56 | +// IN : int rate[nProcessor] ... 取得する領域のアドレス(プロセッサ数分 | |
57 | +// int nProcessor ... 取得対象プロセッサの数 | |
58 | +// | |
59 | +int GetCpuLoad(int rate[], int nProcessor) | |
60 | +{ | |
61 | + int i; | |
62 | + hyper user, kernel, base; | |
63 | + | |
64 | + dprintf("NtQuerySystemInformation\n"); | |
65 | + NtQuerySystemInformation(sizeof(hyper), (hyper *)sysbuf, sizeof(sysbuf), 0); | |
66 | + dprintf("NtQuerySystemInformation End.\n"); | |
67 | + | |
68 | + for (i=0; i<nProcessor; i++) { | |
69 | + dprintf("Get diff. %d - %d\n",sysbuf[i].user, User[i]); | |
70 | + // Get differential value | |
71 | + user = sysbuf[i].user - User[i]; | |
72 | + dprintf("Get diff 2.\n"); | |
73 | + dprintf("Get diff. %d - %d - %d\n",sysbuf[i].kernel, sysbuf[i].user, Kernel[i]); | |
74 | + kernel = sysbuf[i].kernel - sysbuf[i].user - Kernel[i]; | |
75 | + dprintf("Get diff 3.\n"); | |
76 | + base = sysbuf[i].base - sysbuf[i].kernel - Base[i]; | |
77 | + | |
78 | + dprintf("Save cur value.\n"); | |
79 | + // Save current value | |
80 | + User[i] = sysbuf[i].user; | |
81 | + Kernel[i] = sysbuf[i].kernel - sysbuf[i].user; | |
82 | + Base[i] = sysbuf[i].base - sysbuf[i].kernel; | |
83 | + if (first) { | |
84 | + // 初回コール時は0%とする。 | |
85 | + rate[i] = 0; | |
86 | + } else { | |
87 | + dprintf("Calc rate.\n"); | |
88 | + user = 100 - ((user*100)/base); | |
89 | + kernel = (kernel*100)/base; | |
90 | + rate[i] = (user + kernel)/2; | |
91 | + } | |
92 | + } | |
93 | + | |
94 | + first = FALSE; | |
95 | + dprintf("GetCpuLoad Return.\n"); | |
96 | + return 0; | |
97 | +} | |
98 | + | |
99 | +int main(int a, char *b[]) | |
100 | +{ | |
101 | + int rate[64], nProcessor = 1; | |
102 | + | |
103 | + memset(rate, 0, sizeof(rate)); | |
104 | + | |
105 | + Init(); | |
106 | + | |
107 | + while(1) { | |
108 | + GetCpuLoad(rate, nProcessor); | |
109 | + printf("CPU Load : %d%%", rate[0]); | |
110 | + Sleep(1000); | |
111 | + } | |
112 | +} |
@@ -0,0 +1,87 @@ | ||
1 | +/* | |
2 | + * malloc : 指定サイズをmallocしてアクセスする | |
3 | + * | |
4 | + * 1999.12.27 V1.00 by hyper halx.oga | |
5 | + * 2010.03.04 V1.01 support -l (loop option), -w | |
6 | + */ | |
7 | + | |
8 | +#ifdef _WIN32 | |
9 | +#include <windows.h> | |
10 | +#endif | |
11 | +#include <stdio.h> | |
12 | +#include <errno.h> | |
13 | +#include <stdlib.h> | |
14 | + | |
15 | +#ifndef _WIN32 | |
16 | +#define Sleep(x) usleep((x)*1000) | |
17 | +#endif | |
18 | + | |
19 | +int lf = 0; /* -l: loop */ | |
20 | +int wait_sec = 30; /* -w: 30sec */ | |
21 | + | |
22 | +void usage() | |
23 | +{ | |
24 | + printf("usage : malloc <malloc_size[MB]> [-l [-w <wait_sec(%d)>]]\n", wait_sec); | |
25 | + printf(" -l: access loop\n"); | |
26 | + printf(" -w: loop wait time\n"); | |
27 | + exit(1); | |
28 | +} | |
29 | + | |
30 | +main(a,b) | |
31 | +int a; | |
32 | +char *b[]; | |
33 | +{ | |
34 | + char *adr; | |
35 | + int i; | |
36 | + int size = 0; | |
37 | + int cnt = 0; | |
38 | + char buf[1024]; | |
39 | + | |
40 | + for (i = 1; i<a; i++) { | |
41 | + if (!strcmp(b[i], "-h")) { | |
42 | + usage(); | |
43 | + } | |
44 | + if (!strcmp(b[i], "-l")) { | |
45 | + lf = 1; | |
46 | + continue; | |
47 | + } | |
48 | + if (!strcmp(b[i], "-w") && a>i+1) { | |
49 | + wait_sec = atoi(b[++i]); | |
50 | + continue; | |
51 | + } | |
52 | + size = atoi(b[i]) * 1024 * 1024; /* KB→MB V1.01-C */ | |
53 | + } | |
54 | + | |
55 | + if (size == 0) { | |
56 | + usage(); | |
57 | + } | |
58 | + | |
59 | + printf("malloc %d MB\n", size/1024/1024); | |
60 | + if ((adr = malloc(size)) == NULL) { | |
61 | + printf("malloc error %d\n",errno); | |
62 | + return 1; | |
63 | + } | |
64 | + if (lf) { | |
65 | + printf("wait sec: %d sec\n", wait_sec); | |
66 | + while (1) { | |
67 | + printf("access %d ....\n", ++cnt); | |
68 | + for (i = 0; i<size; i++) { | |
69 | + adr[i] = 'a'; | |
70 | + } | |
71 | + Sleep(wait_sec*1000); /* 10sec */ | |
72 | + } | |
73 | + } else { | |
74 | + printf("access ....\n"); | |
75 | + for (i = 0; i<size; i++) { | |
76 | + adr[i] = 'a'; | |
77 | + } | |
78 | + } | |
79 | + printf("enter 'exit' to free : "); | |
80 | + fflush(stdout); | |
81 | + scanf("%s",buf); | |
82 | + printf("free memory ...\n"); | |
83 | + if (adr) free(adr); | |
84 | + return 0; | |
85 | +} | |
86 | + | |
87 | + |
@@ -0,0 +1,46 @@ | ||
1 | +/* | |
2 | + * MIDI API Sample | |
3 | + * | |
4 | + * 10/01/03 V0.10 by oga. | |
5 | + * | |
6 | + * 参考: http://www.deqnotes.net/midi/winapi_midiprog/winapi_midiprog.pdf | |
7 | + */ | |
8 | + | |
9 | +#include <stdio.h> | |
10 | +#include <windows.h> | |
11 | +#include <mmsystem.h> | |
12 | + | |
13 | +int main(int a, char *b) | |
14 | +{ | |
15 | + MIDIOUTCAPS outCaps; | |
16 | + MIDIINCAPS inCaps; | |
17 | + MMRESULT res; | |
18 | + UINT num, devid; | |
19 | + | |
20 | + /* MIDI出力デバイス数を取得*/ | |
21 | + num = midiOutGetNumDevs(); | |
22 | + printf("## Number of output devices: %d\n", num); | |
23 | + /* 各デバイスID に対してfor ループ*/ | |
24 | + for (devid=0; devid<num; devid++) { | |
25 | + /* デバイスの情報をoutCaps に格納*/ | |
26 | + res = midiOutGetDevCaps(devid, &outCaps, sizeof(outCaps)); | |
27 | + /* midiOutGetDevCaps の戻り値が成功でない(=失敗) なら次のループへ*/ | |
28 | + if (res != MMSYSERR_NOERROR) { continue; } | |
29 | + /* デバイスID とそのデバイス名を表示*/ | |
30 | + printf("ID=%d: %s\n", devid, outCaps.szPname); | |
31 | + } | |
32 | + | |
33 | + /* MIDI入力デバイス数を取得*/ | |
34 | + num = midiInGetNumDevs(); | |
35 | + printf("## Number of input devices: %d\n", num); | |
36 | + /* 各デバイスID に対してfor ループ*/ | |
37 | + for (devid=0; devid<num; devid++) { | |
38 | + /* デバイスの情報をoutCaps に格納*/ | |
39 | + res = midiInGetDevCaps(devid, &inCaps, sizeof(inCaps)); | |
40 | + /* midiOutGetDevCaps の戻り値が成功でない(=失敗) なら次のループへ*/ | |
41 | + if (res != MMSYSERR_NOERROR) { continue; } | |
42 | + /* デバイスID とそのデバイス名を表示*/ | |
43 | + printf("ID=%d: %s\n", devid, inCaps.szPname); | |
44 | + } | |
45 | + return 0; | |
46 | +} |
@@ -0,0 +1,91 @@ | ||
1 | +//#include <windows.h> | |
2 | +#include "pcap.h" | |
3 | + | |
4 | +/* prototype of the packet handler */ | |
5 | +void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data); | |
6 | + | |
7 | +main() | |
8 | +{ | |
9 | + pcap_if_t *alldevs; | |
10 | + pcap_if_t *d; | |
11 | + int inum; | |
12 | + int i=0; | |
13 | + pcap_t *adhandle; | |
14 | + char errbuf[PCAP_ERRBUF_SIZE]; | |
15 | + | |
16 | + /* Retrieve the device list */ | |
17 | + if (pcap_findalldevs(&alldevs, errbuf) == -1) | |
18 | + { | |
19 | + fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); | |
20 | + exit(1); | |
21 | + } | |
22 | + | |
23 | + /* Print the list */ | |
24 | + for(d=alldevs; d; d=d->next) | |
25 | + { | |
26 | + printf("%d. %s", ++i, d->name); | |
27 | + if (d->description) | |
28 | + printf(" (%s)\n", d->description); | |
29 | + else | |
30 | + printf(" (No description available)\n"); | |
31 | + } | |
32 | + | |
33 | + if(i==0) { | |
34 | + printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); | |
35 | + return -1; | |
36 | + } | |
37 | + | |
38 | + printf("Enter the interface number (1-%d):",i); | |
39 | + scanf("%d", &inum); | |
40 | + | |
41 | + if(inum < 1 || inum > i) | |
42 | + { | |
43 | + printf("\nInterface number out of range.\n"); | |
44 | + /* Free the device list */ | |
45 | + pcap_freealldevs(alldevs); | |
46 | + return -1; | |
47 | + } | |
48 | + | |
49 | + /* Jump to the selected adapter */ | |
50 | + for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); | |
51 | + | |
52 | + /* Open the adapter */ | |
53 | + if ( (adhandle= pcap_open_live(d->name, // name of the device | |
54 | + 65536, // portion of the packet to capture. | |
55 | + // 65536 grants that the whole packet will be captured on all the MACs. | |
56 | + 1, // promiscuous mode | |
57 | + 1000, // read timeout | |
58 | + errbuf // error buffer | |
59 | + ) ) == NULL) | |
60 | + { | |
61 | + fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n"); | |
62 | + /* Free the device list */ | |
63 | + pcap_freealldevs(alldevs); | |
64 | + return -1; | |
65 | + } | |
66 | + | |
67 | + printf("\nlistening on %s...\n", d->description); | |
68 | + | |
69 | + /* At this point, we don't need any more the device list. Free it */ | |
70 | + pcap_freealldevs(alldevs); | |
71 | + | |
72 | + /* start the capture */ | |
73 | + pcap_loop(adhandle, 0, packet_handler, NULL); | |
74 | + | |
75 | + return 0; | |
76 | +} | |
77 | + | |
78 | + | |
79 | +/* Callback function invoked by libpcap for every incoming packet */ | |
80 | +void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) | |
81 | +{ | |
82 | + struct tm *ltime; | |
83 | + char timestr[16]; | |
84 | + | |
85 | + /* convert the timestamp to readable format */ | |
86 | + ltime=localtime(&header->ts.tv_sec); | |
87 | + strftime( timestr, sizeof timestr, "%H:%M:%S", ltime); | |
88 | + | |
89 | + printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len); | |
90 | + | |
91 | +} |
@@ -0,0 +1,388 @@ | ||
1 | +#include <windows.h> | |
2 | +#include <Mmsystem.h> | |
3 | +#include <stdio.h> | |
4 | + | |
5 | +void play1(char *media_file) | |
6 | +{ | |
7 | + int play_flag = SND_SYNC; | |
8 | + | |
9 | + printf("playing by sndPlaySound()...\n"); | |
10 | + sndPlaySound(media_file, play_flag); | |
11 | + | |
12 | +} | |
13 | + | |
14 | +void play2(char *media_file) | |
15 | +{ | |
16 | + //int play_flag = SND_APPLICATION | SND_SYNC; | |
17 | + int play_flag = SND_SYNC; | |
18 | + | |
19 | + printf("playing by PlaySound()...\n"); | |
20 | + PlaySound(media_file, | |
21 | + NULL, | |
22 | + SND_FILENAME | play_flag); | |
23 | +} | |
24 | + | |
25 | +// ### play3 start | |
26 | +#define OUTPUT_BUFFER_NUM 3 //再生ギャップを起こさないために2以上にする | |
27 | +#define OUTPUT_BUFFER_SIZE 0x8000 //処理が間に合わないならば大きくする | |
28 | +#define dprintf printf | |
29 | +LPWAVEHDR g_OutputBuffer[OUTPUT_BUFFER_NUM]; | |
30 | +HWAVEOUT g_hwo; | |
31 | +FILE *g_fp; | |
32 | +int g_nBuffUseNum; | |
33 | +int g_nBuffSetPos; | |
34 | +int g_playing = 0; | |
35 | +int g_rsize = 0; | |
36 | +int g_total = 0; | |
37 | + | |
38 | + | |
39 | +//再生バッファ確保 | |
40 | +BOOL AllocOutputBuffer() | |
41 | +{ | |
42 | + int i; | |
43 | + for(i = 0 ; i < OUTPUT_BUFFER_NUM ; i++){ | |
44 | + g_OutputBuffer[i] | |
45 | + = (LPWAVEHDR) HeapAlloc(GetProcessHeap(), | |
46 | + HEAP_ZERO_MEMORY, | |
47 | + sizeof(WAVEHDR)); | |
48 | + if(g_OutputBuffer[i]){ | |
49 | + g_OutputBuffer[i]->lpData | |
50 | + = (LPSTR) HeapAlloc(GetProcessHeap(), | |
51 | + HEAP_ZERO_MEMORY, | |
52 | + OUTPUT_BUFFER_SIZE); | |
53 | + if(g_OutputBuffer[i]->lpData) { | |
54 | + g_OutputBuffer[i]->dwBufferLength = OUTPUT_BUFFER_SIZE; | |
55 | + } | |
56 | + } | |
57 | + } | |
58 | + g_nBuffSetPos = g_nBuffUseNum = 0; | |
59 | + return TRUE; | |
60 | +} | |
61 | + | |
62 | +//再生バッファ開放 | |
63 | +void FreeOutputBuffer() | |
64 | +{ | |
65 | + int i; | |
66 | + for(i = 0 ; i < OUTPUT_BUFFER_NUM ; i++){ | |
67 | + if(g_OutputBuffer[i]){ | |
68 | + if(g_OutputBuffer[i]->lpData) { | |
69 | + HeapFree(GetProcessHeap(), 0, g_OutputBuffer[i]->lpData); | |
70 | + } | |
71 | + HeapFree(GetProcessHeap(), 0, g_OutputBuffer[i]); | |
72 | + g_OutputBuffer[i] = NULL; | |
73 | + } | |
74 | + } | |
75 | +} | |
76 | + | |
77 | +//データをバッファに読みこんで再生デバイスに送信 | |
78 | +void FillBuffer(HWAVEOUT hwo, FILE *fp) | |
79 | +{ | |
80 | + MMRESULT mmRes; | |
81 | + WAVEHDR* pHdr; | |
82 | + int size; | |
83 | + | |
84 | + while(g_nBuffUseNum < OUTPUT_BUFFER_NUM) { | |
85 | + pHdr = g_OutputBuffer[g_nBuffSetPos]; | |
86 | + | |
87 | + dprintf("DEBUG: read wav to #%d buff\n", g_nBuffSetPos); | |
88 | + | |
89 | + size = fread(pHdr->lpData, 1, OUTPUT_BUFFER_SIZE, fp); | |
90 | + if (size == 0) { | |
91 | + dprintf("DEBUG: no more data. g_nBuffUseNum = %d\n", g_nBuffUseNum); | |
92 | + break; | |
93 | + } | |
94 | + | |
95 | + pHdr->dwBufferLength = size; | |
96 | + | |
97 | + g_rsize += size; | |
98 | + dprintf("DEBUG: read size %d %3d%% (%d/%d)KB\n", size, | |
99 | + g_rsize/(g_total/100), | |
100 | + g_rsize/1024, | |
101 | + g_total/1024); | |
102 | + | |
103 | + mmRes = waveOutPrepareHeader(hwo, pHdr, sizeof(WAVEHDR)); | |
104 | + if(mmRes != MMSYSERR_NOERROR){ | |
105 | + dprintf("Error in FillBuffer\n"); | |
106 | + break; | |
107 | + } | |
108 | + mmRes = waveOutWrite(hwo, pHdr, sizeof(WAVEHDR)); | |
109 | + if(mmRes != MMSYSERR_NOERROR){ | |
110 | + dprintf("Error in FillBuffer\n"); | |
111 | + break; | |
112 | + } | |
113 | + g_playing = 1; | |
114 | + | |
115 | + g_nBuffSetPos = (g_nBuffSetPos + 1) % OUTPUT_BUFFER_NUM; | |
116 | + g_nBuffUseNum++; | |
117 | + } | |
118 | + | |
119 | + return; | |
120 | +} | |
121 | + | |
122 | + | |
123 | +// コールバック関数 | |
124 | +void CALLBACK waveOutCallBack(HWAVEOUT hwo, | |
125 | + UINT uMsg, | |
126 | + DWORD dwInstance, | |
127 | + DWORD dwParam1, | |
128 | + DWORD dwParam2) | |
129 | +{ | |
130 | + switch(uMsg) { | |
131 | + case WOM_OPEN: | |
132 | + // デバイスOpen | |
133 | + dprintf("WOM_OPEN event!\n"); | |
134 | + break; | |
135 | + | |
136 | + case WOM_CLOSE: | |
137 | + // デバイスClose | |
138 | + dprintf("WOM_CLOSE event!\n"); | |
139 | + break; | |
140 | + | |
141 | + case WOM_DONE: | |
142 | + // データブロック再生終了 | |
143 | + { | |
144 | + int i; | |
145 | + LPWAVEHDR pwh = (LPWAVEHDR)dwParam1; | |
146 | + dprintf("WOM_DONE event!\n"); | |
147 | + | |
148 | + //使用中バッファ数を一つ開放 | |
149 | + g_nBuffUseNum--; | |
150 | + | |
151 | + FillBuffer(hwo, g_fp); | |
152 | + | |
153 | + dprintf("WOM_DONE event! (g_nBuffUseNum:%d)\n", g_nBuffUseNum); | |
154 | + if (g_nBuffUseNum == 0){ | |
155 | + // 再生終了 | |
156 | + // データブロックの非準備状態へ | |
157 | + dprintf("Play done!\n"); | |
158 | + g_playing = 0; | |
159 | + } | |
160 | + } | |
161 | + break; | |
162 | + } | |
163 | +} | |
164 | + | |
165 | +/* data formats */ | |
166 | +typedef struct riff_hdr { | |
167 | + char id[4]; /* RIFF id ("RIFF") */ | |
168 | + unsigned long len; /* length */ | |
169 | + char wave_id[4]; /* data type ("WAVE") */ | |
170 | +} riff_hdr_t; | |
171 | + | |
172 | +typedef struct chunk_hdr { | |
173 | + char id[4]; /* chunk id ("fmt "|"data"...) */ | |
174 | + unsigned long len; /* chunk length */ | |
175 | +} chunk_hdr_t; | |
176 | + | |
177 | +/* chunk type fmt 1 */ | |
178 | +typedef struct ck_fmt { | |
179 | + unsigned short wFormatTag; /* Format category */ | |
180 | + unsigned short wChannels; /* Number of channels */ | |
181 | + unsigned long dwSamplesPerSec; /* Sampling rate */ | |
182 | + unsigned long dwAvgBytesPerSec; /* For buffer estimation */ | |
183 | + unsigned short wBlockAlign; /* Data block size */ | |
184 | + unsigned short wBitsPerSample; /* Sample size (for WAVE_FORMAT_PCM */ | |
185 | + unsigned short unknown; /* Unknown data */ | |
186 | + char dummy[256]; /* dummy area for over run */ | |
187 | +} ck_fmt_t; | |
188 | + | |
189 | +/* chunk type fact 2 */ | |
190 | +typedef struct ck_fact { | |
191 | + int unknown; /* unknown */ | |
192 | +} ck_fact_t; | |
193 | + | |
194 | +int GetWaveFormat(FILE *fp, WAVEFORMATEX *pwfx) | |
195 | +{ | |
196 | + riff_hdr_t riffhdr; | |
197 | + chunk_hdr_t chunkhdr; | |
198 | + char buf_fmt[4096]; | |
199 | + char buf_fact[4096]; | |
200 | + int size; | |
201 | + | |
202 | + ck_fmt_t *ckfmt = (ck_fmt_t *)buf_fmt; | |
203 | + ck_fact_t *ckfact = (ck_fact_t *)buf_fact; | |
204 | + | |
205 | + // Read WAV Header | |
206 | + fread(&riffhdr, sizeof(riffhdr), 1, fp); | |
207 | + if (memcmp(riffhdr.id, "RIFF", 4)) { | |
208 | + dprintf("not RIFF format\n"); | |
209 | + return -1; | |
210 | + } | |
211 | + | |
212 | + // Read Chunk Header | |
213 | + while (1) { | |
214 | + size = fread(&chunkhdr, 1, sizeof(chunkhdr), fp); | |
215 | + if (size <= 0) { | |
216 | + dprintf("Fmt Chunk Not Found Error\n"); | |
217 | + return -1; | |
218 | + } | |
219 | + if (!strncmp(chunkhdr.id, "fmt", 3)) { | |
220 | + // WAVフォーマットチャンク(fmt)発見 | |
221 | + // Read Fmt Chunk Header | |
222 | + size = fread(ckfmt, 1, chunkhdr.len, fp); | |
223 | + if (size != chunkhdr.len) { | |
224 | + dprintf("Fmt Chunk Read Error\n"); | |
225 | + return -1; | |
226 | + } | |
227 | + | |
228 | + // Stereo 16bit 44kHz | |
229 | + //wfx.wFormatTag = WAVE_FORMAT_PCM; | |
230 | + //wfx.nChannels = 2; | |
231 | + //wfx.nSamplesPerSec = 44100; | |
232 | + //wfx.wBitsPerSample = 16; | |
233 | + //wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample/8; | |
234 | + //wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; | |
235 | + //wfx.cbSize = 0; | |
236 | + | |
237 | + pwfx->wFormatTag = ckfmt->wFormatTag; | |
238 | + pwfx->nChannels = ckfmt->wChannels; | |
239 | + pwfx->nSamplesPerSec = ckfmt->dwSamplesPerSec; | |
240 | + pwfx->wBitsPerSample = ckfmt->wBitsPerSample; | |
241 | + pwfx->nBlockAlign = ckfmt->wBlockAlign; | |
242 | + pwfx->nAvgBytesPerSec = ckfmt->dwAvgBytesPerSec; | |
243 | + pwfx->cbSize = 0; | |
244 | + | |
245 | + dprintf("---- fmt chunk (len=%d) ----\n", chunkhdr.len); | |
246 | + dprintf("FormatTag : 0x%04x (%d) %s\n",ckfmt->wFormatTag, | |
247 | + ckfmt->wFormatTag, | |
248 | + (ckfmt->wFormatTag == WAVE_FORMAT_PCM)?"MS PCM":"Other PCM"); | |
249 | + dprintf("Channels : 0x%04x (%d) %s\n",ckfmt->wChannels, | |
250 | + ckfmt->wChannels, | |
251 | + (ckfmt->wChannels == 1)?"Mono":"Stereo"); | |
252 | + dprintf("SamplesPerSec : 0x%08x (%d)\n",ckfmt->dwSamplesPerSec, | |
253 | + ckfmt->dwSamplesPerSec); | |
254 | + dprintf("AvgBytesPerSec : 0x%08x (%d)\n",ckfmt->dwAvgBytesPerSec, | |
255 | + ckfmt->dwAvgBytesPerSec); | |
256 | + dprintf("BlockAlign : 0x%04x (%d)\n",ckfmt->wBlockAlign, | |
257 | + ckfmt->wBlockAlign); | |
258 | + dprintf("BitsPerSample : 0x%04x (%d bits)\n",ckfmt->wBitsPerSample, | |
259 | + ckfmt->wBitsPerSample); | |
260 | + | |
261 | + } else if (!strncmp(chunkhdr.id, "fact", 4)) { | |
262 | + // FACTチャンク(fact)は空読み | |
263 | + fread(ckfact, chunkhdr.len, 1, fp); | |
264 | + } else if (!strncmp(chunkhdr.id, "data", 4)) { | |
265 | + // DATAチャンク(data)発見 | |
266 | + // DATAブロックの先頭位置に位置付けてこの関数は終了 | |
267 | + dprintf("data chunk found. len = %d\n", chunkhdr.len); | |
268 | + g_total = chunkhdr.len; | |
269 | + break; | |
270 | + } | |
271 | + }; | |
272 | + | |
273 | + return 0; | |
274 | +} | |
275 | + | |
276 | +void play3(char *media_file) | |
277 | +{ | |
278 | + MMRESULT mmRes; | |
279 | + WAVEFORMATEX wfx; | |
280 | + int size; | |
281 | + int i; | |
282 | + | |
283 | + // Stereo 16bit 44kHz | |
284 | + //wfx.wFormatTag = WAVE_FORMAT_PCM; | |
285 | + //wfx.nChannels = 2; | |
286 | + //wfx.nSamplesPerSec = 44100; | |
287 | + //wfx.wBitsPerSample = 16; | |
288 | + //wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample/8; | |
289 | + //wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; | |
290 | + //wfx.cbSize = 0; | |
291 | + | |
292 | + // open wav | |
293 | + if ((g_fp = fopen(media_file, "rb")) == NULL) { | |
294 | + perror(media_file); | |
295 | + return; | |
296 | + } | |
297 | + | |
298 | + // get wav format | |
299 | + if (GetWaveFormat(g_fp, &wfx) < 0) { | |
300 | + dprintf("Wave Format Error. %s\n", media_file); | |
301 | + return; | |
302 | + } | |
303 | + | |
304 | + mmRes = waveOutOpen(&g_hwo, | |
305 | + WAVE_MAPPER, | |
306 | + &wfx, | |
307 | + (DWORD)(LPVOID)waveOutCallBack, | |
308 | + 0, | |
309 | + CALLBACK_FUNCTION); | |
310 | + if (mmRes != MMSYSERR_NOERROR) { | |
311 | + dprintf("waveOutOpen Error %d\n", mmRes); | |
312 | + exit(1); | |
313 | + } | |
314 | + | |
315 | + AllocOutputBuffer(); | |
316 | + | |
317 | + // wav読み込み & デバイス書き込み要求 | |
318 | + FillBuffer(g_hwo, g_fp); | |
319 | + | |
320 | + // すべて再生されるまで待つ | |
321 | + while (g_playing) { | |
322 | + Sleep(500); | |
323 | + } | |
324 | + | |
325 | + // データブロックの非準備状態へ | |
326 | + for(i = 0 ; i < OUTPUT_BUFFER_NUM ; i++) { | |
327 | + waveOutUnprepareHeader(g_hwo, | |
328 | + g_OutputBuffer[i], | |
329 | + sizeof(WAVEHDR)); | |
330 | + } | |
331 | + | |
332 | + //再生デバイスをクローズ | |
333 | + dprintf("DEBUG: waveOutClose()\n"); | |
334 | + waveOutClose(g_hwo); | |
335 | + | |
336 | + //再生バッファを開放 | |
337 | + dprintf("DEBUG: FreeOutputBuffer()\n"); | |
338 | + FreeOutputBuffer(); | |
339 | + | |
340 | + //WAVファイルクローズ | |
341 | + dprintf("DEBUG: fclose()\n"); | |
342 | + fclose(g_fp); | |
343 | +} | |
344 | +// ### play3 end | |
345 | + | |
346 | +int main(int a, char *b[]) | |
347 | +{ | |
348 | + int i; | |
349 | + int play_type = 3; | |
350 | + char *media_file = "I:\\WINNT\\Media\\ringin.wav"; | |
351 | + | |
352 | + for (i=1; i<a; i++) { | |
353 | + if (!strcmp(b[i], "-h")) { | |
354 | + printf("usage: play [{-1|-2|<-3>}] [media_file]\n"); | |
355 | + printf(" -1 : sndPlaySound()\n"); | |
356 | + printf(" -2 : PlaySound()\n"); | |
357 | + printf(" -3 : waveOutWrite() (default)\n"); | |
358 | + exit(1); | |
359 | + } | |
360 | + if (!strcmp(b[i], "-1")) { | |
361 | + play_type = 1; | |
362 | + continue; | |
363 | + } | |
364 | + if (!strcmp(b[i], "-2")) { | |
365 | + play_type = 2; | |
366 | + continue; | |
367 | + } | |
368 | + if (!strcmp(b[i], "-3")) { | |
369 | + play_type = 3; | |
370 | + continue; | |
371 | + } | |
372 | + media_file = b[i]; | |
373 | + } | |
374 | + | |
375 | + switch (play_type) { | |
376 | + case 1: | |
377 | + play1(media_file); | |
378 | + break; | |
379 | + case 2: | |
380 | + play2(media_file); | |
381 | + break; | |
382 | + case 3: | |
383 | + play3(media_file); | |
384 | + break; | |
385 | + } | |
386 | + | |
387 | + return 0; | |
388 | +} |
@@ -0,0 +1,53 @@ | ||
1 | +/* | |
2 | + * pwd.c | |
3 | + * | |
4 | + * 96/??/?? V1.00 for DOS | |
5 | + * 97/05/04 V1.01 for X68000 | |
6 | + * | |
7 | + * | |
8 | + * CFLAGS : default PC/AT | |
9 | + * X68K X68000 | |
10 | + */ | |
11 | +#include <stdio.h> | |
12 | +#include <stdlib.h> | |
13 | +#include <string.h> | |
14 | +#ifdef X68K | |
15 | +#include <unistd.h> | |
16 | +#include <sys/param.h> | |
17 | +#else | |
18 | +#include <direct.h> | |
19 | +#endif | |
20 | + | |
21 | +#ifdef X68K | |
22 | +#define _MAX_PATH MAXPATHLEN | |
23 | +#endif | |
24 | + | |
25 | +main() | |
26 | +{ | |
27 | + char buf[_MAX_PATH]; | |
28 | + char buf2[_MAX_PATH]; | |
29 | + int i; | |
30 | + | |
31 | + memset(buf2, 0, _MAX_PATH); | |
32 | + getcwd(buf,_MAX_PATH); | |
33 | + for (i=0; i < strlen(buf); i++) { | |
34 | + buf2[i] = tolower(buf[i]); | |
35 | + if (buf2[i] == '\\') { | |
36 | + buf2[i] = '/'; | |
37 | + } | |
38 | + } | |
39 | + printf("%s\n",buf2); | |
40 | +} | |
41 | + | |
42 | +#if 0 | |
43 | +bzero(buf,len) | |
44 | +char *buf; | |
45 | +int len; | |
46 | +{ | |
47 | + int i; | |
48 | + for (i=0; i<len; i++) | |
49 | + buf[i] = 0; | |
50 | + return len; | |
51 | +} | |
52 | +#endif | |
53 | + |
@@ -0,0 +1,65 @@ | ||
1 | +/* | |
2 | + * rand : ランダムテスト | |
3 | + * | |
4 | + * 2006/05/14 V0.10 by oga. | |
5 | + */ | |
6 | + | |
7 | +#ifdef _WIN32 | |
8 | +#include <windows.h> | |
9 | +#endif | |
10 | +#include <stdio.h> | |
11 | +#include <stdlib.h> | |
12 | +#include <time.h> | |
13 | + | |
14 | +/* macros */ | |
15 | +#define RAND(x) ((rand() & 0xffff)*(x)/(RAND_MAX & 0xffff)) | |
16 | + | |
17 | +/* globals */ | |
18 | +int vf = 0; /* -v */ | |
19 | + | |
20 | +void Usage() | |
21 | +{ | |
22 | + printf("usage: rand [<num(100)>]\n"); | |
23 | +} | |
24 | + | |
25 | +int main(int a, char *b[]) | |
26 | +{ | |
27 | + int i; | |
28 | + int max = 100; | |
29 | + time_t tt; | |
30 | + | |
31 | +#ifdef _WIN32 | |
32 | + SYSTEMTIME syst; | |
33 | +#endif | |
34 | + | |
35 | + for (i=1;i<a;i++) { | |
36 | + if (!strncmp(b[i],"-v",2)) { | |
37 | + ++vf; | |
38 | + continue; | |
39 | + } | |
40 | + if (!strncmp(b[i],"-h",2)) { | |
41 | + Usage(); | |
42 | + exit(1); | |
43 | + } | |
44 | + max = atoi(b[i]); | |
45 | + } | |
46 | + | |
47 | +#ifndef _WIN32 | |
48 | + /* Windowsではいまいち */ | |
49 | + tt = time(0); | |
50 | +#else /* _WIN32 */ | |
51 | + GetLocalTime(&syst); | |
52 | + tt = syst.wHour * 3600 * 1000 + | |
53 | + syst.wMinute * 60 * 1000 + | |
54 | + syst.wSecond * 1000 + | |
55 | + syst.wMilliseconds; | |
56 | +#endif /* _WIN32 */ | |
57 | + srand(tt); | |
58 | + | |
59 | + printf("tt:%d RAND(%d):%d\n", tt, max, RAND(max)); | |
60 | + | |
61 | + return 0; | |
62 | +} | |
63 | + | |
64 | + | |
65 | + |
@@ -0,0 +1,268 @@ | ||
1 | +/* | |
2 | + * raw I/O sample | |
3 | + * | |
4 | + * 07/03/25 V0.10 by oga. | |
5 | + * | |
6 | + */ | |
7 | +#include <stdio.h> | |
8 | +#include <windows.h> | |
9 | +#include <winioctl.h> | |
10 | + | |
11 | +/* | |
12 | + * Dump Data | |
13 | + * | |
14 | + */ | |
15 | +#define READ_SIZE (1024) | |
16 | +void DumpData(unsigned char *buf, int isize) | |
17 | +{ | |
18 | + int c, xx, addr = 0; | |
19 | + int f=0, f2=0; | |
20 | + int kflag = 0; | |
21 | + int fflag = 0; /* -f flush */ | |
22 | + int rev = 0; /* 1:dpの出力結果を元のファイルに戻す */ | |
23 | + int size = 0; | |
24 | + int work; | |
25 | + int pos = 0; /* for read */ | |
26 | + int opos = 0; /* for write */ | |
27 | + unsigned char obuf[1024]; /* for write */ | |
28 | + char asc[17]; | |
29 | + | |
30 | + printf("Location: +0 +4 +8 +C /0123456789ABCDEF\n"); | |
31 | + | |
32 | +#if 0 | |
33 | + /***** (original getc(first)) *****/ | |
34 | + size = fread(buf, 1, READ_SIZE, fp); | |
35 | + pos = 0; | |
36 | + if (size <= 0) { | |
37 | + c = EOF; | |
38 | + } else { | |
39 | + c = buf[pos++]; | |
40 | + } | |
41 | + /**********************************/ | |
42 | +#else | |
43 | + /* dummy read */ | |
44 | + if (isize < READ_SIZE) { | |
45 | + size = isize; | |
46 | + } else { | |
47 | + size = READ_SIZE; | |
48 | + } | |
49 | + if (isize == 0) { | |
50 | + c = EOF; | |
51 | + } else { | |
52 | + c = buf[pos++]; | |
53 | + } | |
54 | +#endif | |
55 | + | |
56 | + opos = 0; | |
57 | + while(c != EOF) { | |
58 | + xx = 0; | |
59 | + strcpy(asc," "); | |
60 | + printf("%08x: ",addr); | |
61 | + f = 0; | |
62 | + while(c != EOF && xx < 16) { | |
63 | + if (fflag) { | |
64 | + printf("%02x",c); /* これが実行時間の60% */ | |
65 | + fflush(stdout); /* 2.2倍くらい遅くなる */ | |
66 | + } else { | |
67 | + work = c >> 4; | |
68 | + obuf[opos++] = ((work > 9)?work-10+'a':work+'0'); | |
69 | + work = c & 0x0f; | |
70 | + obuf[opos++] = ((work > 9)?work-10+'a':work+'0'); | |
71 | + } | |
72 | + | |
73 | + if (c < 32) { | |
74 | + if (f) { | |
75 | + asc[xx] = c; | |
76 | + if (c == 10 || c == 13) { | |
77 | + asc[xx] = '.'; /* 暫定 */ | |
78 | + } | |
79 | + } else { | |
80 | + asc[xx] = '.'; | |
81 | + } | |
82 | + f = 0; | |
83 | + } else if (c >= 127 ) { | |
84 | + if (kflag) { | |
85 | + if (f) { | |
86 | + asc[xx] = c; | |
87 | + f = 0; | |
88 | + } else { | |
89 | + asc[xx] = c; | |
90 | + f = 1; | |
91 | + } | |
92 | + } else { | |
93 | + asc[xx] = '.'; | |
94 | + } | |
95 | + } else { | |
96 | + asc[xx] = c; | |
97 | + f = 0; | |
98 | + } | |
99 | + if (xx == 0 && f2) { | |
100 | + asc[xx] = '.'; | |
101 | + f = 0; | |
102 | + f2 = 0; | |
103 | + } | |
104 | + if ((xx % 4) == 3) { | |
105 | + if (fflag) { | |
106 | + printf(" "); | |
107 | + } else { | |
108 | + obuf[opos++] = ' '; | |
109 | + } | |
110 | + } | |
111 | + ++xx; | |
112 | + ++addr; | |
113 | + | |
114 | + /***** (original getc(next)) 10%up *****/ | |
115 | + if (pos >= size) { | |
116 | +#if 0 | |
117 | + size = fread(buf, 1, READ_SIZE, fp); | |
118 | + pos = 0; | |
119 | + if (size <= 0) { | |
120 | + c = EOF; | |
121 | + } else { | |
122 | + c = buf[pos++]; | |
123 | + } | |
124 | +#else | |
125 | + /* dummy read */ | |
126 | + if (isize < pos + READ_SIZE) { | |
127 | + size = isize - pos; | |
128 | + } | |
129 | + if (isize == 0) { | |
130 | + c = EOF; | |
131 | + } else { | |
132 | + c = buf[pos++]; | |
133 | + } | |
134 | +#endif | |
135 | + } else { | |
136 | + c = buf[pos++]; | |
137 | + } | |
138 | + /****************************************/ | |
139 | + | |
140 | + if (f == 1 && xx >= 16) { | |
141 | + asc[xx++] = c; | |
142 | + f = 0; | |
143 | + f2 = 1; | |
144 | + } | |
145 | + } | |
146 | + if (!fflag) { | |
147 | + obuf[opos] = '\0'; | |
148 | + printf("%s", obuf); | |
149 | + opos = 0; | |
150 | + } | |
151 | + | |
152 | + while (xx <16) { | |
153 | + printf(" "); | |
154 | + if ((xx % 4) == 3) printf(" "); | |
155 | + ++xx; | |
156 | + } | |
157 | + asc[xx]='\0'; | |
158 | + | |
159 | + printf("/%16s \n",asc); | |
160 | + } | |
161 | +} | |
162 | + | |
163 | +int RawRead(char *drive, int offset, int size) | |
164 | +{ | |
165 | + HANDLE hDev; | |
166 | + BOOL bRet; | |
167 | + unsigned char *buf; | |
168 | + DWORD readsize = 0; | |
169 | + DWORD bytesReturnd = 0; | |
170 | + char path[128]; | |
171 | + int ret; | |
172 | + FILE *fp; | |
173 | + | |
174 | + buf = (unsigned char *)malloc(4096); | |
175 | + if (buf == NULL) { | |
176 | + printf("malloc error\n"); | |
177 | + exit(1); | |
178 | + } | |
179 | + memset(buf, 0, 4096); | |
180 | + | |
181 | + sprintf(path, "\\\\.\\%s", drive); | |
182 | + //strcpy(path, drive); /* DEBUG */ | |
183 | + printf("path=[%s]\n", path); | |
184 | + | |
185 | +#if 0 | |
186 | + fp = fopen(path, "rb"); | |
187 | + if (fp == NULL) { | |
188 | + perror(path); | |
189 | + exit(1); | |
190 | + } | |
191 | + fread(buf, 1, size, fp); | |
192 | + DumpData(buf, size); | |
193 | + fclose(fp); | |
194 | + exit(1); | |
195 | +#endif | |
196 | + | |
197 | + /* Open Raw Device */ | |
198 | +#if 0 | |
199 | + hDev = CreateFile(path, 0, 0, NULL, 0, | |
200 | + FILE_FLAG_DELETE_ON_CLOSE, NULL); | |
201 | +#else | |
202 | + hDev = CreateFile(path, | |
203 | + GENERIC_READ, /* 読込み指定 */ | |
204 | + FILE_SHARE_READ | /* 複数プロセスのアクセス許可 */ | |
205 | + FILE_SHARE_WRITE, | |
206 | + NULL, /* セキュリティ属性なし */ | |
207 | + OPEN_EXISTING, /* 既存 or 新規ファイルオープン */ | |
208 | + FILE_ATTRIBUTE_NORMAL, /* ファイル属性なし */ | |
209 | + NULL); /* テンプレートファイルなし */ | |
210 | +#endif | |
211 | + if (hDev == INVALID_HANDLE_VALUE) { | |
212 | + printf("CreateFile Error: error=%d\n", GetLastError()); | |
213 | + exit(1); | |
214 | + } | |
215 | + | |
216 | +#if 0 /* いる? */ | |
217 | + /* Volume Lock */ | |
218 | + ret = DeviceIoControl(hDev, | |
219 | + FSCTL_LOCK_VOLUME, | |
220 | + NULL, | |
221 | + 0, | |
222 | + NULL, | |
223 | + 0, | |
224 | + &bytesReturnd, | |
225 | + NULL); | |
226 | + if (ret == 0) { | |
227 | + printf("DeviceIoControl Error: error=%d\n", GetLastError()); | |
228 | + exit(1); | |
229 | + } | |
230 | +#endif | |
231 | + | |
232 | + /* Read Data */ | |
233 | + bRet = ReadFile(hDev, buf, size, &readsize, NULL); | |
234 | + if (bRet == FALSE) { | |
235 | + printf("ReadFile Error: error=%d\n", GetLastError()); | |
236 | + CloseHandle(hDev); | |
237 | + exit(1); | |
238 | + } | |
239 | + DumpData(buf, size); | |
240 | + CloseHandle(hDev); | |
241 | + return 0; | |
242 | +} | |
243 | + | |
244 | +void usage() | |
245 | +{ | |
246 | + printf("usage: raw <drive>\n"); | |
247 | + exit(1); | |
248 | +} | |
249 | + | |
250 | +int main(int a, char *b[]) | |
251 | +{ | |
252 | + int i; | |
253 | + char *drive = NULL; | |
254 | + | |
255 | + for (i = 1; i < a; i++) { | |
256 | + if (!strcmp(b[i], "-h")) { | |
257 | + usage(); | |
258 | + } | |
259 | + drive = b[i]; | |
260 | + } | |
261 | + if (drive == NULL) { | |
262 | + usage(); | |
263 | + } | |
264 | + | |
265 | + RawRead(drive, 0, 512); /* sectorサイズ単位でないとInvalid Argとなる */ | |
266 | + return 0; | |
267 | +} | |
268 | + |
@@ -0,0 +1,986 @@ | ||
1 | +/* | |
2 | + * readevt | |
3 | + * eventlogkのダンプ | |
4 | + * | |
5 | + * 10/02/03 V0.10 by oga. (http://nienie.com/~masapico/api_ReadEventLog.html) | |
6 | + * 10/02/08 V0.20 support DB,FW collaboration | |
7 | + * 10/02/22 V0.30 support execute netstat -an | |
8 | + * 10/02/26 V0.31 delete bksv access on -fw | |
9 | + * 10/02/27 V0.32 Stop IIS | |
10 | + * 10/02/27 V0.33 Stop WWW | |
11 | + * 10/03/02 V0.34 Kill WinDump | |
12 | + * 10/03/03 V0.35 Fix WWW Service Name | |
13 | + * 10/03/04 V0.36 Fix -s option and support -w | |
14 | + * 10/03/24 V0.37 expand message buffer | |
15 | + * 10/04/09 V0.38 support no bkserver environment | |
16 | + * 10/04/15 V0.39 change send message host | |
17 | + * | |
18 | + */ | |
19 | + | |
20 | +#ifdef _WIN32 | |
21 | +#include <windows.h> | |
22 | +#endif | |
23 | +#include <stdio.h> | |
24 | +#include <time.h> | |
25 | +#include <sys/stat.h> | |
26 | +#include "tlist_common.h" | |
27 | + | |
28 | +#define VER "0.39" | |
29 | +#define TARGET_WIN "Wireshark: Capture from VMware Accelerated AMD PCNet Adapter" | |
30 | +#define TARGET_WIN_FW1 "Wireshark: Capture from Realtek RTL8168D/8111D Family PCI-E GBE NIC" | |
31 | +#define TARGET_WIN_FW2 "Wireshark: Capture from USB2.0 to Gigabit Ethernet Adapter" | |
32 | +//#define TARGET_WIN_DBG "Microsoft Word" | |
33 | +#define TARGET_WIN_DBG "イベント ビューア" | |
34 | +#define TARGET_PROC_WINDUMP "WinDump" | |
35 | + | |
36 | +/* wait msec */ | |
37 | +#define WAIT_VAL (3*60*1000) | |
38 | +#define BKSV_DIR "\\\\gaplus\\oga\\tool" /* V0.39-C */ | |
39 | +#define FW_DIR "\\\\gaplus\\oga\\tool" /* V0.39-C */ | |
40 | + | |
41 | +#define SERVICE_IIS "IIS Admin Service" | |
42 | +//#define SERVICE_WWW "World Wide Web Publishing Service" /* for -dbg */ | |
43 | +#define SERVICE_WWW "W3SVC" /* for -dbg */ | |
44 | + | |
45 | +#define MAX_FCNT 40 | |
46 | + | |
47 | +int vf = 0; /* -v */ | |
48 | +int csvf = 0; /* -csv */ | |
49 | +int dbgf = 0; /* -dbg debug */ | |
50 | +int dbf = 0; /* -db dbsv */ | |
51 | +int fwf = 0; /* -fw F/W */ | |
52 | +int daemonf = 0; /* -d */ | |
53 | +char *bksv_dir = BKSV_DIR; /* bksv_dir */ | |
54 | +char *fw_dir = FW_DIR; /* F/W dir */ | |
55 | +//char *service_iis = SERVICE_IIS; /* IIS */ | |
56 | +char *service_iis = SERVICE_WWW; /* WWW */ | |
57 | + | |
58 | +/* event log check span (sec) */ | |
59 | +int check_span = 1*3600; /* 1h */ | |
60 | + | |
61 | +/* check messsage */ | |
62 | +char check_msg[4096]; | |
63 | +char check_msg2[4096]; | |
64 | + | |
65 | +/* Group code */ | |
66 | +char *grp_code = "com"; | |
67 | + | |
68 | +/* wait val */ | |
69 | +int wait_val = WAIT_VAL; | |
70 | + | |
71 | +int max_fcnt = MAX_FCNT; | |
72 | + | |
73 | +/* for tlist API */ | |
74 | +#define MAX_TASKS 512 | |
75 | + | |
76 | +BOOL ForceKill = TRUE; | |
77 | +TASK_LIST tlist[MAX_TASKS]; | |
78 | + | |
79 | +void Log(char *msg) | |
80 | +{ | |
81 | + FILE *fp = NULL; | |
82 | + if ((fp = fopen("readevt.log", "a")) == NULL) { | |
83 | + perror("readevt.log"); | |
84 | + return; | |
85 | + } | |
86 | + fprintf(fp, "%s\n", msg); | |
87 | + if (fp) fclose(fp); | |
88 | +} | |
89 | + | |
90 | +// | |
91 | +// KillProc | |
92 | +// | |
93 | +// IN pname - process name | |
94 | +// | |
95 | +int KillProc(char *apname) | |
96 | +{ | |
97 | + int i; | |
98 | + int rval = 0; | |
99 | + char pname[MAX_PATH]; | |
100 | + char tname[PROCESS_SIZE+5]; | |
101 | + char *p; | |
102 | + DWORD numTasks; | |
103 | + DWORD ThisPid; | |
104 | + TASK_LIST_ENUM te; | |
105 | + | |
106 | + // 大文字に変換 | |
107 | + strcpy(pname, apname); | |
108 | + strupr(pname); | |
109 | + | |
110 | + printf("kill %s ...\n", pname); | |
111 | + | |
112 | + // | |
113 | + // Obtain the ability to manipulate other processes | |
114 | + // | |
115 | + EnableDebugPriv(); | |
116 | + | |
117 | +#if 0 | |
118 | + if (pid) { | |
119 | + // pidの場合 | |
120 | + tlist[0].dwProcessId = pid; | |
121 | + if (KillProcess( tlist, TRUE )) { | |
122 | + printf( "process #%d killed\n", pid ); | |
123 | + return 0; | |
124 | + } else { | |
125 | + printf( "process #%d could not be killed\n" ); | |
126 | + return 1; | |
127 | + } | |
128 | + } | |
129 | +#endif | |
130 | + | |
131 | + // | |
132 | + // get the task list for the system | |
133 | + // | |
134 | + numTasks = GetTaskList( tlist, MAX_TASKS ); | |
135 | + | |
136 | + // | |
137 | + // enumerate all windows and try to get the window | |
138 | + // titles for each task | |
139 | + // | |
140 | + te.tlist = tlist; | |
141 | + te.numtasks = numTasks; | |
142 | + GetWindowTitles( &te ); | |
143 | + | |
144 | + ThisPid = GetCurrentProcessId(); | |
145 | + | |
146 | + if (vf) printf("numTasks = %d\n", numTasks); | |
147 | + for (i=0; i<numTasks; i++) { | |
148 | + // | |
149 | + // this prevents the user from killing KILL.EXE and | |
150 | + // it's parent cmd window too | |
151 | + // | |
152 | + if (ThisPid == tlist[i].dwProcessId) { | |
153 | + continue; | |
154 | + } | |
155 | + if (MatchPattern( tlist[i].WindowTitle, "*KILL*" )) { | |
156 | + continue; | |
157 | + } | |
158 | + | |
159 | + tname[0] = 0; | |
160 | + | |
161 | + if (vf) printf("Process[%3d] %4d = %s (%s) %d\n", i, tlist[i].dwProcessId, tlist[i].ProcessName, tlist[i].WindowTitle, strlen(tlist[i].ProcessName)); | |
162 | + strcpy( tname, tlist[i].ProcessName ); | |
163 | + p = strchr( tname, '.' ); | |
164 | + if (p) { | |
165 | + p[0] = '\0'; | |
166 | + } | |
167 | + if (MatchPattern( tname, pname )) { | |
168 | + tlist[i].flags = TRUE; | |
169 | + } else if (MatchPattern( tlist[i].ProcessName, pname )) { | |
170 | + tlist[i].flags = TRUE; | |
171 | + } else if (MatchPattern( tlist[i].WindowTitle, pname )) { | |
172 | + tlist[i].flags = TRUE; | |
173 | + } | |
174 | + } | |
175 | + | |
176 | + for (i=0; i<numTasks; i++) { | |
177 | + if (tlist[i].flags) { | |
178 | + if (KillProcess( &tlist[i], ForceKill )) { | |
179 | + printf( "process #%d [%s] killed\n", tlist[i].dwProcessId, tlist[i].ProcessName ); | |
180 | + } else { | |
181 | + printf( "process #%d [%s] could not be killed\n", tlist[i].dwProcessId, tlist[i].ProcessName ); | |
182 | + rval = 1; | |
183 | + } | |
184 | + } | |
185 | + } | |
186 | + | |
187 | + return rval; | |
188 | +} | |
189 | + | |
190 | +// | |
191 | +// コマンドフルパス検索 | |
192 | +// | |
193 | +void Which(char *cmd, char *fullpath) | |
194 | +{ | |
195 | + char *pathp; | |
196 | + char path[2048]; | |
197 | + int pathlen; | |
198 | + int i, j; | |
199 | + int found = 0; | |
200 | + int comsize = 0; | |
201 | + struct stat stbuf; | |
202 | + | |
203 | + strcpy(fullpath, ""); | |
204 | + | |
205 | + pathp = getenv("PATH"); | |
206 | + pathlen = strlen(pathp); | |
207 | + | |
208 | + i = 0; | |
209 | + while (i < pathlen) { | |
210 | + j = 0; | |
211 | + while (pathp[i] != ';' && i < pathlen) { /* path[] = "/usr/bin" */ | |
212 | + path[j] = pathp[i]; | |
213 | + if (path[j] == '\\') { | |
214 | + path[j] = '/'; | |
215 | + } | |
216 | + ++i; | |
217 | + ++j; | |
218 | + } | |
219 | + i++; | |
220 | + path[j] = '\0'; | |
221 | + | |
222 | + strcat(path, "/"); /* path[] = "/usr/bin/" */ | |
223 | + strcat(path, cmd); /* path[] = "/usr/bin/com" */ | |
224 | + | |
225 | + comsize = strlen(path); | |
226 | + strcpy(&path[comsize], ".exe"); /* path[] = "/usr/bin/com.exe"*/ | |
227 | + if (stat(path, &stbuf) == 0) { | |
228 | + found = 1; | |
229 | + break; | |
230 | + } | |
231 | + //strcpy(&path[comsize], ".com"); /* path[] = "/usr/bin/com.com"*/ | |
232 | + //if (stat(path, &stbuf) == 0) { | |
233 | + // found = 1; | |
234 | + // break; | |
235 | + //} | |
236 | + } | |
237 | + if (found) { | |
238 | + strcpy(fullpath, path); | |
239 | + } | |
240 | +} | |
241 | + | |
242 | +// | |
243 | +// ExecCmd() : コマンドを実行する | |
244 | +// | |
245 | +// IN cmd : 実行するコマンド <command> | |
246 | +// IN comargs : コマンド文字列 <command> <args> ... | |
247 | +// OUT ret : >= 0 success (command exit code) | |
248 | +// -1 error | |
249 | +// | |
250 | +DWORD ExecCmd(char *cmd, char *comarg) | |
251 | +{ | |
252 | + BOOL status; | |
253 | + STARTUPINFO si; | |
254 | + PROCESS_INFORMATION pi; | |
255 | + | |
256 | + HANDLE hProc; /* Process Handle */ | |
257 | + DWORD pst; | |
258 | + DWORD st = 0; /* success */ | |
259 | + | |
260 | + /* make command line */ | |
261 | + memset(&si, 0, sizeof(si)); | |
262 | + si.cb = sizeof(si); | |
263 | + status = CreateProcess(cmd, /* 実行モジュール名 */ | |
264 | + comarg, /* コマンドライン */ | |
265 | + NULL, /* プロセスのセキュリティ属性 */ | |
266 | + NULL, /* スレッドのセキュリティ属性 */ | |
267 | + FALSE, /* ハンドルを継承しない */ | |
268 | + 0, /* fdwCreate */ | |
269 | + NULL, /* 環境ブロックのアドレス */ | |
270 | + NULL, /* カレントディレクトリ(親同) */ | |
271 | + &si, /* STARTUPINFO構造体 */ | |
272 | + &pi); /* PROCESS_INFORMATION構造体 */ | |
273 | + if(status != TRUE){ | |
274 | + printf("exec_cmd: CreateProcess returned=%d\n",GetLastError()); | |
275 | + return -1; /* error */ | |
276 | + } | |
277 | + | |
278 | + hProc = pi.hProcess; | |
279 | + status=CloseHandle(pi.hThread); | |
280 | + if(status != TRUE){ | |
281 | + printf("CloseHandle error(%d).\n", GetLastError()); | |
282 | + } | |
283 | + | |
284 | + /* | |
285 | + * wait for command end. | |
286 | + */ | |
287 | + if (WaitForSingleObject(hProc, INFINITE) != WAIT_FAILED) { | |
288 | + /* process end!! */ | |
289 | + if (!GetExitCodeProcess(hProc, &pst)) { | |
290 | + //printf("exec_cmd: GetExitCodeProcess error(%d)\n", GetLastError()); | |
291 | + st = 0; /* error */ | |
292 | + } else { | |
293 | + st = pst; | |
294 | + } | |
295 | + } | |
296 | + | |
297 | + status = CloseHandle(hProc); | |
298 | + if(status != TRUE){ | |
299 | + //printf("CloseHandle error(%d).\n", GetLastError()); | |
300 | + } | |
301 | + return st; | |
302 | +} | |
303 | + | |
304 | +/* web server側が書き込む */ | |
305 | +/* | |
306 | + * msg : "": 通知ファイル0バイト化 | |
307 | + */ | |
308 | +void SendBKSVMsg(char *msg) | |
309 | +{ | |
310 | + FILE *fp = NULL; | |
311 | + char *mode = "a"; | |
312 | + char fname[2048]; | |
313 | + | |
314 | + if (strlen(msg) == 0) { | |
315 | + mode = "w"; | |
316 | + } | |
317 | + | |
318 | + if (!fwf) { | |
319 | + /* bkserver (Web, DB) */ | |
320 | + sprintf(fname, "%s\\%s.sendmsg.txt", bksv_dir, grp_code); | |
321 | + if ((fp = fopen(fname, mode)) == NULL) { | |
322 | + perror(fname); | |
323 | + return; | |
324 | + } | |
325 | + fprintf(fp, "%s", msg); | |
326 | + if (fp) fclose(fp); | |
327 | + } | |
328 | + | |
329 | + /* DBサーバはFWのファイルは処理しない */ | |
330 | + if (dbf) return; | |
331 | + | |
332 | + if (!dbf) { | |
333 | + /* firewall (Web, FW) */ | |
334 | + sprintf(fname, "%s\\fw.sendmsg.txt", fw_dir); | |
335 | + if ((fp = fopen(fname, mode)) == NULL) { | |
336 | + perror(fname); | |
337 | + return; | |
338 | + } | |
339 | + fprintf(fp, "%s", msg); | |
340 | + if (fp) fclose(fp); | |
341 | + } | |
342 | +} | |
343 | + | |
344 | +/* bkserver経由で発生通知があるかチェックする */ | |
345 | +int FileSVChk() | |
346 | +{ | |
347 | + struct stat stbuf; | |
348 | + char fname[2048]; | |
349 | + int ret; | |
350 | + | |
351 | + sprintf(fname, "%s\\%s.sendmsg.txt", bksv_dir, grp_code); | |
352 | + ret = stat(fname, &stbuf); | |
353 | + if (ret == 0 && stbuf.st_size > 5) { | |
354 | + return 1; /* Webサーバからのイベント通知あり */ | |
355 | + } | |
356 | + return 0; /* Webサーバからのイベント通知なし */ | |
357 | +} | |
358 | + | |
359 | +/* F/Wに発生通知があるかチェックする */ | |
360 | +int FWChk() | |
361 | +{ | |
362 | + struct stat stbuf; | |
363 | + char fname[2048]; | |
364 | + int ret; | |
365 | + | |
366 | + sprintf(fname, "%s\\fw.sendmsg.txt", fw_dir); | |
367 | + ret = stat(fname, &stbuf); | |
368 | + if (ret == 0 && stbuf.st_size > 5) { | |
369 | + return 1; /* Webサーバからのイベント通知あり */ | |
370 | + } | |
371 | + return 0; /* Webサーバからのイベント通知なし */ | |
372 | +} | |
373 | + | |
374 | + | |
375 | +/* 埋込み文字列の取得 */ | |
376 | +char **GetArgs(const EVENTLOGRECORD *pBuf) | |
377 | +{ | |
378 | + char *cp; | |
379 | + WORD ArgCount; | |
380 | + char **Args = NULL; | |
381 | + | |
382 | + if(pBuf->NumStrings == 0) return NULL; | |
383 | + | |
384 | + /* 引数リストを取得 */ | |
385 | + Args = GlobalAlloc(GMEM_FIXED, sizeof(char *) * pBuf->NumStrings); | |
386 | + cp = (char *)pBuf + (pBuf->StringOffset); | |
387 | + | |
388 | + for(ArgCount=0; ArgCount<pBuf->NumStrings; ArgCount++) { | |
389 | + Args[ArgCount] = cp; | |
390 | + cp += strlen(cp) + 1; | |
391 | + } | |
392 | + return Args; | |
393 | +} | |
394 | + | |
395 | +/* ソース名からモジュール名を取得 */ | |
396 | +BOOL GetModuleNameFromSourceName( | |
397 | + const char *SourceName, | |
398 | + const char *EntryName, | |
399 | + char *ExpandedName /* 1000バイトのバッファ */) | |
400 | +{ | |
401 | + DWORD lResult; | |
402 | + DWORD ModuleNameSize; | |
403 | + char ModuleName[1000]; | |
404 | + HKEY hAppKey = NULL; | |
405 | + HKEY hSourceKey = NULL; | |
406 | + BOOL bReturn = FALSE; | |
407 | + | |
408 | + /* Applicationログ用のレジストリキーをオープン */ | |
409 | + lResult = RegOpenKeyEx( | |
410 | + HKEY_LOCAL_MACHINE, | |
411 | + "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application", | |
412 | + 0, | |
413 | + KEY_READ, | |
414 | + &hAppKey); | |
415 | + | |
416 | + if(lResult != ERROR_SUCCESS) { | |
417 | + printf("registry can not open.\n"); | |
418 | + goto Exit; | |
419 | + } | |
420 | + | |
421 | + /* ソースの情報が格納されているレジストリをオープン */ | |
422 | + lResult = RegOpenKeyEx( | |
423 | + hAppKey, | |
424 | + SourceName, | |
425 | + 0, | |
426 | + KEY_READ, | |
427 | + &hSourceKey); | |
428 | + | |
429 | + if(lResult != ERROR_SUCCESS) goto Exit; | |
430 | + | |
431 | + ModuleNameSize = 1000; | |
432 | + | |
433 | + /* ソースモジュール名を取得 */ | |
434 | + lResult = RegQueryValueEx( | |
435 | + hSourceKey, | |
436 | + "EventMessageFile", | |
437 | + NULL, | |
438 | + NULL, | |
439 | + ModuleName, | |
440 | + &ModuleNameSize); | |
441 | + | |
442 | + if(lResult != ERROR_SUCCESS) goto Exit; | |
443 | + | |
444 | + /* 環境変数を展開 */ | |
445 | + ExpandEnvironmentStrings(ModuleName, ExpandedName, 1000); | |
446 | + | |
447 | + /* 正常終了 */ | |
448 | + bReturn = TRUE; | |
449 | + | |
450 | +Exit: /* 後処理 */ | |
451 | + if(hSourceKey != NULL) RegCloseKey(hSourceKey); | |
452 | + if(hAppKey != NULL) RegCloseKey(hAppKey); | |
453 | + | |
454 | + return bReturn; | |
455 | +} | |
456 | + | |
457 | + | |
458 | + | |
459 | + | |
460 | +/* メッセージの表示 */ | |
461 | +BOOL DispMessage( | |
462 | + const char *SourceName, | |
463 | + const char *EntryName, | |
464 | + const char **Args, | |
465 | + DWORD MessageId, | |
466 | + char *msgbuf, | |
467 | + int dispf) | |
468 | +{ | |
469 | + BOOL bResult; | |
470 | + BOOL bReturn = FALSE; | |
471 | + HANDLE hSourceModule = NULL; | |
472 | + char SourceModuleName[1000]; | |
473 | + char *pMessage = NULL; | |
474 | + | |
475 | + /* ソースモジュール名を取得 */ | |
476 | + bResult = GetModuleNameFromSourceName(SourceName, EntryName, SourceModuleName); | |
477 | + if(!bResult) goto Exit; | |
478 | + | |
479 | + /* ソースモジュールをロード */ | |
480 | + hSourceModule = LoadLibraryEx( | |
481 | + SourceModuleName, | |
482 | + NULL, | |
483 | + DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE); | |
484 | + | |
485 | + if(hSourceModule == NULL) goto Exit; | |
486 | + | |
487 | + /* メッセージを作成 */ | |
488 | + FormatMessage( | |
489 | + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY, | |
490 | + hSourceModule, | |
491 | + MessageId, | |
492 | + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
493 | + (LPSTR)&pMessage, | |
494 | + 0, | |
495 | + (va_list *)Args); | |
496 | + | |
497 | + /* 正常終了 */ | |
498 | + bReturn = TRUE; | |
499 | + | |
500 | +Exit: /* 後処理 */ | |
501 | + if(pMessage != NULL) { | |
502 | + if (dispf) printf("%s", pMessage); | |
503 | + if (msgbuf) { | |
504 | + strcpy(msgbuf, pMessage); | |
505 | + } | |
506 | + } else { | |
507 | + if (dispf) printf("(%d)\n", MessageId); | |
508 | + } | |
509 | + | |
510 | + if(hSourceModule != NULL) FreeLibrary(hSourceModule); | |
511 | + if(pMessage != NULL) LocalFree(pMessage); | |
512 | + | |
513 | + return bReturn; | |
514 | +} | |
515 | + | |
516 | + | |
517 | +/* | |
518 | + * イベントログの読み取り | |
519 | + * OUT ret : -1 error | |
520 | + * 0 success nop | |
521 | + * 1 success Exsit Specified Event | |
522 | + */ | |
523 | +int ReadLog(void) | |
524 | +{ | |
525 | + DWORD BufSize; | |
526 | + DWORD ReadBytes; | |
527 | + DWORD NextSize; | |
528 | + BOOL bResult; | |
529 | + DWORD i; | |
530 | + char *cp; | |
531 | + char *pSourceName; | |
532 | + char *pComputerName; | |
533 | + HANDLE hEventLog = NULL; | |
534 | + EVENTLOGRECORD *pBuf = NULL; | |
535 | + char **Args = NULL; | |
536 | + char buf1[128]; | |
537 | + char buf2[128]; | |
538 | + char msgbuf[4096*4]; | |
539 | + char wkmsg[4096*4]; | |
540 | + time_t tt; | |
541 | + int dispf = 1; | |
542 | + int ret = 0; | |
543 | + | |
544 | + /* アプリケーションイベントログのオープン */ | |
545 | + hEventLog = OpenEventLog(NULL, "Application"); | |
546 | + | |
547 | + if(hEventLog == NULL) { | |
548 | + printf("event log can not open.\n"); | |
549 | + return -1; | |
550 | + } | |
551 | + | |
552 | + for(;;) { | |
553 | + /* イベントログのサイズ取得 */ | |
554 | + BufSize = 1; | |
555 | + pBuf = GlobalAlloc(GMEM_FIXED, BufSize); | |
556 | + | |
557 | + bResult = ReadEventLog( | |
558 | + hEventLog, | |
559 | + EVENTLOG_FORWARDS_READ | EVENTLOG_SEQUENTIAL_READ, | |
560 | + 0, | |
561 | + pBuf, | |
562 | + BufSize, | |
563 | + &ReadBytes, | |
564 | + &NextSize); | |
565 | + | |
566 | + if(!bResult && GetLastError() != ERROR_INSUFFICIENT_BUFFER) break; | |
567 | + | |
568 | + GlobalFree(pBuf); | |
569 | + pBuf = NULL; | |
570 | + | |
571 | + /* バッファ割り当て */ | |
572 | + BufSize = NextSize; | |
573 | + pBuf = GlobalAlloc(GMEM_FIXED, BufSize); | |
574 | + | |
575 | + /* イベントログの読み取り */ | |
576 | + bResult = ReadEventLog( | |
577 | + hEventLog, | |
578 | + EVENTLOG_FORWARDS_READ | EVENTLOG_SEQUENTIAL_READ, | |
579 | + 0, | |
580 | + pBuf, | |
581 | + BufSize, | |
582 | + &ReadBytes, | |
583 | + &NextSize); | |
584 | + | |
585 | + if(!bResult) break; | |
586 | + | |
587 | + tt = time(0); | |
588 | + | |
589 | + /* 期間内なら表示 / -a(check_span:0) なら全部表示 */ | |
590 | + if (pBuf->TimeWritten > tt - check_span || check_span == 0) { | |
591 | + dispf = 1; /* 表示対象(期間内) */ | |
592 | + } else { | |
593 | + dispf = 0; /* 表示対象外 */ | |
594 | + } | |
595 | + | |
596 | + /* 読み取ったイベントの表示 */ | |
597 | +#if 0 | |
598 | + //printf("レコード番号: %d\n", pBuf->RecordNumber); | |
599 | + //printf("生成時刻: %s", ctime(&pBuf->TimeGenerated)); | |
600 | + //printf("書き込み時刻: %s", ctime(&pBuf->TimeWritten)); | |
601 | + //printf("生成時刻: %s / 書き込み時刻: %s\n", buf1, buf2); | |
602 | +#endif | |
603 | + strcpy(msgbuf, ""); | |
604 | + strftime(buf1, sizeof(buf1), "%y/%m/%d %H:%M:%S", localtime(&pBuf->TimeGenerated)); | |
605 | + strftime(buf2, sizeof(buf2), "%y/%m/%d %H:%M:%S", localtime(&pBuf->TimeWritten)); | |
606 | + | |
607 | + cp = (char *)pBuf; | |
608 | + cp += sizeof(EVENTLOGRECORD); | |
609 | + | |
610 | + pSourceName = cp; | |
611 | + cp += strlen(cp)+1; | |
612 | + | |
613 | + pComputerName = cp; | |
614 | + cp += strlen(cp)+1; | |
615 | + | |
616 | + if ((dispf && (daemonf == 0 || vf)) || vf >= 2) { | |
617 | + printf("レコード番号: %05d\n", pBuf->RecordNumber); | |
618 | + printf("時刻: %s イベントID: %08x\n", buf2, pBuf->EventID); | |
619 | + | |
620 | + printf("イベントの種別: "); | |
621 | + switch(pBuf->EventType) { | |
622 | + case EVENTLOG_SUCCESS: printf("成功\n"); break; | |
623 | + case EVENTLOG_ERROR_TYPE: printf("エラー\n"); break; | |
624 | + case EVENTLOG_WARNING_TYPE: printf("警告\n"); break; | |
625 | + case EVENTLOG_INFORMATION_TYPE: printf("情報\n"); break; | |
626 | + case EVENTLOG_AUDIT_SUCCESS: printf("監査成功\n"); break; | |
627 | + case EVENTLOG_AUDIT_FAILURE: printf("監査失敗\n"); break; | |
628 | + default: printf("不明\n"); break; | |
629 | + } | |
630 | + | |
631 | + printf("ソース名: %s\n", pSourceName); | |
632 | + printf("コンピュータ名: %s\n", pComputerName); | |
633 | + | |
634 | + /* カテゴリの表示 */ | |
635 | + printf("二次カテゴリ: ", pBuf->EventCategory); | |
636 | + DispMessage(pSourceName, "CategoryMessageFile", NULL, pBuf->EventCategory, NULL, 1); | |
637 | + | |
638 | + /* メッセージの表示 */ | |
639 | + Args = GetArgs(pBuf); | |
640 | + | |
641 | + printf("メッセージ: "); | |
642 | + DispMessage(pSourceName, "EventMessageFile", Args, pBuf->EventID, msgbuf, 1); | |
643 | + | |
644 | + if(Args != NULL) { | |
645 | + GlobalFree(Args); | |
646 | + Args = NULL; | |
647 | + } | |
648 | + | |
649 | + /* 固有データの表示 */ | |
650 | + if(pBuf->DataLength > 0 && csvf == 0) { | |
651 | + printf("固有データ: "); | |
652 | + for(i=0; i<pBuf->DataLength; i++) printf("%02x ", *(((unsigned char *)pBuf)+(pBuf->DataOffset)+i)); | |
653 | + printf("\n"); | |
654 | + } | |
655 | + | |
656 | + printf("\n"); | |
657 | + } else { | |
658 | + /* メッセージの表示 */ | |
659 | + Args = GetArgs(pBuf); | |
660 | + | |
661 | + DispMessage(pSourceName, "EventMessageFile", Args, pBuf->EventID, msgbuf, 0); | |
662 | + | |
663 | + if(Args != NULL) { | |
664 | + GlobalFree(Args); | |
665 | + Args = NULL; | |
666 | + } | |
667 | + } | |
668 | + if (dispf && strstr(msgbuf, check_msg)) { | |
669 | + printf("found [%s]: %s %s\n", check_msg, buf2, msgbuf); | |
670 | + sprintf(wkmsg, "found [%s]: %s %s\n", check_msg, buf2, msgbuf); | |
671 | + Log(wkmsg); | |
672 | + if (dbf == 0) SendBKSVMsg(wkmsg); /* WebSV => BKSV => DBSV */ | |
673 | + ret = 1; /* 期間内に目的のメッセージあり */ | |
674 | + } | |
675 | + if (dispf && strstr(msgbuf, check_msg2)) { | |
676 | + printf("found [%s]: %s %s\n", check_msg2, buf2, msgbuf); | |
677 | + sprintf(wkmsg, "found [%s]: %s %s\n", check_msg, buf2, msgbuf); | |
678 | + Log(wkmsg); | |
679 | + if (dbf == 0) SendBKSVMsg(wkmsg); /* WebSV => BKSV => DBSV */ | |
680 | + ret = 1; /* 期間内に目的のメッセージあり */ | |
681 | + } | |
682 | + | |
683 | + /* バッファ解放 */ | |
684 | + GlobalFree(pBuf); | |
685 | + pBuf = NULL; | |
686 | + } | |
687 | + | |
688 | + if(hEventLog != NULL) CloseEventLog(hEventLog); | |
689 | + return ret; | |
690 | +} | |
691 | + | |
692 | + | |
693 | +/* port from findwin start */ | |
694 | +void GetControlList(HWND hWin, int level) | |
695 | +{ | |
696 | + HWND hCntl = NULL; | |
697 | + char classname[4096]; | |
698 | + char ctrltext[4096]; | |
699 | + char spc[1024]; | |
700 | + int ret; | |
701 | + | |
702 | + ++level; | |
703 | + strcpy(spc, " "); | |
704 | + spc[level-1] = '\0'; | |
705 | +#if 0 | |
706 | + if (level > 1) { | |
707 | + spc[level-1] = '+'; | |
708 | + } | |
709 | +#endif | |
710 | + | |
711 | + while (1) { | |
712 | + hCntl = FindWindowEx(hWin, hCntl, NULL, NULL); | |
713 | + if (!hCntl) { | |
714 | + break; | |
715 | + } | |
716 | + GetClassName(hCntl, classname, sizeof(classname)); | |
717 | + ctrltext[0] = '\0'; | |
718 | + ret = GetWindowText(hCntl, ctrltext, sizeof(ctrltext)); | |
719 | + if (ret == 0) { | |
720 | + ret = GetLastError(); | |
721 | + if (ret != ERROR_SUCCESS) { | |
722 | + sprintf(ctrltext, "GetWindowText Error(%d)", ret); | |
723 | + } | |
724 | + } | |
725 | + printf("%sClass:%-20s Text:[%s]\n", spc, classname, ctrltext); | |
726 | +#if 0 | |
727 | + /* replace other window text */ | |
728 | + if (!strcmp(ctrltext, "0.391")) { | |
729 | + SetWindowText(hCntl, "0.222"); | |
730 | + printf("Set 0.222!!\n"); | |
731 | + /* Send Redraw Message to Message Queue */ | |
732 | + SendMessage(hWin, WM_PAINT, 0, 0); | |
733 | + } | |
734 | +#endif | |
735 | + GetControlList(hCntl, level); | |
736 | + } | |
737 | +} | |
738 | + | |
739 | + | |
740 | +/* | |
741 | + * find window and send message | |
742 | + * | |
743 | + * IN WinTitle: Window Title | |
744 | + * IN evid : send event ID | |
745 | + */ | |
746 | +int SendMsg2Win(char *WinTitle, int evid) | |
747 | +{ | |
748 | + HWND hWin = NULL; | |
749 | + int i; | |
750 | + int af = 0; /* all flag */ | |
751 | + int evf = 0; /* send evt flag */ | |
752 | + | |
753 | + /* send message mode */ | |
754 | + evf = 1; | |
755 | + /* evid = WM_CLOSE; */ | |
756 | + | |
757 | + if (af) { | |
758 | + /* find all window */ | |
759 | + GetControlList(NULL, 0); | |
760 | + } else { | |
761 | + /* find specific window */ | |
762 | + hWin = FindWindow(NULL, WinTitle); | |
763 | + if (hWin) { | |
764 | + char classname[4096]; /* V0.11-A */ | |
765 | + | |
766 | + printf("[%s] Window Handle = 0x%08x\n", WinTitle, hWin); | |
767 | + | |
768 | + GetClassName(hWin, classname, sizeof(classname)); /* V0.11-A */ | |
769 | + printf("Class:%-20s Text:[%s]\n", classname, WinTitle); /* V0.11-A */ | |
770 | + GetControlList(hWin, 1); /* V0.11-C */ | |
771 | + } else { | |
772 | + printf("Not found [%s].\n", WinTitle); | |
773 | + } | |
774 | + if (evf) { | |
775 | + printf("Send Message(%d) to [%s].\n", evid, WinTitle); | |
776 | + SendMessage(hWin, evid, 0, 0); | |
777 | + } | |
778 | + } | |
779 | + return 0; | |
780 | +} | |
781 | +/* port from findwin end */ | |
782 | + | |
783 | +/* | |
784 | + * IN service service name | |
785 | + * IN op operation "start" or "stop" | |
786 | + */ | |
787 | +void CtrlService(char *service_name, char *op) | |
788 | +{ | |
789 | + char netcmd[2048]; | |
790 | + char cmd[2048]; | |
791 | + char comarg[2048]; | |
792 | + DWORD ret = 0; | |
793 | + | |
794 | + Which("cmd", cmd); /* search cmd.exe */ | |
795 | + Which("net", netcmd); /* search net.exe */ | |
796 | + // cmd /C "C:/Windows/system32/net.exe start "IIS Admin Service" /y" | |
797 | + sprintf(comarg, "%s /C \"%s %s \"%s\" /y\"", "cmd", netcmd, op, service_name); | |
798 | + printf("%s\n", comarg); | |
799 | + ret = ExecCmd(cmd, comarg); | |
800 | + if (ret != 0) { | |
801 | + printf("%s failed. ret=%d\n", netcmd, ret); | |
802 | + } | |
803 | + | |
804 | +} | |
805 | + | |
806 | +void usage() | |
807 | +{ | |
808 | + printf("readevt Ver %s\n", VER); | |
809 | + printf("usage: readevt [-csv | -d] [{-db|-fw}] [-a] [-s <span(sec)>] [func_key]\n"); | |
810 | + printf(" [-c <num_of_file>]\n"); | |
811 | + printf(" -d: daemon\n"); | |
812 | + printf(" -a: check all event\n"); | |
813 | + printf(" -c: num of netstatXXX.txt default: %d\n", max_fcnt); | |
814 | + printf(" -s: check span(sec) default: %d\n", check_span); | |
815 | + printf(" -w: loop wait time (msec) default: %d\n", wait_val); | |
816 | + printf("ex) K-DB (001): readevt -d kai -db\n"); | |
817 | + printf(" K-Web(002): readevt -d kai\n"); | |
818 | + printf(" Y-DB (003): readevt -d yo -db\n"); | |
819 | + printf(" Y-Web(004): readevt -d yo\n"); | |
820 | + printf(" F/W : readevt -d -fw\n"); | |
821 | + exit(1); | |
822 | +} | |
823 | + | |
824 | +int main(int a, char *b[]) | |
825 | +{ | |
826 | + int i; | |
827 | + char *target_win = TARGET_WIN; | |
828 | + int cnt = 0; | |
829 | + int fcnt = 0; /* netstatXXX.txt counter */ | |
830 | + DWORD ret = 0; | |
831 | + char cmd[2048]; | |
832 | + char netstatcmd[2048]; | |
833 | + char comarg[2048]; | |
834 | + | |
835 | + //strcpy(check_msg, "EA000004"); /* Yosan Network Error */ | |
836 | + strcpy(check_msg, "DBNMPNTW"); | |
837 | + strcpy(check_msg2, "DBMSSOCN"); | |
838 | + | |
839 | + for (i = 1; i<a; i++) { | |
840 | + if (!strcmp(b[i], "-h")) { | |
841 | + usage(); | |
842 | + } | |
843 | + if (!strcmp(b[i], "-csv")) { | |
844 | + csvf = 1; | |
845 | + continue; | |
846 | + } | |
847 | + if (!strcmp(b[i], "-v")) { | |
848 | + ++vf; | |
849 | + continue; | |
850 | + } | |
851 | + if (!strcmp(b[i], "-d")) { | |
852 | + daemonf = 1; /* ループしてチェックするモード */ | |
853 | + continue; | |
854 | + } | |
855 | + if (!strcmp(b[i], "-db")) { | |
856 | + /* 私はDBサーバ */ | |
857 | + dbf = 1; /* bkserver経由で停止通知を受ける */ | |
858 | + continue; | |
859 | + } | |
860 | + if (!strcmp(b[i], "-fw")) { | |
861 | + /* 私はF/Wマシン */ | |
862 | + fwf = 1; /* F/Wへの停止通知を受ける */ | |
863 | + continue; | |
864 | + } | |
865 | + if (!strcmp(b[i], "-dbg")) { | |
866 | + dbgf = 1; /* デバッグ用(for bally) */ | |
867 | + target_win = TARGET_WIN_DBG; | |
868 | + wait_val = 3*1000; /* (3sec) */ | |
869 | + strcpy(check_msg, "Active Directory"); /* Unicodeで指定要かも? */ | |
870 | + bksv_dir = "\\\\galaga\\oga"; | |
871 | + fw_dir = "\\\\galaga\\oga"; | |
872 | + check_span = 8*3600; /* 8h */ | |
873 | + service_iis = SERVICE_WWW; | |
874 | + continue; | |
875 | + } | |
876 | + if (!strcmp(b[i], "-dbg2")) { | |
877 | + dbgf = 2; /* デバッグ用(for honban) */ | |
878 | + target_win = TARGET_WIN_DBG; | |
879 | + wait_val = 5*1000; /* (5sec) */ | |
880 | + strcpy(check_msg, "サービスを開始"); /* Unicodeで指定要かも? */ | |
881 | + //bksv_dir = "\\\\galaga\\oga"; | |
882 | + //fw_dir = "\\\\galaga\\oga"; | |
883 | + check_span = 120; /* 120sec */ | |
884 | + continue; | |
885 | + } | |
886 | + if (!strcmp(b[i], "-a")) { | |
887 | + check_span = 0; /* 全イベント表示 (default: latest 4h) */ | |
888 | + continue; | |
889 | + } | |
890 | + if (!strcmp(b[i], "-s") && a>i+1) { | |
891 | + check_span = atoi(b[++i]); /* sec */ | |
892 | + continue; | |
893 | + } | |
894 | + if (!strcmp(b[i], "-w") && a>i+1) { | |
895 | + wait_val = atoi(b[++i]); /* msec */ | |
896 | + continue; | |
897 | + } | |
898 | + grp_code = b[i]; | |
899 | + } | |
900 | + | |
901 | +#ifdef _WIN32 | |
902 | + // search netstat.exe command path | |
903 | + Which("netstat", netstatcmd); | |
904 | + Which("cmd", cmd); | |
905 | + //strcpy(cmd, "cmd"); | |
906 | +#endif | |
907 | + | |
908 | + /* print options */ | |
909 | + printf("Check Evt Msg1: [%s]\n", check_msg); | |
910 | + printf("Check Evt Msg2: [%s]\n", check_msg2); | |
911 | + printf("Check Evt Span: latest %d sec\n", check_span); | |
912 | + if (fwf) { | |
913 | + printf("Target Win FW1: [%s]\n", TARGET_WIN_FW1); | |
914 | + printf("Target Win FW2: [%s]\n", TARGET_WIN_FW2); | |
915 | + } else { | |
916 | + printf("Target Win : [%s]\n", target_win); | |
917 | + } | |
918 | + printf("Sleep Time : %d msec\n", wait_val); | |
919 | + printf("Send Bksv Dir : %s\n", bksv_dir); | |
920 | + printf("Send FW Dir : %s\n", fw_dir); | |
921 | + printf("Group name : %s\n", grp_code); | |
922 | + printf("netstat : %s\n", netstatcmd); | |
923 | + printf("IIS : %s\n", service_iis); | |
924 | + | |
925 | + //if (dbf == 0) SendBKSVMsg(""); /* clear */ | |
926 | + SendBKSVMsg(""); /* clear */ | |
927 | + | |
928 | + while (1) { | |
929 | +#ifdef _WIN32 | |
930 | + // V0.12-A start | |
931 | + // Exec netstat -an | |
932 | + //sprintf(comarg, "%s -an", netstatcmd); | |
933 | + // Exec cmd /C netstat -an | |
934 | + sprintf(comarg, "%s /C \"%s -an > netstat%03d.txt\"", "cmd", netstatcmd, fcnt); | |
935 | + //printf("%s: [%s]\n", cmd, comarg); | |
936 | + ++fcnt; | |
937 | + if (fcnt >= max_fcnt) { | |
938 | + fcnt = 0; | |
939 | + } | |
940 | + | |
941 | + //ret = ExecCmd(netstatcmd, comarg); | |
942 | + ret = ExecCmd(cmd, comarg); | |
943 | + if (ret != 0) { | |
944 | + printf("%s failed. ret=%d\n", netstatcmd, ret); | |
945 | + } | |
946 | + // V0.12-A end | |
947 | +#endif | |
948 | + | |
949 | + /* Webで発生 DBに通知あり FWに通知あり */ | |
950 | + if (ReadLog() == 1 || (dbf && FileSVChk() == 1) || (fwf && FWChk() == 1)) { | |
951 | + // 2個closeする | |
952 | + if (fwf) { | |
953 | + KillProc(TARGET_PROC_WINDUMP); /* WinDumpを止める */ | |
954 | + printf("close1 %s", TARGET_WIN_FW1); | |
955 | + SendMsg2Win(TARGET_WIN_FW1, WM_CLOSE); | |
956 | + Sleep(2*60*1000); /* 2min wait */ | |
957 | + printf("close2 %s", TARGET_WIN_FW1); | |
958 | + SendMsg2Win(TARGET_WIN_FW2, WM_CLOSE); | |
959 | + } else { | |
960 | + KillProc(TARGET_PROC_WINDUMP); /* WinDumpを止める */ | |
961 | + if (!dbf) { | |
962 | + // Stop Web Server | |
963 | + CtrlService(service_iis, "stop"); | |
964 | + } | |
965 | + printf("close1 %s", target_win); | |
966 | + SendMsg2Win(target_win, WM_CLOSE); | |
967 | + Sleep(2*60*1000); /* 2min wait */ | |
968 | + printf("close2 %s", target_win); | |
969 | + SendMsg2Win(target_win, WM_CLOSE); | |
970 | + } | |
971 | + break; | |
972 | + } | |
973 | + if (daemonf == 0) { | |
974 | + // ループモードでない場合は1回実行して終わり | |
975 | + break; | |
976 | + } | |
977 | + | |
978 | + Sleep(wait_val); /* wait 3min */ | |
979 | + //Sleep(3*1000); /* wait 3sec */ | |
980 | + printf("%d -------------------------------------------\n", ++cnt); | |
981 | + } | |
982 | + return 0; | |
983 | + | |
984 | +} | |
985 | + | |
986 | + |
@@ -0,0 +1,276 @@ | ||
1 | +/* | |
2 | + * ReadLogicalSectors | |
3 | + * http://www.codeguru.com/forum/archive/index.php/t-296963.html | |
4 | + */ | |
5 | +#include <windows.h> | |
6 | +#include <stdio.h> | |
7 | +#include <stdlib.h> | |
8 | +#include <string.h> | |
9 | + | |
10 | +#define VWIN32_DIOC_DOS_INT25 2 | |
11 | +#define VWIN32_DIOC_DOS_INT26 3 | |
12 | +#define VWIN32_DIOC_DOS_DRIVEINFO 6 | |
13 | + | |
14 | +typedef struct _DIOC_REGISTERS { | |
15 | + DWORD reg_EBX; | |
16 | + DWORD reg_EDX; | |
17 | + DWORD reg_ECX; | |
18 | + DWORD reg_EAX; | |
19 | + DWORD reg_EDI; | |
20 | + DWORD reg_ESI; | |
21 | + DWORD reg_Flags; | |
22 | +} DIOC_REGISTERS, *PDIOC_REGISTERS; | |
23 | + | |
24 | +#define CARRY_FLAG 1 | |
25 | + | |
26 | +// OGA change | |
27 | +//#pragma pack(1) | |
28 | +typedef struct _DISKIO { | |
29 | + DWORD dwStartSector; // starting logical sector number | |
30 | + WORD wSectors; // number of sectors | |
31 | + DWORD dwBuffer; // address of read/write buffer | |
32 | +} DISKIO, * PDISKIO; | |
33 | +//#pragma pack() | |
34 | + | |
35 | +/*------------------------------------------------------------------ | |
36 | + ReadLogicalSectors (hDev, bDrive, dwStartSector, wSectors, lpSectBuff) | |
37 | + | |
38 | + Purpose: | |
39 | + Reads sectors from a logical drive. Uses Int 25h. | |
40 | + | |
41 | + Parameters: | |
42 | + hDev | |
43 | + Handle of VWIN32 | |
44 | + | |
45 | + bDrive | |
46 | + The MS-DOS logical drive number. 1 = A, 2 = B, 3 = C, etc. | |
47 | + | |
48 | + dwStartSector | |
49 | + The first logical sector to read | |
50 | + | |
51 | + wSectors | |
52 | + The number of sectors to read | |
53 | + | |
54 | + lpSectBuff | |
55 | + The caller-supplied buffer that will contain the sector data | |
56 | + | |
57 | + Return Value: | |
58 | + Returns TRUE if successful, or FALSE if failure. | |
59 | + | |
60 | + Comments: | |
61 | + This function does not validate its parameters. | |
62 | +------------------------------------------------------------------*/ | |
63 | +BOOL ReadLogicalSectors (HANDLE hDev, BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff) | |
64 | +{ | |
65 | + BOOL fResult; | |
66 | + DWORD cb; | |
67 | + DIOC_REGISTERS reg = {0}; | |
68 | + DISKIO dio = {0}; | |
69 | + | |
70 | + dio.dwStartSector = dwStartSector; | |
71 | + dio.wSectors = wSectors; | |
72 | + dio.dwBuffer = (DWORD)lpSectBuff; | |
73 | + | |
74 | + reg.reg_EAX = bDrive - 1; // Int 25h drive numbers are 0-based. | |
75 | + reg.reg_EBX = (DWORD)&dio; | |
76 | + reg.reg_ECX = 0xFFFF; // use DISKIO struct | |
77 | + | |
78 | + fResult = DeviceIoControl(hDev, VWIN32_DIOC_DOS_INT25, | |
79 | + ®, sizeof(reg), | |
80 | + ®, sizeof(reg), &cb, 0); | |
81 | + | |
82 | + // Determine if the DeviceIoControl call and the read succeeded. | |
83 | + fResult = fResult && !(reg.reg_Flags & CARRY_FLAG); | |
84 | + | |
85 | + return fResult; | |
86 | +} | |
87 | + | |
88 | + | |
89 | +/*------------------------------------------------------------------ | |
90 | + WriteLogicalSectors (hDev, bDrive, dwStartSector, wSectors, lpSectBuff) | |
91 | + | |
92 | + Purpose: | |
93 | + Writes sectors to a logical drive. Uses Int 26h | |
94 | + | |
95 | + Parameters: | |
96 | + hDev | |
97 | + Handle of VWIN32 | |
98 | + | |
99 | + bDrive | |
100 | + The MS-DOS logical drive number. 1 = A, 2 = B, 3 = C, etc. | |
101 | + | |
102 | + dwStartSector | |
103 | + The first logical sector to write | |
104 | + | |
105 | + wSectors | |
106 | + The number of sectors to write | |
107 | + | |
108 | + lpSectBuff | |
109 | + The caller-supplied buffer that contains the sector data | |
110 | + | |
111 | + Return Value: | |
112 | + Returns TRUE if successful, or FALSE if failure. | |
113 | + | |
114 | + Comments: | |
115 | + This function does not validate its parameters. | |
116 | +------------------------------------------------------------------*/ | |
117 | +BOOL WriteLogicalSectors (HANDLE hDev, BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff) | |
118 | +{ | |
119 | + BOOL fResult; | |
120 | + DWORD cb; | |
121 | + DIOC_REGISTERS reg = {0}; | |
122 | + DISKIO dio = {0}; | |
123 | + | |
124 | + dio.dwStartSector = dwStartSector; | |
125 | + dio.wSectors = wSectors; | |
126 | + dio.dwBuffer = (DWORD)lpSectBuff; | |
127 | + | |
128 | + reg.reg_EAX = bDrive - 1; // Int 26h drive numbers are 0-based. | |
129 | + reg.reg_EBX = (DWORD)&dio; | |
130 | + reg.reg_ECX = 0xFFFF; // use DISKIO struct | |
131 | + | |
132 | + fResult = DeviceIoControl(hDev, VWIN32_DIOC_DOS_INT26, | |
133 | + ®, sizeof(reg), | |
134 | + ®, sizeof(reg), &cb, 0); | |
135 | + | |
136 | + // Determine if the DeviceIoControl call and the write succeeded. | |
137 | + fResult = fResult && !(reg.reg_Flags & CARRY_FLAG); | |
138 | + | |
139 | + return fResult; | |
140 | +} | |
141 | + | |
142 | +#if 0 | |
143 | +/*------------------------------------------------------------------ | |
144 | + NewReadSectors(hDev, bDrive, dwStartSector, wSectors, lpSectBuff) | |
145 | + | |
146 | + Purpose: | |
147 | + Reads the specified number of sectors into a caller-supplied | |
148 | + buffer. Uses Int 21h function 7305h | |
149 | + | |
150 | + Parameters: | |
151 | + hDev | |
152 | + Handle of VWIN32 | |
153 | + | |
154 | + bDrive | |
155 | + The MS-DOS logical drive number. 0 = default, 1 = A, 2 = B, 3 = C, etc. | |
156 | + | |
157 | + dwStartSector | |
158 | + The first sector to read. | |
159 | + | |
160 | + wSectors | |
161 | + The number of sectors to read. | |
162 | + | |
163 | + lpSectBuff | |
164 | + The caller-supplied buffer to read into. | |
165 | + | |
166 | + Return Value: | |
167 | + Returns TRUE if successful, or FALSE if failure. | |
168 | + | |
169 | + Comments: | |
170 | + This function does not validate its parameters. It assumes that | |
171 | + lpSectBuff is allocated by the caller and is large enough to | |
172 | + hold all of the data from all of the sectors being read. | |
173 | +------------------------------------------------------------------*/ | |
174 | +BOOL NewReadSectors (HANDLE hDev, BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff) | |
175 | +{ | |
176 | + BOOL fResult; | |
177 | + DWORD cb; | |
178 | + DIOC_REGISTERS reg = {0}; | |
179 | + DISKIO dio; | |
180 | + | |
181 | + dio.dwStartSector = dwStartSector; | |
182 | + dio.wSectors = wSectors; | |
183 | + dio.lpBuffer = (DWORD)lpSectBuff; | |
184 | + | |
185 | + reg.reg_EAX = 0x7305; // Ext_ABSDiskReadWrite | |
186 | + reg.reg_EBX = (DWORD)&dio; | |
187 | + reg.reg_ECX = -1; | |
188 | + reg.reg_EDX = bDrive; // Int 21h, fn 7305h drive numbers are 1-based | |
189 | + | |
190 | + fResult = DeviceIoControl(hDev, VWIN32_DIOC_DOS_DRIVEINFO, | |
191 | + ®, sizeof(reg), | |
192 | + ®, sizeof(reg), &cb, 0); | |
193 | + | |
194 | + // Determine if the DeviceIoControl call and the read succeeded. | |
195 | + fResult = fResult && !(reg.reg_Flags & CARRY_FLAG); | |
196 | + | |
197 | + return fResult; | |
198 | +} | |
199 | + | |
200 | + | |
201 | +/*------------------------------------------------------------------ | |
202 | + NewWriteSectors(hDev, bDrive, dwStartSector, wSectors, lpSectBuff) | |
203 | + | |
204 | + Purpose: | |
205 | + Writes the specified number of sectors from a caller-supplied | |
206 | + buffer. Uses Int 21h function 7305h | |
207 | + | |
208 | + Parameters: | |
209 | + hDev | |
210 | + Handle of VWIN32 | |
211 | + | |
212 | + bDrive | |
213 | + The MS-DOS logical drive number. 0 = default, 1 = A, 2 = B, 3 = C, etc. | |
214 | + | |
215 | + dwStartSector | |
216 | + The first sector to write. | |
217 | + | |
218 | + wSectors | |
219 | + The number of sectors to write. | |
220 | + | |
221 | + lpSectBuff | |
222 | + The caller-supplied buffer from which to write. | |
223 | + | |
224 | + Return Value: | |
225 | + Returns TRUE if successful, or FALSE if failure. | |
226 | + | |
227 | + Comments: | |
228 | + This function does not validate its parameters. It assumes that | |
229 | + lpSectBuff is allocated by the caller and is large enough to | |
230 | + hold all of the data to be written. | |
231 | +------------------------------------------------------------------*/ | |
232 | +BOOL NewWriteSectors (HANDLE hDev, BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff) | |
233 | +{ | |
234 | + BOOL fResult; | |
235 | + DWORD cb; | |
236 | + DIOC_REGISTERS reg = {0}; | |
237 | + DISKIO dio; | |
238 | + | |
239 | + dio.dwStartSector = dwStartSector; | |
240 | + dio.wSectors = wSectors; | |
241 | + dio.lpBuffer = (DWORD)lpSectBuff; | |
242 | + | |
243 | + reg.reg_EAX = 0x7305; // Ext_ABSDiskReadWrite | |
244 | + reg.reg_EBX = (DWORD)&dio; | |
245 | + reg.reg_ECX = -1; | |
246 | + reg.reg_EDX = bDrive; // Int 21h, fn 7305h drive numbers are 1-based | |
247 | + | |
248 | + reg.reg_ESI = 0x6001; // Normal file data (See function | |
249 | + // documentation for other values) | |
250 | + | |
251 | + | |
252 | + fResult = DeviceIoControl(hDev, VWIN32_DIOC_DOS_DRIVEINFO, | |
253 | + ®, sizeof(reg), | |
254 | + ®, sizeof(reg), &cb, 0); | |
255 | + | |
256 | + // Determine if the DeviceIoControl call and the write succeeded. | |
257 | + fResult = fResult && !(reg.reg_Flags & CARRY_FLAG); | |
258 | + | |
259 | + return fResult; | |
260 | +} | |
261 | +#endif | |
262 | + | |
263 | +int main(int a, char *b[]) | |
264 | +{ | |
265 | + /* Open vwin32 */ | |
266 | + HANDLE hvxd = CreateFile("\\\\.\\VWIN32", 0, 0, NULL, 0, | |
267 | + FILE_FLAG_DELETE_ON_CLOSE, NULL); | |
268 | + if (hvxd == INVALID_HANDLE_VALUE) { | |
269 | + printf("CreateFile Error: error=%d\n", GetLastError()); | |
270 | + exit(1); | |
271 | + } | |
272 | + | |
273 | + CloseHandle(hvxd); | |
274 | + | |
275 | + return 0; | |
276 | +} |
@@ -0,0 +1,212 @@ | ||
1 | +/* | |
2 | + * regsv : Service Add/Delete Tool | |
3 | + * | |
4 | + * 2001/03/06 V1.00 by oga. | |
5 | + * 2003/07/16 V1.01 support -checksv | |
6 | + */ | |
7 | +#include <windows.h> | |
8 | +#include <stdio.h> | |
9 | + | |
10 | +int AddService(char *svname, char *exepath) | |
11 | +{ | |
12 | + SC_HANDLE hSCManager; | |
13 | + SC_HANDLE hService; | |
14 | + | |
15 | + printf("### Add service %s (%s)\n", svname, exepath); | |
16 | + | |
17 | + hSCManager = OpenSCManager( | |
18 | + NULL, // machine (NULL == local) | |
19 | + NULL, // database (NULL == default) | |
20 | + SC_MANAGER_ALL_ACCESS // access required | |
21 | + ) ; | |
22 | + | |
23 | + if (hSCManager == NULL) { | |
24 | + printf("OpenSCM Error (%s) (%d)\n",svname, GetLastError()); | |
25 | + return 1; | |
26 | + } | |
27 | + | |
28 | + /* XXX */ | |
29 | + hService = CreateService( | |
30 | + hSCManager, // SCManager database | |
31 | + svname, // name of service | |
32 | + svname, // name to display | |
33 | + SERVICE_ALL_ACCESS, // desired access | |
34 | + SERVICE_WIN32_SHARE_PROCESS,// service type | |
35 | + //SERVICE_AUTO_START, // start type (AUTO) | |
36 | + SERVICE_DEMAND_START, // start type (DEMAND) | |
37 | + SERVICE_ERROR_NORMAL, // error control type | |
38 | + exepath, // service's binary | |
39 | + NULL, // no load ordering group | |
40 | + NULL, // no tag identifier | |
41 | + NULL, // dependencies | |
42 | + NULL, // LocalSystem account | |
43 | + NULL) ; // no password | |
44 | + /* XXX */ | |
45 | + | |
46 | + if (hService == NULL) { | |
47 | + printf("CreateService Error (%s) (%d)\n",svname, GetLastError()); | |
48 | + CloseServiceHandle(hSCManager); | |
49 | + return 1; | |
50 | + } else { | |
51 | + printf("CreateServcie success(%s)\n",svname); | |
52 | + CloseServiceHandle(hService); | |
53 | + } | |
54 | + CloseServiceHandle(hSCManager); | |
55 | + | |
56 | + return 0; | |
57 | +} | |
58 | + | |
59 | +int DelService(char *svname) | |
60 | +{ | |
61 | + SC_HANDLE hSCManager; | |
62 | + SC_HANDLE hService; | |
63 | + | |
64 | + printf("### Delete service %s\n",svname); | |
65 | + | |
66 | + hSCManager = OpenSCManager( | |
67 | + NULL, // machine (NULL == local) | |
68 | + NULL, // database (NULL == default) | |
69 | + SC_MANAGER_ALL_ACCESS // access required | |
70 | + ) ; | |
71 | + | |
72 | + if (hSCManager == NULL) { | |
73 | + printf("OpenSCM Error (%s) (%d)\n",svname, GetLastError()); | |
74 | + return 1; | |
75 | + } | |
76 | + | |
77 | + hService = OpenService( hSCManager, svname, SERVICE_ALL_ACCESS) ; | |
78 | + | |
79 | + if (hService == NULL) { | |
80 | + printf("OpenService Error (%s) (%d)\n",svname, GetLastError()); | |
81 | + return 1; | |
82 | + } | |
83 | + | |
84 | + if(DeleteService(hService) == TRUE) { | |
85 | + printf("DeleteServcie success(%s)\n",svname); | |
86 | + } else { | |
87 | + printf("DeleteService Error %d\n",GetLastError()); | |
88 | + } | |
89 | + | |
90 | + CloseServiceHandle(hService); | |
91 | + CloseServiceHandle(hSCManager); | |
92 | + return 0; | |
93 | +} | |
94 | + | |
95 | +/* サービスの状態をチェックする V1.01-A */ | |
96 | +int CheckService(char *svname) | |
97 | +{ | |
98 | + SC_HANDLE hSCManager; | |
99 | + SC_HANDLE hService; | |
100 | + SERVICE_STATUS svstat; | |
101 | + int dispf = 0; | |
102 | + int i; | |
103 | + | |
104 | + DWORD vals[] = { SERVICE_CONTINUE_PENDING, | |
105 | + SERVICE_PAUSE_PENDING, | |
106 | + SERVICE_PAUSED, | |
107 | + SERVICE_RUNNING, | |
108 | + SERVICE_START_PENDING, | |
109 | + SERVICE_STOP_PENDING, | |
110 | + SERVICE_STOPPED }; | |
111 | + | |
112 | + char *stat_name[] = { "SERVICE_CONTINUE_PENDING", | |
113 | + "SERVICE_PAUSE_PENDING", | |
114 | + "SERVICE_PAUSED", | |
115 | + "SERVICE_RUNNING", | |
116 | + "SERVICE_START_PENDING", | |
117 | + "SERVICE_STOP_PENDING", | |
118 | + "SERVICE_STOPPED" }; | |
119 | + | |
120 | + | |
121 | + printf("### Check service [%s] status\n",svname); | |
122 | + | |
123 | + hSCManager = OpenSCManager( | |
124 | + NULL, // machine (NULL == local) | |
125 | + NULL, // database (NULL == default) | |
126 | + GENERIC_READ // access required | |
127 | + ) ; | |
128 | + | |
129 | + if (hSCManager == NULL) { | |
130 | + printf("OpenSCM Error (%s) (%d)\n",svname, GetLastError()); | |
131 | + return 1; | |
132 | + } | |
133 | + | |
134 | + hService = OpenService( hSCManager, svname, SERVICE_QUERY_STATUS) ; | |
135 | + | |
136 | + if (hService == NULL) { | |
137 | + printf("OpenService Error (%s) (%d)\n",svname, GetLastError()); | |
138 | + return 1; | |
139 | + } | |
140 | + | |
141 | + if(QueryServiceStatus(hService, &svstat) == TRUE) { | |
142 | + /* printf("QueryServiceStatus success(%s)\n",svname); */ | |
143 | + } else { | |
144 | + printf("QueryServiceStatus Error %d\n",GetLastError()); | |
145 | + } | |
146 | + | |
147 | + for (i = 0; i < sizeof(vals)/sizeof(DWORD); i++) { | |
148 | + if (svstat.dwCurrentState == vals[i]) { | |
149 | + printf("[%s] status : %s\n", svname, stat_name[i]); | |
150 | + dispf = 1; | |
151 | + break; | |
152 | + } | |
153 | + } | |
154 | + | |
155 | + if (!dispf) { | |
156 | + printf("unknown service status %d\n", svstat.dwCurrentState); | |
157 | + } | |
158 | + | |
159 | + CloseServiceHandle(hService); | |
160 | + CloseServiceHandle(hSCManager); | |
161 | + return 0; | |
162 | +} | |
163 | + | |
164 | +void usage() | |
165 | +{ | |
166 | + printf("usage: regsv -check <svname>\n"); | |
167 | + printf("usage: regsv -add <svname> <exepath>\n"); | |
168 | + printf("usage: regsv -del <svname>\n"); | |
169 | + printf("usage: regsv -deladd <svname> <exepath>\n"); | |
170 | +} | |
171 | + | |
172 | +int main(int a, char *b[]) | |
173 | +{ | |
174 | + char svname[256]; | |
175 | + char *sid = "dummySID"; | |
176 | + char *sup = "dummySUP"; | |
177 | + char *typ = "dummyTYPE"; | |
178 | + int mpn; | |
179 | + | |
180 | + if (a == 1 || !strcmp(b[1], "-h")) { | |
181 | + usage(); | |
182 | + return 1; | |
183 | + } | |
184 | + if (!strcmp(b[1], "-add")) { | |
185 | + // -add (Add Spec Service) | |
186 | + if (a < 3) { | |
187 | + usage(); | |
188 | + return 1; | |
189 | + } | |
190 | + AddService(b[2], b[3]); | |
191 | + } else if (!strcmp(b[1], "-deladd")) { | |
192 | + // -deladd (Delete & re Add Spec Service) | |
193 | + if (a < 3) { | |
194 | + usage(); | |
195 | + return 1; | |
196 | + } | |
197 | + DelService(b[2]); | |
198 | + AddService(b[2], b[3]); | |
199 | + } else if (!strcmp(b[1], "-del")) { | |
200 | + // Delete Spec Service | |
201 | + DelService(b[2]); | |
202 | + } else if (!strcmp(b[1], "-check")) { | |
203 | + // Check Spec Service | |
204 | + CheckService(b[2]); | |
205 | + } else { | |
206 | + printf("Invalid Option (%s)\n", b[1]); | |
207 | + } | |
208 | + | |
209 | + return 0; | |
210 | +} | |
211 | + | |
212 | + |
@@ -0,0 +1,478 @@ | ||
1 | +/* | |
2 | + * rndplay : ランダムプレイ | |
3 | + * | |
4 | + * 2006/04/25 V0.10 by oga. | |
5 | + * 2006/05/14 V0.20 support -t option | |
6 | + */ | |
7 | +#ifdef _WIN32 | |
8 | +#include <windows.h> | |
9 | +#endif /* _WIN32 */ | |
10 | +#include <stdio.h> | |
11 | +#include <stdlib.h> | |
12 | +#include <string.h> | |
13 | +#include <sys/stat.h> | |
14 | + | |
15 | +/* macros */ | |
16 | +#define VER "0.20" | |
17 | +#define IS_DOT(str) (!strcmp(str,".") || !strcmp(str,"..")) | |
18 | +#define RAND(x) ((rand() & 0xffff)*(x)/(RAND_MAX & 0xffff)) | |
19 | + | |
20 | +#define dprintf if (vf) printf | |
21 | +#define dprintf2 if (vf >= 2) printf | |
22 | + | |
23 | +/* defines */ | |
24 | +#define F_TYPE_DIR 0x01 | |
25 | +#define F_TYPE_FILE 0x02 | |
26 | + | |
27 | +#define SUFF_MP3 ".mp3" | |
28 | +#define PLAYER_MP3 "mpg123" /* mp3 player without .exe */ | |
29 | +#define PLAYER_MS "wmplayer" /* WinMediaPlayer without .exe */ | |
30 | + | |
31 | + | |
32 | +/* globals */ | |
33 | +//char *find_name = NULL; /* -name */ | |
34 | +//int ftype = 0; /* -ftype */ | |
35 | +int vf = 0; /* -v */ | |
36 | +int tf = 0; /* -t */ | |
37 | +int depth = 0; | |
38 | +int cur_list_size = 0; /* path list size */ | |
39 | +char **pathlist = NULL; /* path list */ | |
40 | +int num_ent = 0; /* num of all songs */ | |
41 | +int gnum = 10; /* 演奏する曲数 */ | |
42 | + | |
43 | +void Usage() | |
44 | +{ | |
45 | + printf("rndplay Verson %s by oga.\n",VER); | |
46 | + printf("usage: rndplay [<path(.)>] [-s <suffix(mp3)>] [-n <num(10)>] [-t]\n"); | |
47 | + printf(" -t : print ID3 tag\n"); | |
48 | +} | |
49 | + | |
50 | +/* | |
51 | + * AddPathList() : パスリストにパスを追加する | |
52 | + * | |
53 | + * IN path : 追加するパス名 | |
54 | + * | |
55 | + * OUT num_ent : リスト数 | |
56 | + * | |
57 | + */ | |
58 | +void InitPathList() | |
59 | +{ | |
60 | + cur_list_size = 1000; | |
61 | + pathlist = (char **)malloc(sizeof(char *) * cur_list_size); | |
62 | + memset(pathlist, 0, sizeof(char *) * cur_list_size); | |
63 | +} | |
64 | + | |
65 | +/* | |
66 | + * AddPathList() : パスリストにパスを追加する | |
67 | + * | |
68 | + * IN path : 追加するパス名 | |
69 | + * | |
70 | + * OUT num_ent : リスト数 | |
71 | + * | |
72 | + */ | |
73 | +void AddPathList(char *path) | |
74 | +{ | |
75 | + char **newlist; | |
76 | + | |
77 | + dprintf("add list %s\n", path); | |
78 | + pathlist[num_ent++] = strdup(path); | |
79 | + if (num_ent >= cur_list_size) { | |
80 | + /* pathlistの拡張 */ | |
81 | + cur_list_size += 1000; | |
82 | + newlist = (char **)malloc(sizeof(char *) * (cur_list_size)); | |
83 | + memcpy(newlist, pathlist, sizeof(char *) * (cur_list_size-1000)); | |
84 | + free(pathlist); | |
85 | + pathlist = newlist; | |
86 | + } | |
87 | +} | |
88 | + | |
89 | +/* | |
90 | + * Find() : 指定パス以下のファイルを検索する | |
91 | + * | |
92 | + * IN path : 検索ルートパス名 | |
93 | + * suff : 検索するファイルの拡張子 | |
94 | + * | |
95 | + * OUT sz : ファイルサイズ合計 | |
96 | + * szb : ブロックベースのファイルサイズ合計 | |
97 | + * | |
98 | + */ | |
99 | +void Find(char *path, char *suff) | |
100 | +{ | |
101 | + HANDLE fh; | |
102 | + WIN32_FIND_DATA wfd; | |
103 | + unsigned int subblk; | |
104 | + char wk[MAX_PATH]; | |
105 | + char wkmp3[MAX_PATH]; | |
106 | + int tsv; | |
107 | + int tsvb; | |
108 | + int i; | |
109 | + int ast = 0; /* 1:アスタリスク付き */ | |
110 | + int len; | |
111 | + int name_match = 0; | |
112 | + char *pt; | |
113 | + | |
114 | + depth++; | |
115 | + | |
116 | + strcpy(wk,path); /* copy path */ | |
117 | + if (strlen(path) && path[strlen(wk)-1] != '/') { | |
118 | + if (path[strlen(path)-1] == '*') { | |
119 | + ast = 1; | |
120 | + for (i = strlen(path)-1; i >= 0; i--) { | |
121 | + if (path[i] == '/' || path[i] == '\\') { | |
122 | + if (i) path[i] = '\0'; | |
123 | + break; | |
124 | + } | |
125 | + path[i] = '\0'; | |
126 | + } | |
127 | + dprintf2("xxx path=%s\n",path); | |
128 | + /* wk = "/dir*" */ | |
129 | + /* path = "" */ | |
130 | + } else { | |
131 | + strcat(wk,"/*"); /* add /* */ | |
132 | + /* wk = "dir/*" */ | |
133 | + } | |
134 | + } else { | |
135 | + strcat(wk,"*"); /* add * */ | |
136 | + /* wk = "*" */ | |
137 | + } | |
138 | + | |
139 | + dprintf2("Find: Enter path=[%s] search=[%s]\n",path,wk); | |
140 | + | |
141 | + fh = FindFirstFile(wk,&wfd); | |
142 | + if (fh == INVALID_HANDLE_VALUE) { | |
143 | + printf("Find: FindFirstFile(%s) Error code=%d\n",wk,GetLastError()); | |
144 | + /* exit(1); */ | |
145 | + depth--; /* V1.11-A */ | |
146 | + return; /* V1.11-C */ | |
147 | + } | |
148 | + if (!IS_DOT(wfd.cFileName)) { | |
149 | + name_match = 0; | |
150 | + len = strlen(wfd.cFileName); | |
151 | + if (len > 3 && !stricmp(&wfd.cFileName[len-strlen(suff)], suff)) { | |
152 | + /* mp3ファイルならば有効 */ | |
153 | + name_match = 1; | |
154 | + } | |
155 | + | |
156 | + if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | |
157 | + dprintf2("[%s] is DIR\n",wfd.cFileName); | |
158 | + strcpy(wk,path); /* restore base path */ | |
159 | + if (strlen(wk) && wk[strlen(wk)-1] != '/') strcat(wk,"/"); | |
160 | + strcat(wk,wfd.cFileName); /* make new path */ | |
161 | + Find(wk, suff); | |
162 | + } else { | |
163 | + /* mp3ファイルの場合リスト追加 */ | |
164 | + if (name_match) { | |
165 | + sprintf(wkmp3, "%s/%s",path, wfd.cFileName); | |
166 | + AddPathList(wkmp3); | |
167 | + } | |
168 | + } | |
169 | + } | |
170 | + while(FindNextFile(fh,&wfd)) { | |
171 | + if (IS_DOT(wfd.cFileName)) { | |
172 | + continue; | |
173 | + } | |
174 | + | |
175 | + name_match = 0; | |
176 | + len = strlen(wfd.cFileName); | |
177 | + if (len > 3 && !stricmp(&wfd.cFileName[len-strlen(suff)], suff)) { | |
178 | + /* -name指定があって、名前が一致しない場合は表示しない */ | |
179 | + name_match = 1; | |
180 | + } | |
181 | + | |
182 | + if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | |
183 | + dprintf2("[%s] is DIR\n",wfd.cFileName); | |
184 | + strcpy(wk,path); /* restore base path */ | |
185 | + if (strlen(wk) && wk[strlen(wk)-1] != '/') strcat(wk,"/"); | |
186 | + strcat(wk,wfd.cFileName); /* make new path */ | |
187 | + Find(wk, suff); | |
188 | + } else { | |
189 | + /* mp3ファイルの場合リスト追加 */ | |
190 | + if (name_match) { | |
191 | + sprintf(wkmp3, "%s/%s",path, wfd.cFileName); | |
192 | + AddPathList(wkmp3); | |
193 | + } | |
194 | + } | |
195 | + } | |
196 | + FindClose(fh); | |
197 | + | |
198 | + depth--; | |
199 | + | |
200 | +} /* End Find() */ | |
201 | + | |
202 | +/* | |
203 | + * Which() : PATHからコマンドを探す | |
204 | + * | |
205 | + * IN command : コマンド名 | |
206 | + * | |
207 | + * IN/OUT fullpath : フルパスコマンド (IN:MAX_PATHサイズ以上) | |
208 | + * OUT ret : 0: command found | |
209 | + * -1: command not found | |
210 | + * | |
211 | + */ | |
212 | +int Which(char *command, char *fullpath) | |
213 | +{ | |
214 | + char path[2048]; /* stat path */ | |
215 | + char *pathenvp; | |
216 | + char pathp[2048]; /* ENV path */ | |
217 | + int pathlen, comsize; | |
218 | + int i, j; | |
219 | + struct stat statb; | |
220 | + int allf = 0; | |
221 | + int found = 0; | |
222 | + | |
223 | + /* fullpath指定など、存在したらそれを返す */ | |
224 | + if (stat(command, &statb) == 0) { | |
225 | + strcpy(fullpath, command); | |
226 | + return 0; | |
227 | + } | |
228 | + | |
229 | + strcpy(fullpath, ""); | |
230 | + | |
231 | + dprintf2("command=%s\n",command); | |
232 | + | |
233 | + strcpy(pathp,".;"); | |
234 | + pathenvp=""; | |
235 | + pathenvp = (char *)getenv("PATH"); | |
236 | + | |
237 | + dprintf2("PATH=%s\n",pathenvp); | |
238 | + | |
239 | + if (pathenvp) strcat(pathp,pathenvp); | |
240 | + pathlen = strlen(pathp); | |
241 | + | |
242 | + dprintf2("path=%s\n",pathp); | |
243 | + | |
244 | + i = 0; | |
245 | + | |
246 | + while (i < pathlen) { | |
247 | + j = 0; | |
248 | + while (pathp[i] != ';' && i < pathlen) { /*path[]="/usr/bin"*/ | |
249 | + path[j] = pathp[i]; | |
250 | + if (path[j] == '\\') | |
251 | + path[j] = '/'; | |
252 | + ++i; | |
253 | + ++j; | |
254 | + } | |
255 | + i++; | |
256 | + path[j] = '\0'; | |
257 | + | |
258 | + dprintf2("path=[%s]\n",path); | |
259 | + | |
260 | + strcat(path,"/"); /* path[] = "/usr/bin/" */ | |
261 | + strcat(path,command); /* path[] = "/usr/bin/com" */ | |
262 | + | |
263 | + dprintf2("check path=%s\n",path); | |
264 | + comsize = strlen(path); | |
265 | +#if 0 | |
266 | + /* timexでは.bat未サポート */ | |
267 | + strcpy(&path[comsize],".bat"); /* path[] = "/usr/bin/com.bat"*/ | |
268 | + if (stat(path,&statb) == 0) { | |
269 | + /* printf("%s\n", path); */ | |
270 | + strcpy(fullpath, path); | |
271 | + if (allf) found = 1; | |
272 | + else return 0; | |
273 | + } | |
274 | +#endif | |
275 | + strcpy(&path[comsize],".exe"); /* path[] = "/usr/bin/com.exe"*/ | |
276 | + if (stat(path,&statb) == 0) { | |
277 | + /* printf("%s\n", path); */ | |
278 | + strcpy(fullpath, path); | |
279 | + if (allf) found = 1; | |
280 | + else return 0; | |
281 | + } | |
282 | + strcpy(&path[comsize],".com"); /* path[] = "/usr/bin/com.com"*/ | |
283 | + if (stat(path,&statb) == 0) { | |
284 | + /* printf("%s\n", path); */ | |
285 | + strcpy(fullpath, path); | |
286 | + if (allf) found = 1; | |
287 | + else return 0; | |
288 | + } | |
289 | + strcpy(&path[comsize],".cmd"); /* path[] = "/usr/bin/com.cmd"*/ | |
290 | + if (stat(path,&statb) == 0) { | |
291 | + /* printf("%s\n", path); */ | |
292 | + strcpy(fullpath, path); | |
293 | + if (allf) found = 1; | |
294 | + else return 0; | |
295 | + } | |
296 | + } | |
297 | + if (!found) { | |
298 | + printf("command %s not found.\n",command); | |
299 | + return -1; | |
300 | + } | |
301 | + return 0; | |
302 | +} | |
303 | + | |
304 | +/* | |
305 | + * Play() : playする | |
306 | + * | |
307 | + * IN playno : Play Sequence | |
308 | + * player : playerのパス | |
309 | + * opt : playerのオプション | |
310 | + * list : 曲リスト | |
311 | + * num : 曲番号 | |
312 | + * OUT ret : 0 success | |
313 | + * -1 error | |
314 | + */ | |
315 | +int Play(int playno, char *player, char *opt, char **list, int num) | |
316 | +{ | |
317 | + BOOL status; | |
318 | + STARTUPINFO si; | |
319 | + PROCESS_INFORMATION pi; | |
320 | + | |
321 | + HANDLE hProc; /* Process Handle */ | |
322 | + int pst; | |
323 | + int st = 0; /* success */ | |
324 | + char comarg[MAX_PATH]; | |
325 | + | |
326 | + /* PrintId3(list[num]); */ | |
327 | + | |
328 | + /* make command line */ | |
329 | + sprintf(comarg, "\"%s\" %s \"%s\"",player, opt, list[num]); | |
330 | + dprintf("DEBUG: Play: %s %s\n", player, comarg); | |
331 | + if (tf) { | |
332 | + printf("\n"); /* 見やすくするように */ | |
333 | + } | |
334 | + printf("## No.%d/%d: [%d/%d]%s\n", | |
335 | + playno+1, gnum, | |
336 | + num, num_ent, | |
337 | + list[num]); | |
338 | + | |
339 | + memset(&si, 0, sizeof(si)); | |
340 | + si.cb = sizeof(si); | |
341 | + status = CreateProcess(player, /* 実行モジュール名 */ | |
342 | + comarg, /* コマンドライン */ | |
343 | + NULL, /* プロセスのセキュリティ属性 */ | |
344 | + NULL, /* スレッドのセキュリティ属性 */ | |
345 | + FALSE, /* ハンドルを継承しない */ | |
346 | + 0, /* fdwCreate */ | |
347 | + NULL, /* 環境ブロックのアドレス */ | |
348 | + NULL, /* カレントディレクトリ(親同) */ | |
349 | + &si, /* STARTUPINFO構造体 */ | |
350 | + &pi); /* PROCESS_INFORMATION構造体 */ | |
351 | + if(status != TRUE){ | |
352 | + printf("exec_cmd: CreateProcess returned=%d\n",GetLastError()); | |
353 | + return -1; /* error */ | |
354 | + } | |
355 | + | |
356 | + hProc = pi.hProcess; | |
357 | + status=CloseHandle(pi.hThread); | |
358 | + if(status != TRUE){ | |
359 | + printf("CloseHandle error(%d).\n", GetLastError()); | |
360 | + } | |
361 | + | |
362 | + /* | |
363 | + * wait for command end. | |
364 | + */ | |
365 | + if (WaitForSingleObject(hProc, INFINITE) != WAIT_FAILED) { | |
366 | + /* process end!! */ | |
367 | + if (!GetExitCodeProcess(hProc, &pst)) { | |
368 | + printf("exec_cmd: GetExitCodeProcess error(%d)\n", | |
369 | + GetLastError()); | |
370 | + st = 0; /* error */ | |
371 | + } else { | |
372 | + st = pst; | |
373 | + } | |
374 | + | |
375 | + } | |
376 | + | |
377 | + status=CloseHandle(hProc); | |
378 | + if(status != TRUE){ | |
379 | + printf("CloseHandle error(%d).\n", GetLastError()); | |
380 | + } | |
381 | + return 0; | |
382 | +} | |
383 | + | |
384 | +int main(int a, char *b[]) | |
385 | +{ | |
386 | + char *fname = "."; | |
387 | + int i; | |
388 | + int fn = 0; | |
389 | + char player[MAX_PATH]; | |
390 | + char suffix[MAX_PATH]; | |
391 | + char *opt = NULL; /* player option */ | |
392 | + time_t tt; | |
393 | +#ifdef _WIN32 | |
394 | + SYSTEMTIME syst; | |
395 | +#endif /* _WIN32 */ | |
396 | + | |
397 | + strcpy(player, ""); | |
398 | + strcpy(suffix, SUFF_MP3); | |
399 | + | |
400 | + for (i=1;i<a;i++) { | |
401 | + if (i+1 < a && !strncmp(b[i],"-n",2)) { | |
402 | + gnum = atoi(b[++i]); | |
403 | + continue; | |
404 | + } | |
405 | + if (i+1 < a && !strncmp(b[i],"-s",2)) { | |
406 | + sprintf(suffix, ".%s", b[++i]); | |
407 | + continue; | |
408 | + } | |
409 | + if (!strncmp(b[i],"-t",2)) { | |
410 | + tf = 1; /* print ID3 tag */ | |
411 | + continue; | |
412 | + } | |
413 | + if (!strncmp(b[i],"-v",2)) { | |
414 | + ++vf; | |
415 | + continue; | |
416 | + } | |
417 | + if (!strncmp(b[i],"-",1)) { | |
418 | + Usage(); | |
419 | + exit(1); | |
420 | + } | |
421 | + fname = b[i]; | |
422 | + } | |
423 | + | |
424 | + if (strlen(fname) > 0 && fname[strlen(fname)-1] == '/') { | |
425 | + fname[strlen(fname)-1] = '\0'; | |
426 | + } | |
427 | + if (strlen(fname) > 1 && | |
428 | + ((unsigned char)fname[strlen(fname)-2]) < 0x80 && | |
429 | + fname[strlen(fname)-1] == '\\') { | |
430 | + /* 漢字の2バイト目でない'\'だったら削除 */ | |
431 | + fname[strlen(fname)-1] = '\0'; | |
432 | + } | |
433 | + | |
434 | + InitPathList(); /* パスリストの初期化 */ | |
435 | + Find(fname, suffix); /* find(mp3) 実行 */ | |
436 | + | |
437 | +#ifndef _WIN32 | |
438 | + /* Windowsではなぜかいまいち */ | |
439 | + tt = time(0); | |
440 | +#else /* _WIN32 */ | |
441 | + GetLocalTime(&syst); | |
442 | + tt = syst.wHour * 3600 * 1000 + | |
443 | + syst.wMinute * 60 * 1000 + | |
444 | + syst.wSecond * 1000 + | |
445 | + syst.wMilliseconds; | |
446 | +#endif /* _WIN32 */ | |
447 | + | |
448 | + srand(tt); /* 乱数系列初期化 */ | |
449 | + | |
450 | + if (!stricmp(suffix, SUFF_MP3) && Which(PLAYER_MP3, player)) { | |
451 | + printf("%s not found\n", PLAYER_MP3); | |
452 | + } | |
453 | + | |
454 | + /* set option */ | |
455 | + if (strlen(player)) { | |
456 | + /* for mpg123 */ | |
457 | + if (tf) { | |
458 | + opt = ""; /* print title */ | |
459 | + } else { | |
460 | + opt = "-q"; /* quiet mode */ | |
461 | + } | |
462 | + } else { | |
463 | + opt = "/prefetch:6 /Play"; /* for WMPlayer */ | |
464 | + } | |
465 | + | |
466 | + if (strlen(player) == 0 && Which(PLAYER_MS, player)) { | |
467 | + printf("%s not found\n", PLAYER_MS); | |
468 | + } | |
469 | + | |
470 | + for (i = 0; i < gnum; i++) { | |
471 | + Play(i, player, opt, pathlist, RAND(num_ent)); | |
472 | + } | |
473 | + | |
474 | + return 0; | |
475 | +} | |
476 | + | |
477 | + | |
478 | + |
@@ -0,0 +1,121 @@ | ||
1 | +/* | |
2 | + * sendevt : 他Windowを操作するテスト | |
3 | + * | |
4 | + * 2008/10/13 V0.10 by oga. | |
5 | + */ | |
6 | +#include <windows.h> | |
7 | + | |
8 | +char *WinTitle = "素数べんち"; | |
9 | +int evf = 0; /* -ev flag */ | |
10 | +int evid = 0; /* -ev event id */ | |
11 | + | |
12 | +char class_name[20][1024]; | |
13 | + | |
14 | +int SendMsg(HWND hWin, int evid) | |
15 | +{ | |
16 | + switch (evid) { | |
17 | + case WM_KEYDOWN: | |
18 | + SendMessage(hWin, evid, 'a', 0); | |
19 | + SendMessage(hWin, WM_KEYUP, 'a', 0); | |
20 | + SendMessage(hWin, WM_SETTEXT, 4, (long)"abcd"); // タイトルバーに設定されてしまう | |
21 | + break; | |
22 | + | |
23 | + default: | |
24 | + SendMessage(hWin, evid, 0, 0); | |
25 | + break; | |
26 | + } | |
27 | + return 0; | |
28 | +} | |
29 | + | |
30 | +int GetControlList(HWND hWin, int level) | |
31 | +{ | |
32 | + HWND hCntl = NULL; | |
33 | + char classname[4096]; | |
34 | + char ctrltext[4096]; | |
35 | + char spc[1024]; | |
36 | + int ret; | |
37 | + | |
38 | + ++level; | |
39 | + strcpy(spc, " "); | |
40 | + spc[level-1] = '\0'; | |
41 | +#if 0 | |
42 | + if (level > 1) { | |
43 | + spc[level-1] = '+'; | |
44 | + } | |
45 | +#endif | |
46 | + | |
47 | + while (1) { | |
48 | + hCntl = FindWindowEx(hWin, hCntl, NULL, NULL); | |
49 | + if (!hCntl) { | |
50 | + break; | |
51 | + } | |
52 | + GetClassName(hCntl, classname, sizeof(classname)); | |
53 | + strcpy(class_name[level-1], classname); | |
54 | + ctrltext[0] = '\0'; | |
55 | + ret = GetWindowText(hCntl, ctrltext, sizeof(ctrltext)); | |
56 | + if (ret == 0) { | |
57 | + ret = GetLastError(); | |
58 | + if (ret != ERROR_SUCCESS) { | |
59 | + sprintf(ctrltext, "GetWindowText Error(%d)", ret); | |
60 | + } | |
61 | + } | |
62 | + printf("%sClass:%-20s Text:[%s]\n", spc, classname, ctrltext); | |
63 | + if (evf && !strcmp(class_name[0], "Notepad") && !strcmp(class_name[1], "Edit")) { | |
64 | + //hWin = FindWindow(NULL, ctrltext); /* hWin取り直し */ | |
65 | + //SetForegroundWindow(hWin); | |
66 | + printf("## Send Message(%d) to %s.\n", evid, ctrltext); | |
67 | + SendMsg(hWin, evid); | |
68 | + exit(1); | |
69 | + } | |
70 | +#if 0 | |
71 | + /* replace other window text */ | |
72 | + if (!strcmp(ctrltext, "0.391")) { | |
73 | + SetWindowText(hCntl, "0.222"); | |
74 | + printf("Set 0.222!!\n"); | |
75 | + /* Send Redraw Message to Message Queue */ | |
76 | + SendMessage(hWin, WM_PAINT, 0, 0); | |
77 | + } | |
78 | +#endif | |
79 | + GetControlList(hCntl, level); | |
80 | + } | |
81 | + strcpy(class_name[level-1], ""); | |
82 | + return 0; | |
83 | +} | |
84 | + | |
85 | +int main(int a, char *b[]) | |
86 | +{ | |
87 | + HWND hWin = NULL; | |
88 | + int i; | |
89 | + | |
90 | + memset(class_name, 0, sizeof(class_name)); | |
91 | + | |
92 | + for (i = 1; i<a; i++) { | |
93 | + if (i+1 < a && !strcmp(b[i], "-ev")) { | |
94 | + evf = 1; | |
95 | + evid = atoi(b[++i]); | |
96 | + continue; | |
97 | + } | |
98 | + if (!strcmp(b[i], "-close")) { | |
99 | + evf = 1; | |
100 | + evid = WM_CLOSE; | |
101 | + continue; | |
102 | + } | |
103 | + if (!strcmp(b[i], "-key")) { | |
104 | + evf = 1; | |
105 | + evid = WM_KEYDOWN; | |
106 | + continue; | |
107 | + } | |
108 | + if (!strcmp(b[i], "-h")) { | |
109 | + printf("usage: sendevt { <win_title> [-close | -key | -ev <evt_id>]}\n"); | |
110 | + printf(" -close : send close msg to specific window (evt:%d)\n", WM_CLOSE); | |
111 | + printf(" -ev <evt_id> : send specific event\n"); | |
112 | + exit(1); | |
113 | + } | |
114 | + WinTitle = b[1]; | |
115 | + } | |
116 | + | |
117 | + /* find all window */ | |
118 | + GetControlList(NULL, 0); | |
119 | + | |
120 | + return 0; | |
121 | +} |
@@ -0,0 +1,312 @@ | ||
1 | +// | |
2 | +// shutdown for win | |
3 | +// | |
4 | +// 2001/10/17 V1.00 by oga. | |
5 | +// 2007/04/19 V1.01 support suspend | |
6 | +// 2009/12/17 V1.02 fix ExitWindowsEx() error log | |
7 | +// | |
8 | +#include <windows.h> | |
9 | +#include <stdio.h> | |
10 | +#include <powrprof.h> | |
11 | + | |
12 | +// globals | |
13 | +char *VER = "1.02"; | |
14 | +int logf = 0; // -log option | |
15 | +char *logfile = NULL; // log file name | |
16 | +FILE *fp = NULL; | |
17 | + | |
18 | +void usage() | |
19 | +{ | |
20 | + printf("shutdown Ver %s\n", VER); | |
21 | + printf("usage: shutdown {-h [-pwoff] | -r | -logoff | -s} [-f] [-log <logfile>] <time>\n"); | |
22 | + printf(" -h : shutdown\n"); | |
23 | + printf(" -pwoff : power off after shutdown\n"); | |
24 | + printf(" -r : shutdown and reboot\n"); | |
25 | + printf(" -logoff : only logoff\n"); | |
26 | + printf(" -s : suspend\n"); /* V1.01-A */ | |
27 | + printf(" -b : hibernate\n"); /* V1.01-A */ | |
28 | + printf(" time : now ... shutdown immediately (+0)\n"); | |
29 | + printf(" +<sec> ... shutdown after <sec> seconds\n"); | |
30 | + printf(" hh:mm ... shutdown at hh:mm (24h format)\n"); | |
31 | + printf(" -f : force process to terminate. don't send terminate message.\n"); | |
32 | + printf(" -log : output shutdown log to <logfile>\n"); | |
33 | + exit(1); | |
34 | +} | |
35 | + | |
36 | +int outlog(char *msg) | |
37 | +{ | |
38 | + SYSTEMTIME syst; | |
39 | + if (logf) { | |
40 | + if (fp == NULL) { | |
41 | + if ((fp = fopen(logfile, "a")) == NULL) { | |
42 | + perror("Error: fopen()"); | |
43 | + logf = 0; | |
44 | + } | |
45 | + } | |
46 | + GetLocalTime(&syst); | |
47 | + if (fp) fprintf(fp, "%04d/%02d/%02d %02d:%02d:%02d %s", | |
48 | + syst.wYear, syst.wMonth, syst.wDay, | |
49 | + syst.wHour, syst.wMinute, syst.wSecond, | |
50 | + msg); | |
51 | + } | |
52 | + printf("%s", msg); | |
53 | + fflush(stdout); | |
54 | + | |
55 | + return 0; | |
56 | +} | |
57 | + | |
58 | +// IS_NT() | |
59 | +// OUT : ret : 1:NT series 0: Win95 series or unknown | |
60 | +// | |
61 | +BOOL IS_NT() | |
62 | +{ | |
63 | + char str[4096]; // message buffer | |
64 | + OSVERSIONINFO ver; | |
65 | + | |
66 | + memset(&ver, 0, sizeof(OSVERSIONINFO)); | |
67 | + ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
68 | + if (GetVersionEx(&ver)) { | |
69 | + // get success | |
70 | + if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) { | |
71 | + return TRUE; // NT series | |
72 | + } | |
73 | + } else { | |
74 | + sprintf(str,"GetVersionEx() Error. (%d)\n", GetLastError()); | |
75 | + outlog(str); | |
76 | + } | |
77 | + return FALSE; // non NT | |
78 | +} | |
79 | + | |
80 | +BOOL EnablePrivilege( LPTSTR privilege, BOOL enable ) | |
81 | +{ | |
82 | + char str[4096]; // message buffer | |
83 | + HANDLE hToken; | |
84 | + LUID luid; | |
85 | + TOKEN_PRIVILEGES tokenPrivileges; | |
86 | + | |
87 | + if(!IS_NT()) { | |
88 | + return TRUE; | |
89 | + } | |
90 | + | |
91 | + if(!OpenProcessToken( GetCurrentProcess(), | |
92 | + TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, | |
93 | + &hToken)) { | |
94 | + sprintf(str,"OpenProcessToken() Error. (%d)\n", GetLastError()); | |
95 | + outlog(str); | |
96 | + return FALSE; | |
97 | + } | |
98 | + | |
99 | + if(!LookupPrivilegeValue(0, privilege, &luid)) { | |
100 | + sprintf(str,"LookupPrivilegeValue() Error. (%d)\n", GetLastError()); | |
101 | + outlog(str); | |
102 | + return FALSE; | |
103 | + } | |
104 | + | |
105 | + tokenPrivileges.PrivilegeCount = 1; | |
106 | + tokenPrivileges.Privileges[0].Luid = luid; | |
107 | + tokenPrivileges.Privileges[0].Attributes = (enable?SE_PRIVILEGE_ENABLED:0); | |
108 | + | |
109 | + if(!AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges, 0, 0, 0) ) { | |
110 | + sprintf(str,"AdjustTokenPrivileges() Error. (%d)\n", GetLastError()); | |
111 | + outlog(str); | |
112 | + return FALSE; | |
113 | + } | |
114 | + | |
115 | + return TRUE; | |
116 | +} | |
117 | + | |
118 | +int main(int a, char *b[]) | |
119 | +{ | |
120 | + int shutopt = 0; // shutdown options (-h|-r|-f) | |
121 | + int waitsec = 0; // +<sec> option | |
122 | + int vf = 0; // -v | |
123 | + int ff = 0; // -f /* V1.01-A */ | |
124 | + int sf = 0; // -s /* V1.01-A */ | |
125 | + int bf = 0; // -b /* V1.01-A */ | |
126 | + int logoff = 0; // -logoff | |
127 | + char *timestr = NULL; // down time string | |
128 | + | |
129 | + int i; | |
130 | + int hh = -99; | |
131 | + int mm = -99; | |
132 | + char *command = "shutdown"; | |
133 | + char str[4096]; // message buffer | |
134 | + SYSTEMTIME syst; | |
135 | + | |
136 | + for (i=1; i<a; i++) { | |
137 | + if (!strcmp(b[i], "-v")) { | |
138 | + ++vf; | |
139 | + continue; | |
140 | + } | |
141 | + if (!strcmp(b[i], "-h")) { | |
142 | + shutopt &= ~(EWX_LOGOFF | EWX_REBOOT); | |
143 | + shutopt |= EWX_SHUTDOWN; | |
144 | + continue; | |
145 | + } | |
146 | + if (!strcmp(b[i], "-pwoff")) { | |
147 | + shutopt &= ~(EWX_LOGOFF | EWX_REBOOT); | |
148 | + shutopt |= EWX_POWEROFF; | |
149 | + continue; | |
150 | + } | |
151 | + if (!strcmp(b[i], "-r")) { | |
152 | + shutopt &= ~(EWX_LOGOFF | EWX_SHUTDOWN | EWX_POWEROFF); | |
153 | + shutopt |= EWX_REBOOT; | |
154 | + continue; | |
155 | + } | |
156 | + if (!strcmp(b[i], "-f")) { | |
157 | + shutopt |= EWX_FORCE; | |
158 | + ff = 1; /* V1.01-A */ | |
159 | + continue; | |
160 | + } | |
161 | + /* V1.01-A start */ | |
162 | + if (!strcmp(b[i], "-s")) { | |
163 | + sf = 1; /* suspend */ | |
164 | + continue; | |
165 | + } | |
166 | + if (!strcmp(b[i], "-b")) { | |
167 | + bf = 1; /* hibernate */ | |
168 | + continue; | |
169 | + } | |
170 | + /* V1.01-A end */ | |
171 | + if (!strcmp(b[i], "-log")) { | |
172 | + if (++i < a) { | |
173 | + logf = 1; | |
174 | + logfile = b[i]; | |
175 | + continue; | |
176 | + } else { | |
177 | + printf("Error: log file not specified\n", timestr); | |
178 | + usage(); | |
179 | + } | |
180 | + } | |
181 | + if (!strcmp(b[i], "-logoff")) { | |
182 | + shutopt &= ~(EWX_SHUTDOWN | EWX_POWEROFF | EWX_REBOOT); | |
183 | + shutopt |= EWX_LOGOFF; | |
184 | + continue; | |
185 | + } | |
186 | + timestr = b[i]; | |
187 | + } | |
188 | + if (timestr == NULL) { | |
189 | + printf("Error: time not specified\n"); | |
190 | + usage(); | |
191 | + } | |
192 | + | |
193 | + // -h & -pwoff check | |
194 | + if ((shutopt & EWX_POWEROFF) && ((shutopt & EWX_SHUTDOWN) == 0)) { | |
195 | + usage(); | |
196 | + } | |
197 | + | |
198 | + // analyze time arg | |
199 | + if (!strcmp(timestr, "now")) { | |
200 | + // "now" | |
201 | + waitsec = 0; | |
202 | + } else if (timestr[0] == '+') { | |
203 | + // "+<sec>" | |
204 | + waitsec = atoi(×tr[1]); | |
205 | + } else if (timestr[2] == ':' && strlen(timestr) == 5) { | |
206 | + // "hh:mm" | |
207 | + hh = atoi(×tr[0]); | |
208 | + mm = atoi(×tr[3]); | |
209 | + if (hh < 0 || hh > 23 || mm < 0 || mm > 59) { | |
210 | + // invalid value | |
211 | + printf("Error: Invalid hh:mm value [%s]\n", timestr); | |
212 | + usage(); | |
213 | + } | |
214 | + } else { | |
215 | + printf("Error: Invalid time value [%s]\n", timestr); | |
216 | + usage(); | |
217 | + } | |
218 | + | |
219 | + // | |
220 | + // shutdown main | |
221 | + // | |
222 | + if (shutopt & EWX_LOGOFF) { | |
223 | + command = "logoff"; | |
224 | + logoff = 1; | |
225 | + } | |
226 | + GetLocalTime(&syst); | |
227 | + sprintf(str, "##### shutdown Ver %s at %02d:%02d:%02d #####\n", | |
228 | + VER, | |
229 | + syst.wHour, syst.wMinute, syst.wSecond); | |
230 | + outlog(str); | |
231 | + | |
232 | + sprintf(str, "shutdown opt : %s%s%s%s%s%s\n", /* V1.01-C */ | |
233 | + sf ? "SUSPEND " : "", /* V1.01-A */ | |
234 | + bf ? "HIBERNATE ": "", /* V1.01-A */ | |
235 | + (shutopt & EWX_LOGOFF) ? "LOGOFF " : "", | |
236 | + (shutopt & EWX_SHUTDOWN) ? "SHUTDOWN " : "", | |
237 | + (shutopt & EWX_POWEROFF) ? "POWEROFF " : "", | |
238 | + (shutopt & EWX_REBOOT) ? "REBOOT " : "", | |
239 | + (shutopt & EWX_FORCE) ? "FORCE " : ""); | |
240 | + outlog(str); | |
241 | + | |
242 | + if (hh == -99) { | |
243 | + // shutdown after <sec> | |
244 | + sprintf(str,"%s after %d second%s.\n", | |
245 | + command, waitsec, (waitsec > 1)?"s":""); | |
246 | + outlog(str); | |
247 | + Sleep(waitsec * 1000); | |
248 | + } else { | |
249 | + // shutdown at hh:mm | |
250 | + sprintf(str,"%s at %02d:%02d \n", command, hh, mm); | |
251 | + outlog(str); | |
252 | + while (1) { | |
253 | + GetLocalTime(&syst); | |
254 | + if (syst.wHour == hh && syst.wMinute == mm) { | |
255 | + break; | |
256 | + } | |
257 | + Sleep(5000); | |
258 | + } | |
259 | + } | |
260 | + | |
261 | + // shutdown start message and beep | |
262 | + sprintf(str, "%s start...\n", command); | |
263 | + outlog(str); | |
264 | + printf(""); | |
265 | + fflush(stdout); | |
266 | + | |
267 | + /* V1.01-A start */ | |
268 | + /* suspend/hibernate */ | |
269 | + if (sf || bf) { | |
270 | + /* TRUE:hibernate FALSE:suspend */ | |
271 | + /* TRUE:immediately FALSE:send PBT_APMQUERYSUSPEND */ | |
272 | + /* TRUE:disable wake events FALSE:wake event enable */ | |
273 | + if (FALSE == SetSuspendState(bf?TRUE:FALSE, | |
274 | + ff?TRUE:FALSE, | |
275 | + FALSE)) | |
276 | + { | |
277 | + printf(str, "Error: SetSuspendState() failed. (%d)\n", GetLastError()); | |
278 | + exit(4); | |
279 | + } | |
280 | + } | |
281 | + /* V1.01-A start */ | |
282 | + | |
283 | + // if shutdown, Enable Privilege | |
284 | + if (!logoff && (EnablePrivilege(SE_SHUTDOWN_NAME, TRUE) == FALSE)) { | |
285 | + sprintf(str, "%s cancelled.\n", command); | |
286 | + outlog(str); | |
287 | + exit(2); | |
288 | + } | |
289 | + | |
290 | + if (fp) fclose(fp); // close logfile | |
291 | + logf = 0; | |
292 | + if (!ExitWindowsEx(shutopt, 0)) { | |
293 | + char *msgbuf; | |
294 | + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
295 | + FORMAT_MESSAGE_FROM_SYSTEM, | |
296 | + NULL, | |
297 | + GetLastError(), | |
298 | + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // デフォルト ユーザー言語 | |
299 | + (LPTSTR)&msgbuf, | |
300 | + 0, | |
301 | + NULL); | |
302 | + sprintf(str, "Error: ExitWindowEx() failed. (%d) %s\n", GetLastError(), msgbuf); /* V1.02-C */ | |
303 | + LocalFree(msgbuf); | |
304 | + outlog(str); /* V1.02-A */ | |
305 | + exit(3); | |
306 | + } | |
307 | + return 0; | |
308 | +} | |
309 | + | |
310 | +/* vim:ts=8:sw=4: | |
311 | + */ | |
312 | + |
@@ -0,0 +1,903 @@ | ||
1 | +/* | |
2 | + * sysinfd.c | |
3 | + * System Information Service Daemon | |
4 | + * | |
5 | + * 2001/12/03 V1.00 by oga. (MEM, LOAD) | |
6 | + * 2002/02/03 V1.01 Keep Connection | |
7 | + * 2013/02/25 V1.02 add socket close() | |
8 | + * 2013/11/14 V1.10 support Win | |
9 | + * 2013/12/06 V1.11 fix minus load for win. | |
10 | + * 2013/12/06 V1.12 add non block accept code | |
11 | + * | |
12 | + * Protocol | |
13 | + * HELP | |
14 | + * Return Help String\n\n | |
15 | + * | |
16 | + * GET_MEM | |
17 | + * Get Memory/Swap Info (KB) | |
18 | + * Total Free Shared Buffers Cached SwapTotal SwapFree | |
19 | + * --------------------------------------------------------- | |
20 | + * 69860 1520 4 3008 57292 51400 27728\n\n | |
21 | + * | |
22 | + * GET_LOAD | |
23 | + * Get Load Ave. Info | |
24 | + * 1min 5min 15min | |
25 | + * --------------------------------- | |
26 | + * 0.00 0.00 0.00\n\n | |
27 | + * | |
28 | + * All Rights Reserved. Copyright (C) 2001,2002, Moritaka Ogasawara. | |
29 | + */ | |
30 | + | |
31 | +#include <stdio.h> | |
32 | +#include <errno.h> | |
33 | +#include <string.h> | |
34 | +#include <sys/types.h> | |
35 | +#include <sys/stat.h> | |
36 | +#ifdef _WIN32 | |
37 | +#include <windows.h> | |
38 | +#include <winsock.h> | |
39 | +#else // Linux | |
40 | +#include <sys/socket.h> | |
41 | +#include <netinet/in.h> | |
42 | +#include <arpa/inet.h> | |
43 | +#include <fcntl.h> | |
44 | +#include <signal.h> | |
45 | +#endif // Linux | |
46 | + | |
47 | + | |
48 | +/* Keep Connection V1.01-A */ | |
49 | +#define KEEP_CONN | |
50 | + | |
51 | +/* Non Block accept V1.01-A */ | |
52 | +#define NO_BLOCK_ACCEPT | |
53 | + | |
54 | +#ifndef TCP_PORT | |
55 | +#define TCP_PORT 9998 | |
56 | +#endif | |
57 | + | |
58 | +#define SIZE 1024 | |
59 | + | |
60 | +#ifndef SIGCLD | |
61 | +#define SIGCLD SIGCHLD | |
62 | +#endif | |
63 | + | |
64 | +/* request keywords */ | |
65 | +#define GET_MEM "GET_MEM" | |
66 | +#define GET_LOAD "GET_LOAD" | |
67 | +#define LOGIN "LOGIN" | |
68 | +#define HELP "HELP" | |
69 | +#define EXIT "EXIT" | |
70 | + | |
71 | +/* for help */ | |
72 | +#define HELP_STR "GET_MEM GET_LOAD HELP EXIT\n\n" | |
73 | + | |
74 | +/* 仮のdefine */ | |
75 | + | |
76 | +#ifdef _WIN32 | |
77 | +#define strncasecmp strnicmp | |
78 | +#define CLOSE closesocket | |
79 | + | |
80 | +// 64bit | |
81 | +typedef DWORDLONG mem_uint; | |
82 | + | |
83 | +//typedef struct _procarg_t { | |
84 | +// int clsockfd; /* client socket */ | |
85 | +// int con; /* start number */ | |
86 | +//} procarg_t; | |
87 | + | |
88 | +// | |
89 | +// for getCpuUsage() | |
90 | +// | |
91 | +#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart)) | |
92 | + | |
93 | +typedef struct | |
94 | +{ | |
95 | + DWORD dwUnknown1; | |
96 | + ULONG uKeMaximumIncrement; | |
97 | + ULONG uPageSize; | |
98 | + ULONG uMmNumberOfPhysicalPages; | |
99 | + ULONG uMmLowestPhysicalPage; | |
100 | + ULONG uMmHighestPhysicalPage; | |
101 | + ULONG uAllocationGranularity; | |
102 | + PVOID pLowestUserAddress; | |
103 | + PVOID pMmHighestUserAddress; | |
104 | + ULONG uKeActiveProcessors; | |
105 | + BYTE bKeNumberProcessors; | |
106 | + BYTE bUnknown2; | |
107 | + WORD wUnknown3; | |
108 | +} SYSTEM_BASIC_INFORMATION; | |
109 | + | |
110 | +typedef struct | |
111 | +{ | |
112 | + LARGE_INTEGER liIdleTime; | |
113 | + DWORD dwSpare[76]; | |
114 | +} SYSTEM_PERFORMANCE_INFORMATION; | |
115 | + | |
116 | +typedef struct | |
117 | +{ | |
118 | + LARGE_INTEGER liKeBootTime; | |
119 | + LARGE_INTEGER liKeSystemTime; | |
120 | + LARGE_INTEGER liExpTimeZoneBias; | |
121 | + ULONG uCurrentTimeZoneId; | |
122 | + DWORD dwReserved; | |
123 | +} SYSTEM_TIME_INFORMATION; | |
124 | + | |
125 | +typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG); | |
126 | + | |
127 | +PROCNTQSI NtQuerySystemInformation; | |
128 | + | |
129 | +#else // Linux | |
130 | +#define HANDLE int | |
131 | +#define CLOSE close | |
132 | + | |
133 | +// 64bit | |
134 | +typedef unsigned long long int mem_uint; | |
135 | +#endif // Linux | |
136 | + | |
137 | +typedef struct _mem_t { | |
138 | + mem_uint total; | |
139 | + mem_uint free; | |
140 | + mem_uint shared; | |
141 | + mem_uint buffers; | |
142 | + mem_uint cached; | |
143 | + mem_uint swap_total; | |
144 | + mem_uint swap_free; | |
145 | +} mem_t; | |
146 | + | |
147 | +typedef struct _load_t { | |
148 | + int load1m; // load ave. 1min x100 1.23→123 | |
149 | + int load5m; // load ave. 5min x100 | |
150 | + int load15m; // load ave. 15min x100 | |
151 | +} load_t; | |
152 | + | |
153 | + | |
154 | +/* globals */ | |
155 | +int vf = 0; /* 1:verbose mode */ | |
156 | +int sockfd; | |
157 | + | |
158 | +/* func for windows porting */ | |
159 | +#ifdef _WIN32 | |
160 | + | |
161 | +/* | |
162 | + * READ(fd,buf,size) | |
163 | + * | |
164 | + * OUT ret : >= 0 success (read size) | |
165 | + * : -1 error | |
166 | + */ | |
167 | +int READ(HANDLE fd, void *buf, int count) | |
168 | +{ | |
169 | + int rbytes = 0; | |
170 | + BOOL ret; | |
171 | + | |
172 | + ret = ReadFile( | |
173 | + fd, // ファイルのハンドル | |
174 | + buf, // データバッファ | |
175 | + count, // 読み取り対象のバイト数 | |
176 | + &rbytes, // 読み取ったバイト数 | |
177 | + NULL // オーバーラップ構造体のバッファ | |
178 | + ); | |
179 | + if (ret == FALSE) { | |
180 | + return -1; | |
181 | + } | |
182 | + return rbytes; | |
183 | +} | |
184 | + | |
185 | + | |
186 | +/* | |
187 | + * WRITE(fd,buf,size) | |
188 | + * | |
189 | + * OUT ret : >= 0 success (write size) | |
190 | + * : -1 error | |
191 | + */ | |
192 | +int WRITE(HANDLE fd, void *buf, int count) | |
193 | +{ | |
194 | + int wbytes = 0; | |
195 | + BOOL ret; | |
196 | + | |
197 | + ret = WriteFile( | |
198 | + fd, // ファイルのハンドル | |
199 | + buf, // データバッファ | |
200 | + count, // 書き込み対象のバイト数 | |
201 | + &wbytes, // 書き込んだバイト数 | |
202 | + NULL // オーバーラップ構造体のバッファ | |
203 | + ); | |
204 | + if (ret == FALSE) { | |
205 | + return -1; | |
206 | + } | |
207 | + return wbytes; | |
208 | +} | |
209 | + | |
210 | +// ntdll!NtQuerySystemInformation (NT specific!) | |
211 | +// | |
212 | +// The function copies the system information of the | |
213 | +// specified type into a buffer | |
214 | +// | |
215 | +// NTSYSAPI | |
216 | +// NTSTATUS | |
217 | +// NTAPI | |
218 | +// NtQuerySystemInformation( | |
219 | +// IN UINT SystemInformationClass, // information type | |
220 | +// OUT PVOID SystemInformation, // pointer to buffer | |
221 | +// IN ULONG SystemInformationLength, // buffer size in bytes | |
222 | +// OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit | |
223 | +// // variable that receives | |
224 | +// // the number of bytes | |
225 | +// // written to the buffer | |
226 | +// ); | |
227 | + | |
228 | +#define SystemBasicInformation 0 | |
229 | +#define SystemPerformanceInformation 2 | |
230 | +#define SystemTimeInformation 3 | |
231 | + | |
232 | +/* | |
233 | + * getCpuUsage() | |
234 | + * | |
235 | + * IN interval: 利用率を計測するための間隔(msec) | |
236 | + * OUT ret: CPU利用率 (%) | |
237 | + */ | |
238 | +int getCpuUsage(int interval) | |
239 | +{ | |
240 | + SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo; | |
241 | + SYSTEM_TIME_INFORMATION SysTimeInfo; | |
242 | + SYSTEM_BASIC_INFORMATION SysBaseInfo; | |
243 | + double dbIdleTime; | |
244 | + double dbSystemTime; | |
245 | + LONG status; | |
246 | + LARGE_INTEGER liOldIdleTime = {0,0}; | |
247 | + LARGE_INTEGER liOldSystemTime = {0,0}; | |
248 | + | |
249 | + int iCtl=0, percentage=0; | |
250 | + | |
251 | + NtQuerySystemInformation = (PROCNTQSI)GetProcAddress( | |
252 | + GetModuleHandle("ntdll"), | |
253 | + "NtQuerySystemInformation" | |
254 | + ); | |
255 | + if(!NtQuerySystemInformation) return -1; | |
256 | + | |
257 | + // get number of processors in the system | |
258 | + status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL); | |
259 | + if(status != NO_ERROR) return -1; | |
260 | + | |
261 | + //printf("\n getting CPU Usage\n"); | |
262 | + //while(!_kbhit()) | |
263 | + | |
264 | + while(1) { | |
265 | + // get new system time | |
266 | + status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0); | |
267 | + if(status!=NO_ERROR) | |
268 | + return -1; | |
269 | + | |
270 | + // get new CPU's idle time | |
271 | + status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL); | |
272 | + if(status != NO_ERROR) | |
273 | + return -1; | |
274 | + | |
275 | + // if it's a first call - skip it | |
276 | + if(liOldIdleTime.QuadPart != 0) { | |
277 | + // CurrentValue = NewValue - OldValue | |
278 | + dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime); | |
279 | + dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime); | |
280 | + | |
281 | + // CurrentCpuIdle = IdleTime / SystemTime | |
282 | + dbIdleTime = dbIdleTime / dbSystemTime; | |
283 | + | |
284 | + // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors | |
285 | + dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5; | |
286 | + | |
287 | + //+ 0.5, result is same in task manager | |
288 | + percentage= (UINT)(dbIdleTime); | |
289 | + if (percentage < 0) percentage = 0; // V1.11-A | |
290 | + | |
291 | + //printf("\b\b\b\b%3d%%", percentage); | |
292 | + if(iCtl > 0 ) return percentage; | |
293 | + } | |
294 | + | |
295 | + // store new CPU's idle and system time | |
296 | + liOldIdleTime = SysPerfInfo.liIdleTime; | |
297 | + liOldSystemTime = SysTimeInfo.liKeSystemTime; | |
298 | + | |
299 | + // wait one second | |
300 | + Sleep(interval); | |
301 | + iCtl++; | |
302 | + } | |
303 | + return 0; | |
304 | +} | |
305 | + | |
306 | +#endif // !_WIN32 | |
307 | + | |
308 | +/* wait child process */ | |
309 | +void reapchild() | |
310 | +{ | |
311 | +#ifndef _WIN32 | |
312 | + wait(0); | |
313 | + signal(SIGCLD,reapchild); | |
314 | +#endif | |
315 | +} | |
316 | + | |
317 | +/* end process */ | |
318 | +void sigint() | |
319 | +{ | |
320 | + printf("sysinfd interrupted.\n"); | |
321 | + close(sockfd); | |
322 | + exit(1); | |
323 | +} | |
324 | + | |
325 | +#ifdef _WIN32 | |
326 | +/* | |
327 | + * dwCtrlType | |
328 | + * CTRL_C_EVENT : Ctrl+C | |
329 | + * CTRL_BREAK_EVENT : Ctrl+Break | |
330 | + * CTRL_CLOSE_EVENT : Close Console | |
331 | + * CTRL_LOGOFF_EVENT : Logoff | |
332 | + * CTRL_SHUTDOWN_EVENT : Shutdown | |
333 | + */ | |
334 | +BOOL WINAPI CtrlProc(DWORD dwCtrlType) | |
335 | +{ | |
336 | + // Stop all calse | |
337 | + printf("sysinfd interrupted2.\n"); | |
338 | + closesocket(sockfd); | |
339 | + WSACleanup(); | |
340 | + | |
341 | + return FALSE; // FALSE: NextHandler(END) TRUE: not END | |
342 | +} | |
343 | +#endif | |
344 | + | |
345 | + | |
346 | +char *get_item(char *buf, char *sep, int pos, char *outbuf) | |
347 | +{ | |
348 | + int i; | |
349 | + char *pt; | |
350 | + char *p; | |
351 | + char wk[4096]; | |
352 | + char msglog[4096]; | |
353 | + | |
354 | + strcpy(wk,buf); /* strtok()はbufを破壊するためコピーして利用する */ | |
355 | + | |
356 | + for (i = 0; i<pos; i++) { | |
357 | + if (i == 0) { | |
358 | + pt = (char *)strtok(wk,sep); | |
359 | + } else { | |
360 | + pt = (char *)strtok(NULL,sep); | |
361 | + } | |
362 | + if (pt == NULL) break; | |
363 | + } | |
364 | + if (pt == NULL) { | |
365 | + sprintf(msglog, "Out of item(%s) pos(%d).",buf,pos); | |
366 | + printf(msglog); | |
367 | + /* 結果領域クリア */ | |
368 | + strcpy(outbuf,""); | |
369 | + return NULL; | |
370 | + } | |
371 | + | |
372 | + strcpy(outbuf, pt); | |
373 | + | |
374 | + /* cut tail space */ | |
375 | + p = &outbuf[strlen(outbuf)-1]; /* last char */ | |
376 | + while (*p == ' ' || *p == 0x0a) --p; | |
377 | + *(p+1) = '\0'; | |
378 | + | |
379 | + return outbuf; | |
380 | +} /* get_item */ | |
381 | + | |
382 | + | |
383 | +/* | |
384 | + * fd から読み込んだ内容をnewsockfdに書き込む | |
385 | + * | |
386 | + * not used | |
387 | + */ | |
388 | +void PUT_DATA(int fd, int newsockfd) | |
389 | +{ | |
390 | + char c[4096]; | |
391 | + int size; | |
392 | + while((size = recv(fd, c, SIZE, 0)) != 0) { | |
393 | + send(newsockfd, c, size, 0); | |
394 | + } | |
395 | +} | |
396 | + | |
397 | + | |
398 | +/* | |
399 | + * Help(fd) | |
400 | + * send HELP | |
401 | + * | |
402 | + * IN fd : output fd | |
403 | + * OUT ret : 0 success | |
404 | + * : -1 error | |
405 | + */ | |
406 | +int Help(int fd) | |
407 | +{ | |
408 | + if (vf) printf("%s", HELP_STR); | |
409 | + send(fd, HELP_STR, strlen(HELP_STR), 0); | |
410 | + return 0; | |
411 | +} | |
412 | + | |
413 | +#ifdef _WIN32 | |
414 | +/* | |
415 | + * GetMemInfo(mem_t *memdat) for Win | |
416 | + * | |
417 | + * OUT memdat : memory data | |
418 | + * ret : 0 : success | |
419 | + * -1: error | |
420 | + */ | |
421 | +int GetMemInfo(mem_t *memdat) | |
422 | +{ | |
423 | + MEMORYSTATUSEX mst; // WDF11502-C | |
424 | + int i; | |
425 | + int ret; | |
426 | + int loadavg; | |
427 | + | |
428 | + mst.dwLength = sizeof(MEMORYSTATUSEX); // WDF11502-A | |
429 | + | |
430 | + // メモリ情報取得 | |
431 | + // upper 4GB | |
432 | + GlobalMemoryStatusEx(&mst); | |
433 | + memdat->total = mst.ullTotalPhys/1024; | |
434 | + memdat->free = mst.ullAvailPhys/1024; | |
435 | + memdat->shared = 0; | |
436 | + memdat->buffers = 0; | |
437 | + memdat->cached = 0; | |
438 | + memdat->swap_total = mst.ullTotalPageFile/1024; | |
439 | + memdat->swap_free = mst.ullAvailPageFile/1024; | |
440 | + | |
441 | + if (vf) { | |
442 | + printf("cur_mem=%I64u\n", | |
443 | + memdat->total - memdat->free - memdat->buffers - memdat->cached); | |
444 | + } | |
445 | + | |
446 | + return 0; | |
447 | +} | |
448 | + | |
449 | +/* | |
450 | + * GetLoadInfo(mem_t *memdat) for Win | |
451 | + * | |
452 | + * OUT loaddat : load data | |
453 | + * ret : 0 : success | |
454 | + * -1: error | |
455 | + */ | |
456 | +int GetLoadInfo(load_t *loaddat) | |
457 | +{ | |
458 | + int usage = 0; | |
459 | + | |
460 | + usage = getCpuUsage(100); | |
461 | + | |
462 | + loaddat->load1m = usage; | |
463 | + loaddat->load5m = usage; | |
464 | + loaddat->load15m = usage; | |
465 | + | |
466 | + return 0; | |
467 | +} | |
468 | + | |
469 | +#else // Linux | |
470 | +/* | |
471 | + * GetMemInfo(mem_t *memdat) for Linux | |
472 | + * | |
473 | + * OUT memdat : memory data | |
474 | + * ret : 0 : success | |
475 | + * -1: error | |
476 | + */ | |
477 | +int GetMemInfo(mem_t *memdat) | |
478 | +{ | |
479 | + char buf[2048]; | |
480 | + char wk[2048]; | |
481 | + FILE *fp; | |
482 | + char *path = "/proc/meminfo"; | |
483 | + //ulong total, freem, shared, buffer, cache, swtotal, swfree; | |
484 | + | |
485 | + memset(memdat, 0, sizeof(mem_t)); | |
486 | + | |
487 | + //total = freem = shared = buffer = cache = swtotal = swfree = 0; | |
488 | + | |
489 | + if (!(fp = fopen(path, "r"))) { | |
490 | + sprintf(buf,"GetMem: fopen(%s) error errno=%d\n", path, errno); | |
491 | + printf(buf); | |
492 | + //send(fd, buf, strlen(buf), 0); | |
493 | + return -1; | |
494 | + } | |
495 | + while (fgets(buf, sizeof(buf), fp)) { | |
496 | + if (!strncmp(buf, "MemTotal:", strlen("MemTotal:"))) { | |
497 | + memdat->total = atoi(get_item(buf, " ", 2, wk)); /* 1 */ | |
498 | + | |
499 | + } else if (!strncmp(buf, "MemFree:", strlen("MemFree:"))) { | |
500 | + memdat->free = atoi(get_item(buf, " ", 2, wk)); /* 2 */ | |
501 | + | |
502 | + } else if (!strncmp(buf, "MemShared:", strlen("MemShared:"))) { | |
503 | + memdat->shared = atoi(get_item(buf, " ", 2, wk)); /* 3 */ | |
504 | + | |
505 | + } else if (!strncmp(buf, "Buffers:", strlen("Buffers:"))) { | |
506 | + memdat->buffers = atoi(get_item(buf, " ", 2, wk)); /* 4 */ | |
507 | + | |
508 | + } else if (!strncmp(buf, "Cached:", strlen("Cached:"))) { | |
509 | + memdat->cached = atoi(get_item(buf, " ", 2, wk)); /* 5 */ | |
510 | + | |
511 | + } else if (!strncmp(buf, "SwapTotal:", strlen("SwapTotal:"))) { | |
512 | + memdat->swap_total = atoi(get_item(buf, " ", 2, wk)); /* 6 */ | |
513 | + | |
514 | + } else if (!strncmp(buf, "SwapFree:", strlen("SwapFree:"))) { | |
515 | + memdat->swap_free = atoi(get_item(buf, " ", 2, wk)); /* 7 */ | |
516 | + } | |
517 | + } | |
518 | + fclose(fp); | |
519 | + | |
520 | + return 0; | |
521 | +} | |
522 | + | |
523 | +/* | |
524 | + * GetLoadInfo(mem_t *memdat) for Linux (現在使用せず /proc/loadavgから取得) | |
525 | + * | |
526 | + * OUT loaddat : load data | |
527 | + * ret : 0 : success | |
528 | + * -1: error | |
529 | + */ | |
530 | +int GetLoadInfo(load_t *loaddat) | |
531 | +{ | |
532 | + loaddat->load1m = 0; | |
533 | + loaddat->load5m = 0; | |
534 | + loaddat->load15m = 0; | |
535 | + | |
536 | + return 0; | |
537 | +} | |
538 | +#endif // Linux | |
539 | + | |
540 | +/* | |
541 | + * GetMem(fd) | |
542 | + * | |
543 | + * IN fd : output fd | |
544 | + * OUT ret : 0 success | |
545 | + * : -1 error | |
546 | + */ | |
547 | +int GetMem(int fd) | |
548 | +{ | |
549 | + char buf[2048]; | |
550 | + int ret; | |
551 | + mem_t memdat; | |
552 | + | |
553 | + memset(&memdat, 0, sizeof(mem_t)); | |
554 | + | |
555 | + ret = GetMemInfo(&memdat); // no error | |
556 | + if (ret < 0) return ret; | |
557 | + | |
558 | +#ifdef _WIN32 | |
559 | + sprintf(buf, "%I64u %I64u %I64u %I64u %I64u %I64u %I64u\n\n", | |
560 | + memdat.total, | |
561 | + memdat.free, | |
562 | + memdat.shared, | |
563 | + memdat.buffers, | |
564 | + memdat.cached, | |
565 | + memdat.swap_total, | |
566 | + memdat.swap_free); | |
567 | +#else // Linux | |
568 | + sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n\n", | |
569 | + memdat.total, | |
570 | + memdat.free, | |
571 | + memdat.shared, | |
572 | + memdat.buffers, | |
573 | + memdat.cached, | |
574 | + memdat.swap_total, | |
575 | + memdat.swap_free); | |
576 | +#endif // Linux | |
577 | + | |
578 | + if (vf) printf("%s", buf); | |
579 | + | |
580 | + return send(fd, buf, strlen(buf), 0); | |
581 | +} | |
582 | + | |
583 | +/* | |
584 | + * GetLoad(fd) | |
585 | + * | |
586 | + * IN fd : output fd | |
587 | + * OUT ret : 0 success | |
588 | + * : -1 error | |
589 | + */ | |
590 | +int GetLoad(int fd) | |
591 | +{ | |
592 | + char buf[2048]; | |
593 | + | |
594 | +#ifdef _WIN32 | |
595 | + load_t loaddat; | |
596 | + | |
597 | + memset(&loaddat, 0, sizeof(load_t)); | |
598 | + | |
599 | + GetLoadInfo(&loaddat); | |
600 | + | |
601 | + sprintf(buf, "%1.2f %1.2f %1.2f\n\n", | |
602 | + ((float)loaddat.load1m)/100, | |
603 | + ((float)loaddat.load5m)/100, | |
604 | + ((float)loaddat.load15m)/100); | |
605 | + | |
606 | +#else // Linux | |
607 | + FILE *fp; | |
608 | + char *path = "/proc/loadavg"; | |
609 | + char *pt; | |
610 | + | |
611 | + if (!(fp = fopen(path, "r"))) { | |
612 | + sprintf(buf,"GetLoad: fopen(%s) error errno=%d\n", path, errno); | |
613 | + printf(buf); | |
614 | + send(fd, buf, strlen(buf), 0); | |
615 | + return -1; | |
616 | + } | |
617 | + if (fgets(buf,sizeof(buf),fp) == NULL) { | |
618 | + sprintf(buf,"GetLoad: fgets(%s) error errno=%d\n", path, errno); | |
619 | + printf(buf); | |
620 | + send(fd, buf, strlen(buf), 0); | |
621 | + fclose(fp); | |
622 | + return -1; | |
623 | + } | |
624 | + fclose(fp); | |
625 | + | |
626 | + /* "0.00 0.00 0.00 2/42 494" => "0.00 0.00 0.00" */ | |
627 | + pt = strchr(buf, ' '); | |
628 | + ++pt; | |
629 | + pt = strchr(pt, ' '); | |
630 | + ++pt; | |
631 | + pt = strchr(pt, ' '); | |
632 | + *pt = '\n'; | |
633 | + ++pt; | |
634 | + *pt = '\n'; | |
635 | + ++pt; | |
636 | + *pt = '\0'; | |
637 | +#endif // Linux | |
638 | + | |
639 | + if (vf) printf("%s", buf); | |
640 | + | |
641 | + return send(fd, buf, strlen(buf), 0); | |
642 | +} | |
643 | + | |
644 | +/* | |
645 | + * ErrorMsg(fd,str) | |
646 | + * send error message | |
647 | + * | |
648 | + * IN fd : output fd | |
649 | + * str : request string | |
650 | + * OUT ret : 0 success | |
651 | + * : -1 error | |
652 | + */ | |
653 | +int ErrorMsg(int fd, char *str) | |
654 | +{ | |
655 | + char buf[2048]; | |
656 | + | |
657 | + sprintf(buf,"%s request could not understand.\n",str); | |
658 | + | |
659 | + if (vf) printf("%s", buf); | |
660 | + | |
661 | + return send(fd,buf,strlen(buf), 0) ; | |
662 | +} | |
663 | + | |
664 | +/* | |
665 | + * メッセージ受信と処理 | |
666 | + * | |
667 | + * IN sfd : accept fd | |
668 | + * OUT ret : 0 success | |
669 | + * : -1 error | |
670 | + */ | |
671 | +int GetAndProc(int sfd) | |
672 | +{ | |
673 | + char buf[SIZE]; | |
674 | + char sndbuf[SIZE]; | |
675 | + int size; | |
676 | + int st = 0; | |
677 | + | |
678 | + /* get request */ | |
679 | + if (vf) printf("recv...\n"); | |
680 | + size = recv(sfd, buf, SIZE, 0); | |
681 | + if (size < 0) { | |
682 | + perror("read"); | |
683 | + return -1; | |
684 | + } | |
685 | + | |
686 | + /* delete CR/LF */ | |
687 | + if (buf[size-2] == 0x0d && buf[size-1] == 0x0a) { | |
688 | + buf[size-2] = '\0'; | |
689 | + } else if (buf[size-1] == 0x0a) { | |
690 | + buf[size-1] = '\0'; | |
691 | + } else { | |
692 | + buf[size] = '\0'; | |
693 | + } | |
694 | + | |
695 | + /* dispatch request process */ | |
696 | + if (!strncasecmp(buf,HELP,strlen(HELP))) { | |
697 | + /* HELP */ | |
698 | + if (vf) printf("HELP\n"); | |
699 | + st = Help(sfd); | |
700 | + } else if (!strncasecmp(buf, EXIT, strlen(EXIT))) { | |
701 | + /* EXIT */ | |
702 | + if (vf) printf("exit.\n"); | |
703 | + st = -1; | |
704 | + } else if (!strncasecmp(buf, GET_MEM, strlen(GET_MEM))) { | |
705 | + /* GET_MEM */ | |
706 | + if (vf) printf("GET_MEM\n"); | |
707 | + st = GetMem(sfd); | |
708 | + } else if (!strncasecmp(buf, GET_LOAD, strlen(GET_LOAD))) { | |
709 | + /* GET_LOAD */ | |
710 | + if (vf) printf("GET_LOAD\n"); | |
711 | + st = GetLoad(sfd); | |
712 | + } else { | |
713 | + st = ErrorMsg(sfd,buf); | |
714 | + } | |
715 | + return st; | |
716 | +} | |
717 | + | |
718 | +#ifdef _WIN32 | |
719 | +int ThreadGetAndProc(void *parm) | |
720 | +{ | |
721 | + int ret; | |
722 | + int newsockfd = (int)parm; | |
723 | + | |
724 | +#ifdef KEEP_CONN | |
725 | + do { | |
726 | +#endif | |
727 | + if (vf) printf("Start GetAndProc...\n"); | |
728 | + ret = GetAndProc(newsockfd); | |
729 | +#ifdef KEEP_CONN | |
730 | + } while (ret >= 0); // while not "exit" command | |
731 | +#endif | |
732 | + | |
733 | + if (vf) printf("close socket...\n"); | |
734 | + closesocket(newsockfd); | |
735 | + | |
736 | + if (vf) printf("Thread End.\n"); | |
737 | + return 0; | |
738 | +} | |
739 | +#endif // _WIN32 | |
740 | + | |
741 | + | |
742 | +int main(int a, char *b[]) | |
743 | +{ | |
744 | + int newsockfd; | |
745 | + int clilen,childpid; | |
746 | + struct sockaddr_in cli_addr,serv_addr; | |
747 | + int fd; | |
748 | + char *sendfile; | |
749 | + int con = 0; | |
750 | + int i; | |
751 | + int port = 0; /* port number */ | |
752 | + int ret = 0; | |
753 | + | |
754 | +#ifdef NO_BLOCK_ACCEPT | |
755 | + fd_set rfds; /* V1.12-A */ | |
756 | + struct timeval tmout; /* V1.12-A */ | |
757 | +#endif // NO_BLOCK_ACCEPT | |
758 | + | |
759 | +#ifdef _WIN32 | |
760 | + WSADATA WsaData; | |
761 | + HANDLE thd; /* thread handle */ | |
762 | + DWORD threadID = 0; /* thread ID */ | |
763 | + | |
764 | + SetConsoleCtrlHandler(CtrlProc, TRUE); | |
765 | +#endif | |
766 | + | |
767 | + /* pars args */ | |
768 | + for (i = 1; i<a; i++) { | |
769 | + if (!strncmp(b[i],"-h",2)) { | |
770 | + printf("usage : sysinfd [-v] [<portnum>]\n"); | |
771 | + exit(1); | |
772 | + } | |
773 | + if (!strncmp(b[i],"-v",2)) { | |
774 | + ++vf; | |
775 | + continue; | |
776 | + } | |
777 | + port = atoi(b[i]); | |
778 | + } | |
779 | + | |
780 | + if (!port) { | |
781 | + port = TCP_PORT; | |
782 | + } | |
783 | + | |
784 | +#ifdef _WIN32 | |
785 | + if (ret = WSAStartup(0x0101, &WsaData)) { | |
786 | + printf("WSAStartup Error: code = %d\n", ret); | |
787 | + exit(1); | |
788 | + } | |
789 | +#endif // _WIN32 | |
790 | + | |
791 | + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
792 | + perror("socket"); | |
793 | + exit(-1); | |
794 | + } | |
795 | + | |
796 | + memset((char *)&serv_addr, 0, sizeof(serv_addr)); | |
797 | + serv_addr.sin_family = AF_INET; | |
798 | + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
799 | + serv_addr.sin_port = htons((short)port); | |
800 | + | |
801 | + if (vf) printf("wait on port (%d)\n", port); | |
802 | + | |
803 | + if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ | |
804 | + perror("bind"); | |
805 | + exit(-1); | |
806 | + } | |
807 | + | |
808 | +#ifndef _WIN32 | |
809 | + signal(SIGCLD, reapchild); | |
810 | + signal(SIGINT, sigint); | |
811 | +#endif | |
812 | + | |
813 | + if (vf) printf("listen...\n"); | |
814 | + if(listen(sockfd, 5) < 0){ | |
815 | + perror("listen"); | |
816 | + exit(-1); | |
817 | + } | |
818 | + | |
819 | + while (1) { | |
820 | +#ifdef NO_BLOCK_ACCEPT | |
821 | + // V1.12-A start | |
822 | + if (vf) printf("check incoming...\n"); | |
823 | + tmout.tv_sec = 0; // timeout sec | |
824 | + tmout.tv_usec = 200*1000; // timeout usec (0.2sec) | |
825 | + if (vf >= 2) { | |
826 | + tmout.tv_sec = 1; // timeout sec | |
827 | + tmout.tv_usec = 0; // timeout usec (1.0sec) | |
828 | + } | |
829 | + while (1) { | |
830 | + FD_ZERO(&rfds); | |
831 | + FD_SET(sockfd, &rfds); | |
832 | + ret = select(0, &rfds, 0, 0, &tmout); | |
833 | + if (vf >= 2) printf("select return %d\n", ret); | |
834 | + if (ret > 0) break; // timeout:0 incoming:>0 | |
835 | + } | |
836 | + // V1.12-A end | |
837 | +#endif // NO_BLOCK_ACCEPT | |
838 | + | |
839 | + if (vf) printf("Start accept...\n"); | |
840 | + clilen = sizeof(cli_addr); | |
841 | + newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, | |
842 | + &clilen); | |
843 | + if(newsockfd < 0){ | |
844 | + if (errno == EINTR) { | |
845 | + if (vf) printf("accept interrupted\n"); | |
846 | + continue; | |
847 | + } | |
848 | + perror("accept"); | |
849 | + exit(-1); | |
850 | + } | |
851 | + | |
852 | +#ifdef _WIN32 | |
853 | + //procarg_t procarg; | |
854 | + | |
855 | + //procarg.clsockfd = newsockfd; | |
856 | + //procarg.con = con; | |
857 | + | |
858 | + if (vf) printf("Create New Thread. (%d)\n", con); | |
859 | + | |
860 | + thd = CreateThread( | |
861 | + NULL, | |
862 | + 0, | |
863 | + (LPTHREAD_START_ROUTINE)ThreadGetAndProc, | |
864 | + (void *)newsockfd, | |
865 | + 0, | |
866 | + &threadID); | |
867 | + if (thd == NULL) { | |
868 | + printf("CreateThread Error (%d)\n", GetLastError()); | |
869 | + return -1; | |
870 | + } | |
871 | + | |
872 | +#else // Linux | |
873 | + if (fork() == 0) { | |
874 | + /* this is child process */ | |
875 | + close(sockfd); /* close server socket */ | |
876 | + if (vf) printf("accept %d pid=%d\n", con, getpid()); | |
877 | + | |
878 | + /* サーバ処理 */ | |
879 | +#ifdef KEEP_CONN | |
880 | + while (1) { | |
881 | + /* もらったデータに対する処理 */ | |
882 | + if (GetAndProc(newsockfd) < 0) { | |
883 | + if (vf) printf("Connection closed.\n"); | |
884 | + close(newsockfd); /* close accept sockfd V1.02-A */ | |
885 | + exit(0); | |
886 | + } | |
887 | + } | |
888 | +#else // KEEP_CONN | |
889 | + GetAndProc(newsockfd); /* もらったデータに対する処理 */ | |
890 | + close(newsockfd); /* close accept sockfd V1.02-A */ | |
891 | + exit(0); | |
892 | +#endif // !KEEP_CONN | |
893 | + } | |
894 | + close(newsockfd); /* close accept sockfd */ | |
895 | +#endif // Linux | |
896 | + | |
897 | + con++; | |
898 | + } // accept while loop | |
899 | +} | |
900 | + | |
901 | +/* vim:ts=8:sw=4: | |
902 | + */ | |
903 | + |
@@ -0,0 +1,1360 @@ | ||
1 | +/* | |
2 | + * sysinfdsrv.c | |
3 | + * System Information Service Daemon | |
4 | + * | |
5 | + * 2001/12/03 V1.00 by oga. (MEM, LOAD) | |
6 | + * 2002/02/03 V1.01 Keep Connection | |
7 | + * 2013/02/25 V1.02 add socket close() | |
8 | + * 2013/11/21 V1.10 support Win | |
9 | + * 2013/12/01 V1.20 support Win Service (sysinfdsrv) | |
10 | + * 2013/12/06 V1.21 fix minus load for win. | |
11 | + * 2013/12/06 V1.22 add non block accept code | |
12 | + * | |
13 | + * Protocol | |
14 | + * HELP | |
15 | + * Return Help String\n\n | |
16 | + * | |
17 | + * GET_MEM | |
18 | + * Get Memory/Swap Info (KB) | |
19 | + * Total Free Shared Buffers Cached SwapTotal SwapFree | |
20 | + * --------------------------------------------------------- | |
21 | + * 69860 1520 4 3008 57292 51400 27728\n\n | |
22 | + * | |
23 | + * GET_LOAD | |
24 | + * Get Load Ave. Info | |
25 | + * 1min 5min 15min | |
26 | + * --------------------------------- | |
27 | + * 0.00 0.00 0.00\n\n | |
28 | + * | |
29 | + * All Rights Reserved. Copyright (C) 2001,2002, Moritaka Ogasawara. | |
30 | + */ | |
31 | + | |
32 | +#include <stdio.h> | |
33 | +#include <errno.h> | |
34 | +#include <string.h> | |
35 | +#include <sys/types.h> | |
36 | +#include <sys/stat.h> | |
37 | +#include <time.h> | |
38 | +#ifdef _WIN32 | |
39 | +#include <windows.h> | |
40 | +#include <winsock.h> | |
41 | +#else // Linux | |
42 | +#include <sys/socket.h> | |
43 | +#include <netinet/in.h> | |
44 | +#include <arpa/inet.h> | |
45 | +#include <fcntl.h> | |
46 | +#include <signal.h> | |
47 | +#endif // Linux | |
48 | + | |
49 | + | |
50 | +/* Keep Connection V1.01-A */ | |
51 | +#define KEEP_CONN | |
52 | +#define GET_LOG // ログ取得時指定 | |
53 | + | |
54 | +#ifndef TCP_PORT | |
55 | +#define TCP_PORT 9998 | |
56 | +#endif | |
57 | + | |
58 | +#define SIZE 1024 | |
59 | + | |
60 | +#ifndef SIGCLD | |
61 | +#define SIGCLD SIGCHLD | |
62 | +#endif | |
63 | + | |
64 | +/* request keywords */ | |
65 | +#define GET_MEM "GET_MEM" | |
66 | +#define GET_LOAD "GET_LOAD" | |
67 | +#define LOGIN "LOGIN" | |
68 | +#define HELP "HELP" | |
69 | +#define EXIT "EXIT" | |
70 | + | |
71 | +/* for help */ | |
72 | +#define HELP_STR "GET_MEM GET_LOAD HELP EXIT\n\n" | |
73 | + | |
74 | +/* 仮のdefine */ | |
75 | + | |
76 | +#ifdef _WIN32 | |
77 | +#define strncasecmp strnicmp | |
78 | +#define CLOSE closesocket | |
79 | + | |
80 | +#define OPT_REG 1 | |
81 | +#define OPT_DEL 2 | |
82 | + | |
83 | +#define EXENAME "sysinfdsrv.exe" | |
84 | +#define EXENAME_NOEXT "sysinfdsrv" | |
85 | +#define SERVICE_NAME "Sysinfd" | |
86 | +#define SERVICE_DISPNAME "Sysinfd" | |
87 | +//#define SERVICE_DISPNAME "System Information Servcie" | |
88 | + | |
89 | +// 64bit | |
90 | +typedef DWORDLONG mem_uint; | |
91 | + | |
92 | +//typedef struct _procarg_t { | |
93 | +// int clsockfd; /* client socket */ | |
94 | +// int con; /* start number */ | |
95 | +//} procarg_t; | |
96 | + | |
97 | +// | |
98 | +// for getCpuUsage() | |
99 | +// | |
100 | +#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart)) | |
101 | + | |
102 | +typedef struct | |
103 | +{ | |
104 | + DWORD dwUnknown1; | |
105 | + ULONG uKeMaximumIncrement; | |
106 | + ULONG uPageSize; | |
107 | + ULONG uMmNumberOfPhysicalPages; | |
108 | + ULONG uMmLowestPhysicalPage; | |
109 | + ULONG uMmHighestPhysicalPage; | |
110 | + ULONG uAllocationGranularity; | |
111 | + PVOID pLowestUserAddress; | |
112 | + PVOID pMmHighestUserAddress; | |
113 | + ULONG uKeActiveProcessors; | |
114 | + BYTE bKeNumberProcessors; | |
115 | + BYTE bUnknown2; | |
116 | + WORD wUnknown3; | |
117 | +} SYSTEM_BASIC_INFORMATION; | |
118 | + | |
119 | +typedef struct | |
120 | +{ | |
121 | + LARGE_INTEGER liIdleTime; | |
122 | + DWORD dwSpare[76]; | |
123 | +} SYSTEM_PERFORMANCE_INFORMATION; | |
124 | + | |
125 | +typedef struct | |
126 | +{ | |
127 | + LARGE_INTEGER liKeBootTime; | |
128 | + LARGE_INTEGER liKeSystemTime; | |
129 | + LARGE_INTEGER liExpTimeZoneBias; | |
130 | + ULONG uCurrentTimeZoneId; | |
131 | + DWORD dwReserved; | |
132 | +} SYSTEM_TIME_INFORMATION; | |
133 | + | |
134 | +typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG); | |
135 | + | |
136 | +PROCNTQSI NtQuerySystemInformation; | |
137 | + | |
138 | + | |
139 | +#else // Linux | |
140 | +#define HANDLE int | |
141 | +#define CLOSE close | |
142 | + | |
143 | +// 64bit | |
144 | +typedef unsigned long long int mem_uint; | |
145 | +#endif // Linux | |
146 | + | |
147 | +typedef struct _mem_t { | |
148 | + mem_uint total; | |
149 | + mem_uint free; | |
150 | + mem_uint shared; | |
151 | + mem_uint buffers; | |
152 | + mem_uint cached; | |
153 | + mem_uint swap_total; | |
154 | + mem_uint swap_free; | |
155 | +} mem_t; | |
156 | + | |
157 | +typedef struct _load_t { | |
158 | + int load1m; // load ave. 1min x100 1.23→123 | |
159 | + int load5m; // load ave. 5min x100 | |
160 | + int load15m; // load ave. 15min x100 | |
161 | +} load_t; | |
162 | + | |
163 | + | |
164 | +/* globals */ | |
165 | +int vf = 0; /* 1:verbose mode */ | |
166 | +int port = 0; /* port number */ | |
167 | +int sockfd = 0; | |
168 | +int proc_stop = 0; /* stop request */ | |
169 | + | |
170 | +/* func for windows porting */ | |
171 | +#ifdef _WIN32 | |
172 | +SERVICE_STATUS SvcStatus; | |
173 | +SERVICE_STATUS_HANDLE hSvcStatus; | |
174 | + | |
175 | +HANDLE hThreadMain; | |
176 | +HANDLE hStopEvent; | |
177 | +#endif // _WIN32 | |
178 | + | |
179 | +void Log(char *msg) | |
180 | +{ | |
181 | + FILE *logfp; | |
182 | + char *logpath; | |
183 | + char datetime[128]; | |
184 | + time_t tt; | |
185 | + | |
186 | +#ifdef _WIN32 | |
187 | + logpath = "c:\\sysinfdsrv.log"; | |
188 | +#else // Linux | |
189 | + logpath = "/tmp/sysinfdsrv.log"; | |
190 | +#endif // Linux | |
191 | + | |
192 | + printf("%s\n", msg); | |
193 | + | |
194 | +#ifdef GET_LOG | |
195 | + if ((logfp = fopen(logpath, "a")) == NULL) { | |
196 | + printf("log open error. errno = %d\n", errno); | |
197 | + return; | |
198 | + } | |
199 | + | |
200 | + tt = time(0); | |
201 | + strftime(datetime, sizeof(datetime), "%Y/%m/%d %H:%M:%S", localtime(&tt)); | |
202 | + fprintf(logfp, "%s %s\n", datetime, msg); | |
203 | + | |
204 | + fclose(logfp); | |
205 | +#endif // GET_LOG | |
206 | +} | |
207 | + | |
208 | +#ifdef _WIN32 | |
209 | +/* | |
210 | + * READ(fd,buf,size) | |
211 | + * | |
212 | + * OUT ret : >= 0 success (read size) | |
213 | + * : -1 error | |
214 | + */ | |
215 | +int READ(HANDLE fd, void *buf, int count) | |
216 | +{ | |
217 | + int rbytes = 0; | |
218 | + BOOL ret; | |
219 | + | |
220 | + ret = ReadFile( | |
221 | + fd, // ファイルのハンドル | |
222 | + buf, // データバッファ | |
223 | + count, // 読み取り対象のバイト数 | |
224 | + &rbytes, // 読み取ったバイト数 | |
225 | + NULL // オーバーラップ構造体のバッファ | |
226 | + ); | |
227 | + if (ret == FALSE) { | |
228 | + return -1; | |
229 | + } | |
230 | + return rbytes; | |
231 | +} | |
232 | + | |
233 | + | |
234 | +/* | |
235 | + * WRITE(fd,buf,size) | |
236 | + * | |
237 | + * OUT ret : >= 0 success (write size) | |
238 | + * : -1 error | |
239 | + */ | |
240 | +int WRITE(HANDLE fd, void *buf, int count) | |
241 | +{ | |
242 | + int wbytes = 0; | |
243 | + BOOL ret; | |
244 | + | |
245 | + ret = WriteFile( | |
246 | + fd, // ファイルのハンドル | |
247 | + buf, // データバッファ | |
248 | + count, // 書き込み対象のバイト数 | |
249 | + &wbytes, // 書き込んだバイト数 | |
250 | + NULL // オーバーラップ構造体のバッファ | |
251 | + ); | |
252 | + if (ret == FALSE) { | |
253 | + return -1; | |
254 | + } | |
255 | + return wbytes; | |
256 | +} | |
257 | + | |
258 | +// ntdll!NtQuerySystemInformation (NT specific!) | |
259 | +// | |
260 | +// The function copies the system information of the | |
261 | +// specified type into a buffer | |
262 | +// | |
263 | +// NTSYSAPI | |
264 | +// NTSTATUS | |
265 | +// NTAPI | |
266 | +// NtQuerySystemInformation( | |
267 | +// IN UINT SystemInformationClass, // information type | |
268 | +// OUT PVOID SystemInformation, // pointer to buffer | |
269 | +// IN ULONG SystemInformationLength, // buffer size in bytes | |
270 | +// OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit | |
271 | +// // variable that receives | |
272 | +// // the number of bytes | |
273 | +// // written to the buffer | |
274 | +// ); | |
275 | + | |
276 | +#define SystemBasicInformation 0 | |
277 | +#define SystemPerformanceInformation 2 | |
278 | +#define SystemTimeInformation 3 | |
279 | + | |
280 | +/* | |
281 | + * getCpuUsage() | |
282 | + * | |
283 | + * IN interval: 利用率を計測するための間隔(msec) | |
284 | + * OUT ret: CPU利用率 (%) | |
285 | + */ | |
286 | +int getCpuUsage(int interval) | |
287 | +{ | |
288 | + SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo; | |
289 | + SYSTEM_TIME_INFORMATION SysTimeInfo; | |
290 | + SYSTEM_BASIC_INFORMATION SysBaseInfo; | |
291 | + double dbIdleTime; | |
292 | + double dbSystemTime; | |
293 | + LONG status; | |
294 | + LARGE_INTEGER liOldIdleTime = {0,0}; | |
295 | + LARGE_INTEGER liOldSystemTime = {0,0}; | |
296 | + | |
297 | + int iCtl=0, percentage=0; | |
298 | + | |
299 | + NtQuerySystemInformation = (PROCNTQSI)GetProcAddress( | |
300 | + GetModuleHandle("ntdll"), | |
301 | + "NtQuerySystemInformation" | |
302 | + ); | |
303 | + if (!NtQuerySystemInformation) { | |
304 | + Log("GetProcAddress(NtQuerySystemInformation) error."); | |
305 | + return -1; | |
306 | + } | |
307 | + | |
308 | + // get number of processors in the system | |
309 | + status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL); | |
310 | + if(status != NO_ERROR) { | |
311 | + Log("NtQuerySystemInformation1(SystemBasicInformation) error."); | |
312 | + return -1; | |
313 | + } | |
314 | + | |
315 | + //printf("\n getting CPU Usage\n"); | |
316 | + //while(!_kbhit()) | |
317 | + | |
318 | + while(1) { | |
319 | + // get new system time | |
320 | + status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0); | |
321 | + if(status!=NO_ERROR) { | |
322 | + Log("NtQuerySystemInformation2(SystemTimeInformation) error."); | |
323 | + return -1; | |
324 | + } | |
325 | + | |
326 | + // get new CPU's idle time | |
327 | + status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL); | |
328 | + if(status != NO_ERROR) { | |
329 | + Log("NtQuerySystemInformation3(SystemPerformanceInformation) error."); | |
330 | + return -1; | |
331 | + } | |
332 | + | |
333 | + // if it's a first call - skip it | |
334 | + if(liOldIdleTime.QuadPart != 0) { | |
335 | + // CurrentValue = NewValue - OldValue | |
336 | + dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime); | |
337 | + dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime); | |
338 | + | |
339 | + // CurrentCpuIdle = IdleTime / SystemTime | |
340 | + dbIdleTime = dbIdleTime / dbSystemTime; | |
341 | + | |
342 | + // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors | |
343 | + dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5; | |
344 | + | |
345 | + //+ 0.5, result is same in task manager | |
346 | + percentage= (UINT)(dbIdleTime); | |
347 | + if (percentage < 0) percentage = 0; // V1.21-A | |
348 | + | |
349 | + //printf("\b\b\b\b%3d%%", percentage); | |
350 | + if(iCtl > 0 ) return percentage; | |
351 | + } | |
352 | + | |
353 | + // store new CPU's idle and system time | |
354 | + liOldIdleTime = SysPerfInfo.liIdleTime; | |
355 | + liOldSystemTime = SysTimeInfo.liKeSystemTime; | |
356 | + | |
357 | + // wait one second | |
358 | + Sleep(interval); | |
359 | + iCtl++; | |
360 | + } | |
361 | + return 0; | |
362 | +} | |
363 | + | |
364 | +#endif // !_WIN32 | |
365 | + | |
366 | +/* wait child process */ | |
367 | +void reapchild() | |
368 | +{ | |
369 | +#ifndef _WIN32 | |
370 | + wait(0); | |
371 | + signal(SIGCLD,reapchild); | |
372 | +#endif | |
373 | +} | |
374 | + | |
375 | +/* end process */ | |
376 | +void sigint() | |
377 | +{ | |
378 | + Log("sysinfd interrupted.\n"); | |
379 | + if (sockfd) close(sockfd); | |
380 | + exit(1); | |
381 | +} | |
382 | + | |
383 | +#ifdef _WIN32 | |
384 | +/* | |
385 | + * dwCtrlType | |
386 | + * CTRL_C_EVENT : Ctrl+C | |
387 | + * CTRL_BREAK_EVENT : Ctrl+Break | |
388 | + * CTRL_CLOSE_EVENT : Close Console | |
389 | + * CTRL_LOGOFF_EVENT : Logoff | |
390 | + * CTRL_SHUTDOWN_EVENT : Shutdown | |
391 | + */ | |
392 | +BOOL WINAPI CtrlProc(DWORD dwCtrlType) | |
393 | +{ | |
394 | + // Stop all calse | |
395 | + Log("sysinfd interrupted2.\n"); | |
396 | + if (sockfd) closesocket(sockfd); | |
397 | + WSACleanup(); | |
398 | + | |
399 | + return FALSE; // FALSE: NextHandler(END) TRUE: not END | |
400 | +} | |
401 | + | |
402 | + | |
403 | +/* | |
404 | + * InstallService() | |
405 | + * サービス制御マネージャ・データベースにサービスを登録する。 | |
406 | + * | |
407 | + * IN exefile: サービスで実行するexeファイル(完全パス) | |
408 | + */ | |
409 | +void InstallService(SC_HANDLE hSCManager, char *exefile) | |
410 | +{ | |
411 | + SC_HANDLE hService; // サービス・ハンドル | |
412 | + | |
413 | + /* サービスの登録 */ | |
414 | + hService = CreateService( | |
415 | + hSCManager, // データベース・ハンドル | |
416 | + SERVICE_NAME, // サービス名 | |
417 | + SERVICE_DISPNAME, // サービス表示名 | |
418 | + SERVICE_ALL_ACCESS, // 全アクセスの許可 | |
419 | + SERVICE_WIN32_OWN_PROCESS, // 独立の Win32 サービス・プロセス | |
420 | + SERVICE_DEMAND_START, // 起動要求時に開始 | |
421 | + SERVICE_ERROR_NORMAL, // 起動失敗時にメッセージを表示 | |
422 | + exefile, // 実行ファイル名 | |
423 | + NULL, // ロード順序グループなし | |
424 | + NULL, // タグは不要 | |
425 | + NULL, // 依存グループなし | |
426 | + NULL, // LocalSystem アカウントを使用 | |
427 | + NULL); // パスワードなし | |
428 | + | |
429 | + /* 後処理 */ | |
430 | + if (hService == NULL) { | |
431 | + printf("CreateService(%ld) error.\n", GetLastError()); | |
432 | + } else { | |
433 | + printf("Service [%s] registed.\n", SERVICE_NAME); | |
434 | + CloseServiceHandle(hService); | |
435 | + } | |
436 | +} | |
437 | + | |
438 | +/* | |
439 | + * RemoveServcie() | |
440 | + * サービス制御マネージャ・データベースからサービスを削除する。 | |
441 | + */ | |
442 | +void RemoveService(SC_HANDLE hSCManager) | |
443 | +{ | |
444 | + SC_HANDLE hService; // サービス・ハンドル | |
445 | + | |
446 | + /* サービスのオープン */ | |
447 | + hService = OpenService( | |
448 | + hSCManager, // データベース・ハンドル | |
449 | + SERVICE_NAME, // サービス名 | |
450 | + SERVICE_ALL_ACCESS); // 全アクセス・タイプを許可 | |
451 | + | |
452 | + if (hService == NULL) { | |
453 | + printf ("OpenService(%ld) error.\n", GetLastError()); | |
454 | + return; | |
455 | + } | |
456 | + | |
457 | + /* サービスの削除 */ | |
458 | + if (! DeleteService(hService)) { | |
459 | + printf ("DeleteService(%ld) error.\n", GetLastError()); | |
460 | + } else { | |
461 | + printf ("Service [%s] deleted.\n", SERVICE_NAME); | |
462 | + } | |
463 | + | |
464 | + /* サービス・ハンドルをクローズ */ | |
465 | + CloseServiceHandle(hService); | |
466 | +} | |
467 | + | |
468 | +/* | |
469 | + * RegDelSerivce() | |
470 | + * サービスの登録/削除 | |
471 | + * | |
472 | + * IN opt : OPT_REG:サービス登録 OPT_DEL:サービス削除 | |
473 | + * IN path: 登録するexeのパス (NULLの場合、カレントディレクトリ\自exe) | |
474 | + * OUT ret : 0: success <>0: failed | |
475 | + */ | |
476 | +int RegDelService(int opt, char *path) | |
477 | +{ | |
478 | + | |
479 | + DWORD Operation = 0; // 処理指定子 | |
480 | + DWORD errNum = 0; | |
481 | + char exepath[4096]; | |
482 | + struct stat stbuf; | |
483 | + SC_HANDLE hSCManager; | |
484 | + | |
485 | + | |
486 | + if (path) { | |
487 | + strcpy(exepath, path); | |
488 | + } else { | |
489 | + getcwd(exepath, sizeof(exepath)-1); | |
490 | + strcat(exepath, "\\"); | |
491 | + strcat(exepath, EXENAME); | |
492 | + if (vf) printf("exepath = [%s]\n", exepath); | |
493 | + } | |
494 | + | |
495 | + if (stat(exepath, &stbuf)) { | |
496 | + printf("file not found. (%s)\n", exepath); | |
497 | + return -1; | |
498 | + } | |
499 | + | |
500 | + | |
501 | + // サービス制御マネージャ・データベースをオープン | |
502 | + hSCManager = OpenSCManager( | |
503 | + NULL, // ローカル・システム | |
504 | + NULL, // デフォルトのデータベース | |
505 | + SC_MANAGER_ALL_ACCESS); // 全アクセス・タイプを許可 | |
506 | + if (hSCManager == (SC_HANDLE)NULL) { | |
507 | + errNum = GetLastError(); | |
508 | + printf("OpenSCManager(%ld) error.\n", errNum); | |
509 | + return errNum; | |
510 | + } | |
511 | + | |
512 | + switch (opt) { | |
513 | + case OPT_REG: | |
514 | + InstallService(hSCManager, exepath); | |
515 | + break; | |
516 | + case OPT_DEL: | |
517 | + RemoveService(hSCManager); | |
518 | + break; | |
519 | + default: | |
520 | + printf("RegDelSerivce: unknown option [%d]\n", opt); | |
521 | + break; | |
522 | + } | |
523 | + | |
524 | + /* データベースをクローズ */ | |
525 | + CloseServiceHandle(hSCManager); | |
526 | + | |
527 | + return 0; | |
528 | +} | |
529 | +#endif // _WIN32 | |
530 | + | |
531 | + | |
532 | +char *get_item(char *buf, char *sep, int pos, char *outbuf) | |
533 | +{ | |
534 | + int i; | |
535 | + char *pt; | |
536 | + char *p; | |
537 | + char wk[4096]; | |
538 | + char msglog[4096]; | |
539 | + | |
540 | + strcpy(wk,buf); /* strtok()はbufを破壊するためコピーして利用する */ | |
541 | + | |
542 | + for (i = 0; i<pos; i++) { | |
543 | + if (i == 0) { | |
544 | + pt = (char *)strtok(wk,sep); | |
545 | + } else { | |
546 | + pt = (char *)strtok(NULL,sep); | |
547 | + } | |
548 | + if (pt == NULL) break; | |
549 | + } | |
550 | + if (pt == NULL) { | |
551 | + sprintf(msglog, "Out of item(%s) pos(%d).",buf,pos); | |
552 | + Log(msglog); | |
553 | + /* 結果領域クリア */ | |
554 | + strcpy(outbuf,""); | |
555 | + return NULL; | |
556 | + } | |
557 | + | |
558 | + strcpy(outbuf, pt); | |
559 | + | |
560 | + /* cut tail space */ | |
561 | + p = &outbuf[strlen(outbuf)-1]; /* last char */ | |
562 | + while (*p == ' ' || *p == 0x0a) --p; | |
563 | + *(p+1) = '\0'; | |
564 | + | |
565 | + return outbuf; | |
566 | +} /* get_item */ | |
567 | + | |
568 | + | |
569 | +/* | |
570 | + * fd から読み込んだ内容をnewsockfdに書き込む | |
571 | + * | |
572 | + * not used | |
573 | + */ | |
574 | +void PUT_DATA(int fd, int newsockfd) | |
575 | +{ | |
576 | + char c[4096]; | |
577 | + int size; | |
578 | + while((size = recv(fd, c, SIZE, 0)) != 0) { | |
579 | + send(newsockfd, c, size, 0); | |
580 | + } | |
581 | +} | |
582 | + | |
583 | + | |
584 | +/* | |
585 | + * Help(fd) | |
586 | + * send HELP | |
587 | + * | |
588 | + * IN fd : output fd | |
589 | + * OUT ret : 0 success | |
590 | + * : -1 error | |
591 | + */ | |
592 | +int Help(int fd) | |
593 | +{ | |
594 | + if (vf) printf("%s", HELP_STR); | |
595 | + send(fd, HELP_STR, strlen(HELP_STR), 0); | |
596 | + return 0; | |
597 | +} | |
598 | + | |
599 | +#ifdef _WIN32 | |
600 | +/* | |
601 | + * GetMemInfo(mem_t *memdat) for Win | |
602 | + * | |
603 | + * OUT memdat : memory data | |
604 | + * ret : 0 : success | |
605 | + * -1: error | |
606 | + */ | |
607 | +int GetMemInfo(mem_t *memdat) | |
608 | +{ | |
609 | + MEMORYSTATUSEX mst; // WDF11502-C | |
610 | + int i; | |
611 | + int ret; | |
612 | + int loadavg; | |
613 | + | |
614 | + mst.dwLength = sizeof(MEMORYSTATUSEX); // WDF11502-A | |
615 | + | |
616 | + // メモリ情報取得 | |
617 | + // upper 4GB | |
618 | + GlobalMemoryStatusEx(&mst); | |
619 | + memdat->total = mst.ullTotalPhys/1024; | |
620 | + memdat->free = mst.ullAvailPhys/1024; | |
621 | + memdat->shared = 0; | |
622 | + memdat->buffers = 0; | |
623 | + memdat->cached = 0; | |
624 | + memdat->swap_total = mst.ullTotalPageFile/1024; | |
625 | + memdat->swap_free = mst.ullAvailPageFile/1024; | |
626 | + | |
627 | + if (vf) { | |
628 | + printf("cur_mem=%I64u\n", | |
629 | + memdat->total - memdat->free - memdat->buffers - memdat->cached); | |
630 | + } | |
631 | + | |
632 | + return 0; | |
633 | +} | |
634 | + | |
635 | +/* | |
636 | + * GetLoadInfo(mem_t *memdat) for Win | |
637 | + * | |
638 | + * OUT loaddat : load data | |
639 | + * ret : 0 : success | |
640 | + * -1: error | |
641 | + */ | |
642 | +int GetLoadInfo(load_t *loaddat) | |
643 | +{ | |
644 | + int usage = 0; | |
645 | + | |
646 | + usage = getCpuUsage(100); | |
647 | + | |
648 | + loaddat->load1m = usage; | |
649 | + loaddat->load5m = usage; | |
650 | + loaddat->load15m = usage; | |
651 | + | |
652 | + return 0; | |
653 | +} | |
654 | + | |
655 | +#else // Linux | |
656 | +/* | |
657 | + * GetMemInfo(mem_t *memdat) for Linux | |
658 | + * | |
659 | + * OUT memdat : memory data | |
660 | + * ret : 0 : success | |
661 | + * -1: error | |
662 | + */ | |
663 | +int GetMemInfo(mem_t *memdat) | |
664 | +{ | |
665 | + char buf[2048]; | |
666 | + char wk[2048]; | |
667 | + FILE *fp; | |
668 | + char *path = "/proc/meminfo"; | |
669 | + //ulong total, freem, shared, buffer, cache, swtotal, swfree; | |
670 | + | |
671 | + memset(memdat, 0, sizeof(mem_t)); | |
672 | + | |
673 | + //total = freem = shared = buffer = cache = swtotal = swfree = 0; | |
674 | + | |
675 | + if (!(fp = fopen(path, "r"))) { | |
676 | + sprintf(buf,"GetMem: fopen(%s) error errno=%d\n", path, errno); | |
677 | + printf(buf); | |
678 | + //send(fd, buf, strlen(buf), 0); | |
679 | + return -1; | |
680 | + } | |
681 | + while (fgets(buf, sizeof(buf), fp)) { | |
682 | + if (!strncmp(buf, "MemTotal:", strlen("MemTotal:"))) { | |
683 | + memdat->total = atoi(get_item(buf, " ", 2, wk)); /* 1 */ | |
684 | + | |
685 | + } else if (!strncmp(buf, "MemFree:", strlen("MemFree:"))) { | |
686 | + memdat->free = atoi(get_item(buf, " ", 2, wk)); /* 2 */ | |
687 | + | |
688 | + } else if (!strncmp(buf, "MemShared:", strlen("MemShared:"))) { | |
689 | + memdat->shared = atoi(get_item(buf, " ", 2, wk)); /* 3 */ | |
690 | + | |
691 | + } else if (!strncmp(buf, "Buffers:", strlen("Buffers:"))) { | |
692 | + memdat->buffers = atoi(get_item(buf, " ", 2, wk)); /* 4 */ | |
693 | + | |
694 | + } else if (!strncmp(buf, "Cached:", strlen("Cached:"))) { | |
695 | + memdat->cached = atoi(get_item(buf, " ", 2, wk)); /* 5 */ | |
696 | + | |
697 | + } else if (!strncmp(buf, "SwapTotal:", strlen("SwapTotal:"))) { | |
698 | + memdat->swap_total = atoi(get_item(buf, " ", 2, wk)); /* 6 */ | |
699 | + | |
700 | + } else if (!strncmp(buf, "SwapFree:", strlen("SwapFree:"))) { | |
701 | + memdat->swap_free = atoi(get_item(buf, " ", 2, wk)); /* 7 */ | |
702 | + } | |
703 | + } | |
704 | + fclose(fp); | |
705 | + | |
706 | + return 0; | |
707 | +} | |
708 | + | |
709 | +/* | |
710 | + * GetLoadInfo(mem_t *memdat) for Linux (現在使用せず /proc/loadavgから取得) | |
711 | + * | |
712 | + * OUT loaddat : load data | |
713 | + * ret : 0 : success | |
714 | + * -1: error | |
715 | + */ | |
716 | +int GetLoadInfo(load_t *loaddat) | |
717 | +{ | |
718 | + loaddat->load1m = 0; | |
719 | + loaddat->load5m = 0; | |
720 | + loaddat->load15m = 0; | |
721 | + | |
722 | + return 0; | |
723 | +} | |
724 | +#endif // Linux | |
725 | + | |
726 | +/* | |
727 | + * GetMem(fd) | |
728 | + * | |
729 | + * IN fd : output fd | |
730 | + * OUT ret : 0 success | |
731 | + * : -1 error | |
732 | + */ | |
733 | +int GetMem(int fd) | |
734 | +{ | |
735 | + char buf[2048]; | |
736 | + int ret; | |
737 | + mem_t memdat; | |
738 | + | |
739 | + memset(&memdat, 0, sizeof(mem_t)); | |
740 | + | |
741 | + ret = GetMemInfo(&memdat); // no error | |
742 | + if (ret < 0) return ret; | |
743 | + | |
744 | +#ifdef _WIN32 | |
745 | + sprintf(buf, "%I64u %I64u %I64u %I64u %I64u %I64u %I64u\n\n", | |
746 | + memdat.total, | |
747 | + memdat.free, | |
748 | + memdat.shared, | |
749 | + memdat.buffers, | |
750 | + memdat.cached, | |
751 | + memdat.swap_total, | |
752 | + memdat.swap_free); | |
753 | +#else // Linux | |
754 | + sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n\n", | |
755 | + memdat.total, | |
756 | + memdat.free, | |
757 | + memdat.shared, | |
758 | + memdat.buffers, | |
759 | + memdat.cached, | |
760 | + memdat.swap_total, | |
761 | + memdat.swap_free); | |
762 | +#endif // Linux | |
763 | + | |
764 | + if (vf) printf("%s", buf); | |
765 | + | |
766 | + return send(fd, buf, strlen(buf), 0); | |
767 | +} | |
768 | + | |
769 | +/* | |
770 | + * GetLoad(fd) | |
771 | + * | |
772 | + * IN fd : output fd | |
773 | + * OUT ret : 0 success | |
774 | + * : -1 error | |
775 | + */ | |
776 | +int GetLoad(int fd) | |
777 | +{ | |
778 | + char buf[2048]; | |
779 | + | |
780 | +#ifdef _WIN32 | |
781 | + load_t loaddat; | |
782 | + | |
783 | + memset(&loaddat, 0, sizeof(load_t)); | |
784 | + | |
785 | + GetLoadInfo(&loaddat); | |
786 | + | |
787 | + sprintf(buf, "%1.2f %1.2f %1.2f\n\n", | |
788 | + ((float)loaddat.load1m)/100, | |
789 | + ((float)loaddat.load5m)/100, | |
790 | + ((float)loaddat.load15m)/100); | |
791 | + | |
792 | +#else // Linux | |
793 | + FILE *fp; | |
794 | + char *path = "/proc/loadavg"; | |
795 | + char *pt; | |
796 | + | |
797 | + if (!(fp = fopen(path, "r"))) { | |
798 | + sprintf(buf,"GetLoad: fopen(%s) error errno=%d\n", path, errno); | |
799 | + printf(buf); | |
800 | + send(fd, buf, strlen(buf), 0); | |
801 | + return -1; | |
802 | + } | |
803 | + if (fgets(buf,sizeof(buf),fp) == NULL) { | |
804 | + sprintf(buf,"GetLoad: fgets(%s) error errno=%d\n", path, errno); | |
805 | + printf(buf); | |
806 | + send(fd, buf, strlen(buf), 0); | |
807 | + fclose(fp); | |
808 | + return -1; | |
809 | + } | |
810 | + fclose(fp); | |
811 | + | |
812 | + /* "0.00 0.00 0.00 2/42 494" => "0.00 0.00 0.00" */ | |
813 | + pt = strchr(buf, ' '); | |
814 | + ++pt; | |
815 | + pt = strchr(pt, ' '); | |
816 | + ++pt; | |
817 | + pt = strchr(pt, ' '); | |
818 | + *pt = '\n'; | |
819 | + ++pt; | |
820 | + *pt = '\n'; | |
821 | + ++pt; | |
822 | + *pt = '\0'; | |
823 | +#endif // Linux | |
824 | + | |
825 | + if (vf) printf("%s", buf); | |
826 | + | |
827 | + return send(fd, buf, strlen(buf), 0); | |
828 | +} | |
829 | + | |
830 | +/* | |
831 | + * ErrorMsg(fd,str) | |
832 | + * send error message | |
833 | + * | |
834 | + * IN fd : output fd | |
835 | + * str : request string | |
836 | + * OUT ret : 0 success | |
837 | + * : -1 error | |
838 | + */ | |
839 | +int ErrorMsg(int fd, char *str) | |
840 | +{ | |
841 | + char buf[2048]; | |
842 | + | |
843 | + sprintf(buf,"%s request could not understand.\n",str); | |
844 | + | |
845 | + if (vf) printf("%s", buf); | |
846 | + | |
847 | + return send(fd,buf,strlen(buf), 0) ; | |
848 | +} | |
849 | + | |
850 | +/* | |
851 | + * メッセージ受信と処理 | |
852 | + * | |
853 | + * IN sfd : accept fd | |
854 | + * OUT ret : 0 success | |
855 | + * : -1 error | |
856 | + */ | |
857 | +int GetAndProc(int sfd) | |
858 | +{ | |
859 | + char buf[SIZE]; | |
860 | + char sndbuf[SIZE]; | |
861 | + int size; | |
862 | + int st = 0; | |
863 | + | |
864 | + /* get request */ | |
865 | + if (vf) printf("recv...\n"); | |
866 | + size = recv(sfd, buf, SIZE, 0); | |
867 | + if (size < 0) { | |
868 | + perror("read"); | |
869 | + return -1; | |
870 | + } | |
871 | + | |
872 | + /* delete CR/LF */ | |
873 | + if (buf[size-2] == 0x0d && buf[size-1] == 0x0a) { | |
874 | + buf[size-2] = '\0'; | |
875 | + } else if (buf[size-1] == 0x0a) { | |
876 | + buf[size-1] = '\0'; | |
877 | + } else { | |
878 | + buf[size] = '\0'; | |
879 | + } | |
880 | + | |
881 | + /* dispatch request process */ | |
882 | + if (!strncasecmp(buf,HELP,strlen(HELP))) { | |
883 | + /* HELP */ | |
884 | + if (vf) printf("HELP\n"); | |
885 | + st = Help(sfd); | |
886 | + } else if (!strncasecmp(buf, EXIT, strlen(EXIT))) { | |
887 | + /* EXIT */ | |
888 | + if (vf) printf("exit.\n"); | |
889 | + st = -1; | |
890 | + } else if (!strncasecmp(buf, GET_MEM, strlen(GET_MEM))) { | |
891 | + /* GET_MEM */ | |
892 | + if (vf) printf("GET_MEM\n"); | |
893 | + st = GetMem(sfd); | |
894 | + } else if (!strncasecmp(buf, GET_LOAD, strlen(GET_LOAD))) { | |
895 | + /* GET_LOAD */ | |
896 | + if (vf) printf("GET_LOAD\n"); | |
897 | + st = GetLoad(sfd); | |
898 | + } else { | |
899 | + st = ErrorMsg(sfd,buf); | |
900 | + } | |
901 | + return st; | |
902 | +} | |
903 | + | |
904 | +#ifdef _WIN32 | |
905 | +int ThreadGetAndProc(void *parm) | |
906 | +{ | |
907 | + int ret; | |
908 | + int newsockfd = (int)parm; | |
909 | + | |
910 | +#ifdef KEEP_CONN | |
911 | + do { | |
912 | +#endif | |
913 | + if (vf) printf("Start GetAndProc...\n"); | |
914 | + ret = GetAndProc(newsockfd); | |
915 | +#ifdef KEEP_CONN | |
916 | + } while (ret >= 0); // while not "exit" command | |
917 | +#endif | |
918 | + | |
919 | + if (vf) printf("close socket...\n"); | |
920 | + closesocket(newsockfd); | |
921 | + | |
922 | + if (vf) printf("Thread End.\n"); | |
923 | + return 0; | |
924 | +} | |
925 | +#endif // _WIN32 | |
926 | + | |
927 | +int ServiceMainFunc() | |
928 | +{ | |
929 | + int newsockfd; | |
930 | + int clilen, childpid; | |
931 | + struct sockaddr_in cli_addr,serv_addr; | |
932 | + int fd; | |
933 | + char *sendfile; | |
934 | + int con = 0; | |
935 | + int ret = 0; | |
936 | + char msglog[4096]; | |
937 | + fd_set rfds; /* V1.22-A for select */ | |
938 | + struct timeval tmout; /* V1.22-A for select */ | |
939 | + | |
940 | +#ifdef _WIN32 | |
941 | + WSADATA WsaData; | |
942 | + HANDLE thd; /* thread handle */ | |
943 | + DWORD threadID = 0; /* thread ID */ | |
944 | +#endif | |
945 | + | |
946 | + Log("ServiceMainFunc Start ..."); | |
947 | +#ifdef _WIN32 | |
948 | + if (ret = WSAStartup(0x0101, &WsaData)) { | |
949 | + sprintf(msglog, "WSAStartup Error: code = %d\n", ret); | |
950 | + Log(msglog); | |
951 | + exit(1); | |
952 | + } | |
953 | +#endif // _WIN32 | |
954 | + | |
955 | + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
956 | + perror("socket"); | |
957 | + exit(-1); | |
958 | + } | |
959 | + | |
960 | + memset((char *)&serv_addr, 0, sizeof(serv_addr)); | |
961 | + serv_addr.sin_family = AF_INET; | |
962 | + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
963 | + serv_addr.sin_port = htons((short)port); | |
964 | + | |
965 | + if (vf) printf("wait on port (%d)\n", port); | |
966 | + | |
967 | + if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ | |
968 | + perror("bind"); | |
969 | + exit(-1); | |
970 | + } | |
971 | + | |
972 | + | |
973 | + if (vf) printf("listen...\n"); | |
974 | + if(listen(sockfd, 5) < 0){ | |
975 | + perror("listen"); | |
976 | + exit(-1); | |
977 | + } | |
978 | + | |
979 | + while (1) { | |
980 | + // V1.22-A start | |
981 | + if (vf) printf("check incoming...\n"); | |
982 | + tmout.tv_sec = 0; // timeout sec | |
983 | + tmout.tv_usec = 200*1000; // timeout usec (0.2sec) | |
984 | + while (1) { | |
985 | + FD_ZERO(&rfds); | |
986 | + FD_SET(sockfd, &rfds); | |
987 | + ret = select(0, &rfds, 0, 0, &tmout); | |
988 | + //printf("select return %d\n", ret); | |
989 | + if (ret > 0) break; // timeout:0 incoming:>0 | |
990 | + if (proc_stop) break; // service stop request | |
991 | + } | |
992 | + if (proc_stop) break; // service stop request | |
993 | + // V1.22-A end | |
994 | + | |
995 | + if (vf) printf("Start accept...\n"); | |
996 | + clilen = sizeof(cli_addr); | |
997 | + newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, | |
998 | + &clilen); | |
999 | + if(newsockfd < 0){ | |
1000 | + if (errno == EINTR) { | |
1001 | + if (vf) printf("accept interrupted\n"); | |
1002 | + continue; | |
1003 | + } | |
1004 | + perror("accept"); | |
1005 | + exit(-1); | |
1006 | + } | |
1007 | + | |
1008 | +#ifdef _WIN32 | |
1009 | + //procarg_t procarg; | |
1010 | + | |
1011 | + //procarg.clsockfd = newsockfd; | |
1012 | + //procarg.con = con; | |
1013 | + | |
1014 | + if (vf) printf("Create New Thread. (%d)\n", con); | |
1015 | + | |
1016 | + thd = CreateThread( | |
1017 | + NULL, | |
1018 | + 0, | |
1019 | + (LPTHREAD_START_ROUTINE)ThreadGetAndProc, | |
1020 | + (void *)newsockfd, | |
1021 | + 0, | |
1022 | + &threadID); | |
1023 | + if (thd == NULL) { | |
1024 | + sprintf(msglog, "CreateThread Error (%d)\n", GetLastError()); | |
1025 | + Log(msglog); | |
1026 | + return -1; | |
1027 | + } | |
1028 | + | |
1029 | +#else // Linux | |
1030 | + if (fork() == 0) { | |
1031 | + /* this is child process */ | |
1032 | + close(sockfd); /* close server socket */ | |
1033 | + if (vf) printf("accept %d pid=%d\n", con, getpid()); | |
1034 | + | |
1035 | + /* サーバ処理 */ | |
1036 | +#ifdef KEEP_CONN | |
1037 | + while (1) { | |
1038 | + /* もらったデータに対する処理 */ | |
1039 | + if (GetAndProc(newsockfd) < 0) { | |
1040 | + if (vf) printf("Connection closed.\n"); | |
1041 | + close(newsockfd); /* close accept sockfd V1.02-A */ | |
1042 | + exit(0); | |
1043 | + } | |
1044 | + } | |
1045 | +#else // KEEP_CONN | |
1046 | + GetAndProc(newsockfd); /* もらったデータに対する処理 */ | |
1047 | + close(newsockfd); /* close accept sockfd V1.02-A */ | |
1048 | + exit(0); | |
1049 | +#endif // !KEEP_CONN | |
1050 | + } | |
1051 | + close(newsockfd); /* close accept sockfd */ | |
1052 | +#endif // Linux | |
1053 | + | |
1054 | + con++; | |
1055 | + } // accept while loop | |
1056 | + | |
1057 | + Log("ServiceMainFunc End ..."); | |
1058 | + return 0; | |
1059 | +} | |
1060 | + | |
1061 | +#ifdef _WIN32 | |
1062 | +/* | |
1063 | + * AbortService() | |
1064 | + * サービスの起動中断 | |
1065 | + * (ServiceMain()からコールされる) | |
1066 | + */ | |
1067 | +void AbortService() | |
1068 | +{ | |
1069 | + Log("Abort Service.\n"); | |
1070 | + //if (sockfd) closesocket(sockfd); | |
1071 | + //WSACleanup(); | |
1072 | + | |
1073 | + exit(1); | |
1074 | +} | |
1075 | + | |
1076 | +/* | |
1077 | + * SerivceCtrl() | |
1078 | + * サービス制御ハンドラ | |
1079 | + * | |
1080 | + * IN CtrlCode: 制御コード | |
1081 | + */ | |
1082 | +void ServiceCtrl(DWORD CtrlCode) | |
1083 | +{ | |
1084 | + DWORD State = SERVICE_RUNNING; // サービス状態 | |
1085 | + DWORD ChkPoint = 0; // チェック・ポイント値 | |
1086 | + DWORD Wait = 0; // 処理待機時間(ミリ秒) | |
1087 | + | |
1088 | + /* 制御コード別にディスパッチ */ | |
1089 | + switch(CtrlCode) { | |
1090 | + /* サービスの休止 */ | |
1091 | + case SERVICE_CONTROL_PAUSE: | |
1092 | + Log("ServiceCtrl: SERVICE_CONTROL_PAUSE"); | |
1093 | + if (SvcStatus.dwCurrentState == SERVICE_RUNNING) { | |
1094 | + SuspendThread(hThreadMain); | |
1095 | + State = SERVICE_PAUSED; | |
1096 | + } | |
1097 | + break; | |
1098 | + | |
1099 | + /* 休止サービスの再開 */ | |
1100 | + case SERVICE_CONTROL_CONTINUE: | |
1101 | + Log("ServiceCtrl: SERVICE_CONTROL_CONTINUE"); | |
1102 | + if (SvcStatus.dwCurrentState == SERVICE_PAUSED) { | |
1103 | + ResumeThread(hThreadMain); | |
1104 | + State = SERVICE_RUNNING; | |
1105 | + } | |
1106 | + break; | |
1107 | + | |
1108 | + /* サービスの停止 */ | |
1109 | + case SERVICE_CONTROL_STOP: | |
1110 | + Log("ServiceCtrl: SERVICE_CONTROL_STOP"); | |
1111 | + State = SERVICE_STOP_PENDING; | |
1112 | + ChkPoint = 1; | |
1113 | + Wait = 3000; | |
1114 | + SetEvent(hStopEvent); | |
1115 | + break; | |
1116 | + | |
1117 | + /* サービス状態の更新 */ | |
1118 | + case SERVICE_CONTROL_INTERROGATE: | |
1119 | + Log("ServiceCtrl: SERVICE_CONTROL_INTERROGATE"); | |
1120 | + break; | |
1121 | + | |
1122 | + /* 無効な制御コード */ | |
1123 | + default: | |
1124 | + break; | |
1125 | + } | |
1126 | + /* 更新されたサービス状態をサービス制御マネージャに通知 */ | |
1127 | + ReportStatus(State, ChkPoint, Wait); | |
1128 | +} | |
1129 | + | |
1130 | +/* | |
1131 | + * ReportStatus() | |
1132 | + * サービス制御マネージャ(SCM)に状態通知 | |
1133 | + * | |
1134 | + * IN State : サービス状態 | |
1135 | + * IN ChkPoint: チェック・ポイント値 | |
1136 | + * IN Wait : 処理待機時間(msec) | |
1137 | + * | |
1138 | + */ | |
1139 | +BOOL ReportStatus(DWORD State, DWORD ChkPoint, DWORD Wait) | |
1140 | +{ | |
1141 | + BOOL status; | |
1142 | + DWORD AcceptControl = (SERVICE_ACCEPT_PAUSE_CONTINUE | | |
1143 | + SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN); | |
1144 | + char msglog[4096]; | |
1145 | + | |
1146 | + /* サービス情報の設定 */ | |
1147 | + SvcStatus.dwCurrentState = State; | |
1148 | + if (State == SERVICE_START_PENDING) { | |
1149 | + SvcStatus.dwControlsAccepted = 0; | |
1150 | + } else { | |
1151 | + SvcStatus.dwControlsAccepted = AcceptControl; | |
1152 | + } | |
1153 | + SvcStatus.dwCheckPoint = ChkPoint; | |
1154 | + SvcStatus.dwWaitHint = Wait; | |
1155 | + | |
1156 | + /* サービス情報を通知 */ | |
1157 | + status = SetServiceStatus(hSvcStatus, &SvcStatus); | |
1158 | + if (!status){ | |
1159 | + sprintf(msglog, "SetServiceStatus(%ld) error.", GetLastError()); | |
1160 | + Log(msglog); | |
1161 | + AbortService(); | |
1162 | + } | |
1163 | + return status; | |
1164 | +} | |
1165 | + | |
1166 | +/* | |
1167 | + * ServiceMain() | |
1168 | + * Windowsサービスのメイン関数 | |
1169 | + * (1)SCMへの制御ハンドラ(CtrlHandler)登録 | |
1170 | + * (2)サーバスレッド(ServiceMainFunc)の生成 | |
1171 | + * (3)サービス停止イベント待ち | |
1172 | + * (4)終了処理 | |
1173 | + */ | |
1174 | +void ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) | |
1175 | +{ | |
1176 | + DWORD dwErrorNumber; | |
1177 | + DWORD dwThreadID; | |
1178 | + char msglog[4096]; | |
1179 | + | |
1180 | + /* 制御ハンドラ関数の登録 */ | |
1181 | + hSvcStatus = RegisterServiceCtrlHandler( | |
1182 | + SERVICE_NAME, | |
1183 | + (LPHANDLER_FUNCTION)ServiceCtrl); | |
1184 | + if (hSvcStatus == (SERVICE_STATUS_HANDLE)NULL) { | |
1185 | + dwErrorNumber = GetLastError(); | |
1186 | + sprintf(msglog, "Can't regist CtrlHandler. ret = %d", dwErrorNumber); | |
1187 | + Log(msglog); | |
1188 | + return; | |
1189 | + } | |
1190 | + | |
1191 | + //dwErrorNumber = GetFnameFromRegistry("ServiceErrorLog"); | |
1192 | + | |
1193 | + /* サービス状態に依存しないサービス情報の設定 */ | |
1194 | + SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; | |
1195 | + SvcStatus.dwWin32ExitCode = NO_ERROR; | |
1196 | + SvcStatus.dwServiceSpecificExitCode = 0; | |
1197 | + | |
1198 | + /* サービス起動中であることをサービス制御マネージャに通知 */ | |
1199 | + Log("ServiceMain: report status pending 1"); | |
1200 | + if (!ReportStatus(SERVICE_START_PENDING, 1, 8000)) return; | |
1201 | + | |
1202 | + /* サービス停止を指示するためのイベントを作成 */ | |
1203 | + hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1204 | + if (hStopEvent == NULL) { | |
1205 | + sprintf(msglog, "CreateEvent(%ld) error.", GetLastError()); | |
1206 | + Log(msglog); | |
1207 | + AbortService(); | |
1208 | + return; | |
1209 | + } | |
1210 | + | |
1211 | + /* サービス停止用イベントの作成をサービス制御マネージャに通知 */ | |
1212 | + Log("ServiceMain: report status pending 2 / CreateThread(ServiceMainFunc)"); | |
1213 | + if (!ReportStatus(SERVICE_START_PENDING, 2, 8000)) return; | |
1214 | + | |
1215 | + /* サービス提供用スレッドの作成 */ | |
1216 | + hThreadMain = CreateThread(NULL, 0, | |
1217 | + (LPTHREAD_START_ROUTINE)ServiceMainFunc, | |
1218 | + NULL, 0, &dwThreadID); | |
1219 | + if (hThreadMain == (HANDLE)NULL) { | |
1220 | + sprintf(msglog, "CreateThread(%ld) error.", GetLastError()); | |
1221 | + Log(msglog); | |
1222 | + AbortService(); | |
1223 | + return; | |
1224 | + } | |
1225 | + | |
1226 | + /* サービス・スレッドの実行開始をサービス制御マネージャに通知 */ | |
1227 | + Log("ServiceMain: report status (running)"); | |
1228 | + if (!ReportStatus(SERVICE_RUNNING, 0, 1000)) return; | |
1229 | + | |
1230 | + /* "hStopEvent" がシグナル状態になるまで待機 */ | |
1231 | + WaitForSingleObject(hStopEvent, INFINITE); | |
1232 | + | |
1233 | + Log("ServiceMain: stop event received."); | |
1234 | + | |
1235 | + proc_stop = 1; // V1.22-A | |
1236 | + //Sleep(1000); // V1.22-A | |
1237 | + WaitForSingleObject(hThreadMain, INFINITE); | |
1238 | + Log("ServiceMain: Main Thread End."); | |
1239 | + | |
1240 | + /* socketのクローズ */ | |
1241 | + closesocket(sockfd); | |
1242 | + WSACleanup(); | |
1243 | + | |
1244 | + /* サービスの停止処理 */ | |
1245 | + Log("ServiceMain: report status (stopped)"); | |
1246 | + if (!ReportStatus(SERVICE_STOPPED, 0, 1000)) { | |
1247 | + Log("ReportStatus(SERVICE_STOPPED) error."); | |
1248 | + return; | |
1249 | + } | |
1250 | + | |
1251 | + Log("ServiceMain: end."); | |
1252 | + | |
1253 | + if (hStopEvent != NULL) { | |
1254 | + CloseHandle(hStopEvent); | |
1255 | + } | |
1256 | + | |
1257 | + return; | |
1258 | +} | |
1259 | +#endif // _WIN32 | |
1260 | + | |
1261 | + | |
1262 | +/* [Linux] | |
1263 | + * main() | |
1264 | + * +-ServiceMainFunc() | |
1265 | + * +-accept() | |
1266 | + * +-fork() | |
1267 | + * +-GetAndProc() | |
1268 | + * | |
1269 | + * [Win] | |
1270 | + * main() | |
1271 | + * +-StartServiceCtrlDispatcher(DispatchTable[ServiceMain])) | |
1272 | + * | |
1273 | + * ServiceMain() | |
1274 | + * +-エラー時AbortService() | |
1275 | + * +-CreateThread(ServiceMainFunc) | |
1276 | + * +-WaitForSingleObject(hStopEvent, INFINITE); 停止イベント待ち | |
1277 | + * | |
1278 | + * ServiceMainFunc() | |
1279 | + * +-accept() | |
1280 | + * +-CreateThread(ThreadGetAndProc) | |
1281 | + * | |
1282 | + * ThreadGetAndProc() | |
1283 | + * +-GetAndProc() | |
1284 | + * | |
1285 | + */ | |
1286 | +int main(int a, char *b[]) | |
1287 | +{ | |
1288 | + int i; | |
1289 | + int regdel = 0; | |
1290 | + int ret = 0; | |
1291 | + char msglog[4096]; | |
1292 | + | |
1293 | +#ifdef _WIN32 | |
1294 | + DWORD errNum; | |
1295 | + SERVICE_TABLE_ENTRY DispatchTable[] = { | |
1296 | + {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain}, | |
1297 | + {NULL, NULL} }; // 最後 | |
1298 | + | |
1299 | + SetConsoleCtrlHandler(CtrlProc, TRUE); | |
1300 | +#endif | |
1301 | + | |
1302 | + /* pars args */ | |
1303 | + for (i = 1; i<a; i++) { | |
1304 | + if (!strncmp(b[i],"-h",2)) { | |
1305 | + printf("usage : %s [-v] [<portnum>]\n", EXENAME_NOEXT); | |
1306 | +#ifdef _WIN32 | |
1307 | + printf(" %s {-regsrv|-delsrv}\n", EXENAME_NOEXT); | |
1308 | +#endif | |
1309 | + exit(1); | |
1310 | + } | |
1311 | + if (!strncmp(b[i],"-v",2)) { | |
1312 | + vf = 1; | |
1313 | + continue; | |
1314 | + } | |
1315 | +#ifdef _WIN32 | |
1316 | + if (!strcmp(b[i],"-regsrv")) { | |
1317 | + regdel = OPT_REG; | |
1318 | + continue; | |
1319 | + } | |
1320 | + if (!strcmp(b[i],"-delsrv")) { | |
1321 | + regdel = OPT_DEL; | |
1322 | + continue; | |
1323 | + } | |
1324 | +#endif | |
1325 | + port = atoi(b[i]); | |
1326 | + } | |
1327 | + | |
1328 | + if (!port) { | |
1329 | + port = TCP_PORT; | |
1330 | + } | |
1331 | + | |
1332 | +#ifndef _WIN32 | |
1333 | + signal(SIGCLD, reapchild); | |
1334 | + signal(SIGINT, sigint); | |
1335 | +#endif | |
1336 | + | |
1337 | +#ifdef _WIN32 | |
1338 | + if (regdel) { | |
1339 | + ret = RegDelService(regdel, NULL); | |
1340 | + return ret; | |
1341 | + } | |
1342 | + | |
1343 | + // メインスレッドをサービス制御マネージャに接続 | |
1344 | + if (!StartServiceCtrlDispatcher(DispatchTable)) { | |
1345 | + errNum = GetLastError(); | |
1346 | + sprintf(msglog, "StartServiceCtrlDispatcher(%ld) error.", errNum); | |
1347 | + Log(msglog); | |
1348 | + } | |
1349 | +#else // Linux | |
1350 | + ServiceMainFunc(); | |
1351 | +#endif // Linux | |
1352 | + | |
1353 | + | |
1354 | + return 0; | |
1355 | +} | |
1356 | + | |
1357 | +/* vim:ts=8:sw=4: | |
1358 | + */ | |
1359 | + | |
1360 | + |
@@ -0,0 +1,397 @@ | ||
1 | +/* | |
2 | + * CPU使用率取得サンプル | |
3 | + * | |
4 | + * 01/12/16 V0.10 by oga. | |
5 | + */ | |
6 | + | |
7 | +#include <windows.h> | |
8 | +#include <stdio.h> | |
9 | +//#include <winperf.h> // in windows.h | |
10 | + | |
11 | +#define dprintf if (vf) printf | |
12 | + | |
13 | +void memdump(FILE *fp, unsigned char *buf, int size); | |
14 | + | |
15 | +int vf = 0; | |
16 | + | |
17 | +// | |
18 | +// (0) Get Titles DataBase for Objec, Counter Index | |
19 | +// | |
20 | +int GetTitleDB() | |
21 | +{ | |
22 | + HKEY hkey; //レジストリキーを参照 | |
23 | + DWORD MaxData; //TiltesDataBaseの容量 | |
24 | + FILE *fp; | |
25 | + PSTR TitlesDataBase; | |
26 | + int n; | |
27 | + | |
28 | + // Open Key | |
29 | + RegOpenKeyEx(HKEY_LOCAL_MACHINE, | |
30 | + "Software\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009", | |
31 | + 0, | |
32 | + KEY_READ, | |
33 | + &hkey); | |
34 | + | |
35 | + // Get TitleDB size | |
36 | + RegQueryInfoKey(hkey,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&MaxData,NULL,NULL); | |
37 | + | |
38 | + // データベースの領域確保 | |
39 | + TitlesDataBase = (PSTR)malloc((MaxData+1) * sizeof(TCHAR)); | |
40 | + | |
41 | + // データベースを取得 | |
42 | + RegQueryValueEx(hkey, | |
43 | + (LPTSTR) "Counters", | |
44 | + NULL, | |
45 | + NULL, | |
46 | + (LPBYTE) TitlesDataBase, | |
47 | + &MaxData ); | |
48 | + | |
49 | +#if 0 | |
50 | + // TitlesDataBaseをファイルに出力 | |
51 | + fp = fopen("TitlesDataBase.txt","w"); | |
52 | + for (n=0; n<(int)MaxData; n++) { | |
53 | + if ((char)TitlesDataBase[n] == 0) { | |
54 | + fprintf(fp,"\n"); | |
55 | + } | |
56 | + fprintf(fp,"%c",TitlesDataBase[n]); | |
57 | + } | |
58 | + fclose(fp); | |
59 | +#endif | |
60 | + return 0; | |
61 | +} | |
62 | + | |
63 | +// | |
64 | +// (1)Get Data Block | |
65 | +// | |
66 | +PERF_DATA_BLOCK *GetPerfDataBlock() | |
67 | +{ | |
68 | + DWORD lpType; | |
69 | + DWORD lpSize = 200000; | |
70 | + PERF_DATA_BLOCK *pfdata; | |
71 | + long code; | |
72 | + char buf[1024]; | |
73 | + | |
74 | + pfdata = (PERF_DATA_BLOCK *)malloc(lpSize); | |
75 | + if (pfdata == NULL) { | |
76 | + return NULL; | |
77 | + } | |
78 | + | |
79 | + dprintf("RegQueryValueEx(HKEY_PERFORMANCE_DATA) start\n"); | |
80 | + if (code = RegQueryValueEx(HKEY_PERFORMANCE_DATA, | |
81 | + "Global", | |
82 | + 0, | |
83 | + &lpType, | |
84 | + (LPBYTE)pfdata, | |
85 | + &lpSize) != ERROR_SUCCESS) { | |
86 | + printf("RegQueryValueEx() failed. code=%d size=%d\n", code, lpSize); | |
87 | + if (code == ERROR_MORE_DATA) { | |
88 | + printf("MORE Data size!\n"); | |
89 | + return NULL; | |
90 | + } | |
91 | + } | |
92 | + dprintf("RegQueryValueEx(HKEY_PERFORMANCE_DATA) end\n"); | |
93 | + | |
94 | + RegCloseKey(HKEY_PERFORMANCE_DATA); | |
95 | + | |
96 | + // 正しいデータが取得できたかチェック | |
97 | + if (pfdata->Signature[0] == (WCHAR)'P' && | |
98 | + pfdata->Signature[1] == (WCHAR)'E' && | |
99 | + pfdata->Signature[2] == (WCHAR)'R' && | |
100 | + pfdata->Signature[3] == (WCHAR)'F') { | |
101 | + // collect data get success | |
102 | + dprintf("Perf Data get success. (%d) (%d)\n", | |
103 | + sizeof(PERF_DATA_BLOCK), | |
104 | + lpSize); | |
105 | + //memdump(stdout, (unsigned char *)pfdata, lpSize); | |
106 | + } else { | |
107 | + // Invalid data returnd | |
108 | + return NULL; | |
109 | + } | |
110 | + return pfdata; | |
111 | +} | |
112 | + | |
113 | +void FreePerfDataBlock(PERF_DATA_BLOCK *pfdata) | |
114 | +{ | |
115 | + free(pfdata); | |
116 | +} | |
117 | + | |
118 | +// | |
119 | +// (2)Get Perf Object | |
120 | +// | |
121 | +PERF_OBJECT_TYPE *GetPerfObj(PERF_DATA_BLOCK *pfdata, int idx) | |
122 | +{ | |
123 | + int i; | |
124 | + int found = 0; | |
125 | + PERF_OBJECT_TYPE *pfobj; | |
126 | + | |
127 | + if (idx < 1) { | |
128 | + printf("GetPerfObj: Invalid Argument idx=%d\n", idx); | |
129 | + return NULL; | |
130 | + } | |
131 | + // get 1st object | |
132 | + pfobj = (PERF_OBJECT_TYPE *) ((PBYTE) pfdata + pfdata->HeaderLength); | |
133 | + for (i = 0; i < pfdata->NumObjectTypes; i++) { | |
134 | + if (pfobj->ObjectNameTitleIndex == idx) { | |
135 | + found = 1; | |
136 | + break; // found target object | |
137 | + } | |
138 | + pfobj = (PPERF_OBJECT_TYPE) ((PBYTE) pfobj + pfobj->TotalByteLength); | |
139 | + } | |
140 | + if (found) { | |
141 | + return pfobj; | |
142 | + } | |
143 | + printf("not found object (index = %d)\n", idx); | |
144 | + return NULL; | |
145 | +} | |
146 | + | |
147 | +// | |
148 | +// (3)Get Perf Instance Def | |
149 | +// | |
150 | +// idx : 0- | |
151 | +// | |
152 | +PERF_INSTANCE_DEFINITION *GetPerfInstance(PERF_OBJECT_TYPE *pfobj, int idx) | |
153 | +{ | |
154 | + int i; | |
155 | + int found = 0; | |
156 | + PERF_INSTANCE_DEFINITION *pfins; | |
157 | + | |
158 | + if (idx < 0) { | |
159 | + printf("GetPerfInstance: Invalid Argument idx=%d\n", idx); | |
160 | + return NULL; | |
161 | + } | |
162 | + // get 1st object | |
163 | + pfins = (PERF_INSTANCE_DEFINITION *)((PBYTE) pfobj + pfobj->DefinitionLength); | |
164 | + for (i = 0; i < pfobj->NumInstances; i++) { | |
165 | + if (i == idx) { | |
166 | + dprintf("DEBUG3: instance %d\n", i); | |
167 | + found = 1; | |
168 | + break; | |
169 | + } | |
170 | + pfins = (PPERF_INSTANCE_DEFINITION)((PBYTE)pfins + pfins->ByteLength); | |
171 | + } | |
172 | + if (found) { | |
173 | + return pfins; | |
174 | + } | |
175 | + printf("not exist instance (index = %d)\n", idx); | |
176 | + return NULL; | |
177 | +} | |
178 | + | |
179 | +// | |
180 | +// (4)Get Perf Counter Def | |
181 | +// | |
182 | +PERF_COUNTER_DEFINITION *GetPerfCnt(PERF_OBJECT_TYPE *pfobj, int idx) | |
183 | +{ | |
184 | + int i; | |
185 | + int found = 0; | |
186 | + PERF_COUNTER_DEFINITION *pfcnt; | |
187 | + | |
188 | + if (idx < 1) { | |
189 | + printf("GetPerfCnt: Invalid Argument idx=%d\n", idx); | |
190 | + return NULL; | |
191 | + } | |
192 | + // get 1st object | |
193 | + pfcnt = (PERF_COUNTER_DEFINITION *) ((PCHAR) pfobj + pfobj->HeaderLength); | |
194 | + for (i = 0; i < pfobj->NumCounters; i++) { | |
195 | + if (pfcnt->CounterNameTitleIndex == idx) { | |
196 | + found = 1; | |
197 | + break; // found target counter | |
198 | + } | |
199 | + pfcnt = (PERF_COUNTER_DEFINITION *)((PCHAR) pfcnt + pfcnt->ByteLength); | |
200 | + } | |
201 | + if (found) { | |
202 | + return pfcnt; | |
203 | + } | |
204 | + printf("not found counter (index = %d)\n", idx); | |
205 | + return NULL; | |
206 | +} | |
207 | + | |
208 | +// | |
209 | +// (5)Get Counter Value | |
210 | +// | |
211 | +void *GetPerfCntValue(PERF_INSTANCE_DEFINITION *pfins, | |
212 | + PERF_COUNTER_DEFINITION *pfcnt) | |
213 | +{ | |
214 | + PERF_COUNTER_BLOCK *pfcntblk; | |
215 | + void *data; | |
216 | + | |
217 | + pfcntblk = (PERF_COUNTER_BLOCK *)((PBYTE)pfins + pfins->ByteLength); | |
218 | + data = (void*) ((PBYTE)pfcntblk + pfcnt->CounterOffset); | |
219 | + return data; | |
220 | +} | |
221 | + | |
222 | +// | |
223 | +// Get Perf Value (DWORD) | |
224 | +// | |
225 | +DWORD GetPerfValue(int obj_idx, int ins_no, int cnt_idx) | |
226 | +{ | |
227 | + PERF_DATA_BLOCK *pfdata; | |
228 | + PERF_OBJECT_TYPE *pfobj; | |
229 | + PERF_INSTANCE_DEFINITION *pfins; | |
230 | + PERF_COUNTER_DEFINITION *pfcnt; | |
231 | + DWORD *pvalue; | |
232 | + DWORD value; | |
233 | + | |
234 | + pfdata = GetPerfDataBlock(); | |
235 | + if (pfdata == NULL) { | |
236 | + exit(1); | |
237 | + } | |
238 | + pfobj = GetPerfObj(pfdata, obj_idx); // Object Index | |
239 | + if (pfobj == NULL) { | |
240 | + exit(1); | |
241 | + } | |
242 | + pfins = GetPerfInstance(pfobj, ins_no); // Instance Number | |
243 | + if (pfins == NULL) { | |
244 | + exit(1); | |
245 | + } | |
246 | + pfcnt = GetPerfCnt(pfobj, cnt_idx); // Counter Index | |
247 | + if (pfcnt == NULL) { | |
248 | + exit(1); | |
249 | + } | |
250 | + pvalue = (DWORD *)GetPerfCntValue(pfins, pfcnt); // Get Counter Value | |
251 | + value = *pvalue; | |
252 | + | |
253 | + FreePerfDataBlock(pfdata); | |
254 | + | |
255 | + return value; | |
256 | +} | |
257 | + | |
258 | +/* | |
259 | + * void memdump(fp, buf, size) | |
260 | + * | |
261 | + * bufから、size分を fp にヘキサ形式で出力する | |
262 | + * | |
263 | + * <出力例> | |
264 | + * Location: +0 +4 +8 +C /0123456789ABCDEF | |
265 | + * 00000000: 2f2a0a20 2a205265 76697369 6f6e312e //#. # Revision1. | |
266 | + * 00000010: 31203936 2e30372e 30312074 616b6173 /1 96.07.01 takas | |
267 | + * 00000020: 6869206b 61696e75 6d610a20 2a2f0a2f /hi kainuma. #/./ | |
268 | + * | |
269 | + * IN fp : 出力ファイルポインタ (標準出力の場合はstdoutを指定) | |
270 | + * buf : ダンプメモリ先頭アドレス | |
271 | + * size : ダンプサイズ | |
272 | + * | |
273 | + */ | |
274 | +void memdump(FILE *fp, unsigned char *buf, int size) | |
275 | +{ | |
276 | + int c, xx, addr = 0, i; | |
277 | + int f=0, f2=0; | |
278 | + int kflag = 0; /* 漢字出力 */ | |
279 | + int pos = 0; | |
280 | + char asc[17]; | |
281 | + | |
282 | + /* ヘッダ出力 */ | |
283 | + fprintf(fp, | |
284 | + "Location: +0 +4 +8 +C /0123456789ABCDEF\n"); | |
285 | + | |
286 | + c = buf[pos++]; /* c = getc(infp); */ | |
287 | + while(pos <= size) { | |
288 | + xx = 0; | |
289 | + strcpy(asc," "); | |
290 | + fprintf(fp,"%08x: ",addr); | |
291 | + f = 0; | |
292 | + while(pos <= size && xx < 16) { | |
293 | + fprintf(fp,"%02x",c); | |
294 | + if (c < 32) { | |
295 | + if (f) { | |
296 | + asc[xx] = c; | |
297 | + if (c == 10 || c == 13) { | |
298 | + asc[xx] = '.'; /* 暫定 */ | |
299 | + } | |
300 | + } else { | |
301 | + asc[xx] = '.'; | |
302 | + } | |
303 | + f = 0; | |
304 | + } else if (c > 127 ) { | |
305 | + if (kflag) { | |
306 | + if (f) { | |
307 | + asc[xx] = c; | |
308 | + f = 0; | |
309 | + } else { | |
310 | + asc[xx] = c; | |
311 | + f = 1; | |
312 | + } | |
313 | + } else { | |
314 | + asc[xx] = '.'; | |
315 | + } | |
316 | + } else { | |
317 | + asc[xx] = c; | |
318 | + f = 0; | |
319 | + } | |
320 | + if (xx == 0 && f2) { | |
321 | + asc[xx] = '.'; | |
322 | + f = 0; | |
323 | + f2 = 0; | |
324 | + } | |
325 | + if ((xx % 4) == 3) fprintf(fp," "); | |
326 | + ++xx; | |
327 | + ++addr; | |
328 | + c = buf[pos++]; /* c = getc(infp); */ | |
329 | + if (f == 1 && xx >= 16) { | |
330 | + asc[xx++] = c; | |
331 | + f = 0; | |
332 | + f2 = 1; | |
333 | + } | |
334 | + } | |
335 | + while (xx <16) { | |
336 | + fprintf(fp," "); | |
337 | + if ((xx % 4) == 3) fprintf(fp," "); | |
338 | + ++xx; | |
339 | + } | |
340 | + asc[xx]='\0'; | |
341 | + fprintf(fp,"/%16s \n",asc); | |
342 | + } | |
343 | +} | |
344 | + | |
345 | +unsigned int GetCurMilliSec() | |
346 | +{ | |
347 | + SYSTEMTIME syst; | |
348 | + | |
349 | + GetLocalTime(&syst); | |
350 | + return (unsigned int)(syst.wHour * 3600000 + | |
351 | + syst.wMinute * 60000 + | |
352 | + syst.wSecond * 1000 + | |
353 | + syst.wMilliseconds); | |
354 | +} | |
355 | + | |
356 | +int main(int a, char *b[]) | |
357 | +{ | |
358 | + DWORD value; | |
359 | + DWORD oldvalue = 0; | |
360 | + DWORD usage; | |
361 | + int wait_msec = 1000; | |
362 | + int msec, oldmsec; | |
363 | + int diff_msec; | |
364 | + int i; | |
365 | + | |
366 | + for (i = 1; i < a; i++) { | |
367 | + if (!strcmp(b[i], "-v")) { | |
368 | + ++vf; | |
369 | + continue; | |
370 | + } | |
371 | + } | |
372 | + | |
373 | + // GetTitleDB(); | |
374 | + | |
375 | + // main | |
376 | + while (1) { | |
377 | + value = GetPerfValue(238, // 238:"Processor" Object Index | |
378 | + 0, // 1: 2nd Instance (CPU 0) | |
379 | + 6); // 6:"% Processpr Time" Counter Index | |
380 | + dprintf("GetPerfValue value=%u\n", value); | |
381 | + msec = GetCurMilliSec(); | |
382 | + if (oldvalue) { | |
383 | + diff_msec = msec - oldmsec; | |
384 | + if (diff_msec < 0) { | |
385 | + diff_msec = 24*3600*1000 - oldmsec + msec; | |
386 | + } | |
387 | + usage = 100 - (value - oldvalue)/(100 * diff_msec); | |
388 | + printf("CPU usage = %3d%%\n", usage); | |
389 | + } | |
390 | + oldvalue = value; | |
391 | + oldmsec = msec; | |
392 | + Sleep(wait_msec); | |
393 | + } | |
394 | + | |
395 | + return 0; | |
396 | +} | |
397 | + |
@@ -0,0 +1,75 @@ | ||
1 | +/* | |
2 | + * tee for Windows | |
3 | + * | |
4 | + * 07/01/07 V1.00 by oga. | |
5 | + * | |
6 | + */ | |
7 | + | |
8 | +#include <stdio.h> | |
9 | +#include <stdio.h> | |
10 | +#include <stdlib.h> | |
11 | +#include <string.h> | |
12 | + | |
13 | +#define VER "V1.00" | |
14 | + | |
15 | +void usage() | |
16 | +{ | |
17 | + printf("tee %s by oga.\n", VER); | |
18 | + printf("usage: tee [-a] <filename>\n"); | |
19 | + exit(1); | |
20 | +} | |
21 | + | |
22 | +int main(int a, char *b[]) | |
23 | +{ | |
24 | + int i; | |
25 | + int cnt; /* line count */ | |
26 | + int vf = 0; /* -v: verbose */ | |
27 | + char *filename = NULL; | |
28 | + char *openopt = "w"; | |
29 | + FILE *fpin, *fpout; | |
30 | + char buf[1024]; | |
31 | + | |
32 | + for (i = 1; i<a; i++) { | |
33 | + if (!strcmp(b[i],"-a")) { | |
34 | + openopt = "a"; | |
35 | + continue; | |
36 | + } | |
37 | + if (!strcmp(b[i],"-h")) { | |
38 | + usage(); | |
39 | + } | |
40 | + if (!strcmp(b[i],"-v")) { | |
41 | + vf = 1; | |
42 | + continue; | |
43 | + } | |
44 | + filename = b[i]; | |
45 | + } | |
46 | + | |
47 | + if (filename == NULL) { | |
48 | + usage(); | |
49 | + } | |
50 | + | |
51 | + fpin = stdin; | |
52 | + | |
53 | + if (!(fpout = fopen(filename, openopt))) { | |
54 | + perror("fopen"); | |
55 | + exit(1); | |
56 | + } | |
57 | + | |
58 | + cnt = 0; | |
59 | + while (fgets(buf, sizeof(buf), fpin)) { | |
60 | + if (vf) { | |
61 | + printf("##%5d:[%s]\n", cnt, buf); | |
62 | + } else { | |
63 | + printf("%s", buf); | |
64 | + } | |
65 | + fflush(stdout); | |
66 | + fprintf(fpout, "%s", buf); | |
67 | + ++cnt; | |
68 | + } | |
69 | + | |
70 | + fclose(fpout); | |
71 | +} | |
72 | + | |
73 | +/* vim:ts=8:sw=4: | |
74 | + */ | |
75 | + |
@@ -0,0 +1,23 @@ | ||
1 | +#include <windows.h> | |
2 | +#include <stdio.h> | |
3 | +#include <stdlib.h> | |
4 | +#include <string.h> | |
5 | + | |
6 | +#include <fcntl.h> | |
7 | +#if 0 | |
8 | +#include <stdio.h> | |
9 | +#include <sys/stat.h> | |
10 | +#include <sys/types.h> | |
11 | +#endif | |
12 | + | |
13 | +typedef unsigned __int64 uint64; | |
14 | + | |
15 | +int main(int a, char *b[]) | |
16 | +{ | |
17 | + uint64 aaa = 0; | |
18 | + int fd; | |
19 | + | |
20 | + printf("aaa=%I64u\n", aaa); | |
21 | + | |
22 | + fd = open("c:\\a.txt", O_RDONLY); | |
23 | +} |
@@ -0,0 +1,213 @@ | ||
1 | +/* | |
2 | + * timex for win32 | |
3 | + * | |
4 | + * 1999/08/04 V1.00 by oga. | |
5 | + * 2000/08/31 V1.01 stderr(-e) support | |
6 | + * 2002/04/22 V1.02 support .cmd command | |
7 | + */ | |
8 | +#include <windows.h> | |
9 | +#include <stdio.h> | |
10 | +#include <time.h> | |
11 | +#include <sys/stat.h> | |
12 | +#include <stdlib.h> | |
13 | + | |
14 | +int which(char *command, char *fullpath) | |
15 | +{ | |
16 | + char path[2048]; /* stat path */ | |
17 | + char *pathenvp; | |
18 | + char pathp[2048]; /* ENV path */ | |
19 | + int pathlen, comsize; | |
20 | + int i, j; | |
21 | + struct stat statb; | |
22 | + int allf = 0; | |
23 | + int found = 0; | |
24 | + int vf = 0; | |
25 | + | |
26 | + /* fullpath指定など、存在したらそれを返す */ | |
27 | + if (stat(command, &statb) == 0) { | |
28 | + strcpy(fullpath, command); | |
29 | + return 0; | |
30 | + } | |
31 | + | |
32 | + strcpy(fullpath, ""); | |
33 | + | |
34 | + if (vf) printf("command=%s\n",command); | |
35 | + | |
36 | + strcpy(pathp,".;"); | |
37 | + pathenvp=""; | |
38 | + pathenvp = (char *)getenv("PATH"); | |
39 | + | |
40 | + if (vf) printf("PATH=%s\n",pathenvp); | |
41 | + | |
42 | + if (pathenvp) strcat(pathp,pathenvp); | |
43 | + pathlen = strlen(pathp); | |
44 | + | |
45 | + if (vf) printf("path=%s\n",pathp); | |
46 | + | |
47 | + i = 0; | |
48 | + | |
49 | + while (i < pathlen) { | |
50 | + j = 0; | |
51 | + while (pathp[i] != ';' && i < pathlen) { /*path[]="/usr/bin"*/ | |
52 | + path[j] = pathp[i]; | |
53 | + if (path[j] == '\\') | |
54 | + path[j] = '/'; | |
55 | + ++i; | |
56 | + ++j; | |
57 | + } | |
58 | + i++; | |
59 | + path[j] = '\0'; | |
60 | + | |
61 | + if (vf) printf("path=[%s]\n",path); | |
62 | + | |
63 | + strcat(path,"/"); /* path[] = "/usr/bin/" */ | |
64 | + strcat(path,command); /* path[] = "/usr/bin/com" */ | |
65 | + | |
66 | + if (vf) printf("check path=%s\n",path); | |
67 | + comsize = strlen(path); | |
68 | +#if 0 | |
69 | + /* timexでは.bat未サポート */ | |
70 | + strcpy(&path[comsize],".bat"); /* path[] = "/usr/bin/com.bat"*/ | |
71 | + if (stat(path,&statb) == 0) { | |
72 | + /* printf("%s\n", path); */ | |
73 | + strcpy(fullpath, path); | |
74 | + if (allf) found = 1; | |
75 | + else return 0; | |
76 | + } | |
77 | +#endif | |
78 | + strcpy(&path[comsize],".exe"); /* path[] = "/usr/bin/com.exe"*/ | |
79 | + if (stat(path,&statb) == 0) { | |
80 | + /* printf("%s\n", path); */ | |
81 | + strcpy(fullpath, path); | |
82 | + if (allf) found = 1; | |
83 | + else return 0; | |
84 | + } | |
85 | + strcpy(&path[comsize],".com"); /* path[] = "/usr/bin/com.com"*/ | |
86 | + if (stat(path,&statb) == 0) { | |
87 | + /* printf("%s\n", path); */ | |
88 | + strcpy(fullpath, path); | |
89 | + if (allf) found = 1; | |
90 | + else return 0; | |
91 | + } | |
92 | + strcpy(&path[comsize],".cmd"); /* path[] = "/usr/bin/com.cmd"*/ | |
93 | + if (stat(path,&statb) == 0) { | |
94 | + /* printf("%s\n", path); */ | |
95 | + strcpy(fullpath, path); | |
96 | + if (allf) found = 1; | |
97 | + else return 0; | |
98 | + } | |
99 | + } | |
100 | + if (!found) { | |
101 | + printf("command %s not found.\n",command); | |
102 | + return 1; | |
103 | + } | |
104 | + return 0; | |
105 | +} | |
106 | + | |
107 | +/* | |
108 | + * exec command | |
109 | + * | |
110 | + * IN : | |
111 | + * OUT: 0: success -1:error | |
112 | + */ | |
113 | +int exec_cmd(int a, char *b[]) | |
114 | +{ | |
115 | + BOOL status; | |
116 | + STARTUPINFO si; | |
117 | + PROCESS_INFORMATION pi; | |
118 | + FILE *ofp = stdout; | |
119 | + | |
120 | + HANDLE hProc; /* Process Handle */ | |
121 | + int pst; | |
122 | + int st = 0; /* success */ | |
123 | + char command[2048]; | |
124 | + char comargs[2048]; | |
125 | + | |
126 | + int i; | |
127 | + int start,end; | |
128 | + int point = 1; | |
129 | + | |
130 | + if (a >= 2 && !strcmp(b[point], "-e")) { /* V1.01 */ | |
131 | + ofp = stderr; | |
132 | + ++point; | |
133 | + } | |
134 | + | |
135 | + if (a < point+1) { | |
136 | + /* printf("Missing command\n"); */ | |
137 | + printf("usage: timex [-e] <command string>\n"); | |
138 | + printf(" -e : result output to stderr\n"); | |
139 | + exit(1); | |
140 | + } | |
141 | + | |
142 | + | |
143 | + which(b[point++], command); /* 完全パスに変換 */ | |
144 | + | |
145 | + strcpy(comargs,""); | |
146 | + for (i = point; i<a; i++) { | |
147 | + strcat(comargs," "); | |
148 | + strcat(comargs,b[i]); /* args */ | |
149 | + } | |
150 | + | |
151 | + printf("%s%s\n", command, comargs); | |
152 | + | |
153 | + memset(&si, 0, sizeof(si)); | |
154 | + si.cb = sizeof(si); | |
155 | + start = clock(); /* start point */ | |
156 | + status = CreateProcess(command, /* 実行モジュール名 */ | |
157 | + comargs, /* コマンドライン */ | |
158 | + NULL, /* プロセスのセキュリティ属性 */ | |
159 | + NULL, /* スレッドのセキュリティ属性 */ | |
160 | + FALSE, /* ハンドルを継承しない */ | |
161 | + 0, /* fdwCreate */ | |
162 | + NULL, /* 環境ブロックのアドレス */ | |
163 | + NULL, /* カレントディレクトリ(親同) */ | |
164 | + &si, /* STARTUPINFO構造体 */ | |
165 | + &pi); /* PROCESS_INFORMATION構造体 */ | |
166 | + if(status != TRUE){ | |
167 | + printf("exec_cmd: CreateProcess returned=%d",GetLastError()); | |
168 | + return -1; /* error */ | |
169 | + } | |
170 | + | |
171 | + hProc = pi.hProcess; | |
172 | + status=CloseHandle(pi.hThread); | |
173 | + if(status != TRUE){ | |
174 | + printf("CloseHandle error(%d).", GetLastError()); | |
175 | + } | |
176 | + | |
177 | + /* | |
178 | + * wait for command end. | |
179 | + */ | |
180 | + if (WaitForSingleObject(hProc, INFINITE) != WAIT_FAILED) { | |
181 | + /* process end!! */ | |
182 | + if (!GetExitCodeProcess(hProc, &pst)) { | |
183 | + printf("exec_cmd: GetExitCodeProcess error(%d)\n", | |
184 | + GetLastError()); | |
185 | + st = 0; /* error */ | |
186 | + } else { | |
187 | + st = pst; | |
188 | + } | |
189 | + | |
190 | + } | |
191 | + end = clock(); /* end point */ | |
192 | + | |
193 | + status=CloseHandle(hProc); | |
194 | + if(status != TRUE){ | |
195 | + printf("CloseHandle error(%d).", GetLastError()); | |
196 | + } | |
197 | + | |
198 | + /* disp time */ | |
199 | + fprintf(ofp,"\n-------------------\n"); | |
200 | + fprintf(ofp,"real : %.2f sec\n", ((float)(end-start))/CLOCKS_PER_SEC); | |
201 | + fprintf(ofp,"-------------------\n"); | |
202 | + | |
203 | + return pst; | |
204 | +} | |
205 | + | |
206 | +int main(int a, char *b[]) | |
207 | +{ | |
208 | + int st; | |
209 | + | |
210 | + st = exec_cmd(a,b); | |
211 | + | |
212 | + return st; | |
213 | +} |
@@ -0,0 +1,66 @@ | ||
1 | +/* | |
2 | + * uname.c | |
3 | + * | |
4 | + * 99/02/11 V1.00 by oga. | |
5 | + */ | |
6 | + | |
7 | +#include <windows.h> | |
8 | +#include <stdio.h> | |
9 | +#include <stdlib.h> | |
10 | +#include <string.h> | |
11 | + | |
12 | +int main(int a, char *b[]) | |
13 | +{ | |
14 | + int i; | |
15 | + int af = 0; | |
16 | + int vf = 0; | |
17 | + char *strPlat = "unknown"; | |
18 | + OSVERSIONINFO ver; | |
19 | + | |
20 | + for (i=1; i<a; i++) { | |
21 | + if (!strcmp(b[i],"-a")) { | |
22 | + af = 1; | |
23 | + continue; | |
24 | + } | |
25 | + if (!strcmp(b[i],"-v")) { | |
26 | + vf = 1; | |
27 | + continue; | |
28 | + } | |
29 | + } | |
30 | + | |
31 | + memset(&ver, 0, sizeof(OSVERSIONINFO)); | |
32 | + ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
33 | + | |
34 | + if (GetVersionEx(&ver)) { | |
35 | + if (ver.dwPlatformId == VER_PLATFORM_WIN32s) { | |
36 | + strPlat = "Windows 3.1"; | |
37 | + } else if (ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { | |
38 | + strPlat = "Windows 95"; | |
39 | + } else if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) { | |
40 | + strPlat = "Windows NT"; | |
41 | + } | |
42 | + if (vf) { | |
43 | + printf("Measure : %d\n", ver.dwMajorVersion); | |
44 | + printf("Minor : %d\n", ver.dwMinorVersion); | |
45 | + printf("Build : %d\n", ver.dwBuildNumber); | |
46 | + printf("Platform: %d (%s)\n",ver.dwPlatformId, strPlat); | |
47 | + printf("CSDVer : %s\n", ver.szCSDVersion); | |
48 | + } | |
49 | + if (af) { | |
50 | + printf("%s Version %d.%d build %d %s",strPlat, | |
51 | + ver.dwMajorVersion, | |
52 | + ver.dwMinorVersion, | |
53 | + ver.dwBuildNumber, | |
54 | + ver.szCSDVersion ); | |
55 | + } else { | |
56 | + printf("%s Version %d.%d",strPlat, | |
57 | + ver.dwMajorVersion, | |
58 | + ver.dwMinorVersion); | |
59 | + } | |
60 | + } else { | |
61 | + printf("GetVersionEx() error(%d)\n",GetLastError()); | |
62 | + return 1; | |
63 | + } | |
64 | + return 0; | |
65 | +} | |
66 | + |
@@ -0,0 +1,452 @@ | ||
1 | +#include <windows.h> | |
2 | +#include <stdio.h> | |
3 | + | |
4 | +#pragma comment (lib, "winmm.lib") | |
5 | + | |
6 | +struct EVENT { | |
7 | + BYTE state; // ステータスバイト | |
8 | + BYTE data1; // 第一データバイト | |
9 | + BYTE data2; // 第二データバイト | |
10 | + BYTE type; // タイプ | |
11 | + int nData; // データ長 | |
12 | + LPBYTE lpData; // 可変長データ | |
13 | + DWORD dwDelta; // デルタタイム | |
14 | + | |
15 | + struct EVENT *lpNext; // 次のイベントへのポインタ | |
16 | +}; | |
17 | +typedef struct EVENT EVENT; | |
18 | +typedef struct EVENT *LPEVENT; | |
19 | + | |
20 | +static WORD wTime = 0; | |
21 | +static BOOL bPlayThread = FALSE; | |
22 | +static DWORD dwTempo = 0; | |
23 | +static HANDLE hheap = NULL; | |
24 | +static HANDLE hthread = NULL; | |
25 | +static LPEVENT lpHeader = NULL; | |
26 | +static HMIDIOUT hmo = NULL; | |
27 | + | |
28 | +static void Destroy(void); | |
29 | +static BOOL LoadFile(HANDLE); | |
30 | +static void Reverse(LPVOID, int); | |
31 | +static BOOL OpenDialog(HWND, LPTSTR); | |
32 | +static void ReadDelta(HANDLE, LPDWORD); | |
33 | +static BOOL ReadTrack(HANDLE, LPEVENT *); | |
34 | +static double DeltaToMilliSecond(DWORD); | |
35 | +static LPVOID Alloc(int); | |
36 | +static DWORD WINAPI ThreadProc(LPVOID); | |
37 | +static LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM); | |
38 | + | |
39 | +FILE *fp = NULL; | |
40 | + | |
41 | +int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow) | |
42 | +{ | |
43 | + TCHAR szAppName[] = TEXT("MIDI再生サンプル(フォーマット0)"); | |
44 | + MSG msg; | |
45 | + HWND hwnd; | |
46 | + WNDCLASSEX wc = {0}; | |
47 | + | |
48 | + wc.cbSize = sizeof(WNDCLASSEX); | |
49 | + wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED); | |
50 | + wc.hInstance = hinst; | |
51 | + wc.lpfnWndProc = WindowProc; | |
52 | + wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); | |
53 | + wc.lpszClassName = szAppName; | |
54 | + | |
55 | + if (!RegisterClassEx(&wc)) | |
56 | + return 0; | |
57 | + | |
58 | + hwnd = CreateWindowEx(0, szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinst, NULL); | |
59 | + if (hwnd == NULL) | |
60 | + return 0; | |
61 | + | |
62 | + ShowWindow(hwnd, nCmdShow); | |
63 | + UpdateWindow(hwnd); | |
64 | + | |
65 | + while (GetMessage(&msg, NULL, 0, 0) > 0) { | |
66 | + TranslateMessage(&msg); | |
67 | + DispatchMessage(&msg); | |
68 | + } | |
69 | + | |
70 | + return (int)msg.wParam; | |
71 | +} | |
72 | + | |
73 | +static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) | |
74 | +{ | |
75 | + switch (uMsg) { | |
76 | + | |
77 | + case WM_CREATE: { | |
78 | + MMRESULT mr; | |
79 | + | |
80 | + mr = midiOutOpen(&hmo, MIDIMAPPER, 0, 0, CALLBACK_NULL); | |
81 | + | |
82 | + return mr == MMSYSERR_NOERROR ? 0 : -1; | |
83 | + } | |
84 | + | |
85 | + case WM_LBUTTONDOWN: { | |
86 | + TCHAR szFile[MAX_PATH]; | |
87 | + | |
88 | + if (OpenDialog(hwnd, szFile)) { | |
89 | + DWORD dwThreadId; | |
90 | + HANDLE hfile; | |
91 | + | |
92 | + Destroy(); | |
93 | + | |
94 | + hheap = HeapCreate(0, 4096, 0); | |
95 | + if (hheap == NULL) | |
96 | + return 0; | |
97 | + | |
98 | + hfile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
99 | + if (hfile == INVALID_HANDLE_VALUE) { | |
100 | + MessageBox(NULL, TEXT("ファイルのオープンに失敗しました。"), NULL, MB_ICONWARNING); | |
101 | + return 0; | |
102 | + } | |
103 | + | |
104 | + if (!LoadFile(hfile)) { | |
105 | + MessageBox(NULL, TEXT("MIDIファイルの読み込みに失敗しました。"), NULL, MB_ICONWARNING); | |
106 | + CloseHandle(hfile); | |
107 | + return 0; | |
108 | + } | |
109 | + | |
110 | + CloseHandle(hfile); | |
111 | + | |
112 | + dwTempo = 500000; // テンポのデフォルト値 | |
113 | + bPlayThread = TRUE; | |
114 | + | |
115 | + hthread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, NULL, 0, &dwThreadId); | |
116 | + } | |
117 | + | |
118 | + return 0; | |
119 | + } | |
120 | + | |
121 | + case WM_DESTROY: | |
122 | + if (hmo != NULL) { | |
123 | + Destroy(); | |
124 | + midiOutClose(hmo); | |
125 | + if (fp) fclose(fp); /* oga */ | |
126 | + fp = NULL; /* oga */ | |
127 | + } | |
128 | + | |
129 | + PostQuitMessage(0); | |
130 | + | |
131 | + return 0; | |
132 | + | |
133 | + default: | |
134 | + break; | |
135 | + | |
136 | + } | |
137 | + | |
138 | + return DefWindowProc(hwnd, uMsg, wParam, lParam); | |
139 | +} | |
140 | + | |
141 | +static DWORD WINAPI ThreadProc(LPVOID lpParameter) | |
142 | +{ | |
143 | + MIDIHDR mh = {0}; | |
144 | + LPEVENT lpEvent = NULL; | |
145 | + | |
146 | + if (fp == NULL) { | |
147 | + fp = fopen("winmidisample.log", "w"); | |
148 | + } | |
149 | + | |
150 | + while (bPlayThread) { | |
151 | + if (lpEvent == NULL) | |
152 | + lpEvent = lpHeader; | |
153 | + | |
154 | + Sleep((DWORD)DeltaToMilliSecond(lpEvent->dwDelta)); | |
155 | + | |
156 | + if (lpEvent->state == 0xFF) { // メタイベント | |
157 | + if (lpEvent->type == 0x51) // セットテンポ | |
158 | + dwTempo = (DWORD)(lpEvent->lpData[2] | (lpEvent->lpData[1] << 8) | (lpEvent->lpData[0] << 16)); | |
159 | + } | |
160 | + else if (lpEvent->state == 0xF0) { // SysExイベント | |
161 | + mh.lpData = (LPSTR)lpEvent->lpData; | |
162 | + mh.dwFlags = 0; | |
163 | + mh.dwBufferLength = lpEvent->nData; | |
164 | + mh.dwBytesRecorded = lpEvent->nData; | |
165 | + | |
166 | + midiOutPrepareHeader(hmo, &mh, sizeof(MIDIHDR)); | |
167 | + midiOutLongMsg(hmo, &mh, sizeof(MIDIHDR)); | |
168 | + if (fp) fprintf(fp, "Long : %02x %02x %02x type=%02x BufferLen=%d\n", | |
169 | + lpEvent->state, | |
170 | + lpEvent->data1, | |
171 | + lpEvent->data2, | |
172 | + lpEvent->type, | |
173 | + lpEvent->nData); /* oga */ | |
174 | + | |
175 | + while ((mh.dwFlags & MHDR_DONE) == 0); | |
176 | + | |
177 | + midiOutUnprepareHeader(hmo, &mh, sizeof(MIDIHDR)); | |
178 | + } | |
179 | + else { // MIDIイベント | |
180 | + DWORD dwMsg = (DWORD)(lpEvent->state | (lpEvent->data1 << 8) | (lpEvent->data2 << 16)); | |
181 | + | |
182 | + midiOutShortMsg(hmo, dwMsg); | |
183 | + if (fp) fprintf(fp, "Short: %08x\n", dwMsg); | |
184 | + } | |
185 | + | |
186 | + lpEvent = lpEvent->lpNext; | |
187 | + } | |
188 | + | |
189 | + return 0; | |
190 | +} | |
191 | + | |
192 | +static BOOL LoadFile(HANDLE hfile) | |
193 | +{ | |
194 | + int i; | |
195 | + WORD wTrack; | |
196 | + WORD wFormat; | |
197 | + DWORD dwMagic; | |
198 | + DWORD dwResult; | |
199 | + DWORD dwDataLen; | |
200 | + LPEVENT *lpaEvent; // 各トラック内の最初のイベントを指すポインタ配列 | |
201 | + | |
202 | + ReadFile(hfile, &dwMagic, sizeof(DWORD), &dwResult, NULL); | |
203 | + if (dwMagic != *(LPDWORD)"MThd") | |
204 | + return FALSE; | |
205 | + | |
206 | + ReadFile(hfile, &dwDataLen, sizeof(DWORD), &dwResult, NULL); | |
207 | + Reverse(&dwDataLen, sizeof(DWORD)); | |
208 | + if (dwDataLen != 6) | |
209 | + return FALSE; | |
210 | + | |
211 | + ReadFile(hfile, &wFormat, sizeof(WORD), &dwResult, NULL); | |
212 | + Reverse(&wFormat, sizeof(WORD)); | |
213 | + | |
214 | + ReadFile(hfile, &wTrack, sizeof(WORD), &dwResult, NULL); | |
215 | + Reverse(&wTrack, sizeof(WORD)); | |
216 | + | |
217 | + ReadFile(hfile, &wTime, sizeof(WORD), &dwResult, NULL); | |
218 | + Reverse(&wTime, sizeof(WORD)); | |
219 | + | |
220 | + if (wFormat != 0) { | |
221 | + MessageBox(NULL, TEXT("フォーマット0でなければ再生できません。"), NULL, MB_ICONWARNING); | |
222 | + return FALSE; | |
223 | + } | |
224 | + | |
225 | + lpaEvent = (LPEVENT *)Alloc(sizeof(DWORD) * wTrack); | |
226 | + | |
227 | + for (i = 0; i < wTrack; i++) { | |
228 | + if (!ReadTrack(hfile, &lpaEvent[i])) { | |
229 | + MessageBox(NULL, TEXT("不正なトラックです。"), NULL, MB_ICONWARNING); | |
230 | + return FALSE; | |
231 | + } | |
232 | + } | |
233 | + | |
234 | + lpHeader = lpaEvent[0]; | |
235 | + | |
236 | + return TRUE; | |
237 | +} | |
238 | + | |
239 | +static void Destroy(void) | |
240 | +{ | |
241 | + if (hthread != NULL) { | |
242 | + bPlayThread = FALSE; | |
243 | + | |
244 | + WaitForSingleObject(hthread, INFINITE); // スレッドが終了するまで待機 | |
245 | + hthread = NULL; | |
246 | + | |
247 | + midiOutReset(hmo); // 再生中の音を消す | |
248 | + } | |
249 | + | |
250 | + if (hheap != NULL) { | |
251 | + HeapDestroy(hheap); // スレッドが終了してから | |
252 | + hheap = NULL; | |
253 | + } | |
254 | +} | |
255 | + | |
256 | +static BOOL ReadTrack(HANDLE hfile, LPEVENT *lplpEvent) | |
257 | +{ | |
258 | + BYTE statePrev = 0; // 前のイベントのステータスバイト | |
259 | + DWORD dwLen; | |
260 | + DWORD dwMagic; | |
261 | + DWORD dwResult; | |
262 | + LPEVENT lpEvent; | |
263 | + | |
264 | + ReadFile(hfile, &dwMagic, sizeof(DWORD), &dwResult, NULL); | |
265 | + if (dwMagic != *(LPDWORD)"MTrk") | |
266 | + return FALSE; | |
267 | + | |
268 | + ReadFile(hfile, &dwLen, sizeof(DWORD), &dwResult, NULL); | |
269 | + Reverse(&dwLen, sizeof(DWORD)); | |
270 | + | |
271 | + lpEvent = (LPEVENT)Alloc(sizeof(EVENT)); // 最初のイベントのメモリを確保 | |
272 | + | |
273 | + *lplpEvent = lpEvent; // *lplpEventは常に最初のイベントを指す | |
274 | + | |
275 | + for (;;) { | |
276 | + ReadDelta(hfile, &lpEvent->dwDelta); // デルタタイムを読み込む | |
277 | + | |
278 | + ReadFile(hfile, &lpEvent->state, 1, &dwResult, NULL); // ステータスバイトを読み込む | |
279 | + if (!(lpEvent->state & 0x80)) { // ランニングステータスか | |
280 | + lpEvent->state = statePrev; // 一つ前のイベントのステータスバイトを代入 | |
281 | + SetFilePointer(hfile, -1, NULL, FILE_CURRENT); // ファイルポインタを一つ戻す | |
282 | + } | |
283 | + | |
284 | + switch (lpEvent->state & 0xF0) { // ステータスバイトを基にどのイベントか判別 | |
285 | + | |
286 | + case 0x80: | |
287 | + case 0x90: | |
288 | + case 0xA0: | |
289 | + case 0xB0: | |
290 | + case 0xE0: | |
291 | + ReadFile(hfile, &lpEvent->data1, 1, &dwResult, NULL); | |
292 | + ReadFile(hfile, &lpEvent->data2, 1, &dwResult, NULL); | |
293 | + break; | |
294 | + case 0xC0: | |
295 | + case 0xD0: | |
296 | + ReadFile(hfile, &lpEvent->data1, 1, &dwResult, NULL); | |
297 | + lpEvent->data2 = 0; | |
298 | + break; | |
299 | + | |
300 | + case 0xF0: | |
301 | + if (lpEvent->state == 0xF0) { // SysExイベント | |
302 | + ReadFile(hfile, &lpEvent->nData, 1, &dwResult, NULL); | |
303 | + | |
304 | + lpEvent->lpData = (LPBYTE)Alloc(lpEvent->nData + 1); // 先頭の0xF0を含める | |
305 | + lpEvent->lpData[0] = lpEvent->state; // 可変長データの先頭は0xF0 | |
306 | + ReadFile(hfile, (lpEvent->lpData + 1), lpEvent->nData, &dwResult, NULL); | |
307 | + | |
308 | + lpEvent->nData++; | |
309 | + } | |
310 | + else if (lpEvent->state == 0xFF) { // メタイベント | |
311 | + DWORD dw; | |
312 | + DWORD tmp; | |
313 | + | |
314 | + ReadFile(hfile, &lpEvent->type, 1, &dwResult, NULL); // typeの取得 | |
315 | + | |
316 | + dw = (DWORD)-1; | |
317 | + | |
318 | + switch (lpEvent->type) { | |
319 | + | |
320 | + case 0x00: dw = 2; break; | |
321 | + case 0x01: | |
322 | + case 0x02: | |
323 | + case 0x03: | |
324 | + case 0x04: | |
325 | + case 0x05: | |
326 | + case 0x06: | |
327 | + case 0x07: break; | |
328 | + case 0x20: dw = 1; break; | |
329 | + case 0x21: dw = 1; break; | |
330 | + case 0x2F: dw = 0; break; // エンドオブトラック | |
331 | + case 0x51: dw = 3; break; // セットテンポ | |
332 | + case 0x54: dw = 5; break; | |
333 | + case 0x58: dw = 4; break; | |
334 | + case 0x59: dw = 2; break; | |
335 | + case 0x7F: break; | |
336 | + | |
337 | + default: | |
338 | + MessageBox(NULL, TEXT("存在しないメタイベントです。"), NULL, MB_ICONWARNING); | |
339 | + return FALSE; | |
340 | + | |
341 | + } | |
342 | + | |
343 | + tmp = dw; | |
344 | + | |
345 | + if (dw != -1) { // データ長は固定か | |
346 | + ReadDelta(hfile, &dw); | |
347 | + if (dw != tmp) { | |
348 | + MessageBox(NULL, TEXT("固定長メタイベントのデータ長が不正です。"), NULL, MB_ICONWARNING); | |
349 | + return FALSE; | |
350 | + } | |
351 | + } | |
352 | + else | |
353 | + ReadDelta(hfile, &dw); // 任意のデータ長を取得 | |
354 | + | |
355 | + lpEvent->nData = dw; | |
356 | + lpEvent->lpData = (LPBYTE)Alloc(lpEvent->nData); | |
357 | + ReadFile(hfile, lpEvent->lpData, lpEvent->nData, &dwResult, NULL); // データの取得 | |
358 | + | |
359 | + if (lpEvent->type == 0x2F) // トラックの終端 | |
360 | + return TRUE; | |
361 | + } | |
362 | + else | |
363 | + ; | |
364 | + | |
365 | + break; | |
366 | + | |
367 | + default: | |
368 | + MessageBox(NULL, TEXT("ステータスバイト不正"), NULL, MB_ICONWARNING); | |
369 | + return FALSE; | |
370 | + | |
371 | + } | |
372 | + | |
373 | + statePrev = lpEvent->state; | |
374 | + | |
375 | + lpEvent->lpNext = (LPEVENT)Alloc(sizeof(EVENT)); // 次のイベントのためにメモリを確保 | |
376 | + lpEvent = lpEvent->lpNext; | |
377 | + if (lpEvent == NULL) | |
378 | + break; | |
379 | + } | |
380 | + | |
381 | + return FALSE; | |
382 | +} | |
383 | + | |
384 | +static BOOL OpenDialog(HWND hwnd, LPTSTR lpszFile) | |
385 | +{ | |
386 | + OPENFILENAME ofn = {0}; | |
387 | + | |
388 | + lpszFile[0] = '\0'; // 要初期化 | |
389 | + | |
390 | + ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; | |
391 | + ofn.nMaxFile = MAX_PATH; | |
392 | + ofn.hwndOwner = hwnd; | |
393 | + ofn.lpstrFile = lpszFile; | |
394 | + ofn.lpstrTitle = TEXT("MIDIファイル読み込み"); | |
395 | + ofn.lStructSize = sizeof(OPENFILENAME); | |
396 | + ofn.lpstrFilter = TEXT("MIDI File (*.mid)\0*.mid\0\0"); | |
397 | + | |
398 | + if (!GetOpenFileName(&ofn)) | |
399 | + return FALSE; | |
400 | + | |
401 | + return TRUE; | |
402 | +} | |
403 | + | |
404 | +static void ReadDelta(HANDLE hfile, LPDWORD lpdwDelta) | |
405 | +{ | |
406 | + int i; | |
407 | + BYTE tmp; | |
408 | + DWORD dwResult; | |
409 | + | |
410 | + *lpdwDelta = 0; | |
411 | + | |
412 | + if (fp) fprintf(fp, "Delta: "); /* oga */ | |
413 | + for (i = 0; i < sizeof(DWORD); i++) { | |
414 | + ReadFile(hfile, &tmp, 1, &dwResult, NULL); | |
415 | + if (fp) fprintf(fp, "%02x ", tmp); /* oga */ | |
416 | + | |
417 | + *lpdwDelta = ( (*lpdwDelta) << 7 ) | (tmp & 0x7F); | |
418 | + | |
419 | + if (!(tmp & 0x80)) // MSBが立っていないならば、次のバイトはデルタタイムではないので抜ける | |
420 | + break; | |
421 | + } | |
422 | + if (fp) fprintf(fp, "\n"); /* oga */ | |
423 | +} | |
424 | + | |
425 | +static void Reverse(LPVOID lpData, int nSize) | |
426 | +{ | |
427 | + int i; | |
428 | + BYTE tmp; | |
429 | + LPBYTE lp = (LPBYTE)lpData; | |
430 | + LPBYTE lpTail = lp + nSize - 1; | |
431 | + | |
432 | + for (i = 0; i < nSize / 2; i++) { | |
433 | + tmp = *lp; | |
434 | + *lp = *lpTail; | |
435 | + *lpTail = tmp; | |
436 | + | |
437 | + lp++; | |
438 | + lpTail--; | |
439 | + } | |
440 | +} | |
441 | + | |
442 | +static double DeltaToMilliSecond(DWORD dwDelta) | |
443 | +{ | |
444 | + return (dwDelta * ((double)dwTempo / 1000) ) / wTime; | |
445 | +} | |
446 | + | |
447 | +static LPVOID Alloc(int nSize) | |
448 | +{ | |
449 | + return HeapAlloc(hheap, HEAP_ZERO_MEMORY, nSize); | |
450 | +} | |
451 | + | |
452 | + |