Advertisement
wsrvwsrvwsrv

final string

Jun 11th, 2023
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.46 KB | None | 0 0
  1.  
  2. #include "fifth.h"
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #ifdef __linux__
  7. char *s21_strerror(int errnum) {
  8. #define MAX_NUM_ERR 134
  9.   static const char *Error_[MAX_NUM_ERR] = {
  10.       "Success",
  11.       "Operation not permitted",
  12.       "No such file or directory",
  13.       "No such process",
  14.       "Interrupted system call",
  15.       "Input/output error",
  16.       "No such device or address",
  17.       "Argument list too long",
  18.       "Exec format error",
  19.       "Bad file descriptor",
  20.       "No child processes",
  21.       "Resource temporarily unavailable",
  22.       "Cannot allocate memory",
  23.       "Permission denied",
  24.       "Bad address",
  25.       "Block device required",
  26.       "Device or resource busy",
  27.       "File exists",
  28.       "Invalid cross-device link",
  29.       "No such device",
  30.       "Not a directory",
  31.       "Is a directory",
  32.       "Invalid argument",
  33.       "Too many open files in system",
  34.       "Too many open files",
  35.       "Inappropriate ioctl for device",
  36.       "Text file busy",
  37.       "File too large",
  38.       "No space left on device",
  39.       "Illegal seek",
  40.       "Read-only file system",
  41.       "Too many links",
  42.       "Broken pipe",
  43.       "Numerical argument out of domain",
  44.       "Numerical result out of range",
  45.       "Resource deadlock avoided",
  46.       "File name too long",
  47.       "No locks available",
  48.       "Function not implemented",
  49.       "Directory not empty",
  50.       "Too many levels of symbolic links",
  51.       "Unknown error 41",
  52.       "No message of desired type",
  53.       "Identifier removed",
  54.       "Channel number out of range",
  55.       "Level 2 not synchronized",
  56.       "Level 3 halted",
  57.       "Level 3 reset",
  58.       "Link number out of range",
  59.       "Protocol driver not attached",
  60.       "No CSI structure available",
  61.       "Level 2 halted",
  62.       "Invalid exchange",
  63.       "Invalid request descriptor",
  64.       "Exchange full",
  65.       "No anode",
  66.       "Invalid request code",
  67.       "Invalid slot",
  68.       "Unknown error 58",
  69.       "Bad font file format",
  70.       "Device not a stream",
  71.       "No data available",
  72.       "Timer expired",
  73.       "Out of streams resources",
  74.       "Machine is not on the network",
  75.       "Package not installed",
  76.       "Object is remote",
  77.       "Link has been severed",
  78.       "Advertise error",
  79.       "Srmount error",
  80.       "Communication error on send",
  81.       "Protocol error",
  82.       "Multihop attempted",
  83.       "RFS specific error",
  84.       "Bad message",
  85.       "Value too large for defined data type",
  86.       "Name not unique on network",
  87.       "File descriptor in bad state",
  88.       "Remote address changed",
  89.       "Can not access a needed shared library",
  90.       "Accessing a corrupted shared library",
  91.       ".lib section in a.out corrupted",  //
  92.       "Attempting to link in too many shared libraries",
  93.       "Cannot exec a shared library directly",
  94.       "Invalid or incomplete multibyte or wide character",
  95.       "Interrupted system call should be restarted",
  96.       "Streams pipe error",
  97.       "Too many users",
  98.       "Socket operation on non-socket",
  99.       "Destination address required",
  100.       "Message too long",
  101.       "Protocol wrong type for socket",
  102.       "Protocol not available",
  103.       "Protocol not supported",
  104.       "Socket type not supported",
  105.       "Operation not supported",
  106.       "Protocol family not supported",
  107.       "Address family not supported by protocol",
  108.       "Address already in use",
  109.       "Cannot assign requested address",
  110.       "Network is down",
  111.       "Network is unreachable",
  112.       "Network dropped connection on reset",
  113.       "Software caused connection abort",
  114.       "Connection reset by peer",
  115.       "No buffer space available",
  116.       "Transport endpoint is already connected",
  117.       "Transport endpoint is not connected",
  118.       "Cannot send after transport endpoint shutdown",
  119.       "Too many references: cannot splice",
  120.       "Connection timed out",
  121.       "Connection refused",
  122.       "Host is down",
  123.       "No route to host",
  124.       "Operation already in progress",
  125.       "Operation now in progress",
  126.       "Stale file handle",
  127.       "Structure needs cleaning",
  128.       "Not a XENIX named type file",
  129.       "No XENIX semaphores available",
  130.       "Is a named type file",
  131.       "Remote I/O error",
  132.       "Disk quota exceeded",
  133.       "No medium found",
  134.       "Wrong medium type",
  135.       "Operation canceled",
  136.       "Required key not available",
  137.       "Key has expired",
  138.       "Key has been revoked",
  139.       "Key was rejected by service",
  140.       "Owner died",
  141.       "State not recoverable",
  142.       "Operation not possible due to RF-kill",
  143.       "Memory page has hardware error"};
  144.   char *result = malloc(256 * sizeof(char));
  145.   if (errnum < 0 || errnum >= MAX_NUM_ERR) {
  146.     char *f = "Unknown error: ";
  147.     char d[256];
  148.     s21_itoa(errnum, d);
  149.     s21_strcat(result, f);
  150.     s21_strcat(result, d);
  151.   } else
  152.     s21_strcat(result, Error_[errnum]);
  153.   return (char *)result;
  154. }
  155. #else
  156. char *s21_strerror(int errnum) {
  157. #define MAX_NUM_ERR 107
  158.   static const char *Error_[MAX_NUM_ERR] = {
  159.       "Undefined error: 0",
  160.       "Operation not permitted",
  161.       "No such file or directory",
  162.       "No such process",
  163.       "Interrupted system call",
  164.       "Input/output error",
  165.       "Device not configured",
  166.       "Argument list too long",
  167.       "Exec format error",
  168.       "Bad file descriptor",
  169.       "No child processes",
  170.       "Resource deadlock avoided",
  171.       "Cannot allocate memory",
  172.       "Permission denied",
  173.       "Bad address",
  174.       "Block device required",
  175.       "Resource busy",
  176.       "File exists",
  177.       "Cross-device link",
  178.       "Operation not supported by device",
  179.       "Not a directory",
  180.       "Is a directory",
  181.       "Invalid argument",
  182.       "Too many open files in system",
  183.       "Too many open files",
  184.       "Inappropriate ioctl for device",
  185.       "Text file busy",
  186.       "File too large",
  187.       "No space left on device",
  188.       "Illegal seek",
  189.       "Read-only file system",
  190.       "Too many links",
  191.       "Broken pipe",
  192.       "Numerical argument out of domain",
  193.       "Result too large",
  194.       "Resource temporarily unavailable",
  195.       "Operation now in progress",
  196.       "Operation already in progress",
  197.       "Socket operation on non-socket",
  198.       "Destination address required",
  199.       "Message too long",
  200.       "Protocol wrong type for socket",
  201.       "Protocol not available",
  202.       "Protocol not supported",
  203.       "Socket type not supported",
  204.       "Operation not supported",
  205.       "Protocol family not supported",
  206.       "Address family not supported by protocol family",
  207.       "Address already in use",
  208.       "Can't assign requested address",
  209.       "Network is down",
  210.       "Network is unreachable",
  211.       "Network dropped connection on reset",
  212.       "Software caused connection abort",
  213.       "Connection reset by peer",
  214.       "No buffer space available",
  215.       "Socket is already connected",
  216.       "Socket is not connected",
  217.       "Can't send after socket shutdown",
  218.       "Too many references: can't splice",
  219.       "Operation timed out",
  220.       "Connection refused",
  221.       "Too many levels of symbolic links",
  222.       "File name too long",
  223.       "Host is down",
  224.       "No route to host",
  225.       "Directory not empty",
  226.       "Too many processes",
  227.       "Too many users",
  228.       "Disc quota exceeded",
  229.       "Stale NFS file handle",
  230.       "Too many levels of remote in path",
  231.       "RPC struct is bad",
  232.       "RPC version wrong",
  233.       "RPC prog. not avail",
  234.       "Program version wrong",
  235.       "Bad procedure for program",
  236.       "No locks available",
  237.       "Function not implemented",
  238.       "Inappropriate file type or format",
  239.       "Authentication error",
  240.       "Need authenticator",
  241.       "Device power is off",
  242.       "Device error",
  243.       "Value too large to be stored in data type",
  244.       "Bad executable (or shared library)",
  245.       "Bad CPU type in executable",
  246.       "Shared library version mismatch",
  247.       "Malformed Mach-o file",
  248.       "Operation canceled",
  249.       "Identifier removed",
  250.       "No message of desired type",
  251.       "Illegal byte sequence",
  252.       "Attribute not found",
  253.       "Bad message",
  254.       "EMULTIHOP (Reserved)",
  255.       "No message available on STREAM",
  256.       "ENOLINK (Reserved)",
  257.       "No STREAM resources",
  258.       "Not a STREAM",
  259.       "Protocol error",
  260.       "STREAM ioctl timeout",
  261.       "Operation not supported on socket",
  262.       "Policy not found",
  263.       "State not recoverable",
  264.       "Previous owner died",
  265.       "Interface output queue is full"};
  266.   char *result = malloc(256 * sizeof(char));
  267.   if (errnum < 0 || errnum >= MAX_NUM_ERR) {
  268.     char *f = "Unknown error: ";
  269.     char d[256];
  270.     s21_itoa(errnum, d);
  271.     s21_strcat(result, f);
  272.     s21_strcat(result, d);
  273.   } else
  274.     s21_strcat(result, Error_[errnum]);
  275.   return (char *)result;
  276. }
  277. #endif
  278.  
  279. void s21_delete(char *trim_deleted, int p) {
  280.   for (int i = 0, j = 0; i < s21_strlen(trim_deleted); j++, i++) {
  281.     if (i == p) i++;
  282.     trim_deleted[j] = trim_deleted[i];
  283.   }
  284.   trim_deleted[s21_strlen(trim_deleted) - 1] = '\0';
  285. }
  286.  
  287. char *s21_strcat(char *dest, const char *src) {
  288.   char *destmem = dest;
  289.   while (*dest != 0) dest++;
  290.   while (*src != 0) {
  291.     *dest = *src;
  292.     dest++;
  293.     src++;
  294.   }
  295.   *dest = *src;
  296.   return destmem;
  297. }
  298.  
  299. void s21_itoa(int a, char *result) {
  300.   int end = 0;
  301.   int flag = 0;
  302.   if (a < 0) {
  303.     flag = 1;
  304.     a = a * (-1);
  305.   }
  306.   while (a > 0) {
  307.     int ost = a % 10;
  308.     a = a / 10;
  309.     for (int i = end + 1; i > 0; i--) result[i] = result[i - 1];
  310.     result[0] = ost + '0';
  311.     end++;
  312.   }
  313.   if (flag == 1) {
  314.     for (int i = end + 1; i > 0; i--) result[i] = result[i - 1];
  315.     result[0] = '-';
  316.     end++;
  317.   }
  318.   result[end] = '\0';
  319. }
  320.  
  321. char *s21_strncpy(char *dest, const char *src, s21_size_t n) {
  322.   s21_size_t i;
  323.   for (i = 0; i < n && src[i] != '\0'; i++) {
  324.     dest[i] = src[i];
  325.   }
  326.   if (i < n) {
  327.     dest[i] = '\0';
  328.   }
  329.   return dest;
  330. }
  331.  
  332. s21_size_t s21_strlen(const char *str) {
  333.   int i;
  334.   for (i = 0; str[i] != '\0'; i++)
  335.     ;
  336.   return i;
  337. }
  338.  
  339. void *s21_to_lower(const char *str) {
  340.   char *rezult;
  341.   if (str != NULL) {
  342.     rezult = malloc(sizeof(str));
  343.     s21_strncpy(rezult, str, s21_strlen(str));
  344.     for (int i = 0; i < (int)s21_strlen(str); i++)
  345.       if (rezult[i] >= 'A' && rezult[i] <= 'Z') rezult[i] += 'a' - 'A';
  346.   } else
  347.     rezult = NULL;
  348.   return (char *)rezult;
  349. }
  350.  
  351. void *s21_to_upper(const char *str) {
  352.   char *rezult;
  353.   if (str != NULL) {
  354.     rezult = malloc(sizeof(str));
  355.     s21_strncpy(rezult, str, s21_strlen(str));
  356.     for (int i = 0; i < (int)s21_strlen(str); i++)
  357.       if (rezult[i] >= 'a' && rezult[i] <= 'z') rezult[i] += 'A' - 'a';
  358.   } else
  359.     rezult = NULL;
  360.   return (char *)rezult;
  361. }
  362.  
  363. void *s21_trim(const char *src, const char *trim_chars) {
  364.   char *rezult;
  365.   if (src == NULL)
  366.     rezult = NULL;
  367.   else {
  368.     int len = s21_strlen(src);
  369.     int trim_len = 0;
  370.     if (trim_chars != NULL) trim_len = s21_strlen(trim_chars);
  371.     int start = 0, end = len - 1;
  372.  
  373.     if (trim_chars == NULL) {
  374.       int start = 0, end = len - 1;
  375.       while (src[start] == ' ' || src[start] == '\t' || src[start] == '\n' ||
  376.              src[start] == '\r')
  377.         start++;
  378.       while (src[end] == ' ' || src[end] == '\t' || src[end] == '\n' ||
  379.              src[end] == '\r')
  380.         end--;
  381.       rezult = (char *)malloc(sizeof(char) * (end - start + 1));
  382.       for (int i = start, j = 0; i <= end; j++, i++) {
  383.         rezult[j] = src[i];
  384.         rezult[j + 1] = '\0';
  385.       }
  386.     } else if (len == 0) {
  387.       rezult = malloc(sizeof(char));
  388.       rezult[0] = '\0';
  389.     } else if (trim_len == 0) {
  390.       rezult = malloc(sizeof(src));
  391.       s21_strncpy(rezult, src, len);
  392.     } else {
  393.       char *trim_deleted = malloc(sizeof(trim_chars));
  394.       s21_strncpy(trim_deleted, trim_chars, trim_len);
  395.       for (int i = 0; i < trim_len; i++) {
  396.         if (src[start] == trim_deleted[i]) {
  397.           while (src[start] == trim_deleted[i]) start++;
  398.           s21_delete(trim_deleted, i);
  399.           i = -1;
  400.           trim_len = trim_len - 1;
  401.         }
  402.       }
  403.       trim_len = s21_strlen(trim_chars);
  404.       s21_strncpy(trim_deleted, trim_chars, trim_len);
  405.       for (int i = 0; i < trim_len; i++) {
  406.         if (src[end] == trim_deleted[i]) {
  407.           while (src[end] == trim_deleted[i]) end--;
  408.           s21_delete(trim_deleted, i);
  409.           i = -1;
  410.           trim_len = trim_len - 1;
  411.         }
  412.       }
  413.       if (end <= start) {
  414.         rezult = malloc(sizeof(char));
  415.         rezult[0] = '\0';
  416.       } else {
  417.         rezult = malloc(sizeof(char) * (end - start + 1));
  418.         int res_len = end - start + 1;
  419.         for (int j = 0, i = start; i <= end, j < res_len; j++, i++) {
  420.           rezult[j] = src[i];
  421.         }
  422.       }
  423.     }
  424.   }
  425.   return (char *)rezult;
  426. }
  427.  
  428. // where(src) what(str)
  429. void *s21_insert(const char *src, const char *str, s21_size_t start_index) {
  430.   char *rezult;
  431.   if (src != NULL && str != NULL && start_index < s21_strlen(src)) {
  432.     rezult =
  433.         (char *)malloc((s21_strlen(src) + s21_strlen(str) + 1) * sizeof(char));
  434.     s21_strncpy(rezult, src, start_index);
  435.     s21_strncpy(rezult + start_index, str, s21_strlen(str));
  436.     printf("%s\n", rezult);
  437.     s21_strncpy(rezult + start_index + s21_strlen(str), src + start_index,
  438.                 s21_strlen(src) - start_index + 1);
  439.   } else if (src != NULL && str != NULL && start_index == 0 &&
  440.              s21_strlen(src) == 0 && s21_strlen(str) == 0) {
  441.     rezult = malloc(sizeof(char));
  442.     rezult[0] = '\0';
  443.   } else
  444.     rezult = NULL;
  445.  
  446.   return (char *)rezult;
  447. }
  448.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement