For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > August 2006 > How to look up a system type?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to look up a system type?
Leo.Hou

2006-08-28, 10:05 pm

(This thread was posted to lang.c by mistake. I think here is the right
place...)

Hi experts,

I am new to linux and all the type definitions are driving me mad. What
is the best way to check a type definition in linux? When I use man
page to check some function definition, I may come across new types.
How do you guys look up their definitions?

I tried to lookup ssize_t, what I do is trace (grep in glibc source
code)
to 'typedef __ssize_t ssize_t;',
then '__STD_TYPE __SSIZE_T_TYPE __ssize_t;'
then '#define __SSIZE_T_TYPE __SWORD_TYPE'
and finally '# define __SWORD_TYPE int'.

So 'int' it is, but that took me about 10 minutes... What is a better
way to do so? (any man page, web search, magic glibc function or bash
command that can give me the definition right away?)

=====
Another question popped up when I was trying to do something like:
#define SIZE_OF_T(T) printf("size = %d\n", sizeof(T))
to make my life a bit easier. How can I use 'T' in the "size ="
formating string? I want to get something like
size of ssize_t = 4
as output. Can anyone help?

Thanks a lot x2 for two questions


Best Regards
Leo

Nils O. Selåsdal

2006-08-29, 4:11 am

Leo.Hou wrote:
> (This thread was posted to lang.c by mistake. I think here is the right
> place...)
>
> Hi experts,
>
> I am new to linux and all the type definitions are driving me mad. What
> is the best way to check a type definition in linux? When I use man
> page to check some function definition, I may come across new types.
> How do you guys look up their definitions?
>
> I tried to lookup ssize_t, what I do is trace (grep in glibc source
> code)
> to 'typedef __ssize_t ssize_t;',
> then '__STD_TYPE __SSIZE_T_TYPE __ssize_t;'
> then '#define __SSIZE_T_TYPE __SWORD_TYPE'
> and finally '# define __SWORD_TYPE int'.
>
> So 'int' it is, but that took me about 10 minutes... What is a better
> way to do so? (any man page, web search, magic glibc function or bash
> command that can give me the definition right away?)


Why do you need to know it is an int ?

The "best" thing is likely to look up the guarantees about a type
at the relevant standard, e.g. www.opengroup.org and act accordingly
to it's specification/limits.
jmcgill

2006-08-29, 4:11 am

Leo.Hou wrote:

> I tried to lookup ssize_t, what I do is trace (grep in glibc source
> code)


I think you may be missing the point of this kind of abstraction.

> Another question popped up when I was trying to do something like:
> #define SIZE_OF_T(T) printf("size = %d\n", sizeof(T))
> to make my life a bit easier. How can I use 'T' in the "size ="
> formating string? I want to get something like
> size of ssize_t = 4


You want the literal name of the type substituted into the string. That
is an interesting question, and I'm sure it will require some
preprocessor trick.

Why are you looking for this sort of implementation-specific info? Just
exploring, or are you designing code against a non-portable interface
for some good reason?
Nils O. Selåsdal

2006-08-29, 4:11 am

Leo.Hou wrote:
[..]

> =====
> Another question popped up when I was trying to do something like:
> #define SIZE_OF_T(T) printf("size = %d\n", sizeof(T))


#define SIZE_OF_T(T) printf("size of " #T " = %lu\n", (unsigned long)
sizeof(T))

Paul Pluzhnikov

2006-08-29, 4:11 am

"Leo.Hou" <leo.hou@gmail.com> writes:

> then '__STD_TYPE __SSIZE_T_TYPE __ssize_t;'
> then '#define __SSIZE_T_TYPE __SWORD_TYPE'
> and finally '# define __SWORD_TYPE int'.
>
> So 'int' it is, but that took me about 10 minutes... What is a better
> way to do so?


$ echo '#include <signal.h>' | gcc -E - | grep ssize_t
__extension__ typedef int __ssize_t;

> Another question popped up when I was trying to do something like:
> #define SIZE_OF_T(T) printf("size = %d\n", sizeof(T))


#define SIZE_OF_T(x) do { printf("sizeof(%s) = %ld\n", #x, sizeof(x)); } while (0)

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Leo.Hou

2006-08-29, 4:11 am

Thank you all, problem solved!



Paul Pluzhnikov wrote:
> "Leo.Hou" <leo.hou@gmail.com> writes:
>
>
> $ echo '#include <signal.h>' | gcc -E - | grep ssize_t
> __extension__ typedef int __ssize_t;
>
>
> #define SIZE_OF_T(x) do { printf("sizeof(%s) = %ld\n", #x, sizeof(x)); } while (0)
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.


Leo.Hou

2006-08-29, 4:11 am

That was just an example. Because I am new to this area and I want try
different things. Those types confuses me some time...

And I guess some types might be a composite structure rather than
primitive type. Is that true? Or untrue in linux world?

Leo


Nils O. Sel=E5sdal wrote:
> Leo.Hou wrote:
>
> Why do you need to know it is an int ?
>
> The "best" thing is likely to look up the guarantees about a type
> at the relevant standard, e.g. www.opengroup.org and act accordingly
> to it's specification/limits.


Leo.Hou

2006-08-29, 4:11 am

That was just an example. Because I am new to this area and I want try
different things. Those types confuses me some time...

And I guess some types might be a composite structure rather than
primitive type. Is that true? Or untrue in linux world?

Leo


Nils O. Sel=E5sdal wrote:
> Leo.Hou wrote:
>
> Why do you need to know it is an int ?
>
> The "best" thing is likely to look up the guarantees about a type
> at the relevant standard, e.g. www.opengroup.org and act accordingly
> to it's specification/limits.


Nils O. Selåsdal

2006-08-29, 4:11 am

Leo.Hou wrote:
> That was just an example. Because I am new to this area and I want try
> different things. Those types confuses me some time...

No, it's an important question you should ask yourself too.
Why DO you need to know what it really is ? Mostly the purpose
of the typedef is to hide/abstract away what it really is on a give
platform.

> And I guess some types might be a composite structure rather than
> primitive type. Is that true? Or untrue in linux world?

They might be.
Check the documentation. www.opengroup.org as mentioned is nice. e.g.
http://www.opengroup.org/onlinepubs...ys/types.h.html
has a bit to say about types in sys/types.h, and usually that's all you
need to know.

If it's a struct, the fields is(should be ... ) documented. If not,
you likely have no business ping inside it, for good reasons.
Maxim Yegorushkin

2006-08-29, 7:01 pm


Paul Pluzhnikov wrote:
> "Leo.Hou" <leo.hou@gmail.com> writes:
>
>
> $ echo '#include <signal.h>' | gcc -E - | grep ssize_t
> __extension__ typedef int __ssize_t;


Nice trick. To look up a macro as well gcc -E -dD can be used.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com