Advertisement
HeroBaga

Untitled

Oct 30th, 2021
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. char *s21_strerror(int errnum) {
  2. char *error_message = NULL;
  3. if (errnum > -1 && errnum < 107) { // Если ошибка существует
  4. error_message = (char*)sys_errlist[errnum];
  5. } else if (errnum > 106) {
  6. char unknown_error[30] = "Unknown error: ";
  7. char number[20]; // Для первода errnum в строку
  8. int count = 0;
  9. while (errnum / 10 != 0) {
  10. number[count++] = (errnum % 10) + '0';
  11. errnum /= 10;
  12. }
  13. number[count++] = errnum + '0';
  14. number[count] = '\0';
  15. char buf;
  16. for (int i = 0; i < count / 2; i++) {
  17. buf = number[i];
  18. number[i] = number[count - 1 - i];
  19. number[count- 1 - i] = buf;
  20. }
  21. s21_strcat(unknown_error, number);
  22. static char* result[1];
  23. result[0] = (char*)malloc(s21_strlen(unknown_error)*sizeof(char));
  24. for (s21_size_t i = 0; i < s21_strlen(unknown_error); i++){
  25. result[0][i] = unknown_error[i];
  26. }
  27. error_message = result[0];
  28. free(result[0]); // Никаких утечек памяти в мою смену
  29. } else {
  30. errnum *= -1;
  31. char unknown_error[30] = "Unknown error: -";
  32. char number[20]; // Для первода errnum в строку
  33. int count = 0;
  34. while (errnum / 10 != 0) {
  35. number[count++] = (errnum % 10) + '0';
  36. errnum /= 10;
  37. }
  38. number[count++] = errnum + '0';
  39. number[count] = '\0';
  40. char buf;
  41. for (int i = 0; i < count / 2; i++) {
  42. buf = number[i];
  43. number[i] = number[count - 1 - i];
  44. number[count- 1 - i] = buf;
  45. }
  46. s21_strcat(unknown_error, number);
  47. static char* result[1];
  48. result[0] = (char*)malloc(s21_strlen(unknown_error)*sizeof(char));
  49. for (s21_size_t i = 0; i < s21_strlen(unknown_error); i++){
  50. result[0][i] = unknown_error[i];
  51. }
  52. error_message = result[0];
  53. free(result[0]); // Никаких утечек памяти в мою смену
  54. }
  55. return error_message;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement