Sfoglia il codice sorgente

Update the sbuffer module.

Use the latest sbuffer module from my libnotch library.
develop
parent
commit
ab04d35c7b
4 ha cambiato i file con 86 aggiunte e 112 eliminazioni
  1. +0
    -2
      lib/Makefile.am
  2. +62
    -88
      lib/sbuffer.c
  3. +12
    -11
      lib/sbuffer.h
  4. +12
    -11
      src/fmail.c

+ 0
- 2
lib/Makefile.am Vedi File

@ -4,5 +4,3 @@ libfmail_a_SOURCES = err.compat.h compat.h xmem.c xmem.h \
sbuffer.c sbuffer.h
libfmail_a_LIBADD = $(LIBOBJS)
DEFS = -DEXIT_ON_ENOMEM @DEFS@

+ 62
- 88
lib/sbuffer.c Vedi File

@ -1,6 +1,6 @@
/*
* sbuffer - Incenp.org Notch Library: string buffer module
* Copyright (C) 2011 Damien Goutte-Gattat
* Copyright (C) 2011,2018 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -25,84 +25,52 @@
#include <errno.h>
#include <string.h>
#ifdef EXIT_ON_ENOMEM
#include <xmem.h>
#endif
struct string_buffer_t
{
char *buffer;
size_t len;
size_t size;
size_t block;
};
#define AVAILABLE_SIZE(s) ((s)->size - (s)->len)
#define CURSOR(s) ((s)->buffer + (s)->len)
#ifdef EXIT_ON_NOMEM
#define GROW(s,l) \
do { \
(s)->size = ((((s)->len + (l)) / (s)->block) + 1) * (s)->block; \
(s)->buffer = xrealloc((s)->buffer, (s)->size); \
} while ( 0 )
#else
#define GROW(s,l) \
do { \
size_t ns = ((((s)->len + (l)) / (s)->block) + 1) * (s)->block; \
char *np = realloc((s)->buffer, ns); \
char *np = xrealloc((s)->buffer, ns); \
if ( ! np ) \
return -1; \
(s)->buffer = np; \
(s)->size = ns; \
(s)->error = errno; \
else { \
(s)->buffer = np; \
(s)->size = ns; \
} \
} while ( 0 )
#endif
string_buffer_t *
sb_new(size_t block_size)
int
sb_init(string_buffer_t *s, size_t block_size)
{
string_buffer_t *s;
if ( ! block_size )
block_size = 128;
#ifdef EXIT_ON_ENOMEM
s = xmalloc(sizeof(*s));
s->block = s->size = block_size;
s->buffer = xmalloc(block_size);
sb_empty(s);
#else
if ( (s = malloc(sizeof(*s))) ) {
s->block = s->size = block_size;
if ( (s->buffer = malloc(block_size)) )
sb_empty(s);
else {
free(s);
s = NULL;
}
if ( ! s ) {
errno = EINVAL;
return -1;
}
#endif
return s;
}
s->buffer = NULL;
s->len = s->size = 0;
s->block = block_size ? block_size : 128;
s->error = 0;
void
sb_free(string_buffer_t *s)
{
if ( s ) {
free(s->buffer);
free(s);
}
return 0;
}
const char *
sb_get(string_buffer_t *s)
{
return s ? s->buffer : NULL;
const char *str = NULL;
if ( ! s )
errno = EINVAL;
else if ( s->error )
errno = s->error;
else
str = s->buffer;
return str;
}
char *
@ -110,25 +78,16 @@ sb_get_copy(string_buffer_t *s)
{
char *str = NULL;
if ( s ) {
#ifdef EXIT_ON_ENOMEM
str = xmalloc(s->len + 1);
#else
if ( ! (str = malloc(s->len + 1)) )
return NULL;
#endif
strncpy(str, s->buffer, s->len);
}
if ( ! s )
errno = EINVAL;
else if ( s->error )
errno = s->error;
else if ( (str = xmalloc(s->len + 1)) )
strncpy(str, s->buffer, s->len + 1);
return str;
}
size_t
sb_len(string_buffer_t *s)
{
return s ? s->len : 0;
}
int
sb_empty(string_buffer_t *s)
{
@ -137,8 +96,9 @@ sb_empty(string_buffer_t *s)
return -1;
}
s->buffer[0] = '\0';
s->len = 0;
if ( s->buffer )
s->buffer[0] = '\0';
return 0;
}
@ -151,11 +111,13 @@ sb_addc(string_buffer_t *s, char c)
return -1;
}
if ( AVAILABLE_SIZE(s) < 1 )
GROW(s, 1);
if ( AVAILABLE_SIZE(s) < 2 )
GROW(s, 2);
*(s->buffer + s->len++) = c;
*(s->buffer + s->len) = '\0';
if ( ! s->error ) {
*(s->buffer + s->len++) = c;
*(s->buffer + s->len) = '\0';
}
return 0;
}
@ -179,11 +141,13 @@ sb_addn(string_buffer_t *s, const char *buffer, size_t len)
return -1;
}
if ( AVAILABLE_SIZE(s) < len )
GROW(s, len);
if ( AVAILABLE_SIZE(s) < len + 1)
GROW(s, len + 1);
strncat(CURSOR(s), buffer, len);
s->len += len;
if ( ! s->error ) {
strncat(CURSOR(s), buffer, len);
s->len += len;
}
return 0;
}
@ -199,11 +163,19 @@ sb_vaddf(string_buffer_t *s, const char *fmt, va_list ap)
return -1;
}
if ( ! s->size )
GROW(s, s->block);
if ( s->error )
return 0;
va_copy(aq, ap);
written = vsnprintf(CURSOR(s), AVAILABLE_SIZE(s), fmt, aq);
va_end(aq);
if ( written >= AVAILABLE_SIZE(s) ) {
if ( written >= (int)AVAILABLE_SIZE(s) ) {
GROW(s, written);
if ( s->error )
return 0;
vsprintf(CURSOR(s), fmt, ap);
}
s->len += written;
@ -214,7 +186,7 @@ sb_vaddf(string_buffer_t *s, const char *fmt, va_list ap)
int
sb_addf(string_buffer_t *s, const char *fmt, ...)
{
int ret;
int ret = 0;
va_list ap;
if ( ! s || ! fmt ) {
@ -222,9 +194,11 @@ sb_addf(string_buffer_t *s, const char *fmt, ...)
return -1;
}
va_start(ap, fmt);
ret = sb_vaddf(s, fmt, ap);
va_end(ap);
if ( ! s->error ) {
va_start(ap, fmt);
ret = sb_vaddf(s, fmt, ap);
va_end(ap);
}
return ret;
}

+ 12
- 11
lib/sbuffer.h Vedi File

@ -1,6 +1,6 @@
/*
* sbuffer - Incenp.org Notch Library: string buffer module
* Copyright (C) 2011 Damien Goutte-Gattat
* Copyright (C) 2011,2018 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -22,17 +22,21 @@
#include <stdlib.h>
#include <stdarg.h>
typedef struct
{
char *buffer;
size_t len;
size_t size;
size_t block;
int error;
} string_buffer_t;
#ifdef __cpluscplus
extern "C" {
#endif
typedef struct string_buffer_t string_buffer_t;
string_buffer_t *
sb_new(size_t);
void
sb_free(string_buffer_t *);
int
sb_init(string_buffer_t *, size_t);
const char *
sb_get(string_buffer_t *);
@ -40,9 +44,6 @@ sb_get(string_buffer_t *);
char *
sb_get_copy(string_buffer_t *);
size_t
sb_len(string_buffer_t *);
int
sb_empty(string_buffer_t *);


+ 12
- 11
src/fmail.c Vedi File

@ -636,7 +636,7 @@ int
main(int argc, char *argv[])
{
char c, with_useragent, with_date;
string_buffer_t *headers;
string_buffer_t headers;
fmail_ctx_t ctx;
int do_sign;
@ -664,7 +664,7 @@ main(int argc, char *argv[])
with_useragent = with_date = do_sign = 0;
headers = sb_new(0);
sb_init(&headers, 0);
ctx.crypto_ctx = NULL;
ctx.magic_ctx = initialize_magic();
ctx.attachments = NULL;
@ -714,23 +714,23 @@ main(int argc, char *argv[])
break;
case 'H':
sb_addf(headers, "%s\r\n", optarg);
sb_addf(&headers, "%s\r\n", optarg);
break;
case 'F':
sb_addf(headers, "From: %s\r\n", optarg);
sb_addf(&headers, "From: %s\r\n", optarg);
break;
case 'T':
sb_addf(headers, "To: %s\r\n", optarg);
sb_addf(&headers, "To: %s\r\n", optarg);
break;
case 'C':
sb_addf(headers, "Cc: %s\r\n", optarg);
sb_addf(&headers, "Cc: %s\r\n", optarg);
break;
case 'S':
sb_addf(headers, "Subject: %s\r\n", optarg);
sb_addf(&headers, "Subject: %s\r\n", optarg);
break;
case 'U':
@ -748,19 +748,20 @@ main(int argc, char *argv[])
/* Generate automatic headers */
if ( with_date )
sb_addf(headers, "Date: %s\r\n", rfc2822_date());
sb_addf(&headers, "Date: %s\r\n", rfc2822_date());
if ( with_useragent )
sb_addf(headers, "User-Agent: fmail %s\r\n", VERSION);
sb_addf(&headers, "User-Agent: fmail %s\r\n", VERSION);
/* Fire editor instead of reading from stdin? */
if ( ! ctx.input )
ctx.input = read_input_from_editor();
/* Read user-provided headers. */
read_headers(ctx.input, headers);
read_headers(ctx.input, &headers);
/* Write all headers. */
fprintf(stdout, "%s", sb_get(headers));
fprintf(stdout, "%s", headers.buffer);
free(headers.buffer);
if ( do_sign || ctx.rcp_count ) {
FILE *tmp = tmpfile();


Caricamento…
Annulla
Salva