Home > Archive > Unix Programming > May 2006 > etc/services question
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 |
etc/services question
|
|
| ataanis@gmail.com 2006-05-24, 7:04 pm |
| I'm trying to get the aliases for a given line from the etc/services
file in C language, so I'm using getservbyname() and that function
returns a structure, containing a list the aliases , so to me so far,
the list is h-> s_aliases
but when I try to print those , I can' t get all of them, I could only
get h->s_aliases[1] or 2 etc...
but I can't get all of them, so I thought about first , determining if
the list is empty , but to start with, when I do ( if h->s_aliases ==
null) , it doesn't even work to tell me that the list is empty
so I was wondering if there was a way of simply printing those aliases
in a simpler manner
thanks
| |
|
| On 24 May 2006 10:42:51 -0700
ataanis@gmail.com wrote:
> I'm trying to get the aliases for a given line from the etc/services
> file in C language, so I'm using getservbyname() and that function
> returns a structure, containing a list the aliases , so to me so far,
> the list is h-> s_aliases
> but when I try to print those , I can' t get all of them, I could only
> get h->s_aliases[1] or 2 etc...
>=20
> but I can't get all of them, so I thought about first , determining if
> the list is empty , but to start with, when I do ( if h->s_aliases =3D=3D
> null) , it doesn't even work to tell me that the list is empty
> so I was wondering if there was a way of simply printing those aliases
> in a simpler manner=20
=46rom the man page:
The getservbyname() function returns a servent structure for the line
from /etc/services that matches the service name using protocol proto.
If proto is NULL, any protocol will be matched.
The structure as you already know is this:
struct servent {
char *s_name; /* official service name */
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
}
Therefore I do not think that you will be able to use this function to
get all services.
I suggest you write your own function do to this.
--=20
Regards, Ed :: http://www.s5h.net/unix_beard.php
just another c++ hacker
:%s/Open Source/Free Software/g :: Free DNS available
| |
| ataanis@gmail.com 2006-05-24, 7:04 pm |
| Well what I'm trying to say is , if I provide a protocol and a service
name, I'm able to get the port number for that line (s_port)(by
accessing the returned structure) , then it's possible to access the
list s_aliases too, but the problem is that I want the whole content of
that list
isn't it possible to get the aliases for that service name/protocol?
| |
| Martin Blume 2006-05-24, 7:04 pm |
| <ataanis@gmail.com> schrieb
> I'm trying to get the aliases for a given line from the
> etc/services file in C language, so I'm using getservbyname()
> and that function returns a structure, containing a list the
> aliases , so to me so far,
> the list is h-> s_aliases
> but when I try to print those , I can' t get all of them,
> I could only get h->s_aliases[1] or 2 etc...
>
I didn't see many services in my /etc/services that have many
aliases.
>
> but I can't get all of them, so I thought about first ,
> determining if the list is empty , but to start with, when I
> do ( if h->s_aliases == null) , it doesn't even work to tell
> me that the list is empty so I was wondering if there was a
> way of simply printing those aliases in a simpler manner
> thanks
>
I don't understand your problem, but this piece of code
worked for me:
> cat getserv.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
struct servent *s;
int i;
s = getservbyname(argv[1],
(argc == 3) ? argv[2] : NULL);
if (s)
{
printf("name %s, proto %s, port %d\n",
s->s_name,
s->s_proto,
ntohs(s->s_port));
i = 0;
while (s->s_aliases[i] != NULL)
{
printf("\t%s\n", s->s_aliases[i]);
i++;
} // loop thru aliases
} // entry found
else
{
fprintf(stderr,
"FATAL: unable to getservbyname(%s)\n",
argv[1]);
exit(1);
} // oops, error
return 0;
} // main
| |
| Jim Cochrane 2006-05-24, 7:04 pm |
| On 2006-05-24, ataanis@gmail.com <ataanis@gmail.com> wrote:
> I'm trying to get the aliases for a given line from the etc/services
> file in C language, so I'm using getservbyname() and that function
> returns a structure, containing a list the aliases , so to me so far,
> the list is h-> s_aliases
> but when I try to print those , I can' t get all of them, I could only
> get h->s_aliases[1] or 2 etc...
>
> but I can't get all of them, so I thought about first , determining if
> the list is empty , but to start with, when I do ( if h->s_aliases ==
> null) , it doesn't even work to tell me that the list is empty
> so I was wondering if there was a way of simply printing those aliases
> in a simpler manner
> thanks
>
Looks like you need to go more in depth in your study of C. Note that the
man page (on Linux FC4 - other UNIXes will probably be essentially the
same) says about s_aliases:
s_aliases
A zero terminated list of alternative names for the service.
Now, as you've correctly assumed, since the type is char**, s_aliases
can't be a linked list - it must be the address of an array of char*.
So in this context, 0-terminated list would mean that if there are n
aliases, the array will contain n + 1 elements, where the value of the
last element is 0 (NULL). So you'll want to process the aliases with code
something like:
int i = 0;
while (h->s_aliases[i] != 0) {
process(h->s_aliases[i]);
++i;
}
assert(h->s_aliases[i] == 0);
where 'process' is to be filled in by you.
--
*** Posted via a free Usenet account from http://www.teranews.com ***
|
|
|
|
|