delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/05/27/18:24:27

From: "Mark E." <snowball3 AT bigfoot DOT com>
To: djgpp-workers AT delorie DOT com
Date: Sun, 27 May 2001 18:24:11 -0400
MIME-Version: 1.0
Subject: confstr implementation
Message-ID: <3B11464B.31331.D7FED@localhost>
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp-workers AT delorie DOT com

Hello,

I've implemented confstr and wrote some docs for it. This will enable us to 
implement and provide our own getconf.  I left out the boring patches to 
unistd.h (to add _CS_PATH), Makefile, etc.

/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

size_t
confstr(int name, char *buf, size_t len)
{
  size_t out_len = 0;

  switch (name)
  {
    case _CS_PATH:
    {
      char *djdir = getenv("DJDIR");
      if (djdir)
        out_len = snprintf(buf, len, "%s/bin", djdir);
    }
    default:
    {
      errno = EINVAL;
    }
  }
  return out_len;
}

And the documentation:

@node confstr, posix
@subheading Syntax

@example
#include <unistd.h>

size_t confstr(int name, char *buf, size_t len);
@end example

@subheading Description

This function stores various system-dependent configuration values
in @var{buf}.  @var{name} is one of the following:

@table @code

@item _CS_PATH

Returns a path to the standard POSIX utilities.

@end table

If @var{len} is not zero and @var{name} has a defined value, that value
is copied into @var{buf} and null terminated.  If the length of the string
to be copied plus the null terminator is greater than @var{len} bytes,
the string is truncated to @math{@var{len}-1} bytes and the result is
null terminated.

If @var{len} is zero, nothing is copied into @var{buf} and the size of the
buffer required to store the string is returned.

@subheading Return Value

If @var{name} has a defined value, the minimum size of the buffer required
to hold the string including the terminating null is returned.  If this value
is greater than @var{len}, then @var{buf} is truncated.

If @var{name} does not have a defined value, zero is returned and errno is
set to @code{EINVAL}.

@subheading Portability

@portability !ansi, posix

@subheading Example

@example
char *path;
size_t path_len;

path_len = confstr (_PC_PATH, NULL, 0);
path = malloc(path_len);
confstr(_PC_PATH, path, path_len);
@end example


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019