[ale] My eyes are tired

Christopher Fowler cfowler at outpostsentinel.com
Tue Feb 21 21:45:02 EST 2006


I'm bringing over some code from another device to a new device.  It is
basically a mini string library that helps us deal with strings in C.  I
think it is all working right but I'm in segfault hell with other code
that uses it and my suspicions lie on this API.  I need another set of
eyes to take a look and see if they see it.  I'm tired of looking at it.

rn_string.c
-----------------------------------------------------------------------
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>

#include <sys/types.h>

char *
rn_string(const char *fmt, ...) {
        va_list p;
        char buffer[2048];
        int r_len = 0;
        char *ptr = NULL;

        va_start(p, fmt);
        vsnprintf(buffer , sizeof(buffer), fmt, p);
        va_end(p);

        r_len = strlen(buffer);

        ptr = malloc(r_len + 1);
        strncpy(ptr, buffer, r_len);

        return ptr;

}

void
rn_string_free(char *s) {
        if(s) {
                free(s);
        }

        return;
}

int
rn_string_length(char *s) {
        if(s) {
                return strlen(s);
        }
        return 0;
}
char *
rn_string_append(char *s, const char *fmt, ...) {
        va_list p;
        char buffer[2048];
        int r_len = 0;
        int s_len = 0;

        if(s == NULL) {
                return 0;
        }

        va_start(p, fmt);
        vsnprintf(buffer , sizeof(buffer), fmt, p);
        va_end(p);

        r_len = strlen(buffer);
        s_len = strlen(s);

        s = realloc(s, (s_len + r_len + 1));
        strncpy((s + s_len), buffer, (r_len));

        return s;

}


int
rn_string_to_file(char *s, const char *dest, mode_t mode) {
        int fd;

        if((fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1)
{
                return -1;
        }

        write(fd, s, rn_string_length(s));
        close(fd);

        return 0;

}
char *
rn_string_from_file(const char *src) {
        FILE *fp;
        char *str = NULL;
        char buffer[128];
        int tot = 0;


        if((fp = fopen(src, "r")) == NULL) {
                return NULL;
        }


        while(fgets(buffer, sizeof(buffer), fp) != NULL) {
                if(str == NULL) {
                        str = realloc(str, (strlen(buffer)+1));
                        memcpy(str, buffer, (strlen(buffer)));
                } else {
                        str = realloc(str, (tot + (strlen(buffer)+1)));
                        memcpy((str + tot), buffer, (strlen(buffer)));
                }

                tot += strlen(buffer);
        }

        fclose(fp);

        return str;

}

char *
rn_string_dup(const char *src) {
        if(src == NULL) {
                return NULL;
        }

        return strdup(src);
}
-----------------------------------------------------------------------





More information about the Ale mailing list