diff options
author | Jiawen Geng <technicalcute@gmail.com> | 2022-04-11 09:52:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-11 09:52:28 +0800 |
commit | a78671543b671d93fa349dcaa3f87f8ed4bcd116 (patch) | |
tree | d50a42c2d8024dd8dc1f78b3ab92b3f76dde83f7 /docs/code | |
parent | 69ebb2d720ac476248460e1fb3f0f3d10af483dc (diff) | |
download | libuv-a78671543b671d93fa349dcaa3f87f8ed4bcd116.tar.gz libuv-a78671543b671d93fa349dcaa3f87f8ed4bcd116.zip |
doc: make sample cross-platform build (#3592)
Co-authored-by: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'docs/code')
-rw-r--r-- | docs/code/CMakeLists.txt | 51 | ||||
-rw-r--r-- | docs/code/cgi/main.c | 4 | ||||
-rw-r--r-- | docs/code/interfaces/main.c | 14 | ||||
-rw-r--r-- | docs/code/thread-create/main.c | 5 | ||||
-rw-r--r-- | docs/code/udp-dhcp/main.c | 3 | ||||
-rw-r--r-- | docs/code/uvcat/main.c | 1 | ||||
-rw-r--r-- | docs/code/uvtee/main.c | 1 |
7 files changed, 64 insertions, 15 deletions
diff --git a/docs/code/CMakeLists.txt b/docs/code/CMakeLists.txt new file mode 100644 index 00000000..3d01991b --- /dev/null +++ b/docs/code/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.5) + +project(libuv_sample) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +add_subdirectory("../../" build) + +set(SIMPLE_SAMPLES + cgi + helloworld + dns + detach + default-loop + idle-basic + idle-compute + interfaces + locks + onchange + pipe-echo-server + ref-timer + spawn + tcp-echo-server + thread-create + udp-dhcp + uvcat + uvstop + uvtee + ) +IF (NOT WIN32) + list(APPEND SIMPLE_SAMPLES + signal + progress + queue-cancel + queue-work + tty + tty-gravity + ) +ENDIF() + +foreach (X IN LISTS SIMPLE_SAMPLES) + add_executable(${X} ${X}/main.c) + target_link_libraries(${X} uv_a) +endforeach () + + +FIND_PACKAGE(CURL) +IF(CURL_FOUND) + add_executable(uvwget uvwget/main.c) + target_link_libraries(uvwget uv_a ${CURL_LIBRARIES}) +ENDIF(CURL_FOUND) diff --git a/docs/code/cgi/main.c b/docs/code/cgi/main.c index d2e34265..97422110 100644 --- a/docs/code/cgi/main.c +++ b/docs/code/cgi/main.c @@ -15,8 +15,8 @@ void cleanup_handles(uv_process_t *req, int64_t exit_status, int term_signal) { } void invoke_cgi_script(uv_tcp_t *client) { - size_t size = 500; - char path[size]; + char path[500]; + size_t size = sizeof(path); uv_exepath(path, &size); strcpy(path + (strlen(path) - strlen("cgi")), "tick"); diff --git a/docs/code/interfaces/main.c b/docs/code/interfaces/main.c index cac12c26..744a47f2 100644 --- a/docs/code/interfaces/main.c +++ b/docs/code/interfaces/main.c @@ -11,17 +11,17 @@ int main() { printf("Number of interfaces: %d\n", count); while (i--) { - uv_interface_address_t interface = info[i]; + uv_interface_address_t interface_a = info[i]; - printf("Name: %s\n", interface.name); - printf("Internal? %s\n", interface.is_internal ? "Yes" : "No"); + printf("Name: %s\n", interface_a.name); + printf("Internal? %s\n", interface_a.is_internal ? "Yes" : "No"); - if (interface.address.address4.sin_family == AF_INET) { - uv_ip4_name(&interface.address.address4, buf, sizeof(buf)); + if (interface_a.address.address4.sin_family == AF_INET) { + uv_ip4_name(&interface_a.address.address4, buf, sizeof(buf)); printf("IPv4 address: %s\n", buf); } - else if (interface.address.address4.sin_family == AF_INET6) { - uv_ip6_name(&interface.address.address6, buf, sizeof(buf)); + else if (interface_a.address.address4.sin_family == AF_INET6) { + uv_ip6_name(&interface_a.address.address6, buf, sizeof(buf)); printf("IPv6 address: %s\n", buf); } diff --git a/docs/code/thread-create/main.c b/docs/code/thread-create/main.c index 70224c1e..7e345ef0 100644 --- a/docs/code/thread-create/main.c +++ b/docs/code/thread-create/main.c @@ -1,5 +1,4 @@ #include <stdio.h> -#include <unistd.h> #include <uv.h> @@ -7,7 +6,7 @@ void hare(void *arg) { int tracklen = *((int *) arg); while (tracklen) { tracklen--; - sleep(1); + uv_sleep(1000); fprintf(stderr, "Hare ran another step\n"); } fprintf(stderr, "Hare done running!\n"); @@ -18,7 +17,7 @@ void tortoise(void *arg) { while (tracklen) { tracklen--; fprintf(stderr, "Tortoise ran another step\n"); - sleep(3); + uv_sleep(3000); } fprintf(stderr, "Tortoise done running!\n"); } diff --git a/docs/code/udp-dhcp/main.c b/docs/code/udp-dhcp/main.c index fc2ca0c8..4dc28390 100644 --- a/docs/code/udp-dhcp/main.c +++ b/docs/code/udp-dhcp/main.c @@ -53,7 +53,8 @@ uv_buf_t make_discover_msg() { // HOPS buffer.base[3] = 0x0; // XID 4 bytes - buffer.base[4] = (unsigned int) random(); + if (uv_random(NULL, NULL, &buffer.base[4], 4, 0, NULL)) + abort(); // SECS buffer.base[8] = 0x0; // FLAGS diff --git a/docs/code/uvcat/main.c b/docs/code/uvcat/main.c index b03b0944..01923f2a 100644 --- a/docs/code/uvcat/main.c +++ b/docs/code/uvcat/main.c @@ -1,7 +1,6 @@ #include <assert.h> #include <stdio.h> #include <fcntl.h> -#include <unistd.h> #include <uv.h> void on_read(uv_fs_t *req); diff --git a/docs/code/uvtee/main.c b/docs/code/uvtee/main.c index 6216c2eb..be307b9a 100644 --- a/docs/code/uvtee/main.c +++ b/docs/code/uvtee/main.c @@ -1,6 +1,5 @@ #include <stdio.h> #include <fcntl.h> -#include <unistd.h> #include <string.h> #include <stdlib.h> |