Coverage Report

Created: 2025-07-11 06:33

/src/PROJ/curl/lib/curlx/nonblock.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "../curl_setup.h"
26
27
#ifdef HAVE_SYS_IOCTL_H
28
#include <sys/ioctl.h>
29
#endif
30
#ifdef HAVE_FCNTL_H
31
#include <fcntl.h>
32
#endif
33
34
#ifdef __VMS
35
#include <in.h>
36
#include <inet.h>
37
#endif
38
39
#include "nonblock.h"
40
41
/*
42
 * curlx_nonblock() set the given socket to either blocking or non-blocking
43
 * mode based on the 'nonblock' boolean argument. This function is highly
44
 * portable.
45
 */
46
int curlx_nonblock(curl_socket_t sockfd,    /* operate on this */
47
                   int nonblock   /* TRUE or FALSE */)
48
0
{
49
0
#if defined(HAVE_FCNTL_O_NONBLOCK)
50
  /* most recent Unix versions */
51
0
  int flags;
52
0
  flags = sfcntl(sockfd, F_GETFL, 0);
53
0
  if(flags < 0)
54
0
    return -1;
55
  /* Check if the current file status flags have already satisfied
56
   * the request, if so, it is no need to call fcntl() to replicate it.
57
   */
58
0
  if(!!(flags & O_NONBLOCK) == !!nonblock)
59
0
    return 0;
60
0
  if(nonblock)
61
0
    flags |= O_NONBLOCK;
62
0
  else
63
0
    flags &= ~O_NONBLOCK;
64
0
  return sfcntl(sockfd, F_SETFL, flags);
65
66
#elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
67
68
  /* Amiga */
69
  long flags = nonblock ? 1L : 0L;
70
  return IoctlSocket(sockfd, FIONBIO, (char *)&flags);
71
72
#elif defined(HAVE_IOCTL_FIONBIO)
73
74
  /* older Unix versions */
75
  int flags = nonblock ? 1 : 0;
76
  return ioctl(sockfd, FIONBIO, &flags);
77
78
#elif defined(HAVE_IOCTLSOCKET_FIONBIO)
79
80
  /* Windows */
81
  unsigned long flags = nonblock ? 1UL : 0UL;
82
  return ioctlsocket(sockfd, (long)FIONBIO, &flags);
83
84
#elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
85
86
  /* Orbis OS */
87
  long b = nonblock ? 1L : 0L;
88
  return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
89
90
#else
91
#  error "no non-blocking method was found/used/set"
92
#endif
93
0
}