Coverage Report

Created: 2026-01-25 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curlx/nonblock.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "../curl_setup.h"
25
26
#ifdef HAVE_SYS_IOCTL_H
27
#include <sys/ioctl.h>
28
#endif
29
#ifdef HAVE_FCNTL_H
30
#include <fcntl.h>
31
#endif
32
33
#ifdef __VMS
34
#include <in.h>
35
#include <inet.h>
36
#endif
37
38
#include "nonblock.h"
39
40
/*
41
 * curlx_nonblock() set the given socket to either blocking or non-blocking
42
 * mode based on the 'nonblock' boolean argument. This function is highly
43
 * portable.
44
 */
45
int curlx_nonblock(curl_socket_t sockfd,    /* operate on this */
46
                   int nonblock   /* TRUE or FALSE */)
47
0
{
48
0
#ifdef HAVE_FCNTL_O_NONBLOCK
49
  /* most recent Unix versions */
50
0
  int flags;
51
0
  flags = sfcntl(sockfd, F_GETFL, 0);
52
0
  if(flags < 0)
53
0
    return -1;
54
  /* Check if the current file status flags have already satisfied
55
   * the request, if so, it is no need to call fcntl() to replicate it.
56
   */
57
0
  if(!!(flags & O_NONBLOCK) == !!nonblock)
58
0
    return 0;
59
0
  if(nonblock)
60
0
    flags |= O_NONBLOCK;
61
0
  else
62
0
    flags &= ~O_NONBLOCK;
63
0
  return sfcntl(sockfd, F_SETFL, flags);
64
65
#elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
66
67
  /* Amiga */
68
  long flags = nonblock ? 1L : 0L;
69
  return IoctlSocket(sockfd, FIONBIO, (char *)&flags);
70
71
#elif defined(HAVE_IOCTL_FIONBIO)
72
73
  /* older Unix versions */
74
  int flags = nonblock ? 1 : 0;
75
  return ioctl(sockfd, FIONBIO, &flags);
76
77
#elif defined(HAVE_IOCTLSOCKET_FIONBIO)
78
79
  /* Windows */
80
  unsigned long flags = nonblock ? 1UL : 0UL;
81
  return ioctlsocket(sockfd, (long)FIONBIO, &flags);
82
83
#elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
84
85
  /* Orbis OS */
86
  long b = nonblock ? 1L : 0L;
87
  return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
88
89
#else
90
#error "no non-blocking method was found/used/set"
91
#endif
92
0
}