const int BUF_SIZE = 256;
int main() {
char name[BUF_SIZE];
if (-1 == gethostname(name, BUF_SIZE)) {
cout << "Failed to get the host name " << endl;
} else {
cout << "The host name is " << name << endl;
}
if (somecall() == -1) {
printf("somecall() failed\n");
if (errno == ...) { ... }
}
if (somecall() == -1) {
errorSave = errno;
printf("somecall() failed\n");
if (errorSave == ...) { ... }
}
if (-1 == gethostname(name, 0)) {
cout << "Failed to get the host name " << endl;
} else {
cout << "The host name is " << name << endl;
}
cout << "errno is " << errno << endl;
if (-1 == gethostname(name, 0)) {
perror("gethostbyename failed");
strcpy(name, "UNKNOWN");
}
cout << "The host name is " << name << endl;