Advertisement
Guest User

Ancient c library

a guest
Jun 19th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.83 KB | None | 0 0
  1. /**
  2.  * string.h
  3.  */
  4.  
  5. #ifndef __STRING_H__
  6. #define __STRING_H__
  7.  
  8. int strlen(const char *s);
  9.  
  10. int strcmp(const char *s1, const char *s2);
  11.  
  12. int strcasecmp(const char *s1, const char *s2);
  13.  
  14. int strncmp(const char *s1, const char *s2, int l);
  15.  
  16. int strncasecmp(const char *s1, const char *s2, int l);
  17.  
  18. char *strcpy(char *s1, const char *s2);
  19.  
  20. char *strncpy(char *s1, const char *s2, int l);
  21.  
  22. char *strcat(char *s1, const char *s2);
  23.  
  24. char *strncat(char *s1, const char *s2, int l);
  25.  
  26. char toupper(char c);
  27.  
  28. char tolower(char c);
  29.  
  30. char *strchr(const char *s, char c);
  31.  
  32. char *strnchr(const char *s, char c, int l);
  33.  
  34. char *strrchr(const char *s, char c);
  35.  
  36. char *strrnchr(const char *s, char c, int l);
  37.  
  38. char *strstr(const char *s1, const char *s2);
  39.  
  40. int isalnum(char c);
  41.  
  42. int isaplha(char c);
  43.  
  44. int isdigit(char c);
  45.  
  46. int islower(char c);
  47.  
  48. int isupper(char c);
  49.  
  50. int isascii(char c);
  51.  
  52. int iscntrl(char c);
  53.  
  54. int isgraph(char c);
  55.  
  56. int isprint(char c);
  57.  
  58. int ispunct(char c);
  59.  
  60. int isspace(char c);
  61.  
  62. int isxdigit(char c);
  63.  
  64. int memcmp(const void *m1, const void *m2, int l);
  65.  
  66. void *memcpy(const void *m1, const void *m2, int l);
  67.  
  68. void *memset(void *m1, int l, int v);
  69.  
  70. void swap(int *a, int *b);
  71.  
  72. #endif // __STRING_H__
  73.  
  74.  
  75. /**
  76.  * string.c
  77.  */
  78.  
  79. #include "string.h"
  80.  
  81. #define NULL   (0)
  82.  
  83. int strlen(const char *s)
  84. {
  85.    int i = 0;
  86.  
  87.    while(*s++)
  88.    {
  89.       i++;
  90.    }
  91.  
  92.    return i;
  93. }
  94.  
  95. int strcmp(const char *s1, const char *s2)
  96. {
  97.    while(*s1 && *s2)
  98.    {
  99.       if (*s1 > *s2)
  100.       {
  101.          return 1;
  102.       }
  103.  
  104.       if (*s1 < *s2)
  105.       {
  106.          return -1;
  107.       }
  108.  
  109.       s1++;
  110.       s2++;
  111.    }
  112.  
  113.    if (*s1 && !*s2)
  114.    {
  115.       return 1;
  116.    }
  117.  
  118.    if (!*s1 && *s2)
  119.    {
  120.       return -1;
  121.    }
  122.  
  123.    return 0;
  124. }
  125.  
  126. int strcasecmp(const char *s1, const char *s2)
  127. {
  128.    char c1, c2;
  129.  
  130.    while(*s1 && *s2)
  131.    {
  132.       c1 = toupper(*s1);
  133.       c2 = toupper(*s2);
  134.  
  135.       if (c1 > c2)
  136.       {
  137.          return 1;
  138.       }
  139.  
  140.       if (c1 < c2)
  141.       {
  142.          return -1;
  143.       }
  144.  
  145.       s1++;
  146.       s2++;
  147.    }
  148.  
  149.    if (*s1 && !*s2)
  150.    {
  151.       return 1;
  152.    }
  153.  
  154.    if (!*s1 && *s2)
  155.    {
  156.       return -1;
  157.    }
  158.  
  159.    return 0;
  160. }
  161.  
  162. int strncmp(const char *s1, const char *s2, int l)
  163. {
  164.    while(*s1 && *s2 && (l-- > 0))
  165.    {
  166.       if (*s1 > *s2)
  167.       {
  168.          return 1;
  169.       }
  170.  
  171.       if (*s1 < *s2)
  172.       {
  173.          return -1;
  174.       }
  175.  
  176.       s1++;
  177.       s2++;
  178.    }
  179.  
  180.    if (l > 0)
  181.    {
  182.       if (*s1 && !*s2)
  183.       {
  184.          return 1;
  185.       }
  186.  
  187.       if (!*s1 && *s2)
  188.       {
  189.          return -1;
  190.       }
  191.    }
  192.  
  193.    return 0;
  194. }
  195.  
  196. int strncasecmp(const char *s1, const char *s2, int l)
  197. {
  198.    char c1, c2;
  199.  
  200.    while(*s1 && *s2 && (l-- > 0))
  201.    {
  202.       c1 = toupper(*s1);
  203.       c2 = toupper(*s2);
  204.  
  205.       if (c1 > c2)
  206.       {
  207.          return 1;
  208.       }
  209.  
  210.       if (c1 < c2)
  211.       {
  212.          return -1;
  213.       }
  214.  
  215.       s1++;
  216.       s2++;
  217.    }
  218.  
  219.    if (l > 0) {
  220.       if (*s1 && !*s2)
  221.       {
  222.          return 1;
  223.       }
  224.  
  225.       if (!*s1 && *s2)
  226.       {
  227.          return -1;
  228.       }
  229.    }
  230.  
  231.    return 0;
  232. }
  233.  
  234. char *strcpy(char *s1, const char *s2)
  235. {
  236.    char *p = s1;
  237.  
  238.    while (*s2)
  239.    {
  240.       *p++ = *s2++;
  241.    }
  242.  
  243.    *p = '\0';
  244.    return(s1);
  245. }
  246.  
  247. char *strncpy(char *s1, const char *s2, int l)
  248. {
  249.    char *p = s1;
  250.  
  251.    while (*s2 && (l-- > 0))
  252.    {
  253.       *p++ = *s2++;
  254.    }
  255.  
  256.    if (l > 0)
  257.    {
  258.       *p = '\0';
  259.    }
  260.  
  261.    return s1;
  262. }
  263.  
  264. char *strcat(char *s1, const char *s2)
  265. {
  266.    char *p = s1;
  267.  
  268.    while (*p)
  269.    {
  270.       p++;
  271.    }
  272.  
  273.    while (*s2)
  274.    {
  275.       *p++ = *s2++;
  276.    }
  277.  
  278.    *p = '\0';
  279.    return s1;
  280. }
  281.  
  282. char *strncat(char *s1, const char *s2, int l)
  283. {
  284.    char *p = s1;
  285.  
  286.    while (*p)
  287.    {
  288.       p++;
  289.    }
  290.  
  291.    while (*s2 && (l-- > 0))
  292.    {
  293.       *p++ = *s2++;
  294.    }
  295.  
  296.    *p = '\0';
  297.    return s1;
  298. }
  299.  
  300. char toupper(char c)
  301. {
  302.    if (c >= 'a' && c <= 'z')
  303.    {
  304.       c -= 32;
  305.    }
  306.  
  307.    return c;
  308. }
  309.  
  310. char tolower(char c)
  311. {
  312.    if (c >= 'A' && c <= 'Z')
  313.    {
  314.       c += 32;
  315.    }
  316.  
  317.    return c;
  318. }
  319.  
  320. char *strchr(const char *s, char c)
  321. {
  322.    while (*s)
  323.    {
  324.       if (*s == c)
  325.       {
  326.          return (char *)s;
  327.       }
  328.  
  329.       s++;
  330.    }
  331.  
  332.    return NULL;
  333. }
  334.  
  335. char *strnchr(const char *s, char c, int l)
  336. {
  337.    while (*s && (l-- > 0))
  338.    {
  339.       if (*s == c)
  340.       {
  341.          return (char *)s;
  342.       }
  343.  
  344.       s++;
  345.    }
  346.  
  347.    return NULL;
  348. }
  349.  
  350. char *strrchr(const char *s, char c)
  351. {
  352.    char *p = (char *)s;
  353.    while(*p)
  354.    {
  355.       p++;
  356.    }
  357.  
  358.    while (p != s)
  359.    {
  360.       if (*p == c)
  361.       {
  362.          return (char *)p;
  363.       }
  364.  
  365.       p--;
  366.    }
  367.  
  368.    return NULL;
  369. }
  370.  
  371. char *strrnchr(const char *s, char c, int l)
  372. {
  373.    char *p = (char *)s;
  374.    while(*p)
  375.    {
  376.       p++;
  377.    }
  378.  
  379.    while (p != s && (l-- > 0))
  380.    {
  381.       if (*p == c)
  382.       {
  383.          return (char *)p;
  384.       }
  385.  
  386.       p--;
  387.    }
  388.  
  389.    return NULL;
  390. }
  391.  
  392. char *strstr(const char *s1, const char *s2)
  393. {
  394.    char *p = (char *)s2;
  395.    char *r = NULL;
  396.    int c = 0, d = 0;
  397.  
  398.    while (*p++)
  399.    {
  400.       c++;
  401.    }
  402.  
  403.    p = (char *)s2;
  404.    d = c;
  405.  
  406.    while (*s1 && (c > 0))
  407.    {
  408.       if (*s1 != *p)
  409.       {
  410.          s1++;
  411.          p = (char *)s2;
  412.          r = NULL;
  413.          c = d;
  414.       }
  415.       else
  416.       {
  417.          if (!r)
  418.          {
  419.             r = (char *)s1;
  420.          }
  421.  
  422.          s1++;
  423.          p++;
  424.          c--;
  425.       }
  426.    }
  427.  
  428.    if (c != 0)
  429.    {
  430.       r = NULL;
  431.    }
  432.  
  433.    return r;
  434. }
  435.  
  436. int isalnum(char c)
  437. {
  438.    if ((c >= '0' && c <= '9') ||
  439.        (c >= 'A' && c <= 'Z') ||
  440.        (c >= 'a' && c <= 'z'))
  441.    {
  442.       return 1;
  443.    }
  444.  
  445.    return 0;
  446. }
  447.  
  448. int isalpha(char c)
  449. {
  450.    if ((c >= 'A' && c <= 'Z') ||
  451.        (c >= 'a' && c <= 'z'))
  452.    {
  453.       return 1;
  454.    }
  455.  
  456.    return 0;
  457. }
  458.  
  459. int isdigit(char c)
  460. {
  461.    if (c >= '0' && c <= '9')
  462.    {
  463.       return 1;
  464.    }
  465.  
  466.    return 0;
  467. }
  468.  
  469. int islower(char c)
  470. {
  471.    if (c >= 'a' && c <= 'z')
  472.    {
  473.       return 1;
  474.    }
  475.  
  476.    return 0;
  477. }
  478.  
  479. int isupper(char c)
  480. {
  481.    if (c >= 'A' && c <= 'Z')
  482.    {
  483.       return 1;
  484.    }
  485.  
  486.    return 0;
  487. }
  488.  
  489. int isascii(char c)
  490. {
  491.    if ((unsigned int)c <= 0x7F)
  492.    {
  493.       return 1;
  494.    }
  495.  
  496.    return 0;
  497. }
  498.  
  499. int iscntrl(char c)
  500. {
  501.    if ((c >= 0 && c <= 0x1F) || c == 0x7F)
  502.    {
  503.       return 1;
  504.    }
  505.  
  506.    return 0;
  507. }
  508.  
  509. int isgraph(char c)
  510. {
  511.    if ((unsigned int)c > 0x7F)
  512.    {
  513.       return 1;
  514.    }
  515.  
  516.    return 0;
  517. }
  518.  
  519. int isprint(char c)
  520. {
  521.    if (c >= 0x20 && c < 0x7F)
  522.    {
  523.       return 1;
  524.    }
  525.  
  526.    return 0;
  527. }
  528.  
  529. int ispunct(char c)
  530. {
  531.    if ((c >= ' ' && c <= '/') ||
  532.        (c >= ':' && c <= '@') ||
  533.        (c >= '[' && c <= '`') ||
  534.        (c >= '{' && c <= '~'))
  535.    {
  536.       return 1;
  537.    }
  538.  
  539.    return 0;
  540. }
  541.  
  542. int isspace(char c)
  543. {
  544.    if (c == ' ' || c == '\t')
  545.    {
  546.       return 1;
  547.    }
  548.  
  549.    return 0;
  550. }
  551.  
  552. int isxdigit(char c)
  553. {
  554.    if ((c >= '0' && c <= '9') ||
  555.        (c >= 'A' && c <= 'F') ||
  556.        (c >= 'a' && c <= 'f'))
  557.    {
  558.       return 1;
  559.    }
  560.  
  561.    return 0;
  562. }
  563.  
  564. int memcmp(const void *m1, const void *m2, int l)
  565. {
  566.    const unsigned char *d;
  567.    const unsigned char *s;
  568.  
  569.    d = (const unsigned char *)m1;
  570.    s = (const unsigned char *)m2;
  571.  
  572.    while (l-- > 0)
  573.    {
  574.       if (*d > *s)
  575.       {
  576.          return 1;
  577.       }
  578.  
  579.       if (*d < *s)
  580.       {
  581.          return -1;
  582.       }
  583.  
  584.       d++;
  585.       s++;
  586.    }
  587.  
  588.    return 0;
  589. }
  590.  
  591. void *memcpy(const void *m1, const void *m2, int l)
  592. {
  593.    unsigned char *d;
  594.    const unsigned char *s;
  595.  
  596.    d = (unsigned char *)m1;
  597.    s = (const unsigned char *)m2;
  598.  
  599.    while (l-- > 0)
  600.    {
  601.       *d++ = *s++;
  602.    }
  603.  
  604.    return (void *)m1;
  605. }
  606.  
  607. void *memset(void *m, int v, int l)
  608. {
  609.    unsigned char *d;
  610.  
  611.    d = (unsigned char *)m;
  612.  
  613.    while (l-- > 0)
  614.    {
  615.       *d++ = (unsigned char)v;
  616.    }
  617.  
  618.    return m;
  619. }
  620.  
  621. void swap(int *a, int *b)
  622. {
  623.    int t = *a;
  624.    *a = *b;
  625.    *b = t;
  626. }
  627.  
  628. // End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement