awips2/ldm/src/misc/fdnb.c
root 7dbd17a5aa Initial revision of AWIPS2 11.9.0-7p5
Former-commit-id: 133dc97f67 [formerly a02aeb236c] [formerly 9f19e3f712] [formerly 133dc97f67 [formerly a02aeb236c] [formerly 9f19e3f712] [formerly 06a8b51d6d [formerly 9f19e3f712 [formerly 64fa9254b946eae7e61bbc3f513b7c3696c4f54f]]]]
Former-commit-id: 06a8b51d6d
Former-commit-id: 9bb8decbcf [formerly 8e80217e59] [formerly 377dcd10b9 [formerly 3360eb6c5f]]
Former-commit-id: 377dcd10b9
Former-commit-id: e2ecdcfe33
2012-01-06 08:55:05 -06:00

83 lines
1.6 KiB
C

/*
* Copyright 1993, University Corporation for Atmospheric Research
* See ../COPYRIGHT file for copying and redistribution conditions.
*/
/* $Id: fdnb.c,v 1.5 2002/12/09 18:23:56 steve Exp $ */
#include <ldmconfig.h>
#include <sys/types.h>
#include <fcntl.h>
#include "ulog.h"
#include "fdnb.h"
/* some systems have FNDELAY (== O_NDELAY) different from O_NONBLOCK */
#if defined(__ultrix) || defined(ultrix)
/* ultrix 4.3 (and before?) */
/* #define NB_O_NONBLOCK (FNDELAY | O_NONBLOCK) */
#define NB_O_NONBLOCK FNDELAY
#endif
/* default do like posix sez */
#ifndef NB_O_NONBLOCK
#define NB_O_NONBLOCK O_NONBLOCK
#endif
/*
* Set descriptor for non blocking
* return 1 if changed, 0 otherwise.
*/
int
set_fd_nonblock(int fd)
{
unsigned flags = (unsigned)fcntl(fd, F_GETFL, 0);
if(flags == (unsigned)-1)
{
serror("fcntl(..., F_GETFL,)");
return 0;
}
/* else */
if(flags & NB_O_NONBLOCK)
return 0; /* no change required */
/* else */
flags |= NB_O_NONBLOCK;
if(fcntl(fd, F_SETFL, (int)flags) == -1)
{
serror("fcntl(..., F_SETFL, O_NONBLOCK)");
return 0;
}
return 1;
}
/*
* clear descriptor for non blocking
* (EG, set to blocking)
* return 1 if changed, 0 otherwise.
*/
int
clr_fd_nonblock(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if(flags == -1)
{
serror("fcntl(..., F_GETFL,)");
return 0;
}
/* else */
if(!((unsigned int)flags & NB_O_NONBLOCK))
return 0; /* no change required */
/* else */
flags &= ~NB_O_NONBLOCK;
if(fcntl(fd, F_SETFL, flags) == -1)
{
serror("fcntl(..., F_SETFL, &= ~O_NONBLOCK)");
return 0;
}
return 1;
}