You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.1 KiB
C
85 lines
2.1 KiB
C
#include <iso646.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
|
|
|
|
enum {
|
|
UNKNOWN_FAIL = 1,
|
|
MALLOC_FAIL,
|
|
NEEDARG_FAIL,
|
|
FOPEN_FAIL,
|
|
WRITE_FAIL
|
|
};
|
|
|
|
|
|
/* arrccat -- function to copy arr2len bytes from arr2 to the arr1len'th position of arr1. returns *newarr, and stores the length */
|
|
/* of newarr[] in arr1len (that's why it needs to be a pointer to arr1len and not just a long unsigned integer). */
|
|
char *arrccat(char *arr1, char *arr2, size_t *arr1len, size_t arr2len)
|
|
{
|
|
char *newarr;
|
|
unsigned int i, i2;
|
|
newarr = malloc(*arr1len+arr2len);
|
|
i = i2 = 0;
|
|
|
|
for(i = 0; i < *arr1len; i++)
|
|
{
|
|
newarr[i] = arr1[i];
|
|
}
|
|
|
|
for(i2 = 0; i2 < arr2len; i2++, i++)
|
|
{
|
|
newarr[i] = arr2[i2];
|
|
}
|
|
|
|
*arr1len = *arr1len+arr2len;
|
|
|
|
return newarr;
|
|
}
|
|
|
|
/* Condition should be one of the above enum'd strings. Info is optional; it can just be NULL when irrelevant. */
|
|
void throw(long unsigned int condition, void *info)
|
|
{
|
|
switch(condition)
|
|
{
|
|
case UNKNOWN_FAIL: printf("unknown failure\n");
|
|
exit(UNKNOWN_FAIL);
|
|
case MALLOC_FAIL: printf("malloc failure\n");
|
|
exit(MALLOC_FAIL);
|
|
case NEEDARG_FAIL: printf("usage: %s\n", (char *) info);
|
|
exit(NEEDARG_FAIL);
|
|
case FOPEN_FAIL: printf("failed to open: %s\n", (char *) info);
|
|
exit(FOPEN_FAIL);
|
|
case WRITE_FAIL: printf("failed to write: %s\n", (char *) info);
|
|
exit(WRITE_FAIL);
|
|
default: printf("You shouldn't be seeing this.\nSomething is very wrong.\n");
|
|
exit(-256);
|
|
}
|
|
}
|
|
|
|
/* mastrcat -- improved string concat function. returns a pointer to the first element in a buffer containing the strings str1 */
|
|
/* and str2 joined end-to-end. */
|
|
char *mastrcat(char *str1, char *str2)
|
|
{
|
|
unsigned long int nbi, stri, nbsize;
|
|
char *nbuf;
|
|
nbi = stri = 0;
|
|
nbsize = (strlen(str1) + strlen(str2));
|
|
nbuf = malloc(nbsize);
|
|
|
|
for(stri = 0; str1[stri] != '\0'; nbi++, stri++)
|
|
{
|
|
nbuf[nbi] = str1[stri];
|
|
}
|
|
|
|
for(stri = 0; str2[stri] != '\0'; nbi++, stri++)
|
|
{
|
|
nbuf[nbi] = str2[stri];
|
|
}
|
|
|
|
return nbuf;
|
|
}
|
|
|