diff options
-rw-r--r-- | gl/Makefile.am | 17 | ||||
-rw-r--r-- | gl/m4/gnulib-cache.m4 | 4 | ||||
-rw-r--r-- | gl/m4/gnulib-comp.m4 | 8 | ||||
-rw-r--r-- | gl/m4/minmax.m4 | 41 | ||||
-rw-r--r-- | gl/m4/read-file.m4 | 14 | ||||
-rw-r--r-- | gl/minmax.h | 60 | ||||
-rw-r--r-- | gl/read-file.c | 136 | ||||
-rw-r--r-- | gl/read-file.h | 34 | ||||
-rw-r--r-- | gl/tests/Makefile.am | 8 | ||||
-rw-r--r-- | gl/tests/test-read-file.c | 97 | ||||
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/cli.c | 6 | ||||
-rw-r--r-- | src/serv.c | 9 |
13 files changed, 421 insertions, 19 deletions
diff --git a/gl/Makefile.am b/gl/Makefile.am index 2e07d02407..83d0ec21c2 100644 --- a/gl/Makefile.am +++ b/gl/Makefile.am @@ -9,7 +9,7 @@ # the same distribution terms as the rest of that program. # # Generated by gnulib-tool. -# Reproduce by: gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --libtool --macro-prefix=gl --no-vc-files accept arpa_inet autobuild bind close error fdl gendocs getaddrinfo getline getpass-gnu gnupload gpl-3.0 inet_ntop inet_pton lgpl-2.1 listen maintainer-makefile progname readline select setsockopt shutdown socket version-etc-fsf +# Reproduce by: gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --libtool --macro-prefix=gl --no-vc-files accept arpa_inet autobuild bind close error fdl gendocs getaddrinfo getline getpass-gnu gnupload gpl-3.0 inet_ntop inet_pton lgpl-2.1 listen maintainer-makefile minmax progname read-file readline select setsockopt shutdown socket version-etc-fsf AUTOMAKE_OPTIONS = 1.5 gnits @@ -333,6 +333,12 @@ EXTRA_DIST += $(top_srcdir)/maint.mk ## end gnulib module maintainer-makefile +## begin gnulib module minmax + +libgnu_la_SOURCES += minmax.h + +## end gnulib module minmax + ## begin gnulib module netdb BUILT_SOURCES += $(NETDB_H) @@ -386,6 +392,15 @@ libgnu_la_SOURCES += progname.h progname.c ## end gnulib module progname +## begin gnulib module read-file + + +EXTRA_DIST += read-file.c read-file.h + +EXTRA_libgnu_la_SOURCES += read-file.c + +## end gnulib module read-file + ## begin gnulib module readline diff --git a/gl/m4/gnulib-cache.m4 b/gl/m4/gnulib-cache.m4 index 71e0501577..c79d9dfdc1 100644 --- a/gl/m4/gnulib-cache.m4 +++ b/gl/m4/gnulib-cache.m4 @@ -15,7 +15,7 @@ # Specification in the form of a command-line invocation: -# gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --libtool --macro-prefix=gl --no-vc-files accept arpa_inet autobuild bind close error fdl gendocs getaddrinfo getline getpass-gnu gnupload gpl-3.0 inet_ntop inet_pton lgpl-2.1 listen maintainer-makefile progname readline select setsockopt shutdown socket version-etc-fsf +# gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --libtool --macro-prefix=gl --no-vc-files accept arpa_inet autobuild bind close error fdl gendocs getaddrinfo getline getpass-gnu gnupload gpl-3.0 inet_ntop inet_pton lgpl-2.1 listen maintainer-makefile minmax progname read-file readline select setsockopt shutdown socket version-etc-fsf # Specification in the form of a few gnulib-tool.m4 macro invocations: gl_LOCAL_DIR([gl/override]) @@ -38,7 +38,9 @@ gl_MODULES([ lgpl-2.1 listen maintainer-makefile + minmax progname + read-file readline select setsockopt diff --git a/gl/m4/gnulib-comp.m4 b/gl/m4/gnulib-comp.m4 index 3770bad63e..c5e022b521 100644 --- a/gl/m4/gnulib-comp.m4 +++ b/gl/m4/gnulib-comp.m4 @@ -108,9 +108,11 @@ AC_SUBST([LTALLOCA]) gl_SYS_SOCKET_MODULE_INDICATOR([listen]) gl_FUNC_LSEEK gl_UNISTD_MODULE_INDICATOR([lseek]) + gl_MINMAX gl_HEADER_NETDB gl_HEADER_NETINET_IN AC_PROG_MKDIR_P + gl_FUNC_READ_FILE gl_FUNC_READLINE gl_FUNC_REALLOC_POSIX gl_STDLIB_MODULE_INDICATOR([realloc-posix]) @@ -343,6 +345,7 @@ AC_DEFUN([gl_FILE_LIST], [ lib/intprops.h lib/listen.c lib/lseek.c + lib/minmax.h lib/netdb.in.h lib/netinet_in.in.h lib/printf-args.c @@ -351,6 +354,8 @@ AC_DEFUN([gl_FILE_LIST], [ lib/printf-parse.h lib/progname.c lib/progname.h + lib/read-file.c + lib/read-file.h lib/readline.c lib/readline.h lib/realloc.c @@ -407,10 +412,12 @@ AC_DEFUN([gl_FILE_LIST], [ m4/longlong.m4 m4/lseek.m4 m4/malloc.m4 + m4/minmax.m4 m4/netdb_h.m4 m4/netinet_in_h.m4 m4/perror.m4 m4/printf.m4 + m4/read-file.m4 m4/readline.m4 m4/realloc.m4 m4/size_max.m4 @@ -451,6 +458,7 @@ AC_DEFUN([gl_FILE_LIST], [ tests/test-netinet_in.c tests/test-perror.c tests/test-perror.sh + tests/test-read-file.c tests/test-select.c tests/test-snprintf.c tests/test-sockets.c diff --git a/gl/m4/minmax.m4 b/gl/m4/minmax.m4 new file mode 100644 index 0000000000..bbd1ba0b84 --- /dev/null +++ b/gl/m4/minmax.m4 @@ -0,0 +1,41 @@ +# minmax.m4 serial 2 +dnl Copyright (C) 2005 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_PREREQ(2.52) + +AC_DEFUN([gl_MINMAX], +[ + AC_REQUIRE([gl_PREREQ_MINMAX]) +]) + +# Prerequisites of lib/minmax.h. +AC_DEFUN([gl_PREREQ_MINMAX], +[ + gl_MINMAX_IN_HEADER([limits.h]) + gl_MINMAX_IN_HEADER([sys/param.h]) +]) + +dnl gl_MINMAX_IN_HEADER(HEADER) +dnl The parameter has to be a literal header name; it cannot be macro, +dnl nor a shell variable. (Because autoheader collects only AC_DEFINE +dnl invocations with a literal macro name.) +AC_DEFUN([gl_MINMAX_IN_HEADER], +[ + m4_pushdef([header], AS_TR_SH([$1])) + m4_pushdef([HEADER], AS_TR_CPP([$1])) + AC_CACHE_CHECK([whether <$1> defines MIN and MAX], + [gl_cv_minmax_in_]header, + [AC_TRY_COMPILE([#include <$1> +int x = MIN (42, 17);], [], + [gl_cv_minmax_in_]header[=yes], + [gl_cv_minmax_in_]header[=no])]) + if test $gl_cv_minmax_in_[]header = yes; then + AC_DEFINE([HAVE_MINMAX_IN_]HEADER, 1, + [Define to 1 if <$1> defines the MIN and MAX macros.]) + fi + m4_popdef([HEADER]) + m4_popdef([header]) +]) diff --git a/gl/m4/read-file.m4 b/gl/m4/read-file.m4 new file mode 100644 index 0000000000..8d1b8f3c5f --- /dev/null +++ b/gl/m4/read-file.m4 @@ -0,0 +1,14 @@ +# read-file.m4 serial 2 +dnl Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_READ_FILE], +[ + AC_LIBOBJ([read-file]) + gl_PREREQ_READ_FILE +]) + +# Prerequisites of lib/read-file.c. +AC_DEFUN([gl_PREREQ_READ_FILE], [:]) diff --git a/gl/minmax.h b/gl/minmax.h new file mode 100644 index 0000000000..1ebc22078e --- /dev/null +++ b/gl/minmax.h @@ -0,0 +1,60 @@ +/* MIN, MAX macros. + Copyright (C) 1995, 1998, 2001, 2003, 2005 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef _MINMAX_H +#define _MINMAX_H + +/* Note: MIN, MAX are also defined in <sys/param.h> on some systems + (glibc, IRIX, HP-UX, OSF/1). Therefore you might get warnings about + MIN, MAX macro redefinitions on some systems; the workaround is to + #include this file as the last one among the #include list. */ + +/* Before we define the following symbols we get the <limits.h> file + since otherwise we get redefinitions on some systems if <limits.h> is + included after this file. Likewise for <sys/param.h>. + If more than one of these system headers define MIN and MAX, pick just + one of the headers (because the definitions most likely are the same). */ +#if HAVE_MINMAX_IN_LIMITS_H +# include <limits.h> +#elif HAVE_MINMAX_IN_SYS_PARAM_H +# include <sys/param.h> +#endif + +/* Note: MIN and MAX should be used with two arguments of the + same type. They might not return the minimum and maximum of their two + arguments, if the arguments have different types or have unusual + floating-point values. For example, on a typical host with 32-bit 'int', + 64-bit 'long long', and 64-bit IEEE 754 'double' types: + + MAX (-1, 2147483648) returns 4294967295. + MAX (9007199254740992.0, 9007199254740993) returns 9007199254740992.0. + MAX (NaN, 0.0) returns 0.0. + MAX (+0.0, -0.0) returns -0.0. + + and in each case the answer is in some sense bogus. */ + +/* MAX(a,b) returns the maximum of A and B. */ +#ifndef MAX +# define MAX(a,b) ((a) > (b) ? (a) : (b)) +#endif + +/* MIN(a,b) returns the minimum of A and B. */ +#ifndef MIN +# define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif + +#endif /* _MINMAX_H */ diff --git a/gl/read-file.c b/gl/read-file.c new file mode 100644 index 0000000000..0997b2e387 --- /dev/null +++ b/gl/read-file.c @@ -0,0 +1,136 @@ +/* read-file.c -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + Written by Simon Josefsson and Bruno Haible. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include <config.h> + +#include "read-file.h" + +/* Get realloc, free. */ +#include <stdlib.h> + +/* Get errno. */ +#include <errno.h> + +/* Read a STREAM and return a newly allocated string with the content, + and set *LENGTH to the length of the string. The string is + zero-terminated, but the terminating zero byte is not counted in + *LENGTH. On errors, *LENGTH is undefined, errno preserves the + values set by system functions (if any), and NULL is returned. */ +char * +fread_file (FILE * stream, size_t * length) +{ + char *buf = NULL; + size_t alloc = 0; + size_t size = 0; + int save_errno; + + for (;;) + { + size_t count; + size_t requested; + + if (size + BUFSIZ + 1 > alloc) + { + char *new_buf; + + alloc += alloc / 2; + if (alloc < size + BUFSIZ + 1) + alloc = size + BUFSIZ + 1; + + new_buf = realloc (buf, alloc); + if (!new_buf) + { + save_errno = errno; + break; + } + + buf = new_buf; + } + + requested = alloc - size - 1; + count = fread (buf + size, 1, requested, stream); + size += count; + + if (count != requested) + { + save_errno = errno; + if (ferror (stream)) + break; + buf[size] = '\0'; + *length = size; + return buf; + } + } + + free (buf); + errno = save_errno; + return NULL; +} + +static char * +internal_read_file (const char *filename, size_t * length, const char *mode) +{ + FILE *stream = fopen (filename, mode); + char *out; + int save_errno; + + if (!stream) + return NULL; + + out = fread_file (stream, length); + + save_errno = errno; + + if (fclose (stream) != 0) + { + if (out) + { + save_errno = errno; + free (out); + } + errno = save_errno; + return NULL; + } + + return out; +} + +/* Open and read the contents of FILENAME, and return a newly + allocated string with the content, and set *LENGTH to the length of + the string. The string is zero-terminated, but the terminating + zero byte is not counted in *LENGTH. On errors, *LENGTH is + undefined, errno preserves the values set by system functions (if + any), and NULL is returned. */ +char * +read_file (const char *filename, size_t * length) +{ + return internal_read_file (filename, length, "r"); +} + +/* Open (on non-POSIX systems, in binary mode) and read the contents + of FILENAME, and return a newly allocated string with the content, + and set LENGTH to the length of the string. The string is + zero-terminated, but the terminating zero byte is not counted in + the LENGTH variable. On errors, *LENGTH is undefined, errno + preserves the values set by system functions (if any), and NULL is + returned. */ +char * +read_binary_file (const char *filename, size_t * length) +{ + return internal_read_file (filename, length, "rb"); +} diff --git a/gl/read-file.h b/gl/read-file.h new file mode 100644 index 0000000000..c37804026a --- /dev/null +++ b/gl/read-file.h @@ -0,0 +1,34 @@ +/* read-file.h -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + Written by Simon Josefsson. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef READ_FILE_H +#define READ_FILE_H + +/* Get size_t. */ +#include <stddef.h> + +/* Get FILE. */ +#include <stdio.h> + +extern char *fread_file (FILE * stream, size_t * length); + +extern char *read_file (const char *filename, size_t * length); + +extern char *read_binary_file (const char *filename, size_t * length); + +#endif /* READ_FILE_H */ diff --git a/gl/tests/Makefile.am b/gl/tests/Makefile.am index 6212ffda41..8a07e408d3 100644 --- a/gl/tests/Makefile.am +++ b/gl/tests/Makefile.am @@ -178,6 +178,14 @@ EXTRA_DIST += test-perror.c test-perror.sh ## end gnulib module perror-tests +## begin gnulib module read-file-tests + +TESTS += test-read-file +check_PROGRAMS += test-read-file +EXTRA_DIST += test-read-file.c + +## end gnulib module read-file-tests + ## begin gnulib module select-tests TESTS += test-select diff --git a/gl/tests/test-read-file.c b/gl/tests/test-read-file.c new file mode 100644 index 0000000000..392c4f7eaf --- /dev/null +++ b/gl/tests/test-read-file.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2006-2007 Free Software Foundation + * Written by Simon Josefsson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include "read-file.h" + +#include <stdio.h> +#include <stdlib.h> +#include <sys/stat.h> + +#define FILE1 "/etc/resolv.conf" +#define FILE2 "/dev/null" + +int +main (void) +{ + struct stat statbuf; + int err = 0; + + /* We can perform the test only if the file exists and is readable. + Test whether it exists, then assume it is world-readable. */ + if (stat (FILE1, &statbuf) >= 0) + { + size_t len; + char *out = read_file (FILE1, &len); + + if (!out) + { + perror ("Could not read file"); + err = 1; + } + else + { + if (out[len] != '\0') + { + perror ("BAD: out[len] not zero"); + err = 1; + } + + /* Assume FILE1 is a regular file or a symlink to a regular file. */ + if (len != statbuf.st_size) + { + fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE1); + err = 1; + } + free (out); + } + } + + /* We can perform the test only if the file exists and is readable. + Test whether it exists, then assume it is world-readable. */ + if (stat (FILE2, &statbuf) >= 0) + { + size_t len; + char *out = read_file (FILE2, &len); + + if (!out) + { + perror ("Could not read file"); + err = 1; + } + else + { + if (out[len] != '\0') + { + perror ("BAD: out[len] not zero"); + err = 1; + } + + /* /dev/null should always be empty. Ignore statbuf.st_size, since it + is not a regular file. */ + if (len != 0) + { + fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE2); + err = 1; + } + free (out); + } + } + + return err; +} diff --git a/src/Makefile.am b/src/Makefile.am index 6f8b0c0452..32cb159fc4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,8 +22,6 @@ EXTRA_DIST = README README.srptool SUBDIRS = cfg AM_CPPFLAGS = \ - -I$(srcdir)/../lib/gl \ - -I$(builddir)/../lib/gl \ -I$(srcdir)/../gl \ -I$(builddir)/../gl \ -I$(builddir)/../lib/includes \ @@ -40,7 +38,7 @@ bin_PROGRAMS += srptool endif gnutls_serv_SOURCES = serv.gaa serv-gaa.h serv-gaa.c list.h serv.c \ - common.h common.c select.c certtool-common.h + common.h common.c certtool-common.h gnutls_serv_LDADD = ../lib/libgnutls.la ../libextra/libgnutls-extra.la \ ../gl/libgnu.la @@ -53,7 +51,7 @@ psktool_SOURCES = psk.gaa psk-gaa.h psk-gaa.c psk.c psktool_LDADD = ../lib/libgnutls.la ../gl/libgnu.la ../lib/gl/liblgnu.la gnutls_cli_SOURCES = cli.gaa cli-gaa.h cli-gaa.c cli.c common.h \ - common.c select.c + common.c gnutls_cli_LDADD = ../lib/libgnutls.la ../libextra/libgnutls-extra.la \ ../gl/libgnu.la @@ -48,12 +48,6 @@ #include "common.h" #include "cli-gaa.h" -#if defined _WIN32 || defined __WIN32__ -int _win_select (int max_fd, fd_set * rfds, fd_set * wfds, fd_set * efds, - const struct timeval *tv); -#define select _win_select -#endif - #define SA struct sockaddr #define ERR(err,s) do { if (err==-1) {perror(s);return(1);} } while (0) #define MAX_BUF 4096 diff --git a/src/serv.c b/src/serv.c index f4296279a5..21359852bc 100644 --- a/src/serv.c +++ b/src/serv.c @@ -35,17 +35,12 @@ #include <gnutls/extra.h> #include <gnutls/openpgp.h> #include <sys/time.h> +#include <sys/select.h> #include <fcntl.h> #include <list.h> #include <netdb.h> +#include <error.h> -#if defined _WIN32 || defined __WIN32__ -int _win_select (int max_fd, fd_set * rfds, fd_set * wfds, fd_set * efds, - const struct timeval *tv); -#define select _win_select -#endif - -#include "error.h" #include "progname.h" #include "version-etc.h" #include "read-file.h" |