void main(void) { int result; char s1[] = "12345"; char s2[] = "45678"; char s3[] = "32768"; printf("\natoi() Function with Overflow Detection"); printf("\n======================================="); printf("\n\nString #1 = \"%s\"", s1); if (x_atoi(s1, &result) == NO_ERROR) printf("\nResult = %d", result); else printf("\nResult = OVERFLOW"); printf("\n\nString #2 = \"%s\"", s2); if (x_atoi(s2, &result) == NO_ERROR) printf("\nResult = %d", result); else printf("\nResult = OVERFLOW"); printf("\n\nString #3 = \"%s\"", s3); if (x_atoi(s3, &result) == NO_ERROR) printf("\nResult = %d", result); else printf("\nResult = OVERFLOW"); }
static void deep_sleep(char *x) { time_t t = x_atoi(x); time_t deedline = time(0) + t; if (t<=0) return; if (cfg_verbose>1) msg("Sleeping: ", x); while (1) { nano_sleep(1,0); if (time(0) >= deedline) break; } }
static bool Addr2HostPort( i8 *addr, ul32 *host, u16 *port ) { i8 *p,*q; static i8 HostPortstring[32]; p=addr; q=HostPortstring; while(*p!=':' && *p!=0) *q++ = *p++; /*tolower(*p++); */ *q++=0; if(x_strcmp(HostPortstring,"udp")!=0) { return FALSE; } if(*p++!=':') { return FALSE; } q=HostPortstring; while(*p!=':' && *p!=0) *q++=*p++; *q++=0; *host=x_ntohl(x_inet_addr(HostPortstring)); if(*p++!=':') { return FALSE; } q=HostPortstring; while(*p!=':' && *p!=0) *q++=*p++; *q++=0; *port=x_atoi(HostPortstring); return TRUE; }
int protocol_parse(tcp_request_t *r, u_char *start, u_char *end) { size_t data_len; u_char c, *p; protocol_t *pro; pro = r->protocol; enum { sw_start = 0, sw_type, sw_type_cr, sw_type_lf, sw_headers, sw_headers_cr, sw_headers_lf, sw_data_len, sw_data_len_cr, sw_data_len_lf, sw_data } state; state = pro->state; data_len = pro->tmp_data_len; for (p = start; p != end; p++) { c = *p; switch (state) { case sw_start: if (is_blank(c)) { break; } if (c < 'A' || c > 'Z') { return err_type_invalid; } pro->start = p; state = sw_type; break; case sw_type: if (c == CR) { pro->end = p; state = sw_type_cr; break; } if (c < 'A' || c > 'Z') { return err_type_invalid; } break; case sw_type_cr: if (c != LF) { return err_only_cr; } switch (pro->end - pro->start) { case 3: if (x_strncmp(pro->start, PUT, 3) == 0) { pro->type = PUT_T; break; } if (x_strncmp(pro->start, GET, 3) == 0) { pro->type = GET_T; break; } return err_type_not_found; case 4: if (x_strncmp(pro->start, LIST, 4) == 0) { pro->type = LIST_T; break; } return err_type_not_found; case 5: if (x_strncmp(pro->start, QUEUE, 5) == 0) { pro->type = QUEUE_T; break; } return err_type_not_found; default: return err_type_not_found; } state = sw_type_lf; break; case sw_type_lf: if (c == CR) { state = sw_headers_cr; break; } if (!is_letter(c) && !is_digit(c) && c != ';' && c != '_' && c != '=') { return err_headers_invalid; } pro->headers_start = p; state = sw_headers; break; case sw_headers: if (c == CR) { pro->headers_end = p; state = sw_headers_cr; break; } if (!is_letter(c) && !is_digit(c) && c != ';' && c != '_' && c != '=') { return err_headers_invalid; } break; case sw_headers_cr: if (c != LF) { return PROTOCOL_ERROR; } state = sw_headers_lf; break; case sw_headers_lf: if (pro->type != PUT_T) { return err_needless_data; } else { if (!is_digit(c)) { return err_data_len_invalid; } pro->start = p; state = sw_data_len; break; } case sw_data_len: if (c == CR) { pro->end = p; state = sw_data_len_cr; break; } if (!is_digit(c)) { return err_data_len_invalid; } break; case sw_data_len_cr: if (c != LF) { return err_only_cr; } *(p - 1) = '\0'; data_len = x_atoi(pro->start); pro->data_len = data_len; *(p - 1) = '\0'; if (data_len <= 0) { return err_data_len_invalid; } state = sw_data_len_lf; break; case sw_data_len_lf: pro->data_start_buf = r->last_buffer; pro->data_start = p; data_len--; state = sw_data; break; case sw_data: if (data_len-- == 0) { return err_needless_data; } break; } } if (pro->type != UNKNOW && pro->type != PUT_T && state == sw_headers_lf) { return PROTOCOL_DONE; } if (pro->type == PUT_T && state == sw_data && data_len == 0) { pro->data_end = p; return PROTOCOL_DONE; } pro->state = state; pro->tmp_data_len = data_len; return PROTOCOL_OK; }
int main(int argc, char **argv) { char *x, *me = argv[0]; char *last_msg = "\\c"; errmsg_iam(me); if (argc<2) { help: errmsg_iam(0); carp(Kill_Help); _exit(1); } argv++; x = argv[0]; if (x[0]=='-' && x[1]=='h') goto help; /* -h... --help */ opendevconsole(); /* ignore next signals */ set_sa(SIGQUIT); set_sa(SIGCHLD); set_sa(SIGHUP ); set_sa(SIGTSTP); set_sa(SIGTTIN); set_sa(SIGTTOU); while (argv[0] && argv[0][0]=='-') { int sig=0, cfg_wall=0; char *y = argv[0] + 1; if ((unsigned int)(*y - '0') < 10) { sig = x_atoi(y); goto again; } switch(*y) { case 'v': cfg_verbose++; if (y[1]=='v') cfg_verbose++; break; case 'q': set_sa(SIGINT); break; case 's': chk_opt(argv,x); deep_sleep(x); break; case 'W': ++cfg_wall; case 'M': chk_opt(argv,x); if (x[0] == '%' && x[1] == 0) x = last_msg; else last_msg = x; print_escape(x, cfg_wall); break; case 'E': chk_opt(argv,x); *argv = x; execve(argv[0], argv, environ); carp("Unable to exec: ", argv[0]); _exit(1); default: #ifdef NSVC_SIGNAL_CODED { unsigned char *S, *Sig = (unsigned char *)NSVC_SIGNAL_CODED; for (S=Sig; *S; S += 2) if ((unsigned char)*y == *S) { sig = S[1]; goto again; } } #else #define kk(C,S) case C: sig=S; break kk('t',SIGTERM); kk('a',SIGALRM); kk('p',SIGSTOP); kk('c',SIGCONT); kk('h',SIGHUP); kk('i',SIGINT); kk('k',SIGKILL); #endif goto help; } again: if (sig > 0) { sync(); if (cfg_verbose) msg("Sending signal: ", y); if (kill(-1, sig)) carp("Unable to send signal: ", y); } argv++; } sync(); return 0; }