From aab9ab9fdd05119855383b33b67b27dcec5f3902 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Mon, 26 Jul 1999 23:16:59 +0000 Subject: [PATCH] Manipulate file descriptor flags. Organization: Straylight/Edgeware From: mdw --- fdflags.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++ fdflags.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++ man/fdflags.3 | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 fdflags.c create mode 100644 fdflags.h create mode 100644 man/fdflags.3 diff --git a/fdflags.c b/fdflags.c new file mode 100644 index 0000000..85ef67c --- /dev/null +++ b/fdflags.c @@ -0,0 +1,75 @@ +/* -*-c-*- + * + * $Id: fdflags.c,v 1.1 1999/07/26 23:16:59 mdw Exp $ + * + * Manipulates flags on file descriptors + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the mLib utilities library. + * + * mLib is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * mLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with mLib; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: fdflags.c,v $ + * Revision 1.1 1999/07/26 23:16:59 mdw + * Manipulate file descriptor flags. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include +#include + +#include + +#include "fdflags.h" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @fdflags@ --- * + * + * Arguments: @int fd@ = file descriptor to fiddle with + * @unsigned fbic, fxor@ = file flags to set and clear + * @unsigned fdbic, fdxor@ = descriptor flags to set and clear + * + * Returns: Zero if successful, @-1@ if not. + * + * Use: Sets file descriptor flags in what is, I hope, an obvious + * way. + */ + +int fdflags(int fd, unsigned fbic, unsigned fxor, + unsigned fdbic, unsigned fdxor) +{ + int f; + + if ((f = fcntl(fd, F_GETFL)) == -1 || + fcntl(fd, F_SETFL, (f & ~fbic) ^ fxor) == -1 || + (f = fcntl(fd, F_GETFD)) == -1 || + fcntl(fd, F_SETFD, (f & ~fdbic) ^ fdxor) == -1) + return (-1); + return (0); +} + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/fdflags.h b/fdflags.h new file mode 100644 index 0000000..411fb2d --- /dev/null +++ b/fdflags.h @@ -0,0 +1,77 @@ +/* -*-c-*- + * + * $Id: fdflags.h,v 1.1 1999/07/26 23:16:59 mdw Exp $ + * + * Manipulates flags on file descriptors + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the mLib utilities library. + * + * mLib is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * mLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with mLib; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: fdflags.h,v $ + * Revision 1.1 1999/07/26 23:16:59 mdw + * Manipulate file descriptor flags. + * + */ + +#ifndef FDFLAGS_H +#define FDFLAGS_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include + +/*----- Magic numbers -----------------------------------------------------*/ + +#ifndef FD_CLOEXEC +# define FD_CLOEXEC 1 +#endif + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @fdflags@ --- * + * + * Arguments: @int fd@ = file descriptor to fiddle with + * @unsigned fbic, fxor@ = file flags to set and clear + * @unsigned fdbic, fdxor@ = descriptor flags to set and clear + * + * Returns: Zero if successful. + * + * Use: Sets file descriptor flags. + */ + +int fdflags(int /*fd*/, unsigned /*fbic*/, unsigned /*fxor*/, + unsigned /*fdbic*/, unsigned /*fdxor*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/man/fdflags.3 b/man/fdflags.3 new file mode 100644 index 0000000..77f9147 --- /dev/null +++ b/man/fdflags.3 @@ -0,0 +1,82 @@ +.\" -*-nroff-*- +.de VS +.sp 1 +.RS +.nf +.ft B +.. +.de VE +.ft R +.fi +.RE +.sp 1 +.. +.TH fdflags 3 "23 July 1999" mLib +.SH "NAME" +fdflags \- set file and file descriptor flags +.\" @fdflags +.SH "SYNOPSIS" +.nf +.B "#include " + +.BI "int fdflags(int " fd , +.BI " unsigned " fbic ", unsigned " fxor , +.BI " unsigned " fdbic ", unsigned " fdxor ); +.fi +.SH "DESCRIPTION" +.B fdflags +is a convenience function for setting file and file descriptor flags +using +.BR fcntl (2). +.PP +The file flags are read using +.BR F_GETFL , +the new flags are calculated as +.sp 1 +.RS +.I new-flags += +.BI ( old-flags +.B & +.BI ~ fbic ) +.B ^ +.I fxor +.RE +.sp 1 +and the result written back using +.BR F_SETFL . +.PP +Similarly the file descriptor flags are read using +.BR F_GETFD , +the new flags calculated as +.sp 1 +.RS +.I new-flags += +.BI ( old-flags +.B & +.BI ~ fdbic ) +.B ^ +.I fdxor +.RE +.sp 1 +and the result written back using +.BR F_SETFD . +.PP +If all went well, +.B fdflags +returns zero; if there was an error, \-1 is returned. +.SH "EXAMPLES" +To set the non-blocking and close-on-exec flags: +.VS +fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); +.VE +To clear the non-blocking and close-on-exec flags: +.VS +fdflags(fd, O_NONBLOCK, 0, FD_CLOEXEC, 0); +.VE +.sp -1 +.SH "SEE ALSO" +.BR mLib (3). +.SH "AUTHOR" +Mark Wooding, -- [mdw]