Skip to content
Snippets Groups Projects
Commit 3ea343d9 authored by Steven Murray's avatar Steven Murray Committed by Steven Murray
Browse files

sstrerror_r prototype now matches that of the XSI strerror_r

parent 6129011a
Branches
Tags
No related merge requests found
......@@ -491,10 +491,16 @@ char *sys_secerrlist[ESECMAXERR-ESECBASEOFF+2] =
*------------------------------------------------------------------------
*/
char * sstrerror_r(const int n, char *const buf, const size_t buflen) {
int sstrerror_r(const int n, char *const buf, const size_t buflen) {
char *tmpstr;
char strerror_r_buf[100];
if ( buf == NULL || buflen <= 0 ) return(NULL);
if ( buf == NULL || buflen <= 0 ) {
errno = EINVAL;
serrno = EINVAL;
return -1;
}
memset(buf,'\0',buflen);
tmpstr = NULL;
......@@ -608,14 +614,20 @@ char * sstrerror_r(const int n, char *const buf, const size_t buflen) {
*/
sprintf(buf, "%*s: %10d", (int)buflen-14, sys_serrlist[SEMAXERR+1-SEBASEOFF], n);
}
return(buf);
return 0;
}
void sperror(char *msg)
{
char buf[80];
if (serrno) {
fprintf(stderr,"%s: %s\n",msg,sstrerror_r(serrno,buf,80));
if(sstrerror_r(serrno, buf, sizeof(buf))) {
/* sstrerror_r() failed so just print msg */
fprintf(stderr,"%s\n",msg);
} else {
/* sstrerror_r() succeeded so print both msg and buf */
fprintf(stderr,"%s: %s\n", msg, buf);
}
} else {
perror(msg);
}
......@@ -623,11 +635,20 @@ void sperror(char *msg)
static int sstrerror_key = -1;
char *sstrerror(int n)
{
char *sstrerror(const int n) {
void *buf = NULL;
int buflen = 80;
Cglobals_get(&sstrerror_key,&buf,buflen);
return(sstrerror_r(n,(char *)buf,buflen));
if(Cglobals_get(&sstrerror_key, &buf, buflen)) {
return "Unknown error"
": No thread specific memory to determine error string"
": Cglobals_get() failed";
}
if(sstrerror_r(n,(char *)buf,buflen)) {
return "Unknown error"
": sstrerror_r() failed";
} else {
return (char *)buf;
}
}
......@@ -485,8 +485,35 @@ EXTERN_C int *C__serrno (void);
*/
#define serrno (*C__serrno())
EXTERN_C char *sstrerror_r (const int n, char *const buf, const size_t buflen);
EXTERN_C char *sstrerror (int);
/**
* Writes the string representation of the specified error code into the
* specified buffer.
*
* The string representation is truncated if the specified buffer is too small.
* In case of such a truncation the string written to the specified buffer is
* null terminated.
*
* @param n The numeric error code.
* @param buf The buffer to which the string representation should be written.
* @param buflen The length of the buffer.
* @return 0 on success or -1 on failure. In the case of a failure both errno
* and serrno are set to indicate the type of error.
*/
EXTERN_C int sstrerror_r (const int n, char *const buf, const size_t buflen);
/**
* Returns the string representation of the specified error code.
*
* Allocates some thread specfic memory if needed using Cglobals_get() and then
* passes that memory to sstrerror_r along with the specified error code.
*
* A string is always returned. This function never returns NULL.
*
* @param n The numeric error code.
* @return The string representation.
*/
EXTERN_C char *sstrerror (const int n);
EXTERN_C void sperror (char *);
extern char *sys_serrlist[]; /* Error text array */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment