From: "Tim Van Holder" To: "DJGPP-Workers" Subject: Can a64, l64a and the *env functions go in? Date: Mon, 18 Jun 2001 21:35:17 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Importance: Normal Reply-To: djgpp-workers AT delorie DOT com Just looking for final confirmation - these should be identical to the last drafts I posted. Also, I'd like to know if the additions to pwd/group I (re)posted a short while ago can go in (adding pw_passwd, pw_gecos and gr_passwd). I'll send another diff if required. Index: include/stdlib.h =================================================================== RCS file: /cvs/djgpp/djgpp/include/stdlib.h,v retrieving revision 1.9 diff -u -r1.9 stdlib.h --- include/stdlib.h 2001/06/08 09:59:33 1.9 +++ include/stdlib.h 2001/06/18 19:28:01 @@ -77,6 +77,12 @@ #ifndef __STRICT_ANSI__ +long a64l(const char *_string); +char * l64a(long _value); +int putenv(char *_val); +int setenv(const char *_var, const char *_val, int _overwrite); +int unsetenv(const char *_var); + #ifndef _POSIX_SOURCE typedef struct { @@ -106,9 +112,7 @@ void * memalign (size_t _amt, size_t _align); long mrand48(void); unsigned long nrand48(unsigned short _state[3]); -int putenv(const char *_val); unsigned short *seed48(unsigned short _state_seed[3]); -int setenv(const char *_var, const char *_val, int _replace); void srand48(long _seedval); int stackavail(void); long double _strtold(const char *_s, char **_endptr); Index: include/libc/stubs.h =================================================================== RCS file: /cvs/djgpp/djgpp/include/libc/stubs.h,v retrieving revision 1.8 diff -u -r1.8 stubs.h --- include/libc/stubs.h 2001/06/01 15:23:36 1.8 +++ include/libc/stubs.h 2001/06/18 19:28:01 @@ -27,6 +27,7 @@ #define lseek __lseek #define mkdir __mkdir #define open __open +#define putenv __putenv #define read __read #define tzset __tzset #define write __write @@ -53,7 +54,6 @@ #define movedata __movedata #define pow10 __pow10 #define pow2 __pow2 -#define putenv __putenv #define readlink __readlink #define sbrk __sbrk #define setitimer __setitimer Index: src/docs/kb/wc204.txi =================================================================== RCS file: /cvs/djgpp/djgpp/src/docs/kb/wc204.txi,v retrieving revision 1.74 diff -u -r1.74 wc204.txi --- src/docs/kb/wc204.txi 2001/06/08 10:19:28 1.74 +++ src/docs/kb/wc204.txi 2001/06/18 19:28:03 @@ -470,3 +470,19 @@ was fixed to print an error message and exit to DOS with an exit status that indicates a failure, when a selector for DOS memory could not be allocated by the DPMI host. + +@findex l64a AT r{, added} +@findex a64l AT r{, added} +Two new functions, @code{l64a} and @code{a64l}, allow you to convert a +32-bit integer value to and from a base-64 encoding, respectively. +Note that the encoding used is the one mandated by the current @sc{posix} +draft standard, and not the one used by either @command{uuencode} or the +@sc{mime} base64 encoding. + +@findex setenv AT r{, minor changes for @sc{posix}} +@findex putenv AT r{, minor changes for @sc{posix}} +@findex unsetenv AT r{, added} +The environment functions @code{setenv} and @code{putenv} are now both +part of the @sc{posix} draft standard and were updated to match the +specs set forth by that standard. A new function, @code{unsetenv}, +which is also part of that standard, was added as well. Index: src/libc/compat/stdlib/putenv.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/compat/stdlib/putenv.c,v retrieving revision 1.3 diff -u -r1.3 putenv.c --- src/libc/compat/stdlib/putenv.c 1997/11/16 14:05:00 1.3 +++ src/libc/compat/stdlib/putenv.c 2001/06/18 19:28:05 @@ -2,6 +2,7 @@ /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include +#include #include #include #include @@ -35,7 +36,7 @@ unsigned __environ_changed = 1; int -putenv(const char *val) +putenv(char *val) { int vlen = strlen(val); char *epos = strchr(val, '='); @@ -99,6 +100,7 @@ if (environ[eindex] == 0) { environ[eindex] = oval; + errno = ENOMEM; return -1; } if (vlen > olen) @@ -120,7 +122,10 @@ emax += 10; enew = (char **)malloc((emax+1) * sizeof(char *)); if (enew == 0) + { + errno = ENOMEM; return -1; + } memcpy(enew, environ, ecount * sizeof(char *)); /* If somebody set environ to another array, we can't safely free it. Better leak memory than crash. */ @@ -132,7 +137,10 @@ environ[ecount] = (char *)malloc(vlen+1); if (environ[ecount] == 0) + { + errno = ENOMEM; return -1; + } strcpy(environ[ecount], val); ecount++; Index: src/libc/compat/stdlib/putenv.txh =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/compat/stdlib/putenv.txh,v retrieving revision 1.2 diff -u -r1.2 putenv.txh --- src/libc/compat/stdlib/putenv.txh 1998/09/27 15:21:08 1.2 +++ src/libc/compat/stdlib/putenv.txh 2001/06/18 19:28:05 @@ -4,7 +4,7 @@ @example #include -int putenv(const char *env); +int putenv(char *env); @end example @subheading Description @@ -22,11 +22,13 @@ @subheading Return Value -Zero on success, nonzero on failure. +Zero on success, nonzero on failure. @code{errno} will be set to the +relevant error code (currently only @code{ENOMEM} is possible). @subheading Portability -@portability !ansi, !posix +@port-note posix This function is new to the Posix 1003.1-200x draft +@portability !ansi, posix @subheading Example Index: src/libc/compat/v1/setenv.txh =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/compat/v1/setenv.txh,v retrieving revision 1.3 diff -u -r1.3 setenv.txh --- src/libc/compat/v1/setenv.txh 1999/11/28 11:23:36 1.3 +++ src/libc/compat/v1/setenv.txh 2001/06/18 19:28:05 @@ -20,5 +20,5 @@ @subheading Portability -@portability !ansi, !posix - +@port-note posix This function is new to the Posix 1003.1-200x draft +@portability !ansi, posix Index: src/libc/posix/stdlib/.cvsignore =================================================================== RCS file: .cvsignore diff -N .cvsignore --- /dev/null Tue May 5 16:32:27 1998 +++ .cvsignore Mon Jun 18 15:28:05 2001 @@ -0,0 +1,2 @@ +*.d +*.oh Index: src/libc/posix/stdlib/a64l.c =================================================================== RCS file: a64l.c diff -N a64l.c --- /dev/null Tue May 5 16:32:27 1998 +++ a64l.c Mon Jun 18 15:28:05 2001 @@ -0,0 +1,38 @@ +/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + +#include +#include +#include + +long +a64l(const char* s) +{ + int i = 0; + unsigned long value = 0; + + if (s == NULL || *s == '\0') + return 0L; + + for (i = 0; i < 6; ++i, ++s) + { + if (*s == '\0') + break; + /* Detect overflow; return the conversion of '/2BIG/' */ + if (value > (ULONG_MAX >> 6)) + return 1144341633L; + value <<= 6; + if (*s == '.') /* 0 */ + value += 0; + else if (*s == '/') /* 1 */ + ++value; + else if (*s >= '0' && *s <= '9') /* 2-11 */ + value += (*s - '0') + 2; + else if (*s >= 'A' && *s <= 'Z') /* 12-37 */ + value += (*s - 'A') + 12; + else if (*s >= 'a' && *s <= 'z') /* 38-63 */ + value += (*s - 'a') + 38; + else /* invalid digit */ + return 0L; + } + return (long) value; +} Index: src/libc/posix/stdlib/a64l.txh =================================================================== RCS file: a64l.txh diff -N a64l.txh --- /dev/null Tue May 5 16:32:27 1998 +++ a64l.txh Mon Jun 18 15:28:05 2001 @@ -0,0 +1,37 @@ +@node a64l, string +@subheading Syntax + +@example +#include + +long a64l(const char *string); +@end example + +@subheading Description + +This function takes a pointer to a radix-64 representation, with the +first digit the least significant, and returns the corresponding +@code{long} value. + +If @var{string} contains more than six characters, only the first six are +used. If the first six characters of @var{string} contain a null terminator, +only those characters before the null terminator are used. +@code{a64l} will scan the string from left to right, with the least +significant digit on the left, decoding each character as a 6-bit radix-64 +number. + +The radix-64 representation used by this function is described in the +documentation for the @code{l64a} function (@pxref{l64a}). + +@subheading Return Value + +Returns the @code{long} value resulting from the conversion of the contents +of @var{string}, or @code{0L} if @var{string} is @code{NULL}, points to an +empty string, or points to an invalid string (i.e. one not generated by a +previous call to @code{l64a}). If the result would overflow a @code{long}, +the conversion of @samp{/2BIG/} (@code{1144341633L}) is returned. + +@subheading Portability + +@port-note posix This function is new to the Posix 1003.1-200x draft +@portability !ansi, posix Index: src/libc/posix/stdlib/l64a.c =================================================================== RCS file: l64a.c diff -N l64a.c --- /dev/null Tue May 5 16:32:27 1998 +++ l64a.c Mon Jun 18 15:28:05 2001 @@ -0,0 +1,36 @@ +/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + +#include +#include +#include + +char* +l64a(long _value) +{ + static char radix64[7] = { "" }; + char *s = radix64 + 5; + unsigned long value = (unsigned long) _value; + + memset (radix64, 0, sizeof radix64); + + if (value == 0) + return radix64; + + while (value && s >= radix64) + { + int digit = value & 0x3f; + value >>= 6; + + if (digit == 0) + *s-- = '.'; + else if (digit == 1) + *s-- = '/'; + else if (digit < 12) + *s-- = '0' + (digit - 2); + else if (digit < 38) + *s-- = 'A' + (digit - 12); + else + *s-- = 'a' + (digit - 38); + } + return ++s; +} Index: src/libc/posix/stdlib/l64a.txh =================================================================== RCS file: l64a.txh diff -N l64a.txh --- /dev/null Tue May 5 16:32:27 1998 +++ l64a.txh Mon Jun 18 15:28:05 2001 @@ -0,0 +1,42 @@ +@node l64a, string +@subheading Syntax + +@example +#include + +char *l64a(long value); +@end example + +@subheading Description + +This function takes a @code{long} argument and returns a pointer to its +radix-64 representation. Negative values are supported as well. +The resulting string can be turned back into a @code{long} value by the +@code{a64l} function (@pxref{a64l}). + +@subheading Return Value + +A pointer to a static buffer containing the radix-64 representation of +@var{value}. Subsequent calls will overwrite the contents of this buffer. +If @var{value} is @code{0L}, this function returns an empty string. + +@subheading Radix-64 +@cindex radix-64 + +The radix-64 @sc{ascii} representation is a notation whereby 32-bit integers +are represented by up to 6 @sc{ascii} characters; each character represents +a single radix-64 digit. Radix-64 refers to the fact that each digit in this +representation can take 64 different values. +If the @code{long} type is more than 32 bits in size, only the low-order +32 bits are used. +The characters used to represent digits are @samp{.} (dot) for 0, @samp{/} +for 1, @samp{0} through @samp{9} for 2 to 11, @samp{A} through @samp{Z} for +12 to 37, and @samp{a} through @samp{z} for 38 to 63. + +Note that this is @emph{not} the same encoding used by either uuencode or the +MIME base64 encoding. + +@subheading Portability + +@port-note posix This function is new to the Posix 1003.1-200x draft +@portability !ansi, posix Index: src/libc/posix/stdlib/makefile =================================================================== RCS file: makefile diff -N makefile --- /dev/null Tue May 5 16:32:27 1998 +++ makefile Mon Jun 18 15:28:05 2001 @@ -0,0 +1,9 @@ +# Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details + +TOP=../.. + +SRC += a64l.c +SRC += l64a.c +SRC += unsetenv.c + +include $(TOP)/../makefile.inc Index: src/libc/posix/stdlib/unsetenv.c =================================================================== RCS file: unsetenv.c diff -N unsetenv.c --- /dev/null Tue May 5 16:32:27 1998 +++ unsetenv.c Mon Jun 18 15:28:05 2001 @@ -0,0 +1,32 @@ +/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + +#include +#include +#include +#include + +extern char **environ; + +int +unsetenv(const char *name) +{ + /* No environment == success */ + if (environ == 0) + return 0; + + /* Check for the failure conditions */ + if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) + { + errno = EINVAL; + return -1; + } + + /* Let putenv() do the work + * Note that we can just pass name directly as our putenv() treats + * this the same as passing 'name='. */ + /* The cast is needed because POSIX specifies putenv() to take a non-const + * parameter. Our putenv is well-behaved, so this is OK. */ + putenv (unconst (name, char*)); + + return 0; +} Index: src/libc/posix/stdlib/unsetenv.txh =================================================================== RCS file: unsetenv.txh diff -N unsetenv.txh --- /dev/null Tue May 5 16:32:27 1998 +++ unsetenv.txh Mon Jun 18 15:28:06 2001 @@ -0,0 +1,28 @@ +@node unsetenv, environment +@subheading Syntax + +@example +#include + +int unsetenv(const char *name); +@end example + +@subheading Description + +This function removes the environment variable @var{name} from the +environment. This will update the list of pointers to which the +@var{environ} variable points. +If the specified variable does not exist in the environment, the +environment is not modified and this function is considered to have +been sucessfully completed. + +@subheading Return Value + +If @var{name} is @code{NULL}, points to an empty string, or points to +a string containing a @samp{=}, this function returns -1 and sets +@code{errno} to @code{EINVAL}; otherwise it returns 0. + +@subheading Portability + +@port-note posix This function is new to the Posix 1003.1-200x draft +@portability !ansi, posix