Clean solution to strdup() not being in ISO C.

This commit is contained in:
Martin Dvorak 2014-04-13 15:42:03 +02:00
parent 0ac76a6554
commit 37f684216a
5 changed files with 21 additions and 9 deletions

6
dist/ubuntu-env.sh vendored
View file

@ -1,6 +1,6 @@
#!/bin/bash
export HHVERSION="1.9.1"
export HHVERSION="1.9.10"
export HHFULLVERSION=${HHVERSION}-0ubuntu1
export HH=hh_${HHVERSION}
export HHRELEASE=hh_${HHFULLVERSION}
@ -9,10 +9,10 @@ export NOW=`date +%Y-%m-%d--%H-%M-%S`
export HHBUILD=hstr-${NOW}
## https://wiki.ubuntu.com/Releases
export UBUNTUVERSION=precise
#export UBUNTUVERSION=precise
#export UBUNTUVERSION=quantal
#export UBUNTUVERSION=saucy
#export UBUNTUVERSION=trusty
export UBUNTUVERSION=trusty
export HHBZRMSG="Favorites - favorite commands can be bookmarked and shown in a new view!"

View file

@ -11,12 +11,12 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "include/hstr_favorites.h"
#include "include/hstr_utils.h"
#define FAVORITE_SEGMENT_SIZE 10
extern char *strdup(const char *s);
void favorites_init(FavoriteItems *favorites)
{
favorites->items=NULL;
@ -68,7 +68,7 @@ void favorites_get(FavoriteItems *favorites)
while(pe!=NULL) {
favorites->items[i]=pb;
*pe=0;
favorites->items[i]=(char *)strdup(pb);
favorites->items[i]=hstr_strdup(pb);
pb=pe+1;
pe=strchr(pb, '\n');
i++;
@ -115,11 +115,11 @@ void favorites_add(FavoriteItems *favorites, char *newFavorite)
{
if(favorites->count) {
favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count);
favorites->items[favorites->count-1]=strdup(newFavorite);
favorites->items[favorites->count-1]=hstr_strdup(newFavorite);
favorites_choose(favorites, newFavorite);
} else {
favorites->items=malloc(sizeof(char*));
favorites->items[0]=strdup(newFavorite);
favorites->items[0]=hstr_strdup(newFavorite);
favorites->count=1;
}

View file

@ -8,11 +8,21 @@
*/
#include "include/hstr_utils.h"
#include <ctype.h>
#define DEFAULT_COMMAND "pwd"
#define PROC_HOSTNAME "/proc/sys/kernel/hostname"
// strdup() not in ISO C
char *hstr_strdup(const char * s)
{
size_t len = 1+strlen(s);
char *p = malloc(len);
return p ? memcpy(p, s, len) : NULL;
}
void tiocsti()
{
char buf[] = DEFAULT_COMMAND;

View file

@ -20,6 +20,7 @@
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
char *hstr_strdup(const char * s);
void tiocsti();
void fill_terminal_input(char* cmd, bool padding);
void reverse_char_pointer_array(char **array, unsigned length);

View file

@ -8,6 +8,7 @@
*/
#include "../src/include/hashset.h"
#include "../src/include/hstr_utils.h"
void testBlacklist() {
const char* commandBlacklist[] = { };
@ -19,7 +20,7 @@ void testBlacklist() {
}
for (i = 0; i < 5; i++) {
printf("match %d\n",
hashset_contains(&blacklist, strdup(commandBlacklist[i])));
hashset_contains(&blacklist, hstr_strdup(commandBlacklist[i])));
}
}