File: robstr.c

package info (click to toggle)
fastq-pair 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 804 kB
  • sloc: ansic: 289; sh: 38; makefile: 5
file content (41 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * strdup() is not part of POSIX and is causing crashes on my CentOS systems.
 *
 * This is a simple implementation of strdup() but we can't call it that!
 * 
 * For more information see http://stackoverflow.com/questions/8359966/strdup-returning-address-out-of-bounds
 */

#include "robstr.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char *dupstr(const char *s) {
	if (s == NULL) {
		fprintf(stderr, "Cannot duplicate a NULL string\n");
		return NULL;
	}
	char *const result = malloc(strlen(s) + 1);
	if (result == NULL) {
		fprintf(stderr, "Cannot allocate memory to duplicate %s\n", s);
		return NULL;
	}
	strcpy(result, s);
	return result;
}

char *catstr(const char *s, const char *t) {
	char *const result = malloc(strlen(s) + strlen(t) + 1);
	if (result == NULL) {
		fprintf(stderr, "Cannot allocate memory for catstr of %s and %s\n", s, t);
		exit(1);
	}
	unsigned i;
	for (i=0; i<strlen(s); i++) 
		result[i] = s[i];
	for (unsigned j=0; j<strlen(t); j++)
		result[i++] = t[j];
	result[i]='\0';
	return result;
}