1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/*************************************************************************\
*
* Package: MyLib
* File: gdef.c
* Environment: ANSI C
*
* Copyright (c) 2002 Pierre L'Ecuyer, DIRO, Université de Montréal.
* e-mail: lecuyer@iro.umontreal.ca
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted without a fee for private, research,
* academic, or other non-commercial purposes.
* Any use of this software in a commercial environment requires a
* written licence from the copyright owner.
*
* Any changes made to this package must be clearly identified as such.
*
* In scientific publications which used this software, a reference to it
* would be appreciated.
*
* Redistributions of source code must retain this copyright notice
* and the following disclaimer.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
\*************************************************************************/
#ifdef HAVE_CONFIG_H
#include "gdefconf.h"
#endif
#include "gdef.h"
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#else
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif
void gdef_GetHostName (char machine[], int n)
{
int j;
#ifdef HAVE_SYS_UTSNAME_H
struct utsname Z;
#else
char *name;
#endif
if (n <= 0 || machine == NULL)
return;
machine[0] = '\0';
#ifdef HAVE_SYS_UTSNAME_H
if (uname(&Z) != -1) {
strncpy (machine, Z.nodename, (size_t) n);
j = strlen (machine);
if (n - j > 2)
strncat (machine, ", ", (size_t) 2);
j = strlen (machine);
if (n - j > 0)
strncat (machine, Z.sysname, (size_t) (n - j));
machine[n - 1] = '\0';
return;
}
#else
#ifdef HAVE_UNISTD_H
gethostname (machine, (size_t) n);
machine[n - 1] = '\0';
return;
#else
name = getenv ("HOST");
if (name != NULL) {
strncpy (machine, name, (size_t) n);
j = strlen (machine);
machine[n - 1] = '\0';
if (j < n - 3) {
machine[j++] = ',';
machine[j++] = ' ';
}
}
name = getenv ("OSTYPE");
if (name != NULL) {
if ((int) strlen (name) < n - j)
strncat (machine, name, (size_t) n - j);
machine[n - 1] = '\0';
}
#endif
#endif
}
/*------------------------------------------------------------------------*/
#define MAXBYTES 255
void gdef_WriteHostName (void)
{
char machine[1 + MAXBYTES] = {'\0'};
gdef_GetHostName (machine, MAXBYTES);
printf ("%s\n", machine);
}
#if 0
int main()
{
struct utsname name;
if (uname(&name) != -1)
printf(" %s\n", name.nodename);
else {
fprintf(stderr, "Can't find system name\n");
return 1;
}
gdef_GetHostName (0, MAXBYTES);
gdef_WriteHostName ();
return 0;
}
#endif
|