Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

unix: add os information #675

Closed
wants to merge 1 commit into from

2 participants

@mmalecki

This still needs Windows version.

/cc @bnoordhuis

src/unix/os.c
((13 lines not shown))
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "internal.h"
+
+#include <errno.h>
+#include <string.h>
+#include <sys/utsname.h>
+
+uv_err_t uv_hostname(char* hostname, size_t size) {

Style: two newlines between functions and headers / declarations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/unix/os.c
((14 lines not shown))
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "internal.h"
+
+#include <errno.h>
+#include <string.h>
+#include <sys/utsname.h>
+
+uv_err_t uv_hostname(char* hostname, size_t size) {
+ int r = gethostname(hostname, size);

Style: split the declaration and the assignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/unix/os.c
((18 lines not shown))
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "internal.h"
+
+#include <errno.h>
+#include <string.h>
+#include <sys/utsname.h>
+
+uv_err_t uv_hostname(char* hostname, size_t size) {
+ int r = gethostname(hostname, size);
+
+ if (r < 0) {
+ return uv__new_sys_error(errno);
+ }

In general, single line if statements don't have braces, i.e.:

if (r == -1)
  return uv__new_sys_error(errno);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/unix/os.c
((33 lines not shown))
+ }
+
+ return uv_ok_;
+}
+
+uv_err_t uv_os_type(char* type, size_t size) {
+ struct utsname info;
+
+ int r = uname(&info);
+
+ if (r < 0) {
+ return uv__new_sys_error(errno);
+ }
+
+ strncpy(type, info.sysname, size - 1);
+ type[size] = '\0';

Use uv_strlcpy() here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-platform-output.c
@@ -83,5 +83,17 @@
}
uv_free_interface_addresses(interfaces, count);
+ err = uv_hostname(buffer, 512);

s/512/sizeof(buffer)/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
include/uv.h
@@ -1467,6 +1467,10 @@ UV_EXTERN uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
int count);
+UV_EXTERN uv_err_t uv_hostname(char* hostname, size_t size);
+UV_EXTERN uv_err_t uv_os_type(char* type, size_t size);
+UV_EXTERN uv_err_t uv_os_release(char* release, size_t size);

Can you add some short comments describing what the function does and what inputs it takes / outputs it produces?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mmalecki

@bnoordhuis all fixed.

@bnoordhuis

LGTM but it needs Windows stubs (bonus points for working implementations). I suggest that you copy out an empty string besides returning UV_ENOSYS.

@bnoordhuis

@mmalecki Are you still planning on finishing this? It'll need some rebasing to apply to master.

@bnoordhuis

No followup, closing.

@bnoordhuis bnoordhuis closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2013
  1. @mmalecki

    unix: add os information

    mmalecki authored
This page is out of date. Refresh to see the latest.
View
1  config-unix.mk
@@ -50,6 +50,7 @@ OBJS += src/unix/threadpool.o
OBJS += src/unix/timer.o
OBJS += src/unix/tty.o
OBJS += src/unix/udp.o
+OBJS += src/unix/os.o
OBJS += src/fs-poll.o
OBJS += src/uv-common.o
OBJS += src/inet.o
View
19 include/uv.h
@@ -1468,6 +1468,25 @@ UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
int count);
/*
+ * This gets maximum of `size` chars from machine's hostname and saves it to
+ * `hostname` parameter.
+ */
+UV_EXTERN uv_err_t uv_hostname(char* hostname, size_t size);
+
+
+/*
+ * This gets maximum of `size` chars from machine's OS type and saves it to
+ * `type` parameter.
+ */
+UV_EXTERN uv_err_t uv_os_type(char* type, size_t size);
+
+/*
+ * This gets maximum of `size` chars from machine's OS release version and
+ * saves it to `release` parameter.
+ */
+UV_EXTERN uv_err_t uv_os_release(char* release, size_t size);
+
+/*
* File System Methods.
*
* The uv_fs_* functions execute a blocking system call asynchronously (in a
View
68 src/unix/os.c
@@ -0,0 +1,68 @@
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "internal.h"
+
+#include <errno.h>
+#include <string.h>
+#include <sys/utsname.h>
+
+
+uv_err_t uv_hostname(char* hostname, size_t size) {
+ int r;
+
+ r = gethostname(hostname, size);
+
+ if (r < 0)
+ return uv__new_sys_error(errno);
+
+ return uv_ok_;
+}
+
+
+uv_err_t uv_os_type(char* type, size_t size) {
+ int r;
+ struct utsname info;
+
+ r = uname(&info);
+
+ if (r < 0)
+ return uv__new_sys_error(errno);
+
+ uv_strlcpy(type, info.sysname, size);
+
+ return uv_ok_;
+}
+
+
+uv_err_t uv_os_release(char* release, size_t size) {
+ int r;
+ struct utsname info;
+
+ r = uname(&info);
+
+ if (r < 0)
+ return uv__new_sys_error(errno);
+
+ uv_strlcpy(release, info.release, size);
+
+ return uv_ok_;
+}
View
12 test/test-platform-output.c
@@ -83,5 +83,17 @@ TEST_IMPL(platform_output) {
}
uv_free_interface_addresses(interfaces, count);
+ err = uv_hostname(buffer, sizeof(buffer));
+ ASSERT(UV_OK == err.code);
+ printf("uv_hostname: %s\n", buffer);
+
+ err = uv_os_type(buffer, sizeof(buffer));
+ ASSERT(UV_OK == err.code);
+ printf("uv_os_type: %s\n", buffer);
+
+ err = uv_os_release(buffer, sizeof(buffer));
+ ASSERT(UV_OK == err.code);
+ printf("uv_os_release: %s\n", buffer);
+
return 0;
}
View
1  uv.gyp
@@ -141,6 +141,7 @@
'src/unix/timer.c',
'src/unix/tty.c',
'src/unix/udp.c',
+ 'src/unix/os.c',
],
'link_settings': {
'libraries': [ '-lm' ],
Something went wrong with that request. Please try again.