char* replace(char *s, char ch, char *repl) { int count = 0; const char *t; for (t = s; *t; t++) count += (*t == ch); size_t rlen = strlen(repl); char *res = malloc(strlen(s) + (rlen - 1)*count + 1); if (res == NULL) { free(s); perror_message("replace"); exit(1); } char *ptr = res; for (t = s; *t; t++) { if (*t == ch) { memcpy(ptr, repl, rlen); ptr += rlen; } else { *ptr++ = *t; } } *ptr = 0; return res; }
//creating an image and connecting it to its parent in the UI tree, return if succeeded int create_image(Sint16 xcut, Sint16 ycut, Sint16 xlocation, Sint16 ylocation, Uint16 width, Uint16 height, SDL_Surface* image_surface, gui_tree_node* parent){ gui_tree_node img; SDL_Rect img_rect; img.children.len = 0; img.offset_rect = (SDL_Rect*)malloc(sizeof(SDL_Rect)); if (img.offset_rect == NULL) { perror_message("malloc"); return FALSE; } img.offset_rect->x = xlocation; img.offset_rect->y = ylocation; img.surface = SDL_ConvertSurface(image_surface, image_surface->format, SDL_SWSURFACE); if (img.surface == NULL){ fprintf(stderr, "surface conversion failed: %s\n", SDL_GetError()); free(img.offset_rect); return FALSE; } SDL_GetClipRect(img.surface, &img_rect); img_rect.x = xcut; img_rect.y = ycut; img_rect.w = width; img_rect.h = height; SDL_SetClipRect(img.surface, &img_rect); return add_child(&img, parent); }
int safe_fgetc(FILE *stream){ int res = fgetc(stream); if (res == EOF){ perror_message("fgetc"); if (fail_safe) for (int i = 0; i < mem_count; i++) free(mem_list[i]); abort(); } else return res; }
// safe_funcs verifies that that the original functions succeeded void * safe_malloc(size_t size){ void *res = malloc(size); if (!res && size != 0){ perror_message("malloc"); if (fail_safe) for (int i = 0; i < mem_count; i++) free(mem_list[i]); abort(); } else{ if (fail_safe) add_to_list(res); return res; } }
int main() { char board[BOARD_SIZE][BOARD_SIZE]; init_board(board); board[3][3] = WHITE_K; //board[2][4] = BLACK_M; //board[4][4] = BLACK_M; //board[5][7] = EMPTY; print_board(board); print_message(WRONG_MINIMAX_DEPTH); perror_message("TEST"); Move* m = get_all_moves(board, WHITE); print_moves(m); scanf_s("DONE"); return 0; }
void * safe_realloc(void *old_pointer, size_t size){ void *res = realloc(old_pointer, size); if (!old_pointer && size != 0){ free(old_pointer); perror_message("realloc"); if (fail_safe) for (int i = 0; i < mem_count; i++) free(mem_list[i]); abort(); } else{ if (fail_safe){ remove_from_list(old_pointer); add_to_list(res); } return res; } }
/* * Read a line of data from the NNTP socket. If something gives, do reconnect * * Parameters: "string" has the buffer space for the line received. * "size" is maximum size of the buffer to read. * * Returns: NULL on end of stream, or a line of data. * Basically, we try to act like fgets() on an NNTP stream. * * Side effects: Talks to server, changes contents of "string". * Reopens connection when necessary and requested. * Exits via tin_done() if fatal error occurs. */ char * get_server( char *string, int size) { int retry = NNTP_TRY_RECONNECT; reconnected_in_last_get_server = FALSE; errno = 0; /* * NULL socket reads indicates socket has closed. Try a few times more */ while (nntp_rd_fp == NULL || s_gets(string, size, nntp_rd_fp) == NULL) { if (quitting) /* Don't bother to reconnect */ tin_done(NNTP_ERROR_EXIT); /* And don't try to disconnect again! */ # ifdef DEBUG if (errno != 0 && errno != EINTR) /* Will only confuse end users */ perror_message("get_server()"); # endif /* DEBUG */ /* * Reconnect only if command was not "QUIT" anyway (in which case a * reconnection would be useless because the connection will be * closed immediately). Also prevents tin from asking to reconnect * when user is quitting tin if tinrc.auto_reconnect is false. */ if (strncmp(last_put, "QUIT", 4)) { retry = reconnect(retry); /* Will abort when out of tries */ reconnected_in_last_get_server = TRUE; } else { /* * Use standard NNTP closing message and response code if user is * quitting tin and leave loop. */ strncpy(string, _(txt_nntp_ok_goodbye), size - 2); strcat(string, cCRLF); /* tin_fgets() needs CRLF */ break; } } return string; }
int khm_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName, int FreeContextFlag, krb5_context * ctx, krb5_ccache * cache) { #ifdef NO_KRB5 return 0; #else #ifdef SHOW_MESSAGE_IN_AN_ANNOYING_WAY char message[256]; const char *errText; int krb5Error = ((int)(rc & 255)); errText = perror_message(rc); _snprintf(message, sizeof(message), "%s\n(Kerberos error %ld)\n\n%s failed", errText, krb5Error, FailedFunctionName); MessageBoxA(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND); #endif if (FreeContextFlag == 1) { if (*ctx != NULL) { if (*cache != NULL) { pkrb5_cc_close(*ctx, *cache); *cache = NULL; } pkrb5_free_context(*ctx); *ctx = NULL; } } return rc; #endif //!NO_KRB5 }
int Leash_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName, int FreeContextFlag, krb5_context * ctx, krb5_ccache * cache) { #ifdef NO_KRB5 return 0; #else #ifdef USE_MESSAGE_BOX char message[256]; const char *errText; errText = perror_message(rc); _snprintf(message, sizeof(message), "%s\n(Kerberos error %ld)\n\n%s failed", errText, rc, FailedFunctionName); message[sizeof(message)-1] = 0; MessageBox(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND); #endif /* USE_MESSAGE_BOX */ if (ctx != NULL && *ctx != NULL) { if (cache != NULL && *cache != NULL) { pkrb5_cc_close(*ctx, *cache); *cache = NULL; } if (FreeContextFlag) { pkrb5_free_context(*ctx); *ctx = NULL; } } return rc; #endif //!NO_KRB5 }
//creating a pannel and connecting it to its parent in the UI tree, return if succeeded int create_panel(int x_offset, int y_offset, int width, int height, gui_tree_node* parent){ gui_tree_node panel; Uint32 rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif panel.surface = SDL_CreateRGBSurface(SDL_HWSURFACE, width, height, 32, rmask, gmask, bmask, amask); if (panel.surface == NULL){ fprintf(stderr, "window creation failed: %s\n", SDL_GetError()); return FALSE; } panel.children.len = 0; panel.offset_rect = (SDL_Rect*)malloc(sizeof(SDL_Rect)); if (panel.offset_rect == NULL) { perror_message("malloc"); free(panel.surface); return FALSE; } panel.offset_rect->x = x_offset; panel.offset_rect->y = y_offset; panel.offset_rect->h = height; panel.offset_rect->w = width; if (SDL_SetAlpha(panel.surface, 0, 0) != 0) { printf("ERROR: failed to set alpha properties for panel: %s\n", SDL_GetError()); } return add_child(&panel, parent); }
//return a dialog window (child of the root of the UI tree) int create_dialog_window(int width, int height, gui_tree_node* parent){ gui_tree_node dialog_window; dialog_window.parent = parent; dialog_window.children.len = 0; dialog_window.offset_x = 0; dialog_window.offset_y = 0; dialog_window.offset_rect = (SDL_Rect*)malloc(sizeof(SDL_Rect)); if (dialog_window.offset_rect == NULL) { perror_message("malloc"); return FALSE; } dialog_window.offset_rect->x = 0; dialog_window.offset_rect->y = 0; dialog_window.offset_rect->h = height; dialog_window.offset_rect->w = width; dialog_window.surface = SDL_SetVideoMode(width, height, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); if (dialog_window.surface == NULL){ fprintf(stderr, "window creation failed: %s\n", SDL_GetError()); free(dialog_window.offset_rect); return FALSE; } return add_child(&dialog_window, parent); }
int split(char *str, char c, char ***arr) { int count = 1; int token_len = 1; int i = 0; char *p; char *t; p = str; //1 - counting delimiter\splitted output string while (*p != '\0') { if (*p == c) count++; p++; } int num = sizeof(char*)*count; *arr = (char**)malloc(num); if (*arr == NULL) { free(str); perror_message("split"); exit(1); } p = str; //2 - allocating arr space while (*p != '\0') { if (*p == c) { int num2 = sizeof(char)*token_len; (*arr)[i] = (char*)malloc(num2); if ((*arr)[i] == NULL) { perror_message("split"); free(str); exit(1); } token_len = 0; i++; } p++; token_len++; } int num3 = sizeof(char)*token_len; (*arr)[i] = (char*)malloc(num3); if ((*arr)[i] == NULL) { perror_message("split"); free(str); exit(1); } i = 0; p = str; t = ((*arr)[i]); //3 - splitting the pointer by delimiter while (*p != '\0') { if (*p != c && *p != '\0') { *t = *p; t++; } else { *t = '\0'; i++; t = ((*arr)[i]); } p++; } *t = '\0'; i++; return count; }
void khm_err_describe(long code, wchar_t * buf, khm_size cbbuf, DWORD * suggestion, kherr_suggestion * suggest_code) { const char * com_err_msg; int offset; long table_num; DWORD msg_id = 0; DWORD sugg_id = 0; kherr_suggestion sugg_code = KHERR_SUGGEST_NONE; if (suggestion == NULL || buf == NULL || cbbuf == 0 || suggest_code == 0) return; *buf = L'\0'; offset = (int) (code & 255); table_num = code - offset; com_err_msg = perror_message(code); *suggestion = 0; *suggest_code = KHERR_SUGGEST_NONE; if (WSABASEERR <= code && code < (WSABASEERR + 1064)) { /* winsock error */ table_num = WSABASEERR; offset = code - WSABASEERR; } switch(table_num) { case krb_err_base: case kadm_err_base: case WSABASEERR: break; default: if (code == KRB5KRB_AP_ERR_BAD_INTEGRITY) { *suggestion = MSG_ERR_S_INTEGRITY; } *suggest_code = KHERR_SUGGEST_RETRY; AnsiStrToUnicode(buf, cbbuf, com_err_msg); return; } if (table_num == krb_err_base) { switch(offset) { case KDC_NAME_EXP: /* 001 Principal expired */ case KDC_SERVICE_EXP: /* 002 Service expired */ case KDC_AUTH_EXP: /* 003 Auth expired */ case KDC_PKT_VER: /* 004 Protocol version unknown */ case KDC_P_MKEY_VER: /* 005 Wrong master key version */ case KDC_S_MKEY_VER: /* 006 Wrong master key version */ case KDC_BYTE_ORDER: /* 007 Byte order unknown */ case KDC_PR_N_UNIQUE: /* 009 Principal not unique */ case KDC_NULL_KEY: /* 010 Principal has null key */ case KDC_GEN_ERR: /* 011 Generic error from KDC */ case INTK_W_NOTALL : /* 061 Not ALL tickets returned */ case INTK_PROT : /* 063 Protocol Error */ case INTK_ERR : /* 070 Other error */ msg_id = MSG_ERR_UNKNOWN; sugg_code = KHERR_SUGGEST_RETRY; break; case KDC_PR_UNKNOWN: /* 008 Principal unknown */ msg_id = MSG_ERR_PR_UNKNOWN; sugg_code = KHERR_SUGGEST_RETRY; break; case GC_TKFIL : /* 021 Can't read ticket file */ case GC_NOTKT : /* 022 Can't find ticket or TGT */ msg_id = MSG_ERR_TKFIL; sugg_id = MSG_ERR_S_TKFIL; sugg_code = KHERR_SUGGEST_RETRY; break; case MK_AP_TGTEXP : /* 026 TGT Expired */ /* no extra error msg */ break; case RD_AP_TIME : /* 037 delta_t too big */ msg_id = MSG_ERR_CLOCKSKEW; sugg_id = MSG_ERR_S_CLOCKSKEW; sugg_code = KHERR_SUGGEST_RETRY; break; case RD_AP_UNDEC : /* 031 Can't decode authenticator */ case RD_AP_EXP : /* 032 Ticket expired */ case RD_AP_NYV : /* 033 Ticket not yet valid */ case RD_AP_REPEAT : /* 034 Repeated request */ case RD_AP_NOT_US : /* 035 The ticket isn't for us */ case RD_AP_INCON : /* 036 Request is inconsistent */ case RD_AP_BADD : /* 038 Incorrect net address */ case RD_AP_VERSION : /* 039 protocol version mismatch */ case RD_AP_MSG_TYPE : /* 040 invalid msg type */ case RD_AP_MODIFIED : /* 041 message stream modified */ case RD_AP_ORDER : /* 042 message out of order */ case RD_AP_UNAUTHOR : /* 043 unauthorized request */ /* no extra error msg */ sugg_code = KHERR_SUGGEST_RETRY; break; case GT_PW_NULL: /* 51 Current PW is null */ case GT_PW_BADPW: /* 52 Incorrect current password */ case GT_PW_PROT: /* 53 Protocol Error */ case GT_PW_KDCERR: /* 54 Error returned by KDC */ case GT_PW_NULLTKT: /* 55 Null tkt returned by KDC */ /* no error msg yet */ sugg_code = KHERR_SUGGEST_RETRY; break; /* Values returned by send_to_kdc */ case SKDC_RETRY : /* 56 Retry count exceeded */ case SKDC_CANT : /* 57 Can't send request */ msg_id = MSG_ERR_KDC_CONTACT; break; /* no error message on purpose: */ case INTK_BADPW : /* 062 Incorrect password */ sugg_code = KHERR_SUGGEST_RETRY; break; default: /* no extra error msg */ break; } } else if (table_num == kadm_err_base) { switch(code) { case KADM_INSECURE_PW: /* if( kadm_info != NULL ){ * wsprintf(buf, "%s\n%s", com_err_msg, kadm_info); * } else { * wsprintf(buf, "%s\nPlease see the help file for information " * "about secure passwords.", com_err_msg); * } * com_err_msg = buf; */ /* The above code would be preferred since it allows site * specific information to be delivered from the Kerberos * server. However the message box is too small for VGA * screens. It does work well if we only have to support * 1024x768 */ msg_id = MSG_ERR_INSECURE_PW; sugg_code = KHERR_SUGGEST_RETRY; break; default: /* no extra error msg */ break; } } else if (table_num == WSABASEERR) { switch(code) { case WSAENETDOWN: msg_id = MSG_ERR_NETDOWN; sugg_id = MSG_ERR_S_NETRETRY; sugg_code = KHERR_SUGGEST_RETRY; break; case WSATRY_AGAIN: msg_id = MSG_ERR_TEMPDOWN; sugg_id = MSG_ERR_S_TEMPDOWN; sugg_code = KHERR_SUGGEST_RETRY; break; case WSAENETUNREACH: case WSAENETRESET: case WSAECONNABORTED: case WSAECONNRESET: case WSAETIMEDOUT: case WSAECONNREFUSED: case WSAEHOSTDOWN: case WSAEHOSTUNREACH: msg_id = MSG_ERR_NOHOST; sugg_id = MSG_ERR_S_NOHOST; sugg_code = KHERR_SUGGEST_RETRY; break; } } if (msg_id != 0) { FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, KHERR_HMODULE, msg_id, 0, buf, (int) (cbbuf / sizeof(buf[0])), NULL); } if (sugg_id != 0) { *suggestion = sugg_id; } if (sugg_code != KHERR_SUGGEST_NONE) *suggest_code = sugg_code; }