From 6d9554dbf051fcb04eacc94d9a8c367b562af059 Mon Sep 17 00:00:00 2001 From: Zdenek Dohnal Date: Thu, 9 Feb 2023 15:56:03 +0100 Subject: [PATCH] lib: Remove bundled implementation of glibc funcs The functions implemented in the files are common at present, so IMHO we can start using the system ones. This prevents licensing issues (frontends has to mention the file's license in case it is bundled in) and security issues (in case of security issue the fix has to be applied to all bundled files as well). --- Makefile.in | 2 +- configure | 2 +- configure.in | 2 +- lib/Makefile.in | 95 ---- lib/alloca.c | 493 --------------------- lib/getenv.c | 18 - lib/getopt.c | 831 ----------------------------------- lib/getopt1.c | 180 -------- lib/isfdtype.c | 25 -- lib/snprintf.c | 1119 ----------------------------------------------- lib/strdup.c | 38 -- lib/strndup.c | 39 -- lib/strsep.c | 49 --- lib/usleep.c | 64 --- lib/vsyslog.c | 17 - src/Makefile.in | 13 +- 16 files changed, 9 insertions(+), 2978 deletions(-) delete mode 100644 lib/Makefile.in delete mode 100644 lib/alloca.c delete mode 100644 lib/getenv.c delete mode 100644 lib/getopt.c delete mode 100644 lib/getopt1.c delete mode 100644 lib/isfdtype.c delete mode 100644 lib/snprintf.c delete mode 100644 lib/strdup.c delete mode 100644 lib/strndup.c delete mode 100644 lib/strsep.c delete mode 100644 lib/usleep.c delete mode 100644 lib/vsyslog.c diff --git a/Makefile.in b/Makefile.in index d481f41..0e6ca62 100644 --- a/Makefile.in +++ b/Makefile.in @@ -33,7 +33,7 @@ INSTALL_DATA = @INSTALL_DATA@ @SET_MAKE@ -SUBDIRS = include lib sanei src doc +SUBDIRS = include sanei src doc DISTFILES = AUTHORS COPYING Changelog INSTALL Makefile.in NEWS PROBLEMS \ README acinclude.m4 aclocal.m4 config.guess config.sub configure \ diff --git a/configure b/configure index 71c3fa8..ff338a9 100755 --- a/configure +++ b/configure @@ -9774,7 +9774,7 @@ if test "${enable_foo2+set}" = set; then fi -ac_config_files="$ac_config_files Makefile lib/Makefile src/Makefile sanei/Makefile doc/Makefile include/Makefile" +ac_config_files="$ac_config_files Makefile src/Makefile sanei/Makefile doc/Makefile include/Makefile" cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure diff --git a/configure.in b/configure.in index 73c861e..98a9232 100644 --- a/configure.in +++ b/configure.in @@ -199,7 +199,7 @@ AC_ARG_ENABLE(foo2, [ To add include or library paths call: [env] CPPFLAGS=\"-I/path/to/foo/include\" LDFLAGS=\"-L/path/to/foo/libs\" ./configure]) -AC_OUTPUT([Makefile lib/Makefile src/Makefile sanei/Makefile doc/Makefile +AC_OUTPUT([Makefile src/Makefile sanei/Makefile doc/Makefile include/Makefile],) echo "****************************************************************" diff --git a/lib/Makefile.in b/lib/Makefile.in deleted file mode 100644 index f305185..0000000 --- a/lib/Makefile.in +++ /dev/null @@ -1,95 +0,0 @@ -SHELL = /bin/sh - -VPATH = @srcdir@ -srcdir = @srcdir@ -top_srcdir = @top_srcdir@ -top_builddir = .. - -PACKAGE = @PACKAGE@ -VERSION = @VERSION@ -distdir = $(top_srcdir)/$(PACKAGE)-$(VERSION) - -prefix = @prefix@ -exec_prefix = @exec_prefix@ -bindir = @bindir@ -sbindir = @sbindir@ -libexecdir = @libexecdir@ -datadir = @datadir@ -sysconfdir = @sysconfdir@ -sharedstatedir = @sharedstatedir@ -localstatedir = @localstatedir@ -libdir = @libdir@ -infodir = @infodir@ -mandir = @mandir@ -includedir = @includedir@ -oldincludedir = /usr/include -configdir = ${sysconfdir}/sane.d -sanedatadir = ${datadir}/sane - -INSTALL = @INSTALL@ -INSTALL_PROGRAM = @INSTALL_PROGRAM@ -INSTALL_DATA = @INSTALL_DATA@ - -RANLIB = @RANLIB@ - -CC = @CC@ -INCLUDES = -I. -I$(srcdir) \ - -I$(top_builddir)/include/sane -I$(top_srcdir)/include -CPPFLAGS = @CPPFLAGS@ -CFLAGS = @CFLAGS@ -LDFLAGS = @LDFLAGS@ -LIBS = @LIBS@ -DEFS = @DEFS@ - -COMPILE = $(CC) -c $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) - -@SET_MAKE@ - -LIBLIB_OBJS = alloca.o getopt.o getopt1.o strndup.o \ - strdup.o strsep.o snprintf.o usleep.o isfdtype.o \ - vsyslog.o getenv.o -LIBLIB_LTOBJS = alloca.lo getopt.lo getopt1.lo strndup.lo \ - strdup.lo strsep.lo snprintf.lo usleep.lo isfdtype.lo \ - vsyslog.lo getenv.lo - -TARGETS = $(LIBLIB_OBJS) -TARGETS = liblib.a - -DISTFILES = Makefile.in alloca.c getenv.c getopt.c getopt1.c isfdtype.c \ - snprintf.c strdup.c strndup.c strsep.c usleep.c vsyslog.c - -.PHONY: all check clean depend dist distclean install uninstall - -.SUFFIXES: -.SUFFIXES: .c .o -.c.o: - $(COMPILE) $< - @test -f $@ || $(COMPILE) $< - -all: $(TARGETS) - -liblib.a: $(LIBLIB_OBJS) - ar r $@ $(LIBLIB_OBJS) - $(RANLIB) $@ - -install: all - -uninstall: - -check: - -depend: - makedepend -I. -I../include *.c - -clean: - rm -f *.out *.o *.lo *~ *.a *.bak $(TESTPROGRAMS) - rm -rf .libs - -distclean: clean - rm -f Makefile - -dist: $(DISTFILES) - for file in $(DISTFILES); do \ - ln $$file $(distdir)/lib 2> /dev/null \ - || cp -p $$file $(distdir)/lib ; \ - done diff --git a/lib/alloca.c b/lib/alloca.c deleted file mode 100644 index b7424cd..0000000 --- a/lib/alloca.c +++ /dev/null @@ -1,493 +0,0 @@ -/* alloca.c -- allocate automatically reclaimed memory - (Mostly) portable public-domain implementation -- D A Gwyn - - This implementation of the PWB library alloca function, - which is used to allocate space off the run-time stack so - that it is automatically reclaimed upon procedure exit, - was inspired by discussions with J. Q. Johnson of Cornell. - J.Otto Tennant contributed the Cray support. - - There are some preprocessor constants that can - be defined when compiling for your specific system, for - improved efficiency; however, the defaults should be okay. - - The general concept of this implementation is to keep - track of all alloca-allocated blocks, and reclaim any - that are found to be deeper in the stack than the current - invocation. This heuristic does not reclaim storage as - soon as it becomes invalid, but it will do so eventually. - - As a special case, alloca(0) reclaims storage without - allocating any. It is a good idea to use alloca(0) in - your main control loop, etc. to force garbage collection. */ - -#ifdef HAVE_CONFIG_H -#include <../include/sane/config.h> -#endif - -#ifndef HAVE_ALLOCA - -#ifdef emacs -#include "blockinput.h" -#endif - -/* If compiling with GCC 2, this file's not needed. */ -#if !defined (__GNUC__) || __GNUC__ < 2 - -/* If someone has defined alloca as a macro, - there must be some other way alloca is supposed to work. */ -#ifndef alloca - -#ifdef emacs -#ifdef static -/* actually, only want this if static is defined as "" - -- this is for usg, in which emacs must undefine static - in order to make unexec workable - */ -#ifndef STACK_DIRECTION -you -lose --- must know STACK_DIRECTION at compile-time -#endif /* STACK_DIRECTION undefined */ -#endif /* static */ -#endif /* emacs */ - -/* If your stack is a linked list of frames, you have to - provide an "address metric" ADDRESS_FUNCTION macro. */ - -#if defined (CRAY) && defined (CRAY_STACKSEG_END) -long i00afunc (); -#define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg)) -#else -#define ADDRESS_FUNCTION(arg) &(arg) -#endif - -#if __STDC__ -typedef void *pointer; -#else -typedef char *pointer; -#endif - -#define NULL 0 - -/* Different portions of Emacs need to call different versions of - malloc. The Emacs executable needs alloca to call xmalloc, because - ordinary malloc isn't protected from input signals. On the other - hand, the utilities in lib-src need alloca to call malloc; some of - them are very simple, and don't have an xmalloc routine. - - Non-Emacs programs expect this to call xmalloc. - - Callers below should use malloc. */ - -extern pointer malloc (); - -/* Define STACK_DIRECTION if you know the direction of stack - growth for your system; otherwise it will be automatically - deduced at run-time. - - STACK_DIRECTION > 0 => grows toward higher addresses - STACK_DIRECTION < 0 => grows toward lower addresses - STACK_DIRECTION = 0 => direction of growth unknown */ - -#ifndef STACK_DIRECTION -#define STACK_DIRECTION 0 /* Direction unknown. */ -#endif - -#if STACK_DIRECTION != 0 - -#define STACK_DIR STACK_DIRECTION /* Known at compile-time. */ - -#else /* STACK_DIRECTION == 0; need run-time code. */ - -static int stack_dir; /* 1 or -1 once known. */ -#define STACK_DIR stack_dir - -static void -find_stack_direction () -{ - static char *addr = NULL; /* Address of first `dummy', once known. */ - auto char dummy; /* To get stack address. */ - - if (addr == NULL) - { /* Initial entry. */ - addr = ADDRESS_FUNCTION (dummy); - - find_stack_direction (); /* Recurse once. */ - } - else - { - /* Second entry. */ - if (ADDRESS_FUNCTION (dummy) > addr) - stack_dir = 1; /* Stack grew upward. */ - else - stack_dir = -1; /* Stack grew downward. */ - } -} - -#endif /* STACK_DIRECTION == 0 */ - -/* An "alloca header" is used to: - (a) chain together all alloca'ed blocks; - (b) keep track of stack depth. - - It is very important that sizeof(header) agree with malloc - alignment chunk size. The following default should work okay. */ - -#ifndef ALIGN_SIZE -#define ALIGN_SIZE sizeof(double) -#endif - -typedef union hdr -{ - char align[ALIGN_SIZE]; /* To force sizeof(header). */ - struct - { - union hdr *next; /* For chaining headers. */ - char *deep; /* For stack depth measure. */ - } h; -} header; - -static header *last_alloca_header = NULL; /* -> last alloca header. */ - -/* Return a pointer to at least SIZE bytes of storage, - which will be automatically reclaimed upon exit from - the procedure that called alloca. Originally, this space - was supposed to be taken from the current stack frame of the - caller, but that method cannot be made to work for some - implementations of C, for example under Gould's UTX/32. */ - -pointer -alloca (size) - unsigned size; -{ - auto char probe; /* Probes stack depth: */ - register char *depth = ADDRESS_FUNCTION (probe); - -#if STACK_DIRECTION == 0 - if (STACK_DIR == 0) /* Unknown growth direction. */ - find_stack_direction (); -#endif - - /* Reclaim garbage, defined as all alloca'd storage that - was allocated from deeper in the stack than currently. */ - - { - register header *hp; /* Traverses linked list. */ - -#ifdef emacs - BLOCK_INPUT; -#endif - - for (hp = last_alloca_header; hp != NULL;) - if ((STACK_DIR > 0 && hp->h.deep > depth) - || (STACK_DIR < 0 && hp->h.deep < depth)) - { - register header *np = hp->h.next; - - free ((pointer) hp); /* Collect garbage. */ - - hp = np; /* -> next header. */ - } - else - break; /* Rest are not deeper. */ - - last_alloca_header = hp; /* -> last valid storage. */ - -#ifdef emacs - UNBLOCK_INPUT; -#endif - } - - if (size == 0) - return NULL; /* No allocation required. */ - - /* Allocate combined header + user data storage. */ - - { - register pointer new = malloc (sizeof (header) + size); - /* Address of header. */ - - ((header *) new)->h.next = last_alloca_header; - ((header *) new)->h.deep = depth; - - last_alloca_header = (header *) new; - - /* User storage begins just after header. */ - - return (pointer) ((char *) new + sizeof (header)); - } -} - -#if defined (CRAY) && defined (CRAY_STACKSEG_END) - -#ifdef DEBUG_I00AFUNC -#include -#endif - -#ifndef CRAY_STACK -#define CRAY_STACK -#ifndef CRAY2 -/* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */ -struct stack_control_header - { - long shgrow:32; /* Number of times stack has grown. */ - long shaseg:32; /* Size of increments to stack. */ - long shhwm:32; /* High water mark of stack. */ - long shsize:32; /* Current size of stack (all segments). */ - }; - -/* The stack segment linkage control information occurs at - the high-address end of a stack segment. (The stack - grows from low addresses to high addresses.) The initial - part of the stack segment linkage control information is - 0200 (octal) words. This provides for register storage - for the routine which overflows the stack. */ - -struct stack_segment_linkage - { - long ss[0200]; /* 0200 overflow words. */ - long sssize:32; /* Number of words in this segment. */ - long ssbase:32; /* Offset to stack base. */ - long:32; - long sspseg:32; /* Offset to linkage control of previous - segment of stack. */ - long:32; - long sstcpt:32; /* Pointer to task common address block. */ - long sscsnm; /* Private control structure number for - microtasking. */ - long ssusr1; /* Reserved for user. */ - long ssusr2; /* Reserved for user. */ - long sstpid; /* Process ID for pid based multi-tasking. */ - long ssgvup; /* Pointer to multitasking thread giveup. */ - long sscray[7]; /* Reserved for Cray Research. */ - long ssa0; - long ssa1; - long ssa2; - long ssa3; - long ssa4; - long ssa5; - long ssa6; - long ssa7; - long sss0; - long sss1; - long sss2; - long sss3; - long sss4; - long sss5; - long sss6; - long sss7; - }; - -#else /* CRAY2 */ -/* The following structure defines the vector of words - returned by the STKSTAT library routine. */ -struct stk_stat - { - long now; /* Current total stack size. */ - long maxc; /* Amount of contiguous space which would - be required to satisfy the maximum - stack demand to date. */ - long high_water; /* Stack high-water mark. */ - long overflows; /* Number of stack overflow ($STKOFEN) calls. */ - long hits; /* Number of internal buffer hits. */ - long extends; /* Number of block extensions. */ - long stko_mallocs; /* Block allocations by $STKOFEN. */ - long underflows; /* Number of stack underflow calls ($STKRETN). */ - long stko_free; /* Number of deallocations by $STKRETN. */ - long stkm_free; /* Number of deallocations by $STKMRET. */ - long segments; /* Current number of stack segments. */ - long maxs; /* Maximum number of stack segments so far. */ - long pad_size; /* Stack pad size. */ - long current_address; /* Current stack segment address. */ - long current_size; /* Current stack segment size. This - number is actually corrupted by STKSTAT to - include the fifteen word trailer area. */ - long initial_address; /* Address of initial segment. */ - long initial_size; /* Size of initial segment. */ - }; - -/* The following structure describes the data structure which trails - any stack segment. I think that the description in 'asdef' is - out of date. I only describe the parts that I am sure about. */ - -struct stk_trailer - { - long this_address; /* Address of this block. */ - long this_size; /* Size of this block (does not include - this trailer). */ - long unknown2; - long unknown3; - long link; /* Address of trailer block of previous - segment. */ - long unknown5; - long unknown6; - long unknown7; - long unknown8; - long unknown9; - long unknown10; - long unknown11; - long unknown12; - long unknown13; - long unknown14; - }; - -#endif /* CRAY2 */ -#endif /* not CRAY_STACK */ - -#ifdef CRAY2 -/* Determine a "stack measure" for an arbitrary ADDRESS. - I doubt that "lint" will like this much. */ - -static long -i00afunc (long *address) -{ - struct stk_stat status; - struct stk_trailer *trailer; - long *block, size; - long result = 0; - - /* We want to iterate through all of the segments. The first - step is to get the stack status structure. We could do this - more quickly and more directly, perhaps, by referencing the - $LM00 common block, but I know that this works. */ - - STKSTAT (&status); - - /* Set up the iteration. */ - - trailer = (struct stk_trailer *) (status.current_address - + status.current_size - - 15); - - /* There must be at least one stack segment. Therefore it is - a fatal error if "trailer" is null. */ - - if (trailer == 0) - abort (); - - /* Discard segments that do not contain our argument address. */ - - while (trailer != 0) - { - block = (long *) trailer->this_address; - size = trailer->this_size; - if (block == 0 || size == 0) - abort (); - trailer = (struct stk_trailer *) trailer->link; - if ((block <= address) && (address < (block + size))) - break; - } - - /* Set the result to the offset in this segment and add the sizes - of all predecessor segments. */ - - result = address - block; - - if (trailer == 0) - { - return result; - } - - do - { - if (trailer->this_size <= 0) - abort (); - result += trailer->this_size; - trailer = (struct stk_trailer *) trailer->link; - } - while (trailer != 0); - - /* We are done. Note that if you present a bogus address (one - not in any segment), you will get a different number back, formed - from subtracting the address of the first block. This is probably - not what you want. */ - - return (result); -} - -#else /* not CRAY2 */ -/* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP. - Determine the number of the cell within the stack, - given the address of the cell. The purpose of this - routine is to linearize, in some sense, stack addresses - for alloca. */ - -static long -i00afunc (long address) -{ - long stkl = 0; - - long size, pseg, this_segment, stack; - long result = 0; - - struct stack_segment_linkage *ssptr; - - /* Register B67 contains the address of the end of the - current stack segment. If you (as a subprogram) store - your registers on the stack and find that you are past - the contents of B67, you have overflowed the segment. - - B67 also points to the stack segment linkage control - area, which is what we are really interested in. */ - - stkl = CRAY_STACKSEG_END (); - ssptr = (struct stack_segment_linkage *) stkl; - - /* If one subtracts 'size' from the end of the segment, - one has the address of the first word of the segment. - - If this is not the first segment, 'pseg' will be - nonzero. */ - - pseg = ssptr->sspseg; - size = ssptr->sssize; - - this_segment = stkl - size; - - /* It is possible that calling this routine itself caused - a stack overflow. Discard stack segments which do not - contain the target address. */ - - while (!(this_segment <= address && address <= stkl)) - { -#ifdef DEBUG_I00AFUNC - fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl); -#endif - if (pseg == 0) - break; - stkl = stkl - pseg; - ssptr = (struct stack_segment_linkage *) stkl; - size = ssptr->sssize; - pseg = ssptr->sspseg; - this_segment = stkl - size; - } - - result = address - this_segment; - - /* If you subtract pseg from the current end of the stack, - you get the address of the previous stack segment's end. - This seems a little convoluted to me, but I'll bet you save - a cycle somewhere. */ - - while (pseg != 0) - { -#ifdef DEBUG_I00AFUNC - fprintf (stderr, "%011o %011o\n", pseg, size); -#endif - stkl = stkl - pseg; - ssptr = (struct stack_segment_linkage *) stkl; - size = ssptr->sssize; - pseg = ssptr->sspseg; - result += size; - } - return (result); -} - -#endif /* not CRAY2 */ -#endif /* CRAY */ - -#endif /* no alloca */ -#endif /* not GCC version 2 */ - -#endif /* !HAVE_ALLOCA */ diff --git a/lib/getenv.c b/lib/getenv.c deleted file mode 100644 index c450659..0000000 --- a/lib/getenv.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "../include/sane/config.h" - -#ifndef HAVE_GETENV - -char * -getenv(const char *name) -{ - char *retval = 0; -#ifdef HAVE_OS2_H - if (0 != DosScanEnv (buf, &retval)) - retval = 0; -#else -# error "Missing getenv() on this platform. Please implement." -#endif - return retval; -} - -#endif /* !HAVE_GETENV */ diff --git a/lib/getopt.c b/lib/getopt.c deleted file mode 100644 index 8b42321..0000000 --- a/lib/getopt.c +++ /dev/null @@ -1,831 +0,0 @@ -/* Getopt for GNU. - NOTE: getopt is now part of the C library, so if you don't know what - "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu - before changing it! - - Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 1996 - 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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -/* This tells Alpha OSF/1 not to define a getopt prototype in . - Ditto for AIX 3.2 and . */ -#ifndef _NO_PROTO -#define _NO_PROTO -#endif - -#ifdef HAVE_CONFIG_H -#include <../include/sane/config.h> -#endif - -#if !defined (__STDC__) || !__STDC__ -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -#ifndef const -#define const -#endif -#endif - -#include - -/* Comment out all this code if we are using the GNU C Library, and are not - actually compiling the library itself. This code is part of the GNU C - Library, but also included in many other GNU distributions. Compiling - and linking in this code is a waste when using the GNU C library - (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object files, - it is simpler to just do this in the source for each such file. */ - -#if defined (_LIBC) || !defined (__GNU_LIBRARY__) - - -/* This needs to come after some library #include - to get __GNU_LIBRARY__ defined. */ -#ifdef __GNU_LIBRARY__ -/* Don't include stdlib.h for non-GNU C libraries because some of them - contain conflicting prototypes for getopt. */ -#include -#if defined (_LIBC) || defined (HAVE_UNISTD_H) -#include -#endif -#endif /* GNU C library. */ - -#ifdef VMS -#include -#if HAVE_STRING_H - 0 -#include -#endif -#endif - -#ifdef WIN32 -/* It's not Unix, really. See? Capital letters. */ -#include -#define getpid() GetCurrentProcessId() -#endif - -#ifndef _ -/* This is for other GNU distributions with internationalized messages. - When compiling libc, the _ macro is predefined. */ -#ifdef HAVE_LIBINTL_H -# include -# define _(msgid) gettext (msgid) -#else -# define _(msgid) (msgid) -#endif -#endif - -/* This version of `getopt' appears to the caller like standard Unix `getopt' - but it behaves differently for the user, since it allows the user - to intersperse the options with the other arguments. - - As `getopt' works, it permutes the elements of ARGV so that, - when it is done, all the options precede everything else. Thus - all application programs are extended to handle flexible argument order. - - Setting the environment variable POSIXLY_CORRECT disables permutation. - Then the behavior is completely standard. - - GNU application programs can use a third alternative mode in which - they can distinguish the relative order of options and other arguments. */ - -#include "getopt.h" - -/* For communication from `getopt' to the caller. - When `getopt' finds an option that takes an argument, - the argument value is returned here. - Also, when `ordering' is RETURN_IN_ORDER, - each non-option ARGV-element is returned here. */ - -char *optarg = NULL; - -/* Index in ARGV of the next element to be scanned. - This is used for communication to and from the caller - and for communication between successive calls to `getopt'. - - On entry to `getopt', zero means this is the first call; initialize. - - When `getopt' returns EOF, this is the index of the first of the - non-option elements that the caller should itself scan. - - Otherwise, `optind' communicates from one call to the next - how much of ARGV has been scanned so far. */ - -/* XXX 1003.2 says this must be 1 before any call. */ -int optind = 0; - -/* The next char to be scanned in the option-element - in which the last option character we returned was found. - This allows us to pick up the scan where we left off. - - If this is zero, or a null string, it means resume the scan - by advancing to the next ARGV-element. */ - -static char *nextchar; - -/* Callers store zero here to inhibit the error message - for unrecognized options. */ - -int opterr = 1; - -/* Set to an option character which was unrecognized. - This must be initialized on some systems to avoid linking in the - system's own getopt implementation. */ - -int optopt = '?'; - -/* Describe how to deal with options that follow non-option ARGV-elements. - - If the caller did not specify anything, - the default is REQUIRE_ORDER if the environment variable - POSIXLY_CORRECT is defined, PERMUTE otherwise. - - REQUIRE_ORDER means don't recognize them as options; - stop option processing when the first non-option is seen. - This is what Unix does. - This mode of operation is selected by either setting the environment - variable POSIXLY_CORRECT, or using `+' as the first character - of the list of option characters. - - PERMUTE is the default. We permute the contents of ARGV as we scan, - so that eventually all the non-options are at the end. This allows options - to be given in any order, even with programs that were not written to - expect this. - - RETURN_IN_ORDER is an option available to programs that were written - to expect options and other ARGV-elements in any order and that care about - the ordering of the two. We describe each non-option ARGV-element - as if it were the argument of an option with character code 1. - Using `-' as the first character of the list of option characters - selects this mode of operation. - - The special argument `--' forces an end of option-scanning regardless - of the value of `ordering'. In the case of RETURN_IN_ORDER, only - `--' can cause `getopt' to return EOF with `optind' != ARGC. */ - -static enum -{ - REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER -} ordering; - -/* Value of POSIXLY_CORRECT environment variable. */ -static char *posixly_correct; - -#ifdef __GNU_LIBRARY__ -/* We want to avoid inclusion of string.h with non-GNU libraries - because there are many ways it can cause trouble. - On some systems, it contains special magic macros that don't work - in GCC. */ -#include -#define my_index strchr -#else - -/* Avoid depending on library functions or files - whose names are inconsistent. */ - -char *getenv (); - -static char * -my_index (str, chr) - const char *str; - int chr; -{ - while (*str) - { - if (*str == chr) - return (char *) str; - str++; - } - return 0; -} - -/* If using GCC, we can safely declare strlen this way. - If not using GCC, it is ok not to declare it. */ -#ifdef __GNUC__ -/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. - That was relevant to code that was here before. */ -#if !defined (__STDC__) || !__STDC__ -/* gcc with -traditional declares the built-in strlen to return int, - and has done so at least since version 2.4.5. -- rms. */ -extern int strlen (const char *); -#endif /* not __STDC__ */ -#endif /* __GNUC__ */ - -#endif /* not __GNU_LIBRARY__ */ - -/* Handle permutation of arguments. */ - -/* Describe the part of ARGV that contains non-options that have - been skipped. `first_nonopt' is the index in ARGV of the first of them; - `last_nonopt' is the index after the last of them. */ - -static int first_nonopt; -static int last_nonopt; - -/* Bash 2.0 gives us an environment variable containing flags - indicating ARGV elements that should not be considered arguments. */ - -static const char *nonoption_flags; -static int nonoption_flags_len; - -/* Exchange two adjacent subsequences of ARGV. - One subsequence is elements [first_nonopt,last_nonopt) - which contains all the non-options that have been skipped so far. - The other is elements [last_nonopt,optind), which contains all - the options processed since those non-options were skipped. - - `first_nonopt' and `last_nonopt' are relocated so that they describe - the new indices of the non-options in ARGV after they are moved. */ - -#if defined (__STDC__) && __STDC__ -static void exchange (char **); -#endif - -static void -exchange (argv) - char **argv; -{ - int bottom = first_nonopt; - int middle = last_nonopt; - int top = optind; - char *tem; - - /* Exchange the shorter segment with the far end of the longer segment. - That puts the shorter segment into the right place. - It leaves the longer segment in the right place overall, - but it consists of two parts that need to be swapped next. */ - - while (top > middle && middle > bottom) - { - if (top - middle > middle - bottom) - { - /* Bottom segment is the short one. */ - int len = middle - bottom; - register int i; - - /* Swap it with the top part of the top segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[top - (middle - bottom) + i]; - argv[top - (middle - bottom) + i] = tem; - } - /* Exclude the moved bottom segment from further swapping. */ - top -= len; - } - else - { - /* Top segment is the short one. */ - int len = top - middle; - register int i; - - /* Swap it with the bottom part of the bottom segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[middle + i]; - argv[middle + i] = tem; - } - /* Exclude the moved top segment from further swapping. */ - bottom += len; - } - } - - /* Update records for the slots the non-options now occupy. */ - - first_nonopt += (optind - last_nonopt); - last_nonopt = optind; -} - -/* Initialize the internal data when the first call is made. */ - -#if defined (__STDC__) && __STDC__ -static const char *_getopt_initialize (const char *); -#endif -static const char * -_getopt_initialize (optstring) - const char *optstring; -{ - /* Start processing options with ARGV-element 1 (since ARGV-element 0 - is the program name); the sequence of previously skipped - non-option ARGV-elements is empty. */ - - first_nonopt = last_nonopt = optind = 1; - - nextchar = NULL; - - posixly_correct = getenv ("POSIXLY_CORRECT"); - - /* Determine how to handle the ordering of options and nonoptions. */ - - if (optstring[0] == '-') - { - ordering = RETURN_IN_ORDER; - ++optstring; - } - else if (optstring[0] == '+') - { - ordering = REQUIRE_ORDER; - ++optstring; - } - else if (posixly_correct != NULL) - ordering = REQUIRE_ORDER; - else - ordering = PERMUTE; - - if (posixly_correct == NULL) - { - /* Bash 2.0 puts a special variable in the environment for each - command it runs, specifying which ARGV elements are the results of - file name wildcard expansion and therefore should not be - considered as options. */ - char var[100]; - sprintf (var, "_%d_GNU_nonoption_argv_flags_", getpid ()); - nonoption_flags = getenv (var); - if (nonoption_flags == NULL) - nonoption_flags_len = 0; - else - nonoption_flags_len = strlen (nonoption_flags); - } - - return optstring; -} - -/* Scan elements of ARGV (whose length is ARGC) for option characters - given in OPTSTRING. - - If an element of ARGV starts with '-', and is not exactly "-" or "--", - then it is an option element. The characters of this element - (aside from the initial '-') are option characters. If `getopt' - is called repeatedly, it returns successively each of the option characters - from each of the option elements. - - If `getopt' finds another option character, it returns that character, - updating `optind' and `nextchar' so that the next call to `getopt' can - resume the scan with the following option character or ARGV-element. - - If there are no more option characters, `getopt' returns `EOF'. - Then `optind' is the index in ARGV of the first ARGV-element - that is not an option. (The ARGV-elements have been permuted - so that those that are not options now come last.) - - OPTSTRING is a string containing the legitimate option characters. - If an option character is seen that is not listed in OPTSTRING, - return '?' after printing an error message. If you set `opterr' to - zero, the error message is suppressed but we still return '?'. - - If a char in OPTSTRING is followed by a colon, that means it wants an arg, - so the following text in the same ARGV-element, or the text of the following - ARGV-element, is returned in `optarg'. Two colons mean an option that - wants an optional arg; if there is text in the current ARGV-element, - it is returned in `optarg', otherwise `optarg' is set to zero. - - If OPTSTRING starts with `-' or `+', it requests different methods of - handling the non-option ARGV-elements. - See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. - - Long-named options begin with `--' instead of `-'. - Their names may be abbreviated as long as the abbreviation is unique - or is an exact match for some defined option. If they have an - argument, it follows the option name in the same ARGV-element, separated - from the option name by a `=', or else the in next ARGV-element. - When `getopt' finds a long-named option, it returns 0 if that option's - `flag' field is nonzero, the value of the option's `val' field - if the `flag' field is zero. - - The elements of ARGV aren't really const, because we permute them. - But we pretend they're const in the prototype to be compatible - with other systems. - - LONGOPTS is a vector of `struct option' terminated by an - element containing a name which is zero. - - LONGIND returns the index in LONGOPT of the long-named option found. - It is only valid when a long-named option has been found by the most - recent call. - - If LONG_ONLY is nonzero, '-' as well as '--' can introduce - long-named options. */ - -int -_getopt_internal (argc, argv, optstring, longopts, longind, long_only) - int argc; - char *const *argv; - const char *optstring; - const struct option *longopts; - int *longind; - int long_only; -{ - optarg = NULL; - - if (optind == 0) - { - optstring = _getopt_initialize (optstring); - optind = 1; /* Don't scan ARGV[0], the program name. */ - } - - /* Test whether ARGV[optind] points to a non-option argument. - Either it does not have option syntax, or there is an environment flag - from the shell indicating it is not an option. */ -#define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ - || (optind < nonoption_flags_len \ - && nonoption_flags[optind] == '1')) - - if (nextchar == NULL || *nextchar == '\0') - { - /* Advance to the next ARGV-element. */ - - /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been - moved back by the user (who may also have changed the arguments). */ - if (last_nonopt > optind) - last_nonopt = optind; - if (first_nonopt > optind) - first_nonopt = optind; - - if (ordering == PERMUTE) - { - /* If we have just processed some options following some non-options, - exchange them so that the options come first. */ - - if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange ((char **) argv); - else if (last_nonopt != optind) - first_nonopt = optind; - - /* Skip any additional non-options - and extend the range of non-options previously skipped. */ - - while (optind < argc && NONOPTION_P) - optind++; - last_nonopt = optind; - } - - /* The special ARGV-element `--' means premature end of options. - Skip it like a null option, - then exchange with previous non-options as if it were an option, - then skip everything else like a non-option. */ - - if (optind != argc && !strcmp (argv[optind], "--")) - { - optind++; - - if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange ((char **) argv); - else if (first_nonopt == last_nonopt) - first_nonopt = optind; - last_nonopt = argc; - - optind = argc; - } - - /* If we have done all the ARGV-elements, stop the scan - and back over any non-options that we skipped and permuted. */ - - if (optind == argc) - { - /* Set the next-arg-index to point at the non-options - that we previously skipped, so the caller will digest them. */ - if (first_nonopt != last_nonopt) - optind = first_nonopt; - return EOF; - } - - /* If we have come to a non-option and did not permute it, - either stop the scan or describe it to the caller and pass it by. */ - - if (NONOPTION_P) - { - if (ordering == REQUIRE_ORDER) - return EOF; - optarg = argv[optind++]; - return 1; - } - - /* We have found another option-ARGV-element. - Skip the initial punctuation. */ - - nextchar = (argv[optind] + 1 - + (longopts != NULL && argv[optind][1] == '-')); - } - - /* Decode the current option-ARGV-element. */ - - /* Check whether the ARGV-element is a long option. - - If long_only and the ARGV-element has the form "-f", where f is - a valid short option, don't consider it an abbreviated form of - a long option that starts with f. Otherwise there would be no - way to give the -f short option. - - On the other hand, if there's a long option "fubar" and - the ARGV-element is "-fu", do consider that an abbreviation of - the long option, just like "--fu", and not "-f" with arg "u". - - This distinction seems to be the most useful approach. */ - - if (longopts != NULL - && (argv[optind][1] == '-' - || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) - { - char *nameend; - const struct option *p; - const struct option *pfound = NULL; - int exact = 0; - int ambig = 0; - int indfound; - int option_index; - - for (nameend = nextchar; *nameend && *nameend != '='; nameend++) - /* Do nothing. */ ; - -#ifdef lint /* Suppress `used before initialized' warning. */ - indfound = 0; -#endif - - /* Test all long options for either exact match - or abbreviated matches. */ - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, nextchar, nameend - nextchar)) - { - if (nameend - nextchar == strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else - /* Second or later nonexact match found. */ - ambig = 1; - } - - if (ambig && !exact) - { - if (opterr) - fprintf (stderr, _("%s: option `%s' is ambiguous\n"), - argv[0], argv[optind]); - nextchar += strlen (nextchar); - optind++; - optopt = 0; - return '?'; - } - - if (pfound != NULL) - { - option_index = indfound; - optind++; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - allow it to be used on enums. */ - if (pfound->has_arg) - optarg = nameend + 1; - else - { - if (opterr) - if (argv[optind - 1][1] == '-') - /* --option */ - fprintf (stderr, - _("%s: option `--%s' doesn't allow an argument\n"), - argv[0], pfound->name); - else - /* +option or -option */ - fprintf (stderr, - _("%s: option `%c%s' doesn't allow an argument\n"), - argv[0], argv[optind - 1][0], pfound->name); - - nextchar += strlen (nextchar); - - optopt = pfound->val; - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (optind < argc) - optarg = argv[optind++]; - else - { - if (opterr) - fprintf (stderr, - _("%s: option `%s' requires an argument\n"), - argv[0], argv[optind - 1]); - nextchar += strlen (nextchar); - optopt = pfound->val; - return optstring[0] == ':' ? ':' : '?'; - } - } - nextchar += strlen (nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; - } - - /* Can't find it as a long option. If this is not getopt_long_only, - or the option starts with '--' or is not a valid short - option, then it's an error. - Otherwise interpret it as a short option. */ - if (!long_only || argv[optind][1] == '-' - || my_index (optstring, *nextchar) == NULL) - { - if (opterr) - { - if (argv[optind][1] == '-') - /* --option */ - fprintf (stderr, _("%s: unrecognized option `--%s'\n"), - argv[0], nextchar); - else - /* +option or -option */ - fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), - argv[0], argv[optind][0], nextchar); - } - nextchar = (char *) ""; - optind++; - optopt = 0; - return '?'; - } - } - - /* Look at and handle the next short option-character. */ - - { - char c = *nextchar++; - char *temp = my_index (optstring, c); - - /* Increment `optind' when we start to process its last character. */ - if (*nextchar == '\0') - ++optind; - - if (temp == NULL || c == ':') - { - if (opterr) - { - if (posixly_correct) - /* 1003.2 specifies the format of this message. */ - fprintf (stderr, _("%s: illegal option -- %c\n"), - argv[0], c); - else - fprintf (stderr, _("%s: invalid option -- %c\n"), - argv[0], c); - } - optopt = c; - return '?'; - } - if (temp[1] == ':') - { - if (temp[2] == ':') - { - /* This is an option that accepts an argument optionally. */ - if (*nextchar != '\0') - { - optarg = nextchar; - optind++; - } - else - optarg = NULL; - nextchar = NULL; - } - else - { - /* This is an option that requires an argument. */ - if (*nextchar != '\0') - { - optarg = nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - we must advance to the next element now. */ - optind++; - } - else if (optind == argc) - { - if (opterr) - { - /* 1003.2 specifies the format of this message. */ - fprintf (stderr, - _("%s: option requires an argument -- %c\n"), - argv[0], c); - } - optopt = c; - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - } - else - /* We already incremented `optind' once; - increment it again when taking next ARGV-elt as argument. */ - optarg = argv[optind++]; - nextchar = NULL; - } - } - return c; - } -} - -int -getopt (argc, argv, optstring) - int argc; - char *const *argv; - const char *optstring; -{ - return _getopt_internal (argc, argv, optstring, - (const struct option *) 0, - (int *) 0, - 0); -} - -#endif /* _LIBC or not __GNU_LIBRARY__. */ - -#ifdef TEST - -/* Compile with -DTEST to make an executable for use in testing - the above definition of `getopt'. */ - -int -main (argc, argv) - int argc; - char **argv; -{ - int c; - int digit_optind = 0; - - while (1) - { - int this_option_optind = optind ? optind : 1; - - c = getopt (argc, argv, "abc:d:0123456789"); - if (c == EOF) - break; - - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf ("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf ("option %c\n", c); - break; - - case 'a': - printf ("option a\n"); - break; - - case 'b': - printf ("option b\n"); - break; - - case 'c': - printf ("option c with value `%s'\n", optarg); - break; - - case '?': - break; - - default: - printf ("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) - { - printf ("non-option ARGV-elements: "); - while (optind < argc) - printf ("%s ", argv[optind++]); - printf ("\n"); - } - - exit (0); -} - -#endif /* TEST */ diff --git a/lib/getopt1.c b/lib/getopt1.c deleted file mode 100644 index 140ea10..0000000 --- a/lib/getopt1.c +++ /dev/null @@ -1,180 +0,0 @@ -/* getopt_long and getopt_long_only entry points for GNU getopt. - Copyright (C) 1987, 88, 89, 90, 91, 92, 1993, 1994 - 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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#ifdef HAVE_CONFIG_H -#include <../include/sane/config.h> -#endif - -#include "getopt.h" - -#if !defined (__STDC__) || !__STDC__ -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -#ifndef const -#define const -#endif -#endif - -#include - -/* Comment out all this code if we are using the GNU C Library, and are not - actually compiling the library itself. This code is part of the GNU C - Library, but also included in many other GNU distributions. Compiling - and linking in this code is a waste when using the GNU C library - (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object files, - it is simpler to just do this in the source for each such file. */ - -#if defined (_LIBC) || !defined (__GNU_LIBRARY__) - - -/* This needs to come after some library #include - to get __GNU_LIBRARY__ defined. */ -#ifdef __GNU_LIBRARY__ -#include -#else -char *getenv (); -#endif - -#ifndef NULL -#define NULL 0 -#endif - -int -getopt_long (argc, argv, options, long_options, opt_index) - int argc; - char *const *argv; - const char *options; - const struct option *long_options; - int *opt_index; -{ - return _getopt_internal (argc, argv, options, long_options, opt_index, 0); -} - -/* Like getopt_long, but '-' as well as '--' can indicate a long option. - If an option that starts with '-' (not '--') doesn't match a long option, - but does match a short option, it is parsed as a short option - instead. */ - -int -getopt_long_only (argc, argv, options, long_options, opt_index) - int argc; - char *const *argv; - const char *options; - const struct option *long_options; - int *opt_index; -{ - return _getopt_internal (argc, argv, options, long_options, opt_index, 1); -} - - -#endif /* _LIBC or not __GNU_LIBRARY__. */ - -#ifdef TEST - -#include - -int -main (argc, argv) - int argc; - char **argv; -{ - int c; - int digit_optind = 0; - - while (1) - { - int this_option_optind = optind ? optind : 1; - int option_index = 0; - static struct option long_options[] = - { - {"add", 1, 0, 0}, - {"append", 0, 0, 0}, - {"delete", 1, 0, 0}, - {"verbose", 0, 0, 0}, - {"create", 0, 0, 0}, - {"file", 1, 0, 0}, - {0, 0, 0, 0} - }; - - c = getopt_long (argc, argv, "abc:d:0123456789", - long_options, &option_index); - if (c == EOF) - break; - - switch (c) - { - case 0: - printf ("option %s", long_options[option_index].name); - if (optarg) - printf (" with arg %s", optarg); - printf ("\n"); - break; - - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf ("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf ("option %c\n", c); - break; - - case 'a': - printf ("option a\n"); - break; - - case 'b': - printf ("option b\n"); - break; - - case 'c': - printf ("option c with value `%s'\n", optarg); - break; - - case 'd': - printf ("option d with value `%s'\n", optarg); - break; - - case '?': - break; - - default: - printf ("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) - { - printf ("non-option ARGV-elements: "); - while (optind < argc) - printf ("%s ", argv[optind++]); - printf ("\n"); - } - - exit (0); -} - -#endif /* TEST */ diff --git a/lib/isfdtype.c b/lib/isfdtype.c deleted file mode 100644 index 8e51516..0000000 --- a/lib/isfdtype.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "../include/sane/config.h" - -#ifndef HAVE_ISFDTYPE - -#include - -int -isfdtype(int fd, int fdtype) -{ - struct stat st; - - if (fstat(fd, &st) == -1) return 0; /* couldn't stat fd */ - - if (st.st_mode == 0) - return 1; /* At least Irix doesn't seem to know socket type */ -#if defined(S_ISSOCK) - return S_ISSOCK(st.st_mode) != 0; -#elif defined(S_IFSOCK) && defined(S_IFMT) - return (st.st_mode & S_IFMT) == S_IFSOCK; -#else - return 0; -#endif -} - -#endif /* !HAVE_ISFDTYPE */ diff --git a/lib/snprintf.c b/lib/snprintf.c deleted file mode 100644 index 9fb6a08..0000000 --- a/lib/snprintf.c +++ /dev/null @@ -1,1119 +0,0 @@ -#include <../include/sane/config.h> - -#ifndef HAVE_SNPRINTF - -/************************************************************************** - * Copyright 1994-2003 Patrick Powell, San Diego, CA - **************************************************************************/ - -/* - Overview: - - snprintf( char *buffer, int len, const char *format,...) - plp_unsafe_snprintf( char *buffer, int len, const char *format,...) - its horribly unsafe companion that does NOT protect you from - the printing of evil control characters, but may be necessary - See the man page documentation below - - This version of snprintf was developed originally for printing - on a motley collection of specialized hardware that had NO IO - library. Due to contractual restrictions, a clean room implementation - of the printf() code had to be developed. - - The method chosen for printf was to be as paranoid as possible, - as these platforms had NO memory protection, and very small - address spaces. This made it possible to try to print - very long strings, i.e. - all of memory, very easily. To guard - against this, all printing was done via a buffer, generous enough - to hold strings, but small enough to protect against overruns, - etc. - - Strangely enough, this proved to be of immense importance when - SPRINTFing to a buffer on a stack... The rest, of course, is - well known, as buffer overruns in the stack are a common way to - do horrible things to operating systems, security, etc etc. - - This version of snprintf is VERY limited by modern standards. - - Revision History: - First Released Version - 1994. This version had NO comments. - First Released Version - 1994. This version had NO comments. - Second Major Released Version - Tue May 23 10:43:44 PDT 2000 - Configuration and other items changed. Read this doc. - Treat this as a new version. - Minor Revision - Mon Apr 1 09:41:28 PST 2002 - - fixed up some constants and casts - - COPYRIGHT AND TERMS OF USE: - - You may use, copy, distribute, or otherwise incorporate this software - and documentation into any product or other item, provided that - the copyright in the documentation and source code as well as the - source code generated constant strings in the object, executable - or other code remain in place and are present in executable modules - or objects. - - You may modify this code as appropriate to your usage; however the - modified version must be identified by changing the various source - and object code identification strings as is appropriately noted - in the source code. - - You can use this with the GNU CONFIGURE utility. - This should define the following macros appropriately: - - HAVE_STDARG_H - if the include file is available - HAVE_VARARG_H - if the include file is available - - HAVE_STRERROR - if the strerror() routine is available. - If it is not available, then examine the lines containing - the tests below. - - HAVE_SYS_ERRLIST - have sys_errlist available - HAVE_DECL_SYS_ERRLIST - sys_errlist declaration in include files - HAVE_SYS_NERR - have sys_nerr available - HAVE_DECL_SYS_NERR - sys_nerr declaration in include files - - HAVE_QUAD_T - if the quad_t type is defined - HAVE_LONG_LONG - if the long long type is defined - HAVE_LONG_DOUBLE - if the long double type is defined - - If you are using the GNU configure (autoconf) facility, add the - following line to the configure.in file, to force checking for the - quad_t and long long data types: - - - AC_CHECK_HEADERS(stdlib.h,stdio.h,unistd.h,errno.h) - AC_CHECK_FUNCS(strerror) - AC_CACHE_CHECK(for errno, - ac_cv_errno, - [ - AC_TRY_LINK(,[extern int errno; return (errno);], - ac_cv_errno=yes, ac_cv_errno=no) - ]) - if test "$ac_cv_errno" = yes; then - AC_DEFINE(HAVE_ERRNO) - AC_CACHE_CHECK(for errno declaration, - ac_cv_decl_errno, - [ - AC_TRY_COMPILE([ - #include - #ifdef HAVE_STDLIB_H - #include - #endif - #ifdef HAVE_UNISTD_H - #include - #endif - #ifdef HAVE_ERRNO_H - #include - ],[return(sys_nerr);], - ac_cv_decl_errno=yes, ac_cv_decl_errno=no) - ]) - if test "$ac_cv_decl_errno" = yes; then - AC_DEFINE(HAVE_DECL_ERRNO) - fi; - fi - - AC_CACHE_CHECK(for sys_nerr, - ac_cv_sys_nerr, - [ - AC_TRY_LINK(,[extern int sys_nerr; return (sys_nerr);], - ac_cv_sys_nerr=yes, ac_cv_sys_nerr=no) - ]) - if test "$ac_cv_sys_nerr" = yes; then - AC_DEFINE(HAVE_SYS_NERR) - AC_CACHE_CHECK(for sys_nerr declaration, - ac_cv_decl_sys_nerr, - [ - AC_TRY_COMPILE([ - #include - #ifdef HAVE_STDLIB_H - #include - #endif - #ifdef HAVE_UNISTD_H - #include - #endif],[return(sys_nerr);], - ac_cv_decl_sys_nerr_def=yes, ac_cv_decl_sys_nerr_def=no) - ]) - if test "$ac_cv_decl_sys_nerr" = yes; then - AC_DEFINE(HAVE_DECL_SYS_NERR) - fi - fi - - - AC_CACHE_CHECK(for sys_errlist array, - ac_cv_sys_errlist, - [AC_TRY_LINK(,[extern char *sys_errlist[]; - sys_errlist[0];], - ac_cv_sys_errlist=yes, ac_cv_sys_errlist=no) - ]) - if test "$ac_cv_sys_errlist" = yes; then - AC_DEFINE(HAVE_SYS_ERRLIST) - AC_CACHE_CHECK(for sys_errlist declaration, - ac_cv_sys_errlist_def, - [AC_TRY_COMPILE([ - #include - #include - #ifdef HAVE_STDLIB_H - #include - #endif - #ifdef HAVE_UNISTD_H - #include - #endif],[char *s = sys_errlist[0]; return(*s);], - ac_cv_decl_sys_errlist=yes, ac_cv_decl_sys_errlist=no) - ]) - if test "$ac_cv_decl_sys_errlist" = yes; then - AC_DEFINE(HAVE_DECL_SYS_ERRLIST) - fi - fi - - - - AC_CACHE_CHECK(checking for long long, - ac_cv_long_long, - [ - AC_TRY_COMPILE([ - #include - #include - ], [printf("%d",sizeof(long long));], - ac_cv_long_long=yes, ac_cv_long_long=no) - ]) - if test $ac_cv_long_long = yes; then - AC_DEFINE(HAVE_LONG_LONG) - fi - - AC_CACHE_CHECK(checking for long double, - ac_cv_long_double, - [ - AC_TRY_COMPILE([ - #include - #include - ], [printf("%d",sizeof(long double));], - ac_cv_long_double=yes, ac_cv_long_double=no) - ]) - if test $ac_cv_long_double = yes; then - AC_DEFINE(HAVE_LONG_DOUBLE) - fi - - AC_CACHE_CHECK(checking for quad_t, - ac_cv_quad_t, - [ - AC_TRY_COMPILE([ - #include - #include - ], [printf("%d",sizeof(quad_t));], - ac_cv_quad_t=yes, ac_cv_quad_t=no) - ]) - if test $ac_cv_quad_t = yes; then - AC_DEFINE(HAVE_QUAD_T) - fi - - - - NAME - snprintf, plp_vsnprintf - formatted output conversion - - SYNOPSIS - #include - #include - - int - snprintf(const char *format, size_t size, va_list ap); - int - plp_unsafe_snprintf(const char *format, size_t size, va_list ap); - - AKA snprintf and unsafe_snprintf in the documentation below - - int - vsnprintf(char *str, size_t size, const char *format, va_list ap); - int - unsafe_vsnprintf(char *str, size_t size, const char *format, va_list ap); - - AKA vsnprintf and unsafe_vsnprintf in the documentation below - - (Multithreaded Safe) - - DESCRIPTION - The printf() family of functions produces output according to - a format as described below. Snprintf(), and vsnprintf() - write to the character string str. These functions write the - output under the control of a format string that specifies - how subsequent arguments (or arguments accessed via the - variable-length argument facilities of stdarg(3)) are converted - for output. These functions return the number of characters - printed (not including the trailing `\0' used to end output - to strings). Snprintf() and vsnprintf() will write at most - size-1 of the characters printed into the output string (the - size'th character then gets the terminating `\0'); if the - return value is greater than or equal to the size argument, - the string was too short and some of the printed characters - were discarded. The size or str may be given as zero to find - out how many characters are needed; in this case, the str - argument is ignored. - - By default, the snprintf function will not format control - characters (except new line and tab) in strings. This is a - safety feature that has proven to be extremely critical when - using snprintf for secure applications and when debugging. - If you MUST have control characters formatted or printed, - then use the unsafe_snprintf() and unsafe_vsnprintf() and on - your own head be the consequences. You have been warned. - - There is one exception to the comments above, and that is - the "%c" (character) format. It brutally assumes that the - user will have performed the necessary 'isprint()' or other - checks and uses the integer value as a character. - - The format string is composed of zero or more directives: - ordinary characters (not %), which are copied unchanged to - the output stream; and conversion specifications, each - of which results in fetching zero or more subsequent arguments. - Each conversion specification is introduced by the character - %. The arguments must correspond properly (after type promotion) - with the conversion specifier. After the %, the following - appear in sequence: - - o Zero or more of the following flags: - - - A zero `0' character specifying zero padding. For - all conversions except n, the converted value is padded - on the left with zeros rather than blanks. If a - precision is given with a numeric conversion (d, i, - o, u, i, x, and X), the `0' flag is ignored. - - - A negative field width flag `-' indicates the converted - value is to be left adjusted on the field boundary. Except - for n conversions, the converted value is padded on - the right with blanks, rather than on the left with - blanks or zeros. A `-' overrides a `0' if both are - given. - - - A space, specifying that a blank should be left before - a positive number produced by a signed conversion (d, e, E, f, - g, G, or i). - - - A `+' character specifying that a sign always be placed - before a number produced by a signed conversion. A `+' overrides - a space if both are used. - - o An optional decimal digit string specifying a minimum - field width. If the converted value has fewer - characters than the field width, it will be padded - with spaces on the left (or right, if the - left-adjustment flag has been given) to fill out - the field width. - - o An optional precision, in the form of a period `.' followed - by an optional digit string. If the digit string - is omitted, the precision is taken as zero. This - gives the minimum number of digits to appear for - d, i, o, u, x, and X conversions, the number of - digits to appear after the decimal-point for e, - E, and f conversions, the maximum number of - significant digits for g and G conversions, or - the maximum number of characters to be printed - from a string for s conversions. - - o The optional character h, specifying that a following d, - i, o, u, x, or X conversion corresponds to a short - int or unsigned short int argument, or that a - following n conversion corresponds to a pointer - to a short int argument. - - o The optional character l (ell) specifying that a following - d, i, o, u, x, or X conversion applies to a pointer - to a long int or unsigned long int argument, or - that a following n conversion corresponds to a - pointer to a long int argument. - - o The optional character q, specifying that a following d, - i, o, u, x, or X conversion corresponds to a quad_t - or u_quad_t argument, or that a following n - conversion corresponds to a quad_t argument. - This value is always printed in HEX notation. Tough. - quad_t's are an OS system implementation, and should - not be allowed. - - o The character L specifying that a following e, E, f, g, - or G conversion corresponds to a long double - argument. - - o A character that specifies the type of conversion to be applied. - - - A field width or precision, or both, may be indicated by an asterisk `*' - instead of a digit string. In this case, an int argument supplies the - field width or precision. A negative field width is treated as a left - adjustment flag followed by a positive field width; a negative precision - is treated as though it were missing. - - The conversion specifiers and their meanings are: - - diouxX The int (or appropriate variant) argument is converted to signed - decimal (d and i), unsigned octal (o), unsigned decimal - (u), or unsigned hexadecimal (x and X) notation. The - letters abcdef are used for x conversions; the letters - ABCDEF are used for X conversions. The precision, if - any, gives the minimum number of digits that must - appear; if the converted value requires fewer digits, - it is padded on the left with zeros. - - eE The double argument is rounded and converted in the style - [-]d.ddde+-dd where there is one digit before the decimal-point - character and the number of digits after it is equal - to the precision; if the precision is missing, it is - taken as 6; if the precision is zero, no decimal-point - character appears. An E conversion uses the letter - E (rather than e) to introduce the exponent. - The exponent always contains at least two digits; if - the value is zero, the exponent is 00. - - f The double argument is rounded and converted to decimal notation - in the style [-]ddd.ddd, where the number of digits after the - decimal-point character is equal to the precision specification. - If the precision is missing, it is taken as 6; if the precision - is explicitly zero, no decimal-point character appears. If a - decimal point appears, at least one digit appears before it. - - g The double argument is converted in style f or e (or - E for G conversions). The precision specifies the - number of significant digits. If the precision is - missing, 6 digits are given; if the precision is zero, - it is treated as 1. Style e is used if the exponent - from its conversion is less than -4 or greater than - or equal to the precision. Trailing zeros are removed - from the fractional part of the result; a decimal - point appears only if it is followed by at least one - digit. - - c The int argument is converted to an unsigned char, - and the resulting character is written. - - s The ``char *'' argument is expected to be a pointer to an array - of character type (pointer to a string). Characters - from the array are written up to (but not including) - a terminating NUL character; if a precision is - specified, no more than the number specified are - written. If a precision is given, no null character - need be present; if the precision is not specified, - or is greater than the size of the array, the array - must contain a terminating NUL character. - - % A `%' is written. No argument is converted. The complete - conversion specification is `%%'. - - In no case does a non-existent or small field width cause truncation of a - field; if the result of a conversion is wider than the field width, the - field is expanded to contain the conversion result. - - EXAMPLES - To print a date and time in the form `Sunday, July 3, 10:02', where - weekday and month are pointers to strings: - - #include - fprintf(stdout, "%s, %s %d, %.2d:%.2d\n", - weekday, month, day, hour, min); - - To print pi to five decimal places: - - #include - #include - fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0)); - - To allocate a 128 byte string and print into it: - - #include - #include - #include - char *newfmt(const char *fmt, ...) - { - char *p; - va_list ap; - if ((p = malloc(128)) == NULL) - return (NULL); - va_start(ap, fmt); - (void) vsnprintf(p, 128, fmt, ap); - va_end(ap); - return (p); - } - - SEE ALSO - printf(1), scanf(3) - - STANDARDS - Turkey C Standardization and wimpy POSIX folks did not define - snprintf or vsnprintf(). - - BUGS - The conversion formats %D, %O, and %U are not standard and are provided - only for backward compatibility. The effect of padding the %p format - with zeros (either by the `0' flag or by specifying a precision), and the - benign effect (i.e., none) of the `#' flag on %n and %p conversions, as - well as other nonsensical combinations such as %Ld, are not standard; - such combinations should be avoided. - - The typedef names quad_t and u_quad_t are infelicitous. - -*/ - - -#include -#include -#include -#include -#if defined(HAVE_STRING_H) -# include -#endif -#if defined(HAVE_STRINGS_H) -# include -#endif -#if defined(HAVE_ERRNO_H) -#include -#endif - -/* - * For testing, define these values - */ -#if 0 -#define HAVE_STDARG_H 1 -#define TEST 1 -#define HAVE_QUAD_T 1 -#endif - -/**** ENDINCLUDE ****/ - -/************************************************* - * KEEP THIS STRING - MODIFY AT THE END WITH YOUR REVISIONS - * i.e. - the LOCAL REVISIONS part is for your use - *************************************************/ - - - static char *const _id = "plp_snprintf V2000.08.18 Copyright Patrick Powell 1988-2000 " - "$Id: plp_snprintf.c,v 1.4 2005/04/14 20:05:19 papowell Exp $" - " LOCAL REVISIONS: renamed plp_snprintf to snprintf, conditionalized everything on HAVE_SNPRINTF"; - -/* varargs declarations: */ - -# undef HAVE_STDARGS /* let's hope that works everywhere (mj) */ -# undef VA_LOCAL_DECL -# undef VA_START -# undef VA_SHIFT -# undef VA_END - -#if defined(HAVE_STDARG_H) -# include -# define HAVE_STDARGS /* let's hope that works everywhere (mj) */ -# define VA_LOCAL_DECL va_list ap; -# define VA_START(f) va_start(ap, f) -# define VA_SHIFT(v,t) ; /* no-op for ANSI */ -# define VA_END va_end(ap) -#else -# if defined(HAVE_VARARGS_H) -# include -# undef HAVE_STDARGS -# define VA_LOCAL_DECL va_list ap; -# define VA_START(f) va_start(ap) /* f is ignored! */ -# define VA_SHIFT(v,t) v = va_arg(ap,t) -# define VA_END va_end(ap) -# else - XX ** NO VARARGS ** XX -# endif -#endif - - union value { -#if defined(HAVE_QUAD_T) - quad_t qvalue; -#endif -#if defined(HAVE_LONG_LONG) - long long value; -#else - long value; -#endif - double dvalue; -}; - -#undef CVAL -#define CVAL(s) (*((unsigned char *)s)) -#define safestrlen(s) ((s)?strlen(s):0) - - - static char * plp_Errormsg ( int err, char *buffer ); - static void dopr( int visible_control, char **buffer, int *left, - const char *format, va_list args ); - static void fmtstr( int visible_control, char **buffer, int *left, - char *value, int ljust, int len, int zpad, int precision ); - static void fmtnum( char **buffer, int *left, - union value *value, int base, int dosign, - int ljust, int len, int zpad, int precision ); -#if defined(HAVE_QUAD_T) - static void fmtquad( char **buffer, int *left, - union value *value, int base, int dosign, - int ljust, int len, int zpad, int precision ); -#endif - static void fmtdouble( char **bufer, int *left, - int fmt, double value, - int ljust, int len, int zpad, int precision ); - static void dostr( char **buffer, int *left, char *str ); - static void dopr_outch( char **buffer, int *left, int c ); -/* VARARGS3 */ -#ifdef HAVE_STDARGS - int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args) -#else - int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args) -#endif - -{ - int left; - char *buffer; - if( (int)count < 0 ) count = 0; - left = count; - if( count == 0 ) str = 0; - buffer = str; - dopr( 1, &buffer, &left, fmt, args ); - /* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n", - (int)str, (int)buffer, count, left ); */ - if( str && count > 0 ){ - if( left > 0 ){ - str[count-left] = 0; - } else { - str[count-1] = 0; - } - } - return(count - left); -} - -/* VARARGS3 */ -#ifdef HAVE_STDARGS - int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args) -#else - int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args) -#endif -{ - int left; - char *buffer; - if( (int)count < 0 ) count = 0; - left = count; - if( count == 0 ) str = 0; - buffer = str; - dopr( 0, &buffer, &left, fmt, args ); - /* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n", - (int)str, (int)buffer, count, left ); */ - if( str && count > 0 ){ - if( left > 0 ){ - str[count-left] = 0; - } else { - str[count-1] = 0; - } - } - return(count - left); -} - -/* VARARGS3 */ -#ifdef HAVE_STDARGS - int snprintf (char *str,size_t count,const char *fmt,...) -#else - int snprintf (va_alist) va_dcl -#endif -{ -#ifndef HAVE_STDARGS - char *str; - size_t count; - char *fmt; -#endif - int n = 0; - VA_LOCAL_DECL - - VA_START (fmt); - VA_SHIFT (str, char *); - VA_SHIFT (count, size_t ); - VA_SHIFT (fmt, char *); - n = plp_vsnprintf ( str, count, fmt, ap); - VA_END; - return( n ); -} - - -/* VARARGS3 */ -#ifdef HAVE_STDARGS - int plp_unsafe_snprintf (char *str,size_t count,const char *fmt,...) -#else - int plp_unsafe_snprintf (va_alist) va_dcl -#endif -{ -#ifndef HAVE_STDARGS - char *str; - size_t count; - char *fmt; -#endif - int n = 0; - VA_LOCAL_DECL - - VA_START (fmt); - VA_SHIFT (str, char *); - VA_SHIFT (count, size_t ); - VA_SHIFT (fmt, char *); - n = plp_unsafe_vsnprintf ( str, count, fmt, ap); - VA_END; - return( n ); -} - static void dopr( int visible_control, char **buffer, int *left, const char *format, va_list args ) -{ - int ch; - union value value; - int longflag = 0; - int quadflag = 0; - char *strvalue; - int ljust; - int len; - int zpad; - int precision; - int set_precision; - double dval; - int err = errno; - int base = 0; - int signed_val = 0; - - while( (ch = *format++) ){ - switch( ch ){ - case '%': - longflag = quadflag = - ljust = len = zpad = base = signed_val = 0; - precision = -1; set_precision = 0; - nextch: - ch = *format++; - switch( ch ){ - case 0: - dostr( buffer, left, "**end of format**" ); - return; - case '-': ljust = 1; goto nextch; - case '.': set_precision = 1; precision = 0; goto nextch; - case '*': - if( set_precision ){ - precision = va_arg( args, int ); - } else { - len = va_arg( args, int ); - } - goto nextch; - case '0': /* set zero padding if len not set */ - if(len==0 && set_precision == 0 ) zpad = '0'; - case '1': case '2': case '3': - case '4': case '5': case '6': - case '7': case '8': case '9': - if( set_precision ){ - precision = precision*10 + ch - '0'; - } else { - len = len*10 + ch - '0'; - } - goto nextch; - case 'l': ++longflag; goto nextch; - case 'q': -#if !defined( HAVE_QUAD_T ) - dostr( buffer, left, "*no quad_t support *"); - return; -#endif - quadflag = 1; - goto nextch; - case 'u': case 'U': - if( base == 0 ){ base = 10; signed_val = 0; } - case 'o': case 'O': - if( base == 0 ){ base = 8; signed_val = 0; } - case 'd': case 'D': - if( base == 0 ){ base = 10; signed_val = 1; } - case 'x': - if( base == 0 ){ base = 16; signed_val = 0; } - case 'X': - if( base == 0 ){ base = -16; signed_val = 0; } -#if defined( HAVE_QUAD_T ) - if( quadflag ){ - value.qvalue = va_arg( args, quad_t ); - fmtquad( buffer, left, &value,base,signed_val, ljust, len, zpad, precision ); - break; - } else -#endif - if( longflag > 1 ){ -#if defined(HAVE_LONG_LONG) - if( signed_val ){ - value.value = va_arg( args, long long ); - } else { - value.value = va_arg( args, unsigned long long ); - } -#else - if( signed_val ){ - value.value = va_arg( args, long ); - } else { - value.value = va_arg( args, unsigned long ); - } -#endif - } else if( longflag ){ - if( signed_val ){ - value.value = va_arg( args, long ); - } else { - value.value = va_arg( args, unsigned long ); - } - } else { - if( signed_val ){ - value.value = va_arg( args, int ); - } else { - value.value = va_arg( args, unsigned int ); - } - } - fmtnum( buffer, left, &value,base,signed_val, ljust, len, zpad, precision ); break; - case 's': - strvalue = va_arg( args, char *); - fmtstr( visible_control, buffer, left, strvalue,ljust,len, zpad, precision ); - break; - case 'c': - ch = va_arg( args, int ); - { char b[2]; - b[0] = ch; - b[1] = 0; - fmtstr( 0, buffer, left, b,ljust,len, zpad, precision ); - } - break; - case 'f': case 'g': case 'e': - dval = va_arg( args, double ); - fmtdouble( buffer, left, ch, dval,ljust,len, zpad, precision ); break; - case 'm': - { char shortbuffer[32]; - fmtstr( visible_control, buffer, left, - plp_Errormsg(err, shortbuffer),ljust,len, zpad, precision ); - } - break; - case '%': dopr_outch( buffer, left, ch ); continue; - default: - dostr( buffer, left, "???????" ); - } - longflag = 0; - break; - default: - dopr_outch( buffer, left, ch ); - break; - } - } -} - -/* - * Format '%[-]len[.precision]s' - * - = left justify (ljust) - * len = minimum length - * precision = numbers of chars in string to use - */ - static void - fmtstr( int visible_control, char **buffer, int *left, - char *value, int ljust, int len, int zpad, int precision ) -{ - int padlen, strlenv, i, c; /* amount to pad */ - - if( value == 0 ){ - value = ""; - } - /* cheap strlen so you do not have library call */ - for( strlenv = i = 0; (c=CVAL(value+i)); ++i ){ - if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){ - ++strlenv; - } - ++strlenv; - } - if( precision > 0 && strlenv > precision ){ - strlenv = precision; - } - padlen = len - strlenv; - if( padlen < 0 ) padlen = 0; - if( ljust ) padlen = -padlen; - while( padlen > 0 ) { - dopr_outch( buffer, left, ' ' ); - --padlen; - } - /* output characters */ - for( i = 0; i < strlenv && (c = CVAL(value+i)); ++i ){ - if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){ - dopr_outch(buffer, left, '^'); - c = ('@' | (c & 0x1F)); - } - dopr_outch(buffer, left, c); - } - while( padlen < 0 ) { - dopr_outch( buffer, left, ' ' ); - ++padlen; - } -} - - static void - fmtnum( char **buffer, int *left, - union value *value, int base, int dosign, int ljust, - int len, int zpad, int precision ) -{ - int signvalue = 0; -#if defined(HAVE_LONG_LONG) - unsigned long long uvalue; -#else - unsigned long uvalue; -#endif - char convert[sizeof( union value) * 8 + 16]; - int place = 0; - int padlen = 0; /* amount to pad */ - int caps = 0; - - /* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n", - value, base, dosign, ljust, len, zpad );/ **/ - uvalue = value->value; - if( dosign ){ - if( value->value < 0 ) { - signvalue = '-'; - uvalue = -value->value; - } - } - if( base < 0 ){ - caps = 1; - base = -base; - } - do{ - convert[place++] = - (caps? "0123456789ABCDEF":"0123456789abcdef") - [uvalue % (unsigned)base ]; - uvalue = (uvalue / (unsigned)base ); - }while(uvalue); - convert[place] = 0; - padlen = len - place; - if( padlen < 0 ) padlen = 0; - if( ljust ) padlen = -padlen; - /* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n", - convert,place,signvalue,padlen); / **/ - if( zpad && padlen > 0 ){ - if( signvalue ){ - dopr_outch( buffer, left, signvalue ); - --padlen; - signvalue = 0; - } - while( padlen > 0 ){ - dopr_outch( buffer, left, zpad ); - --padlen; - } - } - while( padlen > 0 ) { - dopr_outch( buffer, left, ' ' ); - --padlen; - } - if( signvalue ) dopr_outch( buffer, left, signvalue ); - while( place > 0 ) dopr_outch( buffer, left, convert[--place] ); - while( padlen < 0 ){ - dopr_outch( buffer, left, ' ' ); - ++padlen; - } -} - -#if defined(HAVE_QUAD_T) - - static void - fmtquad( char **buffer, int *left, - union value *value, int base, int dosign, int ljust, - int len, int zpad, int precision ) -{ - int signvalue = 0; - int place = 0; - int padlen = 0; /* amount to pad */ - int caps = 0; - int i, c; - union { - quad_t qvalue; - unsigned char qconvert[sizeof(quad_t)]; - } vvalue; - char convert[2*sizeof(quad_t)+1]; - - /* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n", - value, base, dosign, ljust, len, zpad );/ **/ - vvalue.qvalue = value->qvalue; - - if( base < 0 ){ - caps = 1; - } - - for( i = 0; i < (int)sizeof(quad_t); ++i ){ - c = vvalue.qconvert[i]; - convert[2*i] = - (caps? "0123456789ABCDEF":"0123456789abcdef")[ (c >> 4) & 0xF]; - convert[2*i+1] = - (caps? "0123456789ABCDEF":"0123456789abcdef")[ c & 0xF]; - } - convert[2*i] = 0; - - place = strlen(convert); - padlen = len - place; - if( padlen < 0 ) padlen = 0; - if( ljust ) padlen = -padlen; - /* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n", - convert,place,signvalue,padlen); / **/ - if( zpad && padlen > 0 ){ - if( signvalue ){ - dopr_outch( buffer, left, signvalue ); - --padlen; - signvalue = 0; - } - while( padlen > 0 ){ - dopr_outch( buffer, left, zpad ); - --padlen; - } - } - while( padlen > 0 ) { - dopr_outch( buffer, left, ' ' ); - --padlen; - } - if( signvalue ) dopr_outch( buffer, left, signvalue ); - while( place > 0 ) dopr_outch( buffer, left, convert[--place] ); - while( padlen < 0 ){ - dopr_outch( buffer, left, ' ' ); - ++padlen; - } -} - -#endif - - static void mystrcat(char *dest, char *src ) -{ - if( dest && src ){ - dest += safestrlen(dest); - strcpy(dest,src); - } -} - - static void - fmtdouble( char **buffer, int *left, - int fmt, double value, int ljust, int len, int zpad, int precision ) -{ - char convert[sizeof( union value) * 8 + 512]; - char formatstr[128]; - - /* fprintf(stderr,"len %d, precision %d\n", len, precision ); */ - if( len > 255 ){ - len = 255; - } - if( precision > 255 ){ - precision = 255; - } - if( precision >= 0 && len > 0 && precision > len ) precision = len; - strcpy( formatstr, "%" ); /* 1 */ - if( ljust ) mystrcat(formatstr, "-" ); /* 1 */ - if( zpad ) mystrcat(formatstr, "0" ); /* 1 */ - if( len >= 0 ){ - sprintf( formatstr+strlen(formatstr), "%d", len ); /* 3 */ - } - if( precision >= 0 ){ - sprintf( formatstr+strlen(formatstr), ".%d", precision ); /* 3 */ - } - /* format string will be at most 10 chars long ... */ - sprintf( formatstr+strlen(formatstr), "%c", fmt ); - /* this is easier than trying to do the portable dtostr */ - /* fprintf(stderr,"format string '%s'\n", formatstr); */ - sprintf( convert, formatstr, value ); - dostr( buffer, left, convert ); -} - - static void dostr( char **buffer, int *left, char *str ) -{ - if(str)while(*str) dopr_outch( buffer, left, *str++ ); -} - - static void dopr_outch( char **buffer, int *left, int c ) -{ - if( *left > 0 ){ - *(*buffer)++ = c; - } - *left -= 1; -} - - -/**************************************************************************** - * static char *plp_errormsg( int err ) - * returns a printable form of the - * errormessage corresponding to the valie of err. - * This is the poor man's version of sperror(), not available on all systems - * Patrick Powell Tue Apr 11 08:05:05 PDT 1995 - ****************************************************************************/ -/****************************************************************************/ - -#if !defined(HAVE_STRERROR) -# undef num_errors -# if defined(HAVE_SYS_ERRLIST) -# if !defined(HAVE_DECL_SYS_ERRLIST) - extern const char *const sys_errlist[]; -# endif -# if defined(HAVE_SYS_NERR) -# if !defined(HAVE_DECL_SYS_NERR) - extern int sys_nerr; -# endif -# define num_errors (sys_nerr) -# endif -# endif -# if !defined(num_errors) -# define num_errors (-1) /* always use "errno=%d" */ -# endif -#endif - - static char * plp_Errormsg ( int err, char *buffer /* int maxlen = 32 */) -{ - char *cp; - -#if defined(HAVE_STRERROR) - cp = (void *)strerror(err); -#else -# if defined(HAVE_SYS_ERRLIST) - if (err >= 0 && err < num_errors) { - cp = (void *)sys_errlist[err]; - } else -# endif - { - (void) sprintf (buffer, "errno=%d", err); - cp = buffer; - } -#endif - return (cp); -} - -#if defined(TEST) -#include - int main( void ) -{ - char buffer[128]; - char *t; - char *test1 = "01234"; - int n; - errno = 1; - buffer[0] = 0; - n = snprintf( buffer, 0, (t="test")); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t="errno '%m'")); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12.1g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12.3g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); -#if defined(HAVE_LONG_LONG) - n = snprintf( buffer, sizeof(buffer), (t = "%llx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%llx"), (long long)1, (long long)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%qx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%qx"), (quad_t)1, (quad_t)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); -#endif - n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0x89ABCDEF, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), t, 0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%12.2f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%0.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%1.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%1.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - n = snprintf( buffer, sizeof(buffer), (t = "%5.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer ); - return(0); -} -#endif - - -#endif /* HAVE_SNPRINTF */ diff --git a/lib/strdup.c b/lib/strdup.c deleted file mode 100644 index 1286b8e..0000000 --- a/lib/strdup.c +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright (C) 1997 Free Software Foundation, Inc. -This file is part of the GNU C Library. - -The GNU C Library 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. - -The GNU C Library 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 the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ - -#include <../include/sane/config.h> - -#include -#include - -#ifndef HAVE_STRDUP - -char * -strdup (const char * s) -{ - char *clone; - size_t size; - - size = strlen (s) + 1; - clone = malloc (size); - memcpy (clone, s, size); - return clone; -} - -#endif /* !HAVE_STRDUP */ diff --git a/lib/strndup.c b/lib/strndup.c deleted file mode 100644 index 35c0597..0000000 --- a/lib/strndup.c +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (C) 1997 Free Software Foundation, Inc. -This file is part of the GNU C Library. - -The GNU C Library 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. - -The GNU C Library 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 the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ - -#include <../include/sane/config.h> - -#ifndef HAVE_STRNDUP - -#include -#include - -#include - -char * -strndup (const char * s, size_t n) -{ - char *clone; - - clone = malloc (n + 1); - strncpy (clone, s, n); - clone[n] = '\0'; - return clone; -} - -#endif /* !HAVE_STRNDUP */ diff --git a/lib/strsep.c b/lib/strsep.c deleted file mode 100644 index 5ac58ae..0000000 --- a/lib/strsep.c +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright (C) 1992, 1993, 1996 Free Software Foundation, Inc. -This file is part of the GNU C Library. - -The GNU C Library 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. - -The GNU C Library 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 the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ - -#include <../include/sane/config.h> - -#include - -#ifndef HAVE_STRSEP - -char * -strsep (char **stringp, const char *delim) -{ - char *begin, *end; - - begin = *stringp; - if (! begin || *begin == '\0') - return NULL; - - /* Find the end of the token. */ - end = strpbrk (begin, delim); - if (end) - { - /* Terminate the token and set *STRINGP past NUL character. */ - *end++ = '\0'; - *stringp = end; - } - else - /* No more delimiters; this is the last token. */ - *stringp = NULL; - - return begin; -} - -#endif /* !HAVE_STRSEP */ diff --git a/lib/usleep.c b/lib/usleep.c deleted file mode 100644 index 0be9dd0..0000000 --- a/lib/usleep.c +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright (C) 1992 Free Software Foundation, Inc. -This file is part of the GNU C Library. - -The GNU C Library 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. - -The GNU C Library 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 the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 675 Mass Ave, -Cambridge, MA 02139, USA. */ - -#ifdef __TANDEM -#include -#endif - -#include <../include/sane/config.h> - -#ifndef HAVE_USLEEP - -#include -#ifdef HAVE_SYS_TIME_H -# include -#endif - -#ifdef HAVE_SYS_SELECT_H -# include -#endif - -#ifdef apollo -# include -# include - static time_$clock_t DomainTime100mS = - { - 0, 100000/4 - }; - static status_$t DomainStatus; -#endif - -/* Sleep USECONDS microseconds, or until a previously set timer goes off. */ -unsigned int -usleep (unsigned int useconds) -{ -#ifdef apollo - /* The usleep function does not work under the SYS5.3 environment. - Use the Domain/OS time_$wait call instead. */ - time_$wait (time_$relative, DomainTime100mS, &DomainStatus); -#else - struct timeval delay; - - delay.tv_sec = 0; - delay.tv_usec = useconds; - select (0, 0, 0, 0, &delay); - return 0; -#endif -} - -#endif /* !HAVE_USLEEP */ diff --git a/lib/vsyslog.c b/lib/vsyslog.c deleted file mode 100644 index ce86301..0000000 --- a/lib/vsyslog.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "../include/sane/config.h" - -#include "stdio.h" -#include -#include - -#ifndef HAVE_VSYSLOG - -void -vsyslog(int priority, const char *format, va_list args) -{ - char buf[1024]; - vsnprintf(buf, sizeof(buf), format, args); - syslog(priority, "%s", buf); -} - -#endif /* !HAVE_VSYSLOG */ diff --git a/src/Makefile.in b/src/Makefile.in index af064d0..f9aee49 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -51,7 +51,6 @@ LINK = $(CC) $(LDFLAGS) -o $@ @SET_MAKE@ PROGRAMS = $(BINPROGS) -LIBLIB = ../lib/liblib.a LIBSANEI = ../sanei/libsanei.a XSCANIMAGE_OBJS = xscanimage.o progress.o preview.o preferences.o gtkglue.o @@ -92,16 +91,16 @@ uninstall: done rm -f $(sanedatadir)/sane-style.rc -xscanimage: $(XSCANIMAGE_OBJS) $(LIBSANEI) $(LIBLIB) +xscanimage: $(XSCANIMAGE_OBJS) $(LIBSANEI) $(LINK) $(XSCANIMAGE_OBJS) $(LIBSANEI) \ - $(LIBLIB) $(GIMP_LIBS) $(GTK_LIBS) $(LIBS) + $(GIMP_LIBS) $(GTK_LIBS) $(LIBS) -xcam: $(XCAM_OBJS) $(LIBSANEI) $(LIBLIB) +xcam: $(XCAM_OBJS) $(LIBSANEI) $(LINK) $(XCAM_OBJS) $(LIBSANEI) \ - $(LIBLIB) $(GTK_LIBS) $(LIBS) + $(GTK_LIBS) $(LIBS) -scanadf: $(SCANADF_OBJS) $(LIBLIB) - $(LINK) $(SCANADF_OBJS) $(LIBLIB) $(LIBS) +scanadf: $(SCANADF_OBJS) + $(LINK) $(SCANADF_OBJS) $(LIBS) clean: rm -f *.o *~ .*~ *.bak