LibC/Shell: Implement gethostname() and shell uses it for hostname
This commit is contained in:
parent
edc30cd71d
commit
20d38ed28c
|
@ -119,6 +119,19 @@ int isatty(int fildes)
|
|||
return syscall(SYS_ISATTY, fildes) >= 0;
|
||||
}
|
||||
|
||||
int gethostname(char* name, size_t namelen)
|
||||
{
|
||||
FILE* fp = fopen("/etc/hostname", "r");
|
||||
if (fp == NULL)
|
||||
return -1;
|
||||
size_t nread = fread(name, namelen - 1, 1, fp);
|
||||
while (nread > 0 && name[nread - 1] == '\n')
|
||||
nread--;
|
||||
name[nread] = '\0';
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int execl(const char* pathname, const char* arg0, ...)
|
||||
{
|
||||
if (arg0 == nullptr)
|
||||
|
|
|
@ -915,21 +915,9 @@ int main(int argc, char** argv)
|
|||
|
||||
tcgetattr(0, &old_termios);
|
||||
|
||||
{
|
||||
FILE* fp = fopen("/etc/hostname", "r");
|
||||
if (fp != NULL)
|
||||
{
|
||||
char buffer[512];
|
||||
while (size_t nbyte = fread(buffer, 1, sizeof(buffer), fp))
|
||||
{
|
||||
if (nbyte == 0)
|
||||
break;
|
||||
MUST(hostname.append(BAN::StringView(buffer, nbyte)));
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
if (!hostname.empty() && hostname.back() == '\n')
|
||||
hostname.pop_back();
|
||||
char hostname_buffer[HOST_NAME_MAX];
|
||||
if (gethostname(hostname_buffer, sizeof(hostname_buffer)) == 0) {
|
||||
MUST(hostname.append(hostname_buffer));
|
||||
}
|
||||
|
||||
if (argc >= 2)
|
||||
|
|
Loading…
Reference in New Issue