linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: How does one get the description of an errno value
  2009-03-15 11:20 ` Steve Graegert
@ 2009-03-15 10:43   ` Tiago Maluta
  2009-03-15 13:09     ` Sergio Luis
                       ` (2 more replies)
  2009-03-16  1:23   ` Jon Mayo
  1 sibling, 3 replies; 8+ messages in thread
From: Tiago Maluta @ 2009-03-15 10:43 UTC (permalink / raw)
  To: Steve Graegert; +Cc: Angel Tsankov, linux-c-programming

Steve,

> The following code snippet should do it.  It basically sets errno
> manually and outputs the associated error message.
> 
> --- BEGIN ---
> 
> #define _ALL_SOURCE
> 
> #include <stdio.h>
> #include <errno.h> /* for _MAX_ERRNO */
> 

I've searched in headers on /usr/include for MAX_ERRNO but not found.
Where MAX_ERRNO is defined?

A *very* simple workaround was check for "Unknown" word, so I tried:

int main(void) {
	int i=0;
	extern int errno;
	char str[128];

	while (strncmp(str,"Unknown",7)) {
		sprintf(str,"%s",strerror(i));
		fprintf(stderr, "%3d", i);
		errno = i;
		perror(" ");
		i++;
	}

	return (0);
}


But there is "Unknown" between valid error messages.
I think there is a better way to do it....


Best regards,

--tm


^ permalink raw reply	[flat|nested] 8+ messages in thread

* How does one get the description of an errno value
@ 2009-03-15 10:46 Angel Tsankov
  2009-03-15 11:20 ` Bert Wesarg
  2009-03-15 11:20 ` Steve Graegert
  0 siblings, 2 replies; 8+ messages in thread
From: Angel Tsankov @ 2009-03-15 10:46 UTC (permalink / raw)
  To: linux-c-programming

Hello,

Is there a function which returns the description of an errno value and, if 
so, which is it?

Regards,
Angel Tsankov 




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How does one get the description of an errno value
  2009-03-15 10:46 How does one get the description of an errno value Angel Tsankov
@ 2009-03-15 11:20 ` Bert Wesarg
  2009-03-15 11:20 ` Steve Graegert
  1 sibling, 0 replies; 8+ messages in thread
From: Bert Wesarg @ 2009-03-15 11:20 UTC (permalink / raw)
  To: Angel Tsankov; +Cc: linux-c-programming

On Sun, Mar 15, 2009 at 11:46, Angel Tsankov <fn42551@fmi.uni-sofia.bg> wrote:
> Hello,
>
> Is there a function which returns the description of an errno value and, if
> so, which is it?
strerror(3)

Bert
>
> Regards,
> Angel Tsankov
>
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How does one get the description of an errno value
  2009-03-15 10:46 How does one get the description of an errno value Angel Tsankov
  2009-03-15 11:20 ` Bert Wesarg
@ 2009-03-15 11:20 ` Steve Graegert
  2009-03-15 10:43   ` Tiago Maluta
  2009-03-16  1:23   ` Jon Mayo
  1 sibling, 2 replies; 8+ messages in thread
From: Steve Graegert @ 2009-03-15 11:20 UTC (permalink / raw)
  To: Angel Tsankov; +Cc: linux-c-programming

Angel,

The following code snippet should do it.  It basically sets errno
manually and outputs the associated error message.

--- BEGIN ---

#define _ALL_SOURCE

#include <stdio.h>
#include <errno.h> /* for _MAX_ERRNO */

int main(void) {
	int i;
	extern int errno;
	
	for (i = 0; i < MAX_ERRNO; i++) {
		fprintf(stderr, "%3d", i);
		errno = i;
		perror(" ");
	}
	
	return (0);
}

--- END ---

	\Steve

--

Steve Graegert
www.graegert.com



On Sun, Mar 15, 2009 at 11:46 AM, Angel Tsankov
<fn42551@fmi.uni-sofia.bg> wrote:
> Hello,
>
> Is there a function which returns the description of an errno value and, if
> so, which is it?
>
> Regards,
> Angel Tsankov
>
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How does one get the description of an errno value
  2009-03-15 10:43   ` Tiago Maluta
@ 2009-03-15 13:09     ` Sergio Luis
  2009-03-15 13:10     ` Steve Graegert
       [not found]     ` <a913862f0903150619o43657d87q2f45dbdbc7071659@mail.gmail.com>
  2 siblings, 0 replies; 8+ messages in thread
From: Sergio Luis @ 2009-03-15 13:09 UTC (permalink / raw)
  To: Tiago Maluta; +Cc: Steve Graegert, Angel Tsankov, linux-c-programming

On Sun, Mar 15, 2009 at 7:43 AM, Tiago Maluta <maluta_tiago@yahoo.com.br> wrote:
> Steve,
>
>> The following code snippet should do it.  It basically sets errno
>> manually and outputs the associated error message.
>>
>> --- BEGIN ---
>>
>> #define _ALL_SOURCE
>>
>> #include <stdio.h>
>> #include <errno.h> /* for _MAX_ERRNO */
>>
>
> I've searched in headers on /usr/include for MAX_ERRNO but not found.
> Where MAX_ERRNO is defined?

you could define it with something like this:

#ifndef MAX_ERRNO
#define MAX_ERRNO 4095
#endif

Sergio

>
> A *very* simple workaround was check for "Unknown" word, so I tried:
>
> int main(void) {
>        int i=0;
>        extern int errno;
>        char str[128];
>
>        while (strncmp(str,"Unknown",7)) {
>                sprintf(str,"%s",strerror(i));
>                fprintf(stderr, "%3d", i);
>                errno = i;
>                perror(" ");
>                i++;
>        }
>
>        return (0);
> }
>
>
> But there is "Unknown" between valid error messages.
> I think there is a better way to do it....
>
>
> Best regards,
>
> --tm
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How does one get the description of an errno value
  2009-03-15 10:43   ` Tiago Maluta
  2009-03-15 13:09     ` Sergio Luis
@ 2009-03-15 13:10     ` Steve Graegert
       [not found]     ` <a913862f0903150619o43657d87q2f45dbdbc7071659@mail.gmail.com>
  2 siblings, 0 replies; 8+ messages in thread
From: Steve Graegert @ 2009-03-15 13:10 UTC (permalink / raw)
  To: Tiago Maluta; +Cc: Angel Tsankov, linux-c-programming

Tiago,

You're correct.  MAX_ERRNO is a custom macro being defined elsewhere
and actually expands to to sys_nerr.  I picked that code from some
examples I've prepared some years ago.  Sorry for the inconvenience
but I think you get the idea.  see perror(3) for more info.

	\Steve

--

Steve Graegert
www.graegert.com



On Sun, Mar 15, 2009 at 11:43 AM, Tiago Maluta
<maluta_tiago@yahoo.com.br> wrote:
> Steve,
>
>> The following code snippet should do it.  It basically sets errno
>> manually and outputs the associated error message.
>>
>> --- BEGIN ---
>>
>> #define _ALL_SOURCE
>>
>> #include <stdio.h>
>> #include <errno.h> /* for _MAX_ERRNO */
>>
>
> I've searched in headers on /usr/include for MAX_ERRNO but not found.
> Where MAX_ERRNO is defined?
>
> A *very* simple workaround was check for "Unknown" word, so I tried:
>
> int main(void) {
>        int i=0;
>        extern int errno;
>        char str[128];
>
>        while (strncmp(str,"Unknown",7)) {
>                sprintf(str,"%s",strerror(i));
>                fprintf(stderr, "%3d", i);
>                errno = i;
>                perror(" ");
>                i++;
>        }
>
>        return (0);
> }
>
>
> But there is "Unknown" between valid error messages.
> I think there is a better way to do it....
>
>
> Best regards,
>
> --tm
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How does one get the description of an errno value
       [not found]       ` <49BD1E25.4010305@yahoo.com.br>
@ 2009-03-15 18:13         ` Akos Marton
  0 siblings, 0 replies; 8+ messages in thread
From: Akos Marton @ 2009-03-15 18:13 UTC (permalink / raw)
  To: Tiago Maluta; +Cc: linux-c-programming

On Sun, Mar 15, 2009 at 4:26 PM, Tiago Maluta <maluta_tiago@yahoo.com.br> wrote:
> Akos Marton wrote:
>> hi,
>>
>> /usr/include/linux/err.h on my PC with the value 4095.
>>
>> mAkos
>>
>
> What lib contains err.h?
> My /usr/include/linux contains only errno.h
>
> I'm using glibc 2.8

I'm using glibc 2.7
You can do a find in your /usr/include/ as
sh$ find /usr/include/ -name "*h" -exec cat {} + | grep MAX_ERRNO
if there are some output, check which file contains this macro.

mAkos
-- 
People seldom notice clothes, if you wear a big smile.
OLVASD: http://napirajz.hu

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How does one get the description of an errno value
  2009-03-15 11:20 ` Steve Graegert
  2009-03-15 10:43   ` Tiago Maluta
@ 2009-03-16  1:23   ` Jon Mayo
  1 sibling, 0 replies; 8+ messages in thread
From: Jon Mayo @ 2009-03-16  1:23 UTC (permalink / raw)
  To: linux-c-programming

On Sun, Mar 15, 2009 at 4:20 AM, Steve Graegert <graegerts@gmail.com> wrote:
> Angel,
>
> The following code snippet should do it.  It basically sets errno
> manually and outputs the associated error message.
>
> --- BEGIN ---
>
> #define _ALL_SOURCE
>
> #include <stdio.h>
> #include <errno.h> /* for _MAX_ERRNO */
>
> int main(void) {
>        int i;
>        extern int errno;
>
>        for (i = 0; i < MAX_ERRNO; i++) {
>                fprintf(stderr, "%3d", i);
>                errno = i;
>                perror(" ");
>        }
>
>        return (0);
> }
>
> --- END ---
>
>        \Steve
>
> --
>
> Steve Graegert
> www.graegert.com
>
>
>
> On Sun, Mar 15, 2009 at 11:46 AM, Angel Tsankov
> <fn42551@fmi.uni-sofia.bg> wrote:
>> Hello,
>>
>> Is there a function which returns the description of an errno value and, if
>> so, which is it?
>>
>> Regards,
>> Angel Tsankov
>>

A little fancier snippet that I wrote years ago to deal with programs
that only output an errno number and no description is this:

/* error.c : utility for looking up errno error numbers */
/* original version: 2006 June 27
 * 2006 June 29 -- version 1.04
 * contributors: Jon Mayo <jon.mayo@acm.org>
 * No copyright claimed -- Public Domain */

/* TODO: convert error strings into error numbers/names */

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define T(x) if(e==(x)) return #x

static const char **id_lookup_table;
static unsigned id_max;

static const char *err_id(int e) {
#ifdef EPERM
	T(EPERM);
#endif
#ifdef ENOENT
	T(ENOENT);
#endif
#ifdef ESRCH
	T(ESRCH);
#endif
#ifdef EINTR
	T(EINTR);
#endif
#ifdef EIO
	T(EIO);
#endif
#ifdef ENXIO
	T(ENXIO);
#endif
#ifdef E2BIG
	T(E2BIG);
#endif
#ifdef ENOEXEC
	T(ENOEXEC);
#endif
#ifdef EBADF
	T(EBADF);
#endif
#ifdef ECHILD
	T(ECHILD);
#endif
#ifdef EAGAIN
	T(EAGAIN);
#endif
#ifdef ENOMEM
	T(ENOMEM);
#endif
#ifdef EACCES
	T(EACCES);
#endif
#ifdef EFAULT
	T(EFAULT);
#endif
#ifdef ENOTBLK
	T(ENOTBLK);
#endif
#ifdef EBUSY
	T(EBUSY);
#endif
#ifdef EEXIST
	T(EEXIST);
#endif
#ifdef EXDEV
	T(EXDEV);
#endif
#ifdef ENODEV
	T(ENODEV);
#endif
#ifdef ENOTDIR
	T(ENOTDIR);
#endif
#ifdef EISDIR
	T(EISDIR);
#endif
#ifdef EINVAL
	T(EINVAL);
#endif
#ifdef ENFILE
	T(ENFILE);
#endif
#ifdef EMFILE
	T(EMFILE);
#endif
#ifdef ENOTTY
	T(ENOTTY);
#endif
#ifdef ETXTBSY
	T(ETXTBSY);
#endif
#ifdef EFBIG
	T(EFBIG);
#endif
#ifdef ENOSPC
	T(ENOSPC);
#endif
#ifdef ESPIPE
	T(ESPIPE);
#endif
#ifdef EROFS
	T(EROFS);
#endif
#ifdef EMLINK
	T(EMLINK);
#endif
#ifdef EPIPE
	T(EPIPE);
#endif
#ifdef EDOM
	T(EDOM);
#endif
#ifdef ERANGE
	T(ERANGE);
#endif
#ifdef EDEADLK
	T(EDEADLK);
#endif
#ifdef ENAMETOOLONG
	T(ENAMETOOLONG);
#endif
#ifdef ENOLCK
	T(ENOLCK);
#endif
#ifdef ENOSYS
	T(ENOSYS);
#endif
#ifdef ENOTEMPTY
	T(ENOTEMPTY);
#endif
#ifdef ELOOP
	T(ELOOP);
#endif
#ifdef EWOULDBLOCK
	T(EWOULDBLOCK);
#endif
#ifdef ENOMSG
	T(ENOMSG);
#endif
#ifdef EIDRM
	T(EIDRM);
#endif
#ifdef ECHRNG
	T(ECHRNG);
#endif
#ifdef EL2NSYNC
	T(EL2NSYNC);
#endif
#ifdef EL3HLT
	T(EL3HLT);
#endif
#ifdef EL3RST
	T(EL3RST);
#endif
#ifdef ELNRNG
	T(ELNRNG);
#endif
#ifdef EUNATCH
	T(EUNATCH);
#endif
#ifdef ENOCSI
	T(ENOCSI);
#endif
#ifdef EL2HLT
	T(EL2HLT);
#endif
#ifdef EBADE
	T(EBADE);
#endif
#ifdef EBADR
	T(EBADR);
#endif
#ifdef EXFULL
	T(EXFULL);
#endif
#ifdef ENOANO
	T(ENOANO);
#endif
#ifdef EBADRQC
	T(EBADRQC);
#endif
#ifdef EBADSLT
	T(EBADSLT);
#endif
#ifdef EDEADLOCK
	T(EDEADLOCK);
#endif
#ifdef EBFONT
	T(EBFONT);
#endif
#ifdef ENOSTR
	T(ENOSTR);
#endif
#ifdef ENODATA
	T(ENODATA);
#endif
#ifdef ETIME
	T(ETIME);
#endif
#ifdef ENOSR
	T(ENOSR);
#endif
#ifdef ENONET
	T(ENONET);
#endif
#ifdef ENOPKG
	T(ENOPKG);
#endif
#ifdef EREMOTE
	T(EREMOTE);
#endif
#ifdef ENOLINK
	T(ENOLINK);
#endif
#ifdef EADV
	T(EADV);
#endif
#ifdef ESRMNT
	T(ESRMNT);
#endif
#ifdef ECOMM
	T(ECOMM);
#endif
#ifdef EPROTO
	T(EPROTO);
#endif
#ifdef EMULTIHOP
	T(EMULTIHOP);
#endif
#ifdef EDOTDOT
	T(EDOTDOT);
#endif
#ifdef EBADMSG
	T(EBADMSG);
#endif
#ifdef EOVERFLOW
	T(EOVERFLOW);
#endif
#ifdef ENOTUNIQ
	T(ENOTUNIQ);
#endif
#ifdef EBADFD
	T(EBADFD);
#endif
#ifdef EREMCHG
	T(EREMCHG);
#endif
#ifdef ELIBACC
	T(ELIBACC);
#endif
#ifdef ELIBBAD
	T(ELIBBAD);
#endif
#ifdef ELIBSCN
	T(ELIBSCN);
#endif
#ifdef ELIBMAX
	T(ELIBMAX);
#endif
#ifdef ELIBEXEC
	T(ELIBEXEC);
#endif
#ifdef EILSEQ
	T(EILSEQ);
#endif
#ifdef ERESTART
	T(ERESTART);
#endif
#ifdef ESTRPIPE
	T(ESTRPIPE);
#endif
#ifdef EUSERS
	T(EUSERS);
#endif
#ifdef ENOTSOCK
	T(ENOTSOCK);
#endif
#ifdef ETOOMANYREF
	T(ETOOMANYREF);
#endif
#ifdef TADDRREQ
	T(TADDRREQ);
#endif
#ifdef EMSGSIZE
	T(EMSGSIZE);
#endif
#ifdef EPROTOTYPE
	T(EPROTOTYPE);
#endif
#ifdef ENOPROTOOPT
	T(ENOPROTOOPT);
#endif
#ifdef EPROTONOSUPPORT
	T(EPROTONOSUPPORT);
#endif
#ifdef ESOCKTNOSUPPORT
	T(ESOCKTNOSUPPORT);
#endif
#ifdef EOPNOTSUPP
	T(EOPNOTSUPP);
#endif
#ifdef EPFNOSUPPORT
	T(EPFNOSUPPORT);
#endif
#ifdef EAFNOSUPPORT
	T(EAFNOSUPPORT);
#endif
#ifdef EADDRINUSE
	T(EADDRINUSE);
#endif
#ifdef EADDRNOTAVAIL
	T(EADDRNOTAVAIL);
#endif
#ifdef ENETDOWN
	T(ENETDOWN);
#endif
#ifdef ENETUNREACH
	T(ENETUNREACH);
#endif
#ifdef ENETRESET
	T(ENETRESET);
#endif
#ifdef ECONNABORTED
	T(ECONNABORTED);
#endif
#ifdef ECONNRESET
	T(ECONNRESET);
#endif
#ifdef ENOBUFS
	T(ENOBUFS);
#endif
#ifdef EISCONN
	T(EISCONN);
#endif
#ifdef ENOTCONN
	T(ENOTCONN);
#endif
#ifdef ESHUTDOWN
	T(ESHUTDOWN);
#endif
#ifdef ETOOMANYREFS
	T(ETOOMANYREFS);
#endif
#ifdef ECONNREFUSED
	T(ECONNREFUSED);
#endif
#ifdef EHOSTDOWN
	T(EHOSTDOWN);
#endif
#ifdef EHOSTUNREACH
	T(EHOSTUNREACH);
#endif
#ifdef EALREADY
	T(EALREADY);
#endif
#ifdef EINPROGRESS
	T(EINPROGRESS);
#endif
#ifdef ESTALE
	T(ESTALE);
#endif
#ifdef EUCLEAN
	T(EUCLEAN);
#endif
#ifdef ENOTNAM
	T(ENOTNAM);
#endif
#ifdef ENAVAIL
	T(ENAVAIL);
#endif
#ifdef EISNAM
	T(EISNAM);
#endif
#ifdef EREMOTEIO
	T(EREMOTEIO);
#endif
#ifdef EDQUOT
	T(EDQUOT);
#endif
#ifdef ENOMEDIUM
	T(ENOMEDIUM);
#endif
#ifdef EMEDIUMTYPE
	T(EMEDIUMTYPE);
#endif
#ifdef ECANCELED
	T(ECANCELED);
#endif
#ifdef ENOKEY
	T(ENOKEY);
#endif
#ifdef EKEYEXPIRED
	T(EKEYEXPIRED);
#endif
#ifdef EKEYREVOKED
	T(EKEYREVOKED);
#endif
#ifdef EKEYREJECTED
	T(EKEYREJECTED);
#endif
	return 0;
}

/* attempt to guess the largest error value. assume gaps of 100 means
that's the end of the list of errors */
static int guess_max(void) {
	int i;
	int miss_count;
	for(i=0,miss_count=0;i<INT_MAX;i++) {
		if(err_id(i)) {
			miss_count=0;
		} else {
			miss_count++;
			if(miss_count>100) break;
		}
	}
	return i-miss_count;
}

static void init_id_lookup_table(void) {
	int i;
	id_max=guess_max();
	id_lookup_table=calloc(sizeof *id_lookup_table, id_max+1);
	for(i=0;i<=id_max;i++) {
		id_lookup_table[i]=err_id(i);
	}
}

static int lookup_id_str(const char *str) {
	int i;
	assert(id_lookup_table!=NULL);
	assert(id_max>0);
	for(i=0;i<=id_max;i++) {
		if(id_lookup_table[i] && !strcmp(str, id_lookup_table[i])) {
			return i;
		}
	}
	return -1;
}

static void show_error(int e) {
	const char *id;
	id=err_id(e);
	if(!id) id="???";
	printf("%d:%s:%s\n", e, id, strerror(e));
}

int main(int argc, char **argv) {
	if(argc==1) {
		int max;
		max=guess_max();
		printf("max error: %d\n", max);
	} else {
		int i;
		init_id_lookup_table();
		for(i=1;i<argc;i++) {
			int e;
			char *endptr;
			e=strtol(argv[i],&endptr,10);
			if(!endptr || *endptr) {
				e=lookup_id_str(argv[i]);
				if(e<0) {
					fprintf(stderr, "error '%s' unknown.\n", argv[i]);
					continue;
				}
			}
			if(e<0) e=-e;
			show_error(e);
		}
	}
	return 0;
}
/** END **/

-- 
Jon Mayo
<jon.mayo@gmail.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-03-16  1:23 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-15 10:46 How does one get the description of an errno value Angel Tsankov
2009-03-15 11:20 ` Bert Wesarg
2009-03-15 11:20 ` Steve Graegert
2009-03-15 10:43   ` Tiago Maluta
2009-03-15 13:09     ` Sergio Luis
2009-03-15 13:10     ` Steve Graegert
     [not found]     ` <a913862f0903150619o43657d87q2f45dbdbc7071659@mail.gmail.com>
     [not found]       ` <49BD1E25.4010305@yahoo.com.br>
2009-03-15 18:13         ` Akos Marton
2009-03-16  1:23   ` Jon Mayo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).