Skip to content

Instantly share code, notes, and snippets.

@aaronryank
Last active June 12, 2020 07:05
Show Gist options
  • Save aaronryank/6a2cf32ff146ff3411516c6a5b63a4e4 to your computer and use it in GitHub Desktop.
Save aaronryank/6a2cf32ff146ff3411516c6a5b63a4e4 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include "include/string.h"
const char *errlist[] = {
"no error (operation succeeded", /* 0 */
"not owner", /* EPERM */
"no such file or directory", /* ENOENT */
"no such process", /* ESRCH */
"interrupted system call", /* EINTR */
"I/O error", /* EIO */
"no such device or address", /* ENXIO */
"argument list too long", /* E2BIG */
"exec format error", /* ENOEXEC */
"bad file number", /* EBADF */
"no children", /* ECHILD */
"no more processes", /* EAGAIN */
"not enough memory", /* ENOMEM */
"permission denied", /* EACCESS */
"bad address in system call argument", /* EFAULT */
"block device required", /* ENOTBLK */
"mount device busy", /* EBUSY */
"file already exists", /* EEXIST */
"cross-domain link", /* EXDEV */
"no such device", /* ENODEV */
"not a directory", /* ENOTDIR */
"illegal operation on a directory", /* EISDIR */
"invalid argument", /* EINVAL */
"file table overflow", /* ENFILE */
"too many open files", /* EMFILE */
"inappropriate device for ioctl", /* ENOTTY */
"text file or pseudo-device busy", /* ETXTBSY */
"file too large", /* EFBIG */
"no space left in file system domain", /* ENOSPC */
"illegal seek", /* ESPIPE */
"read-only file system", /* EROFS */
"too many links", /* EMLINK */
"broken pipe", /* EPIPE */
"math argument out of range", /* EDOM */
"math result unrepresentable", /* ERANGE */
"operation would block", /* EWOULDBLOCK */
"operation now in progress", /* EINPROGRESS */
"operation already in progress", /* EALREADY */
"socket operation on non-socket", /* ENOTSOCK */
"destination address required", /* EDESTADDRREQ */
"message too long", /* EMSGSIZE */
"protocol wrong type for socket", /* EPROTOTYPE */
"bad proocol option", /* ENOPROTOOPT */
"protocol not suppored", /* EPROTONOSUPPORT */
"socket type not supported", /* ESOCKTNOSUPPORT */
"operation not supported on socket", /* EOPNOTSUPP */
"protocol family not supported", /* EPFNOSUPPORT */
"address family not supported by protocol family", /* EAFNOSUPPORT */
"address already in use", /* EADDRINUSE */
"can't assign requested address", /* EADDRNOTAVAIL */
"network is down", /* ENETDOWN */
"network is unreachable", /* ENETUNREACH */
"network dropped connection on reset", /* ENETRESET */
"software caused connection abort", /* ECONNABORTED */
"connection reset by peer", /* ECONNRESET */
"no buffer space available", /* ENOBUFS */
"socket is already connected", /* EISCONN */
"socket is not connected", /* ENOTCONN */
"can't send afer socket shutdown", /* ESHUTDOWN */
"undefined error (59)", /* not used */
"connection timed out", /* ETIMEDOUT */
"connection refused", /* ECONNREFUSED */
"too many levels of symbolic links", /* ELOOP */
"file name too long", /* ENAMETOOLONG */
"host is down", /* EHOSTDOWN */
"host is unreachable", /* EHOSTUNREACH */
"directory not empty", /* ENOTEMPTY */
"too many processes", /* EPROCLIM */
"too many users", /* EUSERS */
"disk quota exceeded", /* EDQUOT */
"stale remote file handle", /* ESTALE */
"pathname hit remote file system", /* EREMOTE */
"undefined error (72)", /* not used */
"undefined error (73)", /* not used */
"undefined error (74)", /* not used */
"undefined error (75)", /* not used */
"undefined error (76)", /* not used */
"identifier removed", /* EIDRM */
};
char * strerror (int error)
{
int err_count = (sizeof(errlist)/sizeof(char *));
if ((error <= err_count) && (error > 0))
return (char *) errlist[error];
}
int strlen (const char *str)
{
int i;
while (*str++)
i++;
return i;
}
char * strchr(const char *s, int c)
{
const char chr = c;
for (; *s != chr; s++)
if (*s == '\0')
return 0;
return (char *) s;
}
int chr_eq(char c, const char *delim)
{
while (*delim)
if (c == *delim++)
return 1;
return 0;
}
char * strpbrk (const char *s, const char *accept)
{
for (; !chr_eq(*s, accept); s++)
if (*s == '\0')
return 0;
return (char *) s;
}
char * strrchr (const char *s, int c)
{
const char chr = c;
// seek to the last character
while (*s)
s++;
s--; // if we leave it at that, we return NULL every time
for (; *s != chr; s--)
if (*s == '\0')
return 0;
return (char *) s;
}
int strspn (const char *s, const char *accept)
{
int count = 0;
for (; *s; *s++)
count += chr_eq(*s, accept);
return count;
}
int strcspn (const char *s, const char *reject)
{
int count = 0;
for (; *s; *s++)
count += !chr_eq(*s, reject);
return count;
}
int strcmp (const char *s1, const char *s2)
{
int i;
for (i = 0; s1[i] || s2[i]; i++) {
if (s1[i] == s2[i])
continue;
else if (s1[i] > s2[i])
return 1;
else if (s1[i] < s2[i])
return -1;
}
return 0;
}
int strncmp (const char *s1, const char *s2, unsigned int n)
{
int i;
for (i = 0; (s1[i] || s2[i]) && (i < n); i++) {
if (s1[i] == s2[i])
continue;
else if (s1[i] > s2[i])
return 1;
else if (s1[i] < s2[i])
return -1;
}
return 0;
}
char * strncpy (char *dest, const char *src, unsigned int n)
{
while ((*dest++ = *src++) && (n-- > 1))
;
*dest = 0;
return dest;
}
char * strcpy (char *dest, const char *src)
{
while (*dest++ = *src++)
;
*dest = 0;
return dest;
}
char * strncat (char *dest, const char *src, unsigned int bytes)
{
while (*dest++)
;
*dest--;
while (*src && (bytes-- > 0))
*dest++ = *src++;
return dest;
}
char * strcat (char *dest, const char *src)
{
while (*dest++)
;
*dest--;
while (*src)
*dest++ = *src++;
return dest;
}
char * strtok (char *str, const char *delim)
{
static char *ptr;
static int ptr_idx = 0;
char *retval = malloc(1024);
int ret_idx = 0;
memset(retval, 0, 1024);
if (str)
ptr = str;
// skip delimiters
while (chr_eq(ptr[ptr_idx], delim)) ptr_idx++;
// get string
for (; !chr_eq(ptr[ptr_idx], delim) && ptr[ptr_idx]; retval[ret_idx++] = ptr[ptr_idx++]);
// skip delimiters
while (chr_eq(ptr[ptr_idx], delim)) ptr_idx++;
return retval;
}
int strxfrm (char *dst, const char *src, unsigned int n)
{
int len, size;
len = strlen(src);
if (n) {
size = len < n ? len : n - 1;
memcpy(dst, src, size);
dst[size] = 0;
}
return len;
}
// VERY simplified.
void * memset(void *s, int c, unsigned int n)
{
register char *dst;
dst = s;
while (n--)
*dst++ = c;
return s;
}
void * memcpy (void *dst0, const void *src0, unsigned int bytes)
{
char *dst = dst0;
const char *src = src0;
if (!bytes || dst == src)
return dst0;
if ((unsigned long) dst < (unsigned long) src)
{
while ((*dst++ = *src++) && bytes--)
;
}
else {
dst += bytes;
src += bytes;
while ((*--dst = *--src) && bytes--)
;
}
return dst0;
}
void * memchr(const void *s, int c, unsigned int n)
{
const char *str = s;
for (; *str != c; str++)
if (*str == '\0')
return NULL;
return (void *) str;
}
int memcmp (const void *p1, const void *p2, unsigned int n)
{
const char *s1 = p1;
const char *s2 = p2;
int i;
return strncmp(s1, s2, n);
}
// strindex from K&R: search for a string in a string
// Provided instead of strstr.
int strindex(const char *s, const char *t)
{
int i, j, k, last = -1;
for (i = 0; s[i]; i++) {
for (j = i, k = 0; t[k] && s[j] == t[k]; j++, k++)
;
if (k > 0 && t[k] == '\0')
last = i;
}
return last;
}
char * strpchr(char *s, char c)
{
int i, l = strlen(s);
for (i = l; i; i--)
s[i] = s[i-1];
s[0] = c;
return s;
}
char * strsnp(char *str, unsigned int idx)
{
int i, l = strlen(str);
for (i = idx; i < l; i++)
str[i] = str[i+1];
return str;
}
char *reverse(char *str)
{
char tmp, *src, *dst;
size_t len;
if (!str)
return NULL;
/* this is inefficient as he'll */
len = strlen(str);
if (len > 1) {
src = str;
dst = src + len - 1;
while (src < dst) {
tmp = *src;
*src++ = *dst;
*dst-- = tmp;
}
}
return str;
}
@BaseMax
Copy link

BaseMax commented Jun 12, 2020

Your link is 404 Page not found, Anyway good thing.

Written for no good reason. Gist'ed for http://stackoverflow.com/a/42771998/6850771

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment