forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_util.C
executable file
·789 lines (713 loc) · 24.2 KB
/
str_util.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
// Berkeley Open Infrastructure for Network Computing
// http://boinc.berkeley.edu
// Copyright (C) 2005 University of California
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// To view the GNU Lesser General Public License visit
// http://www.gnu.org/copyleft/lesser.html
// or write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
#include "boinc_win.h"
#endif
#ifdef _WIN32
#include "win_util.h"
#endif
#ifndef _WIN32
#include "config.h"
#include <string>
#include <cmath>
#include <cstring>
#include <cstdlib>
#endif
#include "error_numbers.h"
#include "common_defs.h"
#include "filesys.h"
#include "str_util.h"
#ifdef _USING_FCGI_
#include "fcgi_stdio.h"
#endif
using std::string;
// Use this instead of strncpy().
// Result will always be null-terminated, and it's faster.
// see http://www.gratisoft.us/todd/papers/strlcpy.html
//
#if !defined(HAVE_STRLCPY)
size_t strlcpy(char *dst, const char *src, size_t size) {
size_t ret = strlen(src);
if (size) {
size_t len = (ret >= size) ? size-1 : ret;
memcpy(dst, src, len);
dst[len] = '\0';
}
return ret;
}
#endif
#if !defined(HAVE_STRLCAT)
size_t strlcat(char *dst, const char *src, size_t size) {
size_t dst_len = strlen(dst);
size_t src_len = strlen(src);
if (size) {
size_t len = (src_len >= size-dst_len) ? (size-dst_len-1) : src_len;
memcpy(&dst[dst_len], src, len);
dst[dst_len + len] = '\0';
}
return dst_len + src_len;
}
#endif // !HAVE_STRLCAT
// Converts a double precision time (where the value of 1 represents
// a day) into a string. smallest_timescale determines the smallest
// unit of time division used
// smallest_timescale: 0=seconds, 1=minutes, 2=hours, 3=days, 4=years
//
int ndays_to_string (double x, int smallest_timescale, char *buf) {
double years, days, hours, minutes, seconds;
char year_buf[64], day_buf[16], hour_buf[16], min_buf[16], sec_buf[16];
if (x < 0 || buf == NULL) return ERR_NULL;
years = x / 365.25;
days = fmod(x, 365.25);
hours = fmod(x*24, 24);
minutes = fmod(x*24*60, 60);
seconds = fmod(x*24*60*60, 60);
if (smallest_timescale==4) {
sprintf( year_buf, "%.3f yr ", years );
} else if (years > 1 && smallest_timescale < 4) {
sprintf( year_buf, "%d yr ", (int)years );
} else {
strcpy( year_buf, "" );
}
if (smallest_timescale==3) {
sprintf( day_buf, "%.2f day%s ", days, (days>1?"s":"") );
} else if (days > 1 && smallest_timescale < 3) {
sprintf( day_buf, "%d day%s ", (int)days, (days>1?"s":"") );
} else {
strcpy( day_buf, "" );
}
if (smallest_timescale==2) {
sprintf( hour_buf, "%.2f hr ", hours );
} else if (hours > 1 && smallest_timescale < 2) {
sprintf( hour_buf, "%d hr ", (int)hours );
} else {
strcpy( hour_buf, "" );
}
if (smallest_timescale==1) {
sprintf( min_buf, "%.2f min ", minutes );
} else if (minutes > 1 && smallest_timescale < 1) {
sprintf( min_buf, "%d min ", (int)minutes );
} else {
strcpy( min_buf, "" );
}
if (smallest_timescale==0) {
sprintf( sec_buf, "%.2f sec ", seconds );
} else if (seconds > 1 && smallest_timescale < 0) {
sprintf( sec_buf, "%d sec ", (int)seconds );
} else {
strcpy( sec_buf, "" );
}
// the "-0.05" below is to prevent it from printing 60.0 sec
// when the real value is e.g. 59.91
//
sprintf(buf, "%s%s%s%s%s", year_buf, day_buf, hour_buf, min_buf, sec_buf);
return 0;
}
// Convert nbytes into a string. If total_bytes is non-zero,
// convert the two into a fractional display (i.e. 4/16 KB)
//
void nbytes_to_string(double nbytes, double total_bytes, char* str, int len) {
char buf[256];
double xTera = (1024.0*1024.0*1024.0*1024.0);
double xGiga = (1024.0*1024.0*1024.0);
double xMega = (1024.0*1024.0);
double xKilo = (1024.0);
if (total_bytes != 0) {
if (total_bytes >= xTera) {
sprintf(buf, "%0.2f/%0.2f TB", nbytes/xTera, total_bytes/xTera);
} else if (total_bytes >= xGiga) {
sprintf(buf, "%0.2f/%0.2f GB", nbytes/xGiga, total_bytes/xGiga);
} else if (total_bytes >= xMega) {
sprintf(buf, "%0.2f/%0.2f MB", nbytes/xMega, total_bytes/xMega);
} else if (total_bytes >= xKilo) {
sprintf(buf, "%0.2f/%0.2f KB", nbytes/xKilo, total_bytes/xKilo);
} else {
sprintf(buf, "%0.0f/%0.0f bytes", nbytes, total_bytes);
}
} else {
if (nbytes >= xTera) {
sprintf(buf, "%0.2f TB", nbytes/xTera);
} else if (nbytes >= xGiga) {
sprintf(buf, "%0.2f GB", nbytes/xGiga);
} else if (nbytes >= xMega) {
sprintf(buf, "%0.2f MB", nbytes/xMega);
} else if (nbytes >= xKilo) {
sprintf(buf, "%0.2f KB", nbytes/xKilo);
} else {
sprintf(buf, "%0.0f bytes", nbytes);
}
}
strlcpy(str, buf, len);
}
// take a string containing some space separated words.
// return an array of pointers to the null-terminated words.
// Modifies the string arg.
// Returns argc
// TODO: use strtok here
#define NOT_IN_TOKEN 0
#define IN_SINGLE_QUOTED_TOKEN 1
#define IN_DOUBLE_QUOTED_TOKEN 2
#define IN_UNQUOTED_TOKEN 3
int parse_command_line(char* p, char** argv) {
int state = NOT_IN_TOKEN;
int argc=0;
while (*p) {
switch(state) {
case NOT_IN_TOKEN:
if (isspace(*p)) {
} else if (*p == '\'') {
p++;
argv[argc++] = p;
state = IN_SINGLE_QUOTED_TOKEN;
break;
} else if (*p == '\"') {
p++;
argv[argc++] = p;
state = IN_DOUBLE_QUOTED_TOKEN;
break;
} else {
argv[argc++] = p;
state = IN_UNQUOTED_TOKEN;
}
break;
case IN_SINGLE_QUOTED_TOKEN:
if (*p == '\'') {
*p = 0;
state = NOT_IN_TOKEN;
}
break;
case IN_DOUBLE_QUOTED_TOKEN:
if (*p == '\"') {
*p = 0;
state = NOT_IN_TOKEN;
}
break;
case IN_UNQUOTED_TOKEN:
if (isspace(*p)) {
*p = 0;
state = NOT_IN_TOKEN;
}
break;
}
p++;
}
argv[argc] = 0;
return argc;
}
static char x2c(char *what) {
register char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}
void c2x(char *what) {
char buf[3];
char num = atoi(what);
char d1 = num / 16;
char d2 = num % 16;
int abase1, abase2;
if (d1 < 10) abase1 = 48;
else abase1 = 55;
if (d2 < 10) abase2 = 48;
else abase2 = 55;
buf[0] = d1+abase1;
buf[1] = d2+abase2;
buf[2] = 0;
strcpy(what, buf);
}
// remove whitespace from start and end of a string
//
void strip_whitespace(char *str) {
int n;
while (1) {
if (!str[0]) break;
if (!isascii(str[0])) break;
if (!isspace(str[0])) break;
strcpy(str, str+1);
}
while (1) {
n = (int)strlen(str);
if (n == 0) break;
if (!isascii(str[n-1])) break;
if (!isspace(str[n-1])) break;
str[n-1] = 0;
}
}
void strip_whitespace(string& str) {
int n;
while (1) {
if (str.length() == 0) break;
if (!isascii(str[0])) break;
if (!isspace(str[0])) break;
str.erase(0, 1);
}
while (1) {
n = (int)str.length();
if (n == 0) break;
if (!isascii(str[n-1])) break;
if (!isspace(str[n-1])) break;
str.erase(n-1, 1);
}
}
void unescape_url(char *url) {
int x,y;
for (x=0,y=0;url[y];++x,++y) {
if ((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}
void unescape_url_safe(char *url, int url_size) {
int x,y;
for (x=0,y=0; url[y] && (x<url_size);++x,++y) {
if ((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}
// unescape_url needs to be able to handle potentially hostile
// urls.
void unescape_url(string& url) {
char buf[1024];
strncpy(buf, url.c_str(), sizeof(buf));
unescape_url_safe(buf, sizeof(buf));
url = buf;
}
void escape_url(char *in, char*out) {
int x, y;
for (x=0, y=0; in[x]; ++x) {
if (isalnum(in[x])) {
out[y] = in[x];
++y;
} else {
out[y] = '%';
++y;
out[y] = 0;
char buf[256];
sprintf(buf, "%d", (char)in[x]);
c2x(buf);
strcat(out, buf);
y += 2;
}
}
out[y] = 0;
}
void escape_url_safe(const char *in, char*out, int out_size) {
int x, y;
for (x=0, y=0; in[x] && (y<out_size); ++x) {
if (isalnum(in[x])) {
out[y] = in[x];
++y;
} else {
out[y] = '%';
++y;
out[y] = 0;
char buf[256];
sprintf(buf, "%d", (char)in[x]);
c2x(buf);
strcat(out, buf);
y += 2;
}
}
out[y] = 0;
}
// escape_url needs to be able to handle potentially hostile
// urls
void escape_url(string& url) {
char buf[1024];
escape_url_safe(url.c_str(), buf, sizeof(buf));
url = buf;
}
// Escape a URL for the project directory, cutting off the "http://",
// converting everthing other than alphanumbers, ., - and _ to "_".
//
void escape_url_readable(char *in, char* out) {
int x, y;
char *temp;
temp = strstr(in,"://");
if (temp) {
in = temp + strlen("://");
}
for (x=0, y=0; in[x]; ++x) {
if (isalnum(in[x]) || in[x]=='.' || in[x]=='-' || in[x]=='_') {
out[y] = in[x];
++y;
} else {
out[y] = '_';
++y;
}
}
out[y] = 0;
}
// Canonicalize a master url.
// - Convert the first part of a URL (before the "://") to http://,
// or prepend it
// - Remove double slashes in the rest
// - Add a trailing slash if necessary
//
void canonicalize_master_url(char* url) {
char buf[1024];
size_t n;
bool bSSL = false; // keep track if they sent in https://
char *p = strstr(url, "://");
if (p) {
bSSL = (bool) (p == url + 5);
strcpy(buf, p+3);
} else {
strcpy(buf, url);
}
while (1) {
p = strstr(buf, "//");
if (!p) break;
strcpy(p, p+1);
}
n = strlen(buf);
if (buf[n-1] != '/') {
strcat(buf, "/");
}
sprintf(url, "http%s://%s", (bSSL ? "s" : ""), buf);
}
void canonicalize_master_url(string& url) {
char buf[1024];
strcpy(buf, url.c_str());
canonicalize_master_url(buf);
url = buf;
}
// is the string a valid master URL, in canonical form?
//
bool valid_master_url(char* buf) {
char* p, *q;
size_t n;
bool bSSL = false;
p = strstr(buf, "http://");
if (p != buf) {
// allow https
p = strstr(buf, "https://");
if (p == buf) {
bSSL = true;
} else {
return false; // no http or https, it's bad!
}
}
q = p+strlen(bSSL ? "https://" : "http://");
p = strstr(q, ".");
if (!p) return false;
if (p == q) return false;
q = p+1;
p = strstr(q, "/");
if (!p) return false;
if (p == q) return false;
n = strlen(buf);
if (buf[n-1] != '/') return false;
return true;
}
char* time_to_string(double t) {
static char buf[100];
time_t x = (time_t)t;
struct tm* tm = localtime(&x);
strftime(buf, sizeof(buf)-1, "%d-%b-%Y %H:%M:%S", tm);
return buf;
}
char* precision_time_to_string(double t) {
static char buf[100];
char finer[16];
int hundreds_of_microseconds=(int)(10000*(t-(int)t));
if (hundreds_of_microseconds == 10000) {
// paranoia -- this should never happen!
//
hundreds_of_microseconds=0;
t+=1.0;
}
time_t x = (time_t)t;
struct tm* tm = localtime(&x);
strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S", tm);
sprintf(finer, ".%04d", hundreds_of_microseconds);
strcat(buf, finer);
return buf;
}
string timediff_format(double diff) {
char buf[256];
int tdiff = (int)diff;
int sex = tdiff % 60;
tdiff /= 60;
if (!tdiff) {
sprintf(buf, "%d sec", sex);
return buf;
}
int min = tdiff % 60;
tdiff /= 60;
if (!tdiff) {
sprintf(buf, "%d min %d sec", min, sex);
return buf;
}
int hours = tdiff % 24;
tdiff /= 24;
if (!tdiff) {
sprintf(buf, "%d hr %d min %d sec", hours, min, sex);
return buf;
}
int days = tdiff % 7;
tdiff /= 7;
if (!tdiff) {
sprintf(buf, "%d days %d hr %d min %d sec", days, hours, min, sex);
return buf;
}
sprintf(buf, "%d weeks %d days %d hrs %d min %d sec", (int)tdiff, days, hours, min, sex);
return buf;
}
void escape_project_url(char *in, char* out) {
escape_url_readable(in, out);
char& last = out[strlen(out)-1];
// remove trailing _
if (last == '_') {
last = '\0';
}
}
void mysql_timestamp(double dt, char* p) {
struct tm* tmp;
time_t t = (time_t)dt;
tmp = localtime(&t); // MySQL timestamps are in local time
sprintf(p, "%4d%02d%02d%02d%02d%02d",
tmp->tm_year+1900, tmp->tm_mon+1, tmp->tm_mday,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec
);
}
// Return a text-string description of a given error.
// Must be kept consistent with error_numbers.h
//
const char* boincerror(int which_error) {
switch (which_error) {
case BOINC_SUCCESS: return "Success";
case ERR_SELECT: return "select() failed";
case ERR_MALLOC: return "malloc() failed";
case ERR_READ: return "read() failed";
case ERR_WRITE: return "write() failed";
case ERR_FREAD: return "fread() failed";
case ERR_FWRITE: return "fwrite() failed";
case ERR_IO: return "system I/O error";
case ERR_CONNECT: return "connect() failed";
case ERR_FOPEN: return "fopen() failed";
case ERR_RENAME: return "rename() failed";
case ERR_UNLINK: return "unlink() failed";
case ERR_OPENDIR: return "opendir() failed";
case ERR_XML_PARSE: return "unexpected XML tag or syntax";
case ERR_GETHOSTBYNAME: return "can't resolve hostname";
case ERR_GIVEUP_DOWNLOAD: return "file download timed out";
case ERR_GIVEUP_UPLOAD: return "file upload timed out";
case ERR_NULL: return "unexpected null pointer";
case ERR_NEG: return "unexpected negative value";
case ERR_BUFFER_OVERFLOW: return "buffer overflow";
case ERR_MD5_FAILED: return "md5 checksum failed for file";
case ERR_RSA_FAILED: return "RSA key check failed for file";
case ERR_OPEN: return "open() failed";
case ERR_DUP2: return "dup() failed";
case ERR_NO_SIGNATURE: return "no signature";
case ERR_THREAD: return "thread failure";
case ERR_SIGNAL_CATCH: return "caught signal";
case ERR_UPLOAD_TRANSIENT: return "transient upload error";
case ERR_UPLOAD_PERMANENT: return "permanent upload error";
case ERR_IDLE_PERIOD: return "user preferences say can't start work";
case ERR_ALREADY_ATTACHED: return "already attached to project";
case ERR_FILE_TOO_BIG: return "file size too big";
case ERR_GETRUSAGE: return "getrusage() failed";
case ERR_BENCHMARK_FAILED: return "benchmark failed";
case ERR_BAD_HEX_FORMAT: return "hex format key data bad";
case ERR_DB_NOT_FOUND: return "no database rows found in lookup/enumerate";
case ERR_DB_NOT_UNIQUE: return "database lookup not unique";
case ERR_DB_CANT_CONNECT: return "can't connect to database";
case ERR_GETS: return "gets()/fgets() failedj";
case ERR_SCANF: return "scanf()/fscanf() failed";
case ERR_READDIR: return "readdir() failed";
case ERR_SHMGET: return "shmget() failed";
case ERR_SHMCTL: return "shmctl() failed";
case ERR_SHMAT: return "shmat() failed";
case ERR_FORK: return "fork() failed";
case ERR_EXEC: return "exec() failed";
case ERR_NOT_EXITED: return "process didn't exit";
case ERR_NOT_IMPLEMENTED: return "system call not implemented";
case ERR_GETHOSTNAME: return "gethostname() failed";
case ERR_NETOPEN: return "netopen() failed";
case ERR_SOCKET: return "socket() failed";
case ERR_FCNTL: return "fcntl() failed";
case ERR_AUTHENTICATOR: return "authentication error";
case ERR_SCHED_SHMEM: return "scheduler shared memory contents bad";
case ERR_ASYNCSELECT: return "async select() failed";
case ERR_BAD_RESULT_STATE: return "bad result state";
case ERR_DB_CANT_INIT: return "can't init database";
case ERR_NOT_UNIQUE: return "state files have redundant entries";
case ERR_NOT_FOUND: return "not found";
case ERR_NO_EXIT_STATUS: return "no exit status in scheduler request";
case ERR_FILE_MISSING: return "file missing";
case ERR_SEMGET: return "semget() failed";
case ERR_SEMCTL: return "semctl() failed";
case ERR_SEMOP: return "semop() failed";
case ERR_FTOK: return "ftok() failed";
case ERR_SOCKS_UNKNOWN_FAILURE: return "SOCKS: unknown error";
case ERR_SOCKS_REQUEST_FAILED: return "SOCKS: request failed";
case ERR_SOCKS_BAD_USER_PASS: return "SOCKS: bad user password";
case ERR_SOCKS_UNKNOWN_SERVER_VERSION: return "SOCKS: unknown server version";
case ERR_SOCKS_UNSUPPORTED: return "SOCKS: unsupported";
case ERR_SOCKS_CANT_REACH_HOST: return "SOCKS: can't reach host";
case ERR_SOCKS_CONN_REFUSED: return "SOCKS: connection refused";
case ERR_TIMER_INIT: return "timer init";
case ERR_RSC_LIMIT_EXCEEDED: return "resource limit exceeded";
case ERR_INVALID_PARAM: return "invalid parameter";
case ERR_SIGNAL_OP: return "signal op";
case ERR_BIND: return "bind() failed";
case ERR_LISTEN: return "listen() failed";
case ERR_TIMEOUT: return "timeout";
case ERR_PROJECT_DOWN: return "project down";
case ERR_HTTP_ERROR: return "HTTP error";
case ERR_RESULT_START: return "result start failed";
case ERR_RESULT_DOWNLOAD: return "result download failed";
case ERR_RESULT_UPLOAD: return "result upload failed";
case ERR_INVALID_URL: return "invalid URL";
case ERR_MAJOR_VERSION: return "bad major version";
case ERR_NO_OPTION: return "no option";
case ERR_MKDIR: return "mkdir() failed";
case ERR_INVALID_EVENT: return "invalid event";
case ERR_ALREADY_RUNNING: return "already running";
case ERR_NO_APP_VERSION: return "no app version";
case ERR_WU_USER_RULE: return "user already did result for this workunit";
case ERR_ABORTED_VIA_GUI: return "result aborted via GUI";
case ERR_INSUFFICIENT_RESOURCE: return "insufficient resources";
case ERR_RETRY: return "retry";
case ERR_WRONG_SIZE: return "wrong size";
case ERR_USER_PERMISSION: return "user permission";
case ERR_BAD_EMAIL_ADDR: return "bad email address";
case ERR_BAD_PASSWD: return "bad password";
case ERR_SHMEM_NAME: return "can't get shared mem segment name";
case ERR_NO_NETWORK_CONNECTION: return "no available network connection";
case ERR_IN_PROGRESS: return "operation in progress";
case ERR_ACCT_CREATION_DISABLED: return "account creation disabled";
case ERR_ATTACH_FAIL_INIT: return "Couldn't start master page download";
case ERR_ATTACH_FAIL_DOWNLOAD: return "Couldn't download master page";
case ERR_ATTACH_FAIL_PARSE: return "Couldn't parse master page";
case ERR_ATTACH_FAIL_BAD_KEY: return "Invalid account key";
case ERR_ATTACH_FAIL_FILE_WRITE: return "Couldn't write account file";
case ERR_FFLUSH: return "fflush() failed";
case ERR_FSYNC: return "fsync() failed";
case ERR_TRUNCATE: return "truncate() failed";
case ERR_ABORTED_BY_PROJECT: return "Aborted by project";
case ERR_GETGRNAM: return "getgrnam() failed";
case ERR_CHOWN: return "chown() failed";
case ERR_FILE_NOT_FOUND: return "file not found";
case ERR_BAD_FILENAME: return "file name is empty or has '..'";
case ERR_TOO_MANY_EXITS: return "application exited too many times";
case ERR_RMDIR: return "rmdir() failed";
case 404: return "HTTP file not found";
case 407: return "HTTP proxy authentication failure";
case 416: return "HTTP range request error";
case 500: return "HTTP internal server error";
case 501: return "HTTP not implemented";
case 502: return "HTTP bad gateway";
case 503: return "HTTP service unavailable";
case 504: return "HTTP gateway timeout";
}
static char buf[128];
sprintf(buf, "Error %d", which_error);
return buf;
}
const char* network_status_string(int n) {
switch (n) {
case NETWORK_STATUS_ONLINE: return "online";
case NETWORK_STATUS_WANT_CONNECTION: return "need connection";
case NETWORK_STATUS_WANT_DISCONNECT: return "don't need connection";
case NETWORK_STATUS_LOOKUP_PENDING: return "reference site lookup pending";
default: return "unknown";
}
}
const char* rpc_reason_string(int reason) {
switch (reason) {
case RPC_REASON_USER_REQ: return "Requested by user";
case RPC_REASON_NEED_WORK: return "To fetch work";
case RPC_REASON_RESULTS_DUE: return "To report completed tasks";
case RPC_REASON_TRICKLE_UP: return "To send trickle-up message";
case RPC_REASON_ACCT_MGR_REQ: return "Requested by account manager";
case RPC_REASON_INIT: return "Project initialization";
case RPC_REASON_PROJECT_REQ: return "Requested by project";
default: return "Unknown reason";
}
}
#ifdef WIN32
// get message for last error
//
char* windows_error_string(char* pszBuf, int iSize) {
DWORD dwRet;
LPTSTR lpszTemp = NULL;
dwRet = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ARGUMENT_ARRAY,
NULL,
GetLastError(),
LANG_NEUTRAL,
(LPTSTR)&lpszTemp,
0,
NULL
);
// supplied buffer is not long enough
if ( !dwRet || ( (long)iSize < (long)dwRet+14 ) ) {
pszBuf[0] = TEXT('\0');
} else {
lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
sprintf( pszBuf, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
}
if ( lpszTemp ) {
LocalFree((HLOCAL) lpszTemp );
}
return pszBuf;
}
// get message for given error
//
char* windows_format_error_string(
unsigned long dwError, char* pszBuf, int iSize
) {
DWORD dwRet;
LPTSTR lpszTemp = NULL;
dwRet = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ARGUMENT_ARRAY,
NULL,
dwError,
LANG_NEUTRAL,
(LPTSTR)&lpszTemp,
0,
NULL
);
// supplied buffer is not long enough
if ( !dwRet || ( (long)iSize < (long)dwRet+14 ) ) {
pszBuf[0] = TEXT('\0');
} else {
lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
sprintf( pszBuf, TEXT("%s (0x%x)"), lpszTemp, dwError );
}
if ( lpszTemp ) {
LocalFree((HLOCAL) lpszTemp );
}
return pszBuf;
}
#endif
const char *BOINC_RCSID_ab90e1e = "$Id$";