[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ale] My eyes are tired
- Subject: [ale] My eyes are tired
- From: jknapka at kneuro.net (Joe Knapka)
- Date: Tue, 21 Feb 2006 21:18:34 -0700
- In-reply-to: <[email protected]>
- References: <[email protected]>
Christopher Fowler wrote:
>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.
>
>
What jumps out at first glance is that you should probably start by
checking the
return values of all malloc() and realloc() calls.
How much are you paying me for this?
-- JK
>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);
>}
>-----------------------------------------------------------------------
>
>
>_______________________________________________
>Ale mailing list
>Ale at ale.org
>http://www.ale.org/mailman/listinfo/ale
>
>
>
>