home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Fish 1
/
GoldFishApril1994_CD2.img
/
d4xx
/
d473
/
cnewssrc
/
cnews_src.lzh
/
libamiga
/
gethostname.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-12-25
|
2KB
|
114 lines
/* :ts=4
* v7 gethostname simulation
* taken from pathalias and cleaned up. Thanks, Peter.
*
* $Id: gethostname.c,v 1.1 90/05/21 23:50:53 crash Exp Locker: crash $
*
* $Log: gethostname.c,v $
* Revision 1.1 90/05/21 23:50:53 crash
* Initial revision
*
*/
#include <stdio.h>
#ifndef index
# define index strchr
# define rindex strrchr
#endif
/* imports from libc */
extern char *index(), *strncpy();
extern FILE *fopen(), *popen();
/* forwards */
void readhostfile();
#define MAXHOST 256
#ifndef min
# define min(a,b) ((a) < (b)? (a): (b))
#endif
static char defhost[] = "anonymous";
#ifdef AMIGA
static char buff[BUFSIZ];
#else
static char *namefiles[] = {
"/etc/whoami",
"/etc/systemid",
NULL,
};
#endif /* AMIGA */
int gethostname( register char *hostname, int size)
{
#ifdef AMIGA
*hostname = '\0';
/* try the NEWSCTL directory for a file called "whoami" */
strcpy(buff, ctlfile("whoami"));
readhostfile(hostname, size, buff);
if (*hostname != '\0')
return( 0 );
#else
register FILE *whoami;
register char **namep;
*hostname = '\0';
/* try files in namefiles */
for (namep = namefiles; *namep != NULL; namep++) {
readhostfile(hostname, size, *namep);
if (*hostname != '\0')
return 0;
}
/* try /usr/include/whoami.h */
if ((whoami = fopen("/usr/include/whoami.h", "r")) != NULL) {
while (!feof(whoami)) {
char sysname[MAXHOST];
if (fgets(sysname, MAXHOST, whoami) == NULL)
break;
if (sscanf(sysname, "#define sysname \"%[^\"]\"",
hostname) > 0)
break;
}
(void) fclose(whoami);
if (*hostname != '\0')
return 0;
}
/* ask uucp */
if ((whoami = popen("PATH=/bin:/usr/bin:/usr/ucb uuname -l", "r")) != NULL) {
register char *ptr;
(void) fgets(hostname, size, whoami);
(void) pclose(whoami);
if ((ptr = index(hostname, '\n')) != NULL)
*ptr = '\0';
}
if (*hostname != '\0')
return 0;
#endif /* AMIGA */
/* aw hell, i give up! is this a real unix? */
(void) strncpy(hostname, defhost, min(sizeof defhost, size));
return 0;
}
/* read host name from file */
static void readhostfile( char *hostname, int size, char *file)
{
register FILE *whoami;
if ((whoami = fopen(file, "r")) != NULL) {
register char *ptr;
(void) fgets(hostname, size, whoami);
(void) fclose(whoami);
if ((ptr = index(hostname, '\n')) != NULL)
*ptr = '\0';
}
}