Re: FreeTDS port to VMS V9.x on x86?

Liste des GroupesRevenir à co vms 
Sujet : Re: FreeTDS port to VMS V9.x on x86?
De : cross (at) *nospam* spitfire.i.gajendra.net (Dan Cross)
Groupes : comp.os.vms
Date : 04. Jul 2025, 05:38:29
Autres entêtes
Organisation : PANIX Public Access Internet and UNIX, NYC
Message-ID : <1047ls5$aie$2@reader1.panix.com>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <10479sq$edc4$1@dont-email.me>,
Arne Vajhøj  <arne@vajhoej.dk> wrote:
On 7/3/2025 9:00 PM, Craig A. Berry wrote:
[snip]
I haven't done any testing of your code.  I do agree with Dan that
reducing the repeated code would be a benefit.  You don't need the first
#if because the file won't even be built if HAS_VASPRINTF is detected
during configuration.
>
I know. But just in case it did get called.
>
                      Which means for the remaining 4 cases you've got
this in all of them that could be put outside the #ifdefs, and without
any nesting:
 
va_list cp;
va_copy(cp, ap);
<something1>
(len < 0) return len;
<something2>
*ret = malloc(len + 1);
if(!*ret) return -1;
return vsprintf(*ret, fmt, cp);
 
where something1 is always 1-3 lines and something2 is zero or one line
>
There are duplicated lines.
>
But I still believe the one block of code per case is more readable
than common code + some case specific code + common code + some
case specific code + common code.

Do you have a rationale for that belief, or is it entirely
subjective?  It's ok if it is, but best to be up front about it
if so.

Also imagine if someone was to add yet another approach. Now it
is just put in the #if for the case and then write the block of
code. If mixing it would all depend on the new approach - maybe
it would fit into the existing structure, maybe it would require
changing the structure if it does not fit.

Here's an example, with the "try to vsnprintf into a buffer and
just copy if that succeds" approach I mentioned earlier.

There's no nesting of ifdefs, but because the calculation of the
buffer size is decoupled from the logic of allocation and
formatting, the latter is simpler.

- Dan C.

/*
 * SPDX-License-Identifier: LGPL 2.1 OR Apache 2.0
 *
 * vasprintf implementation
 */

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

static const size_t FAIL = ~(size_t)0;

static inline int
tds_vsnprintf(char *dst, size_t len, const char *fmt, va_list ap)
{
#if defined(HAVE_VSNPRINTF)
return vsnprintf(dst, len, fmt, ap);
#else
assert(dst != NULL);
assert(len != 0);
return vsprintf(dst, fmt, ap);
#endif
}

#if defined(REENTRANT)
#define neednull(fp) 1
#define putnull(fp) fclose(fp)
#else
#define neednull(fp) ((fp) == NULL)
#define putnull(fp)
#endif

static inline size_t
tds_vasprintf_bufsize(const char *fmt, va_list ap)
{
int len;
#if defined(HAVE_VSCPRINTF)
len = _vscprintf(fmt, ap);
#elif defined(HAVE_VSNPRINTF)
len = vsnprintf(NULL, 0, fmt, ap);
#else
static FILE *fp = NULL;
if (neednull(fp))
fp = fopen(TDS_PATH_DEVNULL, "w");
if (fp == NULL)
return FAIL;
len = vfprintf(fp, fmt, ap);
putnull(fp);
#endif
if(len < 0)
return FAIL;
return (size_t)len + 1;
}

int
tds_vasprintf(char **outp, const char *fmt, va_list ap)
{
#if defined(HAVE_VASPRINTF)
return vasprintf(outp, fmt, ap);
#elif !defined(va_copy)
#error "va_copy macro required"
#endif
va_list save_ap;
va_copy(save_ap, ap);
char *dst;
size_t len;
int rv;
#if defined(HAVE_VSNPRINTF)
char buf[1024];
rv = vsnprintf(buf, sizeof(buf), fmt, ap);
if (rv < 0)
return -1;
len = (size_t)rv + 1;
if (len <= sizeof(buf)) {
dst = malloc(len);
if (dst == NULL)
return -1;
memcpy(dst, buf, len);
*outp = dst;
return rv;
}
#else
len = tds_vasprintf_bufsize(fmt, ap);
#endif
if (len == FAIL)
return -1;
dst = malloc(len);
if (dst == NULL)
return -1;
rv = tds_vsnprintf(dst, len, fmt, save_ap);
*outp = dst;
return rv;
}

#if defined(TEST)

#include <assert.h>
#include <string.h>

char *
dovasprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char *out = NULL;
int rv = tds_vasprintf(&out, fmt, ap);
va_end(ap);
if (rv < 0)
return NULL;
return out;
}

int
main()
{
char *p = dovasprintf("This is the %dth test: %s", 10, "test string");
assert(strcmp(p, "This is the 10th test: test string") == 0);
return EXIT_SUCCESS;
}

#endif


Date Sujet#  Auteur
3 Jun 25 * FreeTDS port to VMS V9.x on x86?56Richard Jordan
3 Jun 25 `* Re: FreeTDS port to VMS V9.x on x86?55Arne Vajhøj
3 Jun 25  `* Re: FreeTDS port to VMS V9.x on x86?54Arne Vajhøj
3 Jun 25   +* Re: FreeTDS port to VMS V9.x on x86?45Craig A. Berry
5 Jun 25   i`* Re: FreeTDS port to VMS V9.x on x86?44Arne Vajhøj
5 Jun 25   i `* Re: FreeTDS port to VMS V9.x on x86?43Arne Vajhøj
5 Jun 25   i  `* Re: FreeTDS port to VMS V9.x on x86?42Simon Clubley
5 Jun 25   i   `* Re: FreeTDS port to VMS V9.x on x86?41Craig A. Berry
6 Jun 25   i    `* Re: FreeTDS port to VMS V9.x on x86?40Arne Vajhøj
6 Jun 25   i     `* Re: FreeTDS port to VMS V9.x on x86?39Craig A. Berry
6 Jun 25   i      `* Re: FreeTDS port to VMS V9.x on x86?38Arne Vajhøj
6 Jun 25   i       `* Re: FreeTDS port to VMS V9.x on x86?37Craig A. Berry
6 Jun 25   i        `* Re: FreeTDS port to VMS V9.x on x86?36Arne Vajhøj
6 Jun 25   i         `* Re: FreeTDS port to VMS V9.x on x86?35Arne Vajhøj
23 Jun 25   i          `* Re: FreeTDS port to VMS V9.x on x86?34Craig A. Berry
24 Jun 25   i           `* Re: FreeTDS port to VMS V9.x on x86?33Arne Vajhøj
24 Jun 25   i            `* Re: FreeTDS port to VMS V9.x on x86?32Lawrence D'Oliveiro
24 Jun 25   i             `* Re: FreeTDS port to VMS V9.x on x86?31Arne Vajhøj
24 Jun 25   i              +* Re: FreeTDS port to VMS V9.x on x86?3Lawrence D'Oliveiro
24 Jun 25   i              i`* Re: FreeTDS port to VMS V9.x on x86?2Arne Vajhøj
24 Jun 25   i              i `- Re: FreeTDS port to VMS V9.x on x86?1Lawrence D'Oliveiro
24 Jun 25   i              `* Re: FreeTDS port to VMS V9.x on x86?27Craig A. Berry
24 Jun 25   i               `* Re: FreeTDS port to VMS V9.x on x86?26Arne Vajhøj
24 Jun 25   i                +- Re: FreeTDS port to VMS V9.x on x86?1Arne Vajhøj
24 Jun 25   i                `* Re: FreeTDS port to VMS V9.x on x86?24Arne Vajhøj
24 Jun 25   i                 `* Re: FreeTDS port to VMS V9.x on x86?23Craig A. Berry
24 Jun 25   i                  `* Re: FreeTDS port to VMS V9.x on x86?22Arne Vajhøj
25 Jun 25   i                   +* Re: FreeTDS port to VMS V9.x on x86?19Arne Vajhøj
25 Jun 25   i                   i`* Re: FreeTDS port to VMS V9.x on x86?18Craig A. Berry
1 Jul 25   i                   i `* Re: FreeTDS port to VMS V9.x on x86?17Arne Vajhøj
2 Jul 25   i                   i  +- Re: FreeTDS port to VMS V9.x on x86?1Lawrence D'Oliveiro
3 Jul 25   i                   i  `* Re: FreeTDS port to VMS V9.x on x86?15Arne Vajhøj
3 Jul18:40   i                   i   `* Re: FreeTDS port to VMS V9.x on x86?14Dan Cross
3 Jul19:15   i                   i    `* Re: FreeTDS port to VMS V9.x on x86?13Arne Vajhøj
3 Jul21:07   i                   i     +* Re: FreeTDS port to VMS V9.x on x86?9Dan Cross
3 Jul23:31   i                   i     i+* Re: FreeTDS port to VMS V9.x on x86?6Craig A. Berry
4 Jul01:24   i                   i     ii+* Re: FreeTDS port to VMS V9.x on x86?4Arne Vajhøj
4 Jul02:00   i                   i     iii`* Re: FreeTDS port to VMS V9.x on x86?3Craig A. Berry
4 Jul02:14   i                   i     iii `* Re: FreeTDS port to VMS V9.x on x86?2Arne Vajhøj
4 Jul05:38   i                   i     iii  `- Re: FreeTDS port to VMS V9.x on x86?1Dan Cross
4 Jul05:31   i                   i     ii`- Re: FreeTDS port to VMS V9.x on x86?1Dan Cross
4 Jul01:21   i                   i     i`* Re: FreeTDS port to VMS V9.x on x86?2Arne Vajhøj
4 Jul06:06   i                   i     i `- Re: FreeTDS port to VMS V9.x on x86?1Dan Cross
4 Jul01:51   i                   i     `* Re: FreeTDS port to VMS V9.x on x86?3Lawrence D'Oliveiro
4 Jul02:21   i                   i      `* Re: FreeTDS port to VMS V9.x on x86?2Arne Vajhøj
4 Jul02:41   i                   i       `- Re: FreeTDS port to VMS V9.x on x86?1Lawrence D'Oliveiro
27 Jun 25   i                   `* Re: FreeTDS port to VMS V9.x on x86?2Lawrence D'Oliveiro
1 Jul 25   i                    `- Re: FreeTDS port to VMS V9.x on x86?1Arne Vajhøj
3 Jun 25   +- Re: FreeTDS port to VMS V9.x on x86?1Richard Jordan
14 Jun 25   `* Re: FreeTDS port to VMS V9.x on x86?7Arne Vajhøj
15 Jun 25    `* Re: FreeTDS port to VMS V9.x on x86?6Lawrence D'Oliveiro
15 Jun 25     `* Re: FreeTDS port to VMS V9.x on x86?5Arne Vajhøj
15 Jun 25      `* Re: FreeTDS port to VMS V9.x on x86?4Lawrence D'Oliveiro
16 Jun 25       `* Re: FreeTDS port to VMS V9.x on x86?3Arne Vajhøj
16 Jun 25        `* Re: FreeTDS port to VMS V9.x on x86?2Lawrence D'Oliveiro
16 Jun 25         `- Re: FreeTDS port to VMS V9.x on x86?1Arne Vajhøj

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal