Update of /cvsroot/mingw/msys/packages/bash/2.05b/lib/sh In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10360/2.05b/lib/sh Added Files: Makefile.in clktck.c clock.c fmtullong.c fmtulong.c fmtumax.c getcwd.c getenv.c inet_aton.c itos.c mailstat.c makepath.c memset.c mktime.c netconn.c netopen.c oslib.c pathcanon.c pathphys.c rename.c setlinebuf.c shquote.c shtty.c snprintf.c spell.c strcasecmp.c strerror.c strftime.c strindex.c stringlist.c stringvec.c strpbrk.c strtod.c strtoimax.c strtol.c strtoll.c strtoul.c strtoull.c strtoumax.c strtrans.c times.c timeval.c tmpfile.c vprint.c xstrchr.c zcatfd.c zread.c zwrite.c Log Message: Pristine source --- NEW FILE: mailstat.c --- /* mailstat.c -- stat a mailbox file, handling maildir-type mail directories */ /* Copyright (C) 2001 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. Bash 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. Bash 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 Bash; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ #include <config.h> #include <stdio.h> #include <errno.h> #include <bashtypes.h> #include <posixstat.h> #include <posixdir.h> #include <bashansi.h> #ifndef _MINIX # include <sys/param.h> #endif #include <maxpath.h> /* * Stat a file. If it's a maildir, check all messages * in the maildir and present the grand total as a file. * The fields in the 'struct stat' are from the mail directory. * The following fields are emulated: * * st_nlink always 1, unless st_blocks is not present, in which case it's * the total number of messages * st_size total number of bytes in all files * st_blocks total number of messages, if present in struct stat * st_atime access time of newest file in maildir * st_mtime modify time of newest file in maildir * st_mode S_IFDIR changed to S_IFREG * * This is good enough for most mail-checking applications. */ int mailstat(path, st) const char *path; struct stat *st; { static struct stat st_new_last, st_ret_last; struct stat st_ret, st_tmp; DIR *dd; struct dirent *fn; char dir[PATH_MAX * 2], file[PATH_MAX * 2]; int i, l; time_t atime, mtime; atime = mtime = 0; /* First see if it's a directory. */ if ((i = stat(path, st)) != 0 || S_ISDIR(st->st_mode) == 0) return i; if (strlen(path) > sizeof(dir) - 5) { #ifdef ENAMETOOLONG errno = ENAMETOOLONG; #else errno = EINVAL; #endif return -1; } st_ret = *st; st_ret.st_nlink = 1; st_ret.st_size = 0; #ifdef HAVE_STRUCT_STAT_ST_BLOCKS st_ret.st_blocks = 0; #else st_ret.st_nlink = 0; #endif st_ret.st_mode &= ~S_IFDIR; st_ret.st_mode |= S_IFREG; /* See if cur/ is present */ sprintf(dir, "%s/cur", path); if (stat(dir, &st_tmp) || S_ISDIR(st_tmp.st_mode) == 0) return 0; st_ret.st_atime = st_tmp.st_atime; /* See if tmp/ is present */ sprintf(dir, "%s/tmp", path); if (stat(dir, &st_tmp) || S_ISDIR(st_tmp.st_mode) == 0) return 0; st_ret.st_mtime = st_tmp.st_mtime; /* And new/ */ sprintf(dir, "%s/new", path); if (stat(dir, &st_tmp) || S_ISDIR(st_tmp.st_mode) == 0) return 0; st_ret.st_mtime = st_tmp.st_mtime; /* Optimization - if new/ didn't change, nothing else did. */ if (st_tmp.st_dev == st_new_last.st_dev && st_tmp.st_ino == st_new_last.st_ino && st_tmp.st_atime == st_new_last.st_atime && st_tmp.st_mtime == st_new_last.st_mtime) { *st = st_ret_last; return 0; } st_new_last = st_tmp; /* Loop over new/ and cur/ */ for (i = 0; i < 2; i++) { sprintf(dir, "%s/%s", path, i ? "cur" : "new"); sprintf(file, "%s/", dir); l = strlen(file); if ((dd = opendir(dir)) == NULL) return 0; while ((fn = readdir(dd)) != NULL) { if (fn->d_name[0] == '.' || strlen(fn->d_name) + l >= sizeof(file)) continue; strcpy(file + l, fn->d_name); if (stat(file, &st_tmp) != 0) continue; st_ret.st_size += st_tmp.st_size; #ifdef HAVE_STRUCT_STAT_ST_BLOCKS st_ret.st_blocks++; #else st_ret.st_nlink++; #endif if (st_tmp.st_atime != st_tmp.st_mtime && st_tmp.st_atime > atime) atime = st_tmp.st_atime; if (st_tmp.st_mtime > mtime) mtime = st_tmp.st_mtime; } closedir(dd); } if (atime) st_ret.st_atime = atime; if (mtime) st_ret.st_mtime = mtime; *st = st_ret_last = st_ret; return 0; } --- NEW FILE: netopen.c --- /* * netopen.c -- functions to make tcp/udp connections * * Chet Ramey * ch...@in... */ /* Copyright (C) 1987-2002 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. Bash 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. Bash 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 Bash; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ #include <config.h> #if defined (HAVE_NETWORK) #if defined (HAVE_UNISTD_H) # include <unistd.h> #endif #include <stdio.h> #include <sys/types.h> #if defined (HAVE_SYS_SOCKET_H) # include <sys/socket.h> #endif #if defined (HAVE_NETINET_IN_H) # include <netinet/in.h> #endif #if defined (HAVE_NETDB_H) # include <netdb.h> #endif #if defined (HAVE_ARPA_INET_H) # include <arpa/inet.h> #endif #include <bashansi.h> #include <errno.h> #include <shell.h> #include <xmalloc.h> #ifndef errno extern int errno; #endif #if !defined (HAVE_INET_ATON) extern int inet_aton __P((const char *, struct in_addr *)); #endif #ifndef HAVE_GETADDRINFO /* Stuff the internet address corresponding to HOST into AP, in network byte order. Return 1 on success, 0 on failure. */ static int _getaddr (host, ap) char *host; struct in_addr *ap; { struct hostent *h; int r; r = 0; if (host[0] >= '0' && host[0] <= '9') { /* If the first character is a digit, guess that it's an Internet address and return immediately if inet_aton succeeds. */ r = inet_aton (host, ap); if (r) return r; } #if !defined (HAVE_GETHOSTBYNAME) return 0; #else h = gethostbyname (host); if (h && h->h_addr) { bcopy(h->h_addr, (char *)ap, h->h_length); return 1; } #endif return 0; } /* Return 1 if SERV is a valid port number and stuff the converted value into PP in network byte order. */ static int _getserv (serv, proto, pp) char *serv; int proto; unsigned short *pp; { intmax_t l; unsigned short s; if (legal_number (serv, &l)) { s = (unsigned short)(l & 0xFFFF); if (s != l) return (0); s = htons (s); if (pp) *pp = s; return 1; } else #if defined (HAVE_GETSERVBYNAME) { struct servent *se; se = getservbyname (serv, (proto == 't') ? "tcp" : "udp"); if (se == 0) return 0; if (pp) *pp = se->s_port; /* ports returned in network byte order */ return 1; } #else /* !HAVE_GETSERVBYNAME */ return 0; #endif /* !HAVE_GETSERVBYNAME */ } /* * Open a TCP or UDP connection to HOST on port SERV. Uses the * traditional BSD mechanisms. Returns the connected socket or -1 on error. */ static int _netopen4(host, serv, typ) char *host, *serv; int typ; { struct in_addr ina; struct sockaddr_in sin; unsigned short p; int s, e; if (_getaddr(host, &ina) == 0) { internal_error ("%s: host unknown", host); errno = EINVAL; return -1; } if (_getserv(serv, typ, &p) == 0) { internal_error("%s: invalid service", serv); errno = EINVAL; return -1; } memset ((char *)&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = p; sin.sin_addr = ina; s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0); if (s < 0) { sys_error ("socket"); return (-1); } if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0) { e = errno; sys_error("connect"); close(s); errno = e; return (-1); } return(s); } #endif /* ! HAVE_GETADDRINFO */ #ifdef HAVE_GETADDRINFO /* * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3) * which provides support for IPv6. Returns the connected socket or -1 * on error. */ static int _netopen6 (host, serv, typ) char *host, *serv; int typ; { int s, e; struct addrinfo hints, *res, *res0; int gerr; memset ((char *)&hints, 0, sizeof (hints)); /* XXX -- if problems with IPv6, set to PF_INET for IPv4 only */ #ifdef DEBUG /* PF_INET is the one that works for me */ hints.ai_family = PF_INET; #else hints.ai_family = PF_UNSPEC; #endif hints.ai_socktype = (typ == 't') ? SOCK_STREAM : SOCK_DGRAM; gerr = getaddrinfo (host, serv, &hints, &res0); if (gerr) { if (gerr == EAI_SERVICE) internal_error ("%s: %s", serv, gai_strerror (gerr)); else internal_error ("%s: %s", host, gai_strerror (gerr)); errno = EINVAL; return -1; } for (res = res0; res; res = res->ai_next) { if ((s = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) { if (res->ai_next) continue; sys_error ("socket"); freeaddrinfo (res0); return -1; } if (connect (s, res->ai_addr, res->ai_addrlen) < 0) { if (res->ai_next) { close (s); continue; } e = errno; sys_error ("connect"); close (s); freeaddrinfo (res0); errno = e; return -1; } freeaddrinfo (res0); break; } return s; } #endif /* HAVE_GETADDRINFO */ /* * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3) * if available, falling back to the traditional BSD mechanisms otherwise. * Returns the connected socket or -1 on error. */ static int _netopen(host, serv, typ) char *host, *serv; int typ; { #ifdef HAVE_GETADDRINFO return (_netopen6 (host, serv, typ)); #else return (_netopen4 (host, serv, typ)); #endif } /* * Open a TCP or UDP connection given a path like `/dev/tcp/host/port' to * host `host' on port `port' and return the connected socket. */ int netopen (path) char *path; { char *np, *s, *t; int fd; np = (char *)xmalloc (strlen (path) + 1); strcpy (np, path); s = np + 9; t = strchr (s, '/'); if (t == 0) { internal_error ("%s: bad network path specification", path); return -1; } *t++ = '\0'; fd = _netopen (s, t, path[5]); free (np); return fd; } #if 0 /* * Open a TCP connection to host `host' on the port defined for service * `serv' and return the connected socket. */ int tcpopen (host, serv) char *host, *serv; { return (_netopen (host, serv, 't')); } /* * Open a UDP connection to host `host' on the port defined for service * `serv' and return the connected socket. */ int udpopen (host, serv) char *host, *serv; { return _netopen (host, serv, 'u'); } #endif #else /* !HAVE_NETWORK */ int netopen (path) char *path; { internal_error ("network operations not supported"); return -1; } #endif /* !HAVE_NETWORK */ --- NEW FILE: strtoumax.c --- /* Convert string representation of a number into an uintmax_t value. Copyright 1999, 2001 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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Written by Paul Eggert. Modified by Chet Ramey for Bash. */ #if HAVE_CONFIG_H # include <config.h> #endif #if HAVE_INTTYPES_H # include <inttypes.h> #endif #if HAVE_STDLIB_H # include <stdlib.h> #endif #include <stdc.h> /* Verify a requirement at compile-time (unlike assert, which is runtime). */ #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } #ifndef HAVE_DECL_STRTOUL "this configure-time declaration test was not run" #endif #if !HAVE_DECL_STRTOUL extern unsigned long strtoul __P((const char *, char **, int)); #endif #ifndef HAVE_DECL_STRTOULL "this configure-time declaration test was not run" #endif #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG extern unsigned long long strtoull __P((const char *, char **, int)); #endif uintmax_t strtoumax (ptr, endptr, base) const char *ptr; char **endptr; int base; { #if HAVE_UNSIGNED_LONG_LONG verify (size_is_that_of_unsigned_long_or_unsigned_long_long, (sizeof (uintmax_t) == sizeof (unsigned long) || sizeof (uintmax_t) == sizeof (unsigned long long))); if (sizeof (uintmax_t) != sizeof (unsigned long)) return (strtoull (ptr, endptr, base)); #else verify (size_is_that_of_unsigned_long, sizeof (uintmax_t) == sizeof (unsigned long)); #endif return (strtoul (ptr, endptr, base)); } #ifdef TESTING # include <stdio.h> int main () { char *p, *endptr; uintmax_t x; #if HAVE_UNSIGNED_LONG_LONG unsigned long long y; #endif unsigned long z; printf ("sizeof uintmax_t: %d\n", sizeof (uintmax_t)); #if HAVE_UNSIGNED_LONG_LONG printf ("sizeof unsigned long long: %d\n", sizeof (unsigned long long)); #endif printf ("sizeof unsigned long: %d\n", sizeof (unsigned long)); x = strtoumax("42", &endptr, 10); #if HAVE_LONG_LONG y = strtoull("42", &endptr, 10); #else y = 0; #endif z = strtoul("42", &endptr, 10); printf ("%llu %llu %lu\n", x, y, z); exit (0); } #endif --- NEW FILE: strtod.c --- /* Copyright (C) 1991, 1992 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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #if HAVE_CONFIG_H # include <config.h> #endif #ifndef HAVE_STRTOD #include <errno.h> #ifndef errno extern int errno; #endif #include <chartypes.h> #include <math.h> #if HAVE_FLOAT_H # include <float.h> #else # define DBL_MAX 1.7976931348623159e+308 # define DBL_MIN 2.2250738585072010e-308 #endif #include <bashansi.h> #ifndef NULL # define NULL 0 #endif #ifndef HUGE_VAL # define HUGE_VAL HUGE #endif /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the character after the last one used in the number is put in *ENDPTR. */ double strtod (nptr, endptr) const char *nptr; char **endptr; { register const char *s; short sign; /* The number so far. */ double num; int got_dot; /* Found a decimal point. */ int got_digit; /* Seen any digits. */ /* The exponent of the number. */ long int exponent; if (nptr == NULL) { errno = EINVAL; goto noconv; } s = nptr; /* Eat whitespace. */ while (ISSPACE ((unsigned char)*s)) ++s; /* Get the sign. */ sign = *s == '-' ? -1 : 1; if (*s == '-' || *s == '+') ++s; num = 0.0; got_dot = 0; got_digit = 0; exponent = 0; for (;; ++s) { if (DIGIT (*s)) { got_digit = 1; /* Make sure that multiplication by 10 will not overflow. */ if (num > DBL_MAX * 0.1) /* The value of the digit doesn't matter, since we have already gotten as many digits as can be represented in a `double'. This doesn't necessarily mean the result will overflow. The exponent may reduce it to within range. We just need to record that there was another digit so that we can multiply by 10 later. */ ++exponent; else num = (num * 10.0) + (*s - '0'); /* Keep track of the number of digits after the decimal point. If we just divided by 10 here, we would lose precision. */ if (got_dot) --exponent; } else if (!got_dot && *s == '.') /* Record that we have found the decimal point. */ got_dot = 1; else /* Any other character terminates the number. */ break; } if (!got_digit) goto noconv; if (TOLOWER ((unsigned char)*s) == 'e') { /* Get the exponent specified after the `e' or `E'. */ int save = errno; char *end; long int exp; errno = 0; ++s; exp = strtol (s, &end, 10); if (errno == ERANGE) { /* The exponent overflowed a `long int'. It is probably a safe assumption that an exponent that cannot be represented by a `long int' exceeds the limits of a `double'. */ if (endptr != NULL) *endptr = end; if (exp < 0) goto underflow; else goto overflow; } else if (end == s) /* There was no exponent. Reset END to point to the 'e' or 'E', so *ENDPTR will be set there. */ end = (char *) s - 1; errno = save; s = end; exponent += exp; } if (endptr != NULL) *endptr = (char *) s; if (num == 0.0) return 0.0; /* Multiply NUM by 10 to the EXPONENT power, checking for overflow and underflow. */ if (exponent < 0) { if (num < DBL_MIN * pow (10.0, (double) -exponent)) goto underflow; } else if (exponent > 0) { if (num > DBL_MAX * pow (10.0, (double) -exponent)) goto overflow; } num *= pow (10.0, (double) exponent); return num * sign; overflow: /* Return an overflow error. */ errno = ERANGE; return HUGE_VAL * sign; underflow: /* Return an underflow error. */ if (endptr != NULL) *endptr = (char *) nptr; errno = ERANGE; return 0.0; noconv: /* There was no number. */ if (endptr != NULL) *endptr = (char *) nptr; return 0.0; } #endif /* !HAVE_STRTOD */ --- NEW FILE: Makefile.in --- # # Makefile for the Bash library # # # Copyright (C) 1998-2002 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. srcdir = @srcdir@ VPATH = .:@srcdir@ topdir = @top_srcdir@ BUILD_DIR = @BUILD_DIR@ BASHINCDIR = ${topdir}/include INSTALL = @INSTALL@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_DATA = @INSTALL_DATA@ CC = @CC@ RANLIB = @RANLIB@ AR = @AR@ ARFLAGS = @ARFLAGS@ RM = rm -f CP = cp MV = mv SHELL = @MAKE_SHELL@ CFLAGS = @CFLAGS@ LOCAL_CFLAGS = @LOCAL_CFLAGS@ ${DEBUG} CPPFLAGS = @CPPFLAGS@ LDFLAGS = @LDFLAGS@ @LOCAL_LDFLAGS@ PROFILE_FLAGS = @PROFILE_FLAGS@ DEFS = @DEFS@ LOCAL_DEFS = @LOCAL_DEFS@ INCLUDES = -I. -I../.. -I$(topdir) -I$(topdir)/lib -I$(BASHINCDIR) -I$(srcdir) CCFLAGS = ${PROFILE_FLAGS} ${INCLUDES} $(DEFS) $(LOCAL_DEFS) $(LOCAL_CFLAGS) \ $(CFLAGS) $(CPPFLAGS) GCC_LINT_FLAGS = -Wall -Wshadow -Wpointer-arith -Wcast-qual \ -Wcast-align -Wstrict-prototypes -Wconversion \ -Wmissing-prototypes -Wtraditional -Wredundant-decls -pedantic .c.o: $(CC) -c $(CCFLAGS) $< # The name of the library target. LIBRARY_NAME = libsh.a # The C code source files for this library. CSOURCES = clktck.c clock.c getcwd.c getenv.c oslib.c setlinebuf.c \ strcasecmp.c strerror.c strtod.c strtol.c strtoul.c \ vprint.c itos.c rename.c zread.c zwrite.c shtty.c \ inet_aton.c netconn.c netopen.c strpbrk.c timeval.c makepath.c \ pathcanon.c pathphys.c tmpfile.c stringlist.c stringvec.c spell.c \ shquote.c strtrans.c strindex.c snprintf.c mailstat.c \ fmtulong.c fmtullong.c fmtumax.c \ strtoll.c strtoull.c strtoimax.c strtoumax.c memset.c \ mktime.c strftime.c xstrchr.c zcatfd.c # The header files for this library. HSOURCES = # The object files contained in $(LIBRARY_NAME) LIBOBJS = @LIBOBJS@ OBJECTS = clktck.o clock.o getenv.o oslib.o setlinebuf.o \ itos.o zread.o zwrite.o shtty.o \ netconn.o netopen.o timeval.o makepath.o pathcanon.o \ pathphys.o tmpfile.o stringlist.o stringvec.o spell.o shquote.o \ strtrans.o strindex.o snprintf.o mailstat.o fmtulong.o \ fmtullong.o fmtumax.o xstrchr.o zcatfd.o ${LIBOBJS} SUPPORT = Makefile all: $(LIBRARY_NAME) $(LIBRARY_NAME): $(OBJECTS) $(RM) $@ $(AR) $(ARFLAGS) $@ $(OBJECTS) -test -n "$(RANLIB)" && $(RANLIB) $@ force: # The rule for 'includes' is written funny so that the if statement # always returns TRUE unless there really was an error installing the # include files. install: clean: $(RM) $(OBJECTS) $(LIBRARY_NAME) realclean distclean maintainer-clean: clean $(RM) Makefile mostlyclean: clean # Dependencies # rules for losing makes, like SunOS clktck.o: clktck.c clock.o: clock.c fmtullong.o: fmtullong.c fmtulong.o: fmtulong.c fmtumax.o: fmtumax.c getcwd.o: getcwd.c getenv.o: getenv.c inet_aton.o: inet_aton.c itos.o: itos.c mailstat.o: mailstat.c makepath.o: makepath.c memset.o: memset.c mktime.o: mktime.c netconn.o: netconn.c netopen.o: netopen.c oslib.o: oslib.c pathcanon.o: pathcanon.c pathphys.o: pathphys.c rename.o: rename.c setlinebuf.o: setlinebuf.c shquote.o: shquote.c shtty.o: shtty.c snprintf.o: snprintf.c spell.o: spell.c strcasecmp.o: strcasecmp.c strerror.o: strerror.c strftime.o: strftime.c strindex.o: strindex.c stringlist.o: stringlist.c stringvec.o: stringvec.c strpbrk.o: strpbrk.c strtod.o: strtod.c strtoimax.o: strtoimax.c strtol.o: strtol.c strtoll.o: strtoll.c strtoul.o: strtoul.c strtoull.o: strtoull.c strtoumax.o: strtoumax.c strtrans.o: strtrans.c times.o: times.c timeval.o: timeval.c tmpfile.o: tmpfile.c vprint.o: vprint.c xstrchr.o: xstrchr.c zcatfd.o: zcatfd.c zread.o: zread.c zwrite.o: zwrite.c # dependencies for c files that include other c files fmtullong.o: fmtulong.c fmtumax.o: fmtulong.c strtoll.o: strtol.c strtoul.o: strtol.c strtoull.o: strtol.c # all files in the library depend on config.h clktck.o: ${BUILD_DIR}/config.h clock.o: ${BUILD_DIR}/config.h fmtullong.o: ${BUILD_DIR}/config.h fmtulong.o: ${BUILD_DIR}/config.h fmtumax.o: ${BUILD_DIR}/config.h getcwd.o: ${BUILD_DIR}/config.h getenv.o: ${BUILD_DIR}/config.h inet_aton.o: ${BUILD_DIR}/config.h itos.o: ${BUILD_DIR}/config.h mailstat.o: ${BUILD_DIR}/config.h makepath.o: ${BUILD_DIR}/config.h memset.o: ${BUILD_DIR}/config.h mktime.o: ${BUILD_DIR}/config.h netconn.o: ${BUILD_DIR}/config.h netopen.o: ${BUILD_DIR}/config.h oslib.o: ${BUILD_DIR}/config.h pathcanon.o: ${BUILD_DIR}/config.h pathphys.o: ${BUILD_DIR}/config.h rename.o: ${BUILD_DIR}/config.h setlinebuf.o: ${BUILD_DIR}/config.h shquote.o: ${BUILD_DIR}/config.h shtty.o: ${BUILD_DIR}/config.h snprintf.o: ${BUILD_DIR}/config.h spell.o: ${BUILD_DIR}/config.h strcasecmp.o: ${BUILD_DIR}/config.h strerror.o: ${BUILD_DIR}/config.h strftime.o: ${BUILD_DIR}/config.h strindex.o: ${BUILD_DIR}/config.h stringlist.o: ${BUILD_DIR}/config.h stringvec.o: ${BUILD_DIR}/config.h strpbrk.o: ${BUILD_DIR}/config.h strtod.o: ${BUILD_DIR}/config.h strtoimax.o: ${BUILD_DIR}/config.h strtol.o: ${BUILD_DIR}/config.h strtoll.o: ${BUILD_DIR}/config.h strtoul.o: ${BUILD_DIR}/config.h strtoull.o: ${BUILD_DIR}/config.h strtoumax.o: ${BUILD_DIR}/config.h strtrans.o: ${BUILD_DIR}/config.h times.o: ${BUILD_DIR}/config.h timeval.o: ${BUILD_DIR}/config.h tmpfile.o: ${BUILD_DIR}/config.h vprint.o: ${BUILD_DIR}/config.h xstrchr.o: ${BUILD_DIR}/config.h zcatfd.o: ${BUILD_DIR}/config.h zread.o: ${BUILD_DIR}/config.h zwrite.o: ${BUILD_DIR}/config.h clktck.o: ${topdir}/bashtypes.h getcwd.o: ${topdir}/bashtypes.h ${topdir}/bashansi.h ${BASHINCDIR}/maxpath.h getcwd.o: ${BASHINCDIR}/posixstat.h ${BASHINCDIR}/posixdir.h getcwd.o: ${BASHINCDIR}/memalloc.h ${BASHINCDIR}/ansi_stdlib.h getenv.o: ${topdir}/bashansi.h ${BASHINCDIR}/ansi_stdlib.h getenv.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h getenv.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h getenv.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h getenv.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h getenv.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h getenv.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h getenv.o: ${topdir}/pathnames.h ${topdir}/externs.h inet_aton.o: ${topdir}/bashansi.h ${BASHINCDIR}/ansi_stdlib.h inet_aton.o: ${BASHINCDIR}/stdc.h itos.o: ${topdir}/bashansi.h ${BASHINCDIR}/ansi_stdlib.h itos.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h itos.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h itos.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h itos.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h itos.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h itos.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h itos.o: ${topdir}/pathnames.h ${topdir}/externs.h makepath.o: ${topdir}/bashansi.h ${BASHINCDIR}/ansi_stdlib.h makepath.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h makepath.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h makepath.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h makepath.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h makepath.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h makepath.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h makepath.o: ${topdir}/pathnames.h ${topdir}/externs.h netconn.o: ${BASHINCDIR}/posixstat.h ${BASHINCDIR}/filecntl.h netconn.o: ${topdir}/bashtypes.h netopen.o: ${topdir}/bashansi.h ${BASHINCDIR}/ansi_stdlib.h ${topdir}/xmalloc.h netopen.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h netopen.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h netopen.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h netopen.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h netopen.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h netopen.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h netopen.o: ${topdir}/pathnames.h ${topdir}/externs.h oslib.o: ${topdir}/bashtypes.h ${topdir}/bashansi.h ${BASHINCDIR}/maxpath.h oslib.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h oslib.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h oslib.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h oslib.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h oslib.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h oslib.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h oslib.o: ${topdir}/pathnames.h ${topdir}/externs.h oslib.o: ${BASHINCDIR}/posixstat.h ${BASHINCDIR}/filecntl.h oslib.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h pathcanon.o: ${topdir}/bashtypes.h ${topdir}/bashansi.h ${BASHINCDIR}/maxpath.h pathcanon.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h pathcanon.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h pathcanon.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h pathcanon.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h pathcanon.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h pathcanon.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h pathcanon.o: ${topdir}/pathnames.h ${topdir}/externs.h pathcanon.o: ${BASHINCDIR}/posixstat.h ${BASHINCDIR}/filecntl.h pathcanon.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h pathphys.o: ${topdir}/bashtypes.h ${topdir}/bashansi.h ${BASHINCDIR}/maxpath.h pathphys.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h pathphys.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h pathphys.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h pathphys.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h pathphys.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h pathphys.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h pathphys.o: ${topdir}/pathnames.h ${topdir}/externs.h pathphys.o: ${BASHINCDIR}/posixstat.h ${BASHINCDIR}/filecntl.h pathphys.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h rename.o: ${topdir}/bashtypes.h ${BASHINCDIR}/stdc.h rename.o: ${BASHINCDIR}/posixstat.h setlinebuf.o: ${topdir}/xmalloc.h ${topdir}/bashansi.h setlinebuf.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/stdc.h shquote.o: ${BASHINCDIR}/stdc.h ${topdir}/bashansi.h shquote.o: ${BASHINCDIR}/ansi_stdlib.h ${topdir}/xmalloc.h shtty.o: ${BASHINCDIR}/shtty.h shtty.o: ${BASHINCDIR}/stdc.h snprintf.o: ${BASHINCDIR}/stdc.h ${topdir}/bashansi.h ${topdir}/xmalloc.h snprintf.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h snprintf.o: ${BASHINCDIR}/typemax.h spell.o: ${topdir}/bashtypes.h spell.o: ${BASHINCDIR}/posixstat.h ${BASHINCDIR}/posixdir.h spell.o: ${BASHINCDIR}/ansi_stdlib.h strcasecmp.o: ${BASHINCDIR}/stdc.h ${topdir}/bashansi.h strcasecmp.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h strerror.o: ${topdir}/bashtypes.h strerror.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h strerror.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h strerror.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h strerror.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h strerror.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h strerror.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h strerror.o: ${topdir}/pathnames.h ${topdir}/externs.h strindex.o: ${BASHINCDIR}/stdc.h ${topdir}/bashansi.h strindex.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h stringlist.o: ${topdir}/bashansi.h stringlist.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h stringlist.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h stringlist.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h stringlist.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h stringlist.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h stringlist.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h stringlist.o: ${topdir}/pathnames.h ${topdir}/externs.h stringvec.o: ${topdir}/bashansi.h ${BASHINCDIR}/chartypes.h stringvec.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h stringvec.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h stringvec.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h stringvec.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h stringvec.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h stringvec.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h stringvec.o: ${topdir}/pathnames.h ${topdir}/externs.h strpbrk.o: ${BASHINCDIR}/stdc.h strtod.o: ${topdir}/bashansi.h strtod.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h strtoimax.o: ${BASHINCDIR}/stdc.h strtol.o: ${topdir}/bashansi.h strtol.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h strtol.o: ${BASHINCDIR}/typemax.h strtoll.o: ${topdir}/bashansi.h strtoll.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h strtoll.o: ${BASHINCDIR}/typemax.h strtoul.o: ${topdir}/bashansi.h strtoul.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h strtoul.o: ${BASHINCDIR}/typemax.h strtoull.o: ${topdir}/bashansi.h strtoull.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h strtoull.o: ${BASHINCDIR}/typemax.h strtoumax.o: ${BASHINCDIR}/stdc.h strtrans.o: ${topdir}/bashansi.h strtrans.o: ${BASHINCDIR}/ansi_stdlib.h ${BASHINCDIR}/chartypes.h strtrans.o: ${topdir}/shell.h ${topdir}/syntax.h ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h strtrans.o: ${topdir}/command.h ${BASHINCDIR}/stdc.h ${topdir}/error.h strtrans.o: ${topdir}/general.h ${topdir}/bashtypes.h ${topdir}/variables.h ${topdir}/conftypes.h strtrans.o: ${topdir}/array.h ${topdir}/hashlib.h ${topdir}/quit.h strtrans.o: ${topdir}/unwind_prot.h ${topdir}/dispose_cmd.h strtrans.o: ${topdir}/make_cmd.h ${topdir}/subst.h ${topdir}/sig.h strtrans.o: ${topdir}/pathnames.h ${topdir}/externs.h times.o: ${BASHINCDIR}/systimes.h times.o: ${BASHINCDIR}/posixtime.h timeval.o: ${BASHINCDIR}/posixtime.h tmpfile.o: ${topdir}/bashtypes.h tmpfile.o: ${BASHINCDIR}/posixstat.h tmpfile.o: ${BASHINCDIR}/filecntl.h clock.o: ${BASHINCDIR}/posixtime.h mailstat.o: ${topdir}/bashansi.h mailstat.o: ${topdir}/bashtypes.h mailstat.o: ${BASHINCDIR}/ansi_stdlib.h mailstat.o: ${BASHINCDIR}/posixstat.h mailstat.o: ${BASHINCDIR}/posixdir.h mailstat.o: ${BASHINCDIR}/maxpath.h fmtulong.o: ${topdir}/bashansi.h fmtulong.o: ${BASHINCDIR}/ansi_stdlib.h fmtulong.o: ${BASHINCDIR}/chartypes.h fmtulong.o: ${BASHINCDIR}/stdc.h fmtulong.o: ${BASHINCDIR}/typemax.h fmtullong.o: ${topdir}/bashansi.h fmtullong.o: ${BASHINCDIR}/ansi_stdlib.h fmtullong.o: ${BASHINCDIR}/chartypes.h fmtullong.o: ${BASHINCDIR}/stdc.h fmtullong.o: ${BASHINCDIR}/typemax.h fmtumax.o: ${topdir}/bashansi.h fmtumax.o: ${BASHINCDIR}/ansi_stdlib.h fmtumax.o: ${BASHINCDIR}/chartypes.h fmtumax.o: ${BASHINCDIR}/stdc.h fmtumax.o: ${BASHINCDIR}/typemax.h xstrchr.o: ${topdir}/bashansi.h xstrchr.o: ${BASHINCDIR}/ansi_stdlib.h xstrchr.o: ${BASHINCDIR}/shmbutil.h --- NEW FILE: tmpfile.c --- /* * tmpfile.c - functions to create and safely open temp files for the shell. */ /* Copyright (C) 2000 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. Bash 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. Bash 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 Bash; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ #include <config.h> #include <bashtypes.h> #include <posixstat.h> #include <posixtime.h> #include <filecntl.h> #if defined (HAVE_UNISTD_H) # include <unistd.h> #endif #include <stdio.h> #include <errno.h> #include <shell.h> #ifndef errno extern int errno; #endif #define BASEOPENFLAGS (O_CREAT | O_TRUNC | O_EXCL) #define DEFAULT_TMPDIR "." /* bogus default, should be changed */ #define DEFAULT_NAMEROOT "shtmp" extern pid_t dollar_dollar_pid; static char *sys_tmpdir = (char *)NULL; static int ntmpfiles; static int tmpnamelen = -1; static unsigned long filenum = 1L; static char * get_sys_tmpdir () { struct stat sb; if (sys_tmpdir) return sys_tmpdir; #ifdef P_tmpdir sys_tmpdir = P_tmpdir; if (stat (sys_tmpdir, &sb) == 0) return sys_tmpdir; #endif sys_tmpdir = "/tmp"; if (stat (sys_tmpdir, &sb) == 0) return sys_tmpdir; sys_tmpdir = "/var/tmp"; if (stat (sys_tmpdir, &sb) == 0) return sys_tmpdir; sys_tmpdir = "/usr/tmp"; if (stat (sys_tmpdir, &sb) == 0) return sys_tmpdir; sys_tmpdir = DEFAULT_TMPDIR; return sys_tmpdir; } static char * get_tmpdir (flags) int flags; { char *tdir; tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL; if (tdir == 0) tdir = get_sys_tmpdir (); #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX) if (tmpnamelen == -1) tmpnamelen = pathconf (tdir, _PC_NAME_MAX); #else tmpnamelen = 0; #endif return tdir; } char * sh_mktmpname (nameroot, flags) char *nameroot; int flags; { char *filename, *tdir, *lroot; struct stat sb; int r, tdlen; filename = (char *)xmalloc (PATH_MAX + 1); tdir = get_tmpdir (flags); tdlen = strlen (tdir); lroot = nameroot ? nameroot : DEFAULT_NAMEROOT; #ifdef USE_MKTEMP sprintf (filename, "%s/%s.XXXXXX", tdir, lroot); if (mktemp (filename) == 0) { free (filename); filename = NULL; } #else /* !USE_MKTEMP */ while (1) { filenum = (filenum << 1) ^ (unsigned long) time ((time_t *)0) ^ (unsigned long) dollar_dollar_pid ^ (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++); sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum); if (tmpnamelen > 0 && tmpnamelen < 32) filename[tdlen + 1 + tmpnamelen] = '\0'; # ifdef HAVE_LSTAT r = lstat (filename, &sb); # else r = stat (filename, &sb); # endif if (r < 0 && errno == ENOENT) break; } #endif /* !USE_MKTEMP */ return filename; } int sh_mktmpfd (nameroot, flags, namep) char *nameroot; int flags; char **namep; { char *filename, *tdir, *lroot; int fd, tdlen; filename = (char *)xmalloc (PATH_MAX + 1); tdir = get_tmpdir (flags); tdlen = strlen (tdir); lroot = nameroot ? nameroot : DEFAULT_NAMEROOT; #ifdef USE_MKSTEMP sprintf (filename, "%s/%s.XXXXXX", tdir, lroot); fd = mkstemp (filename); if (fd < 0 || namep == 0) { free (filename); filename = NULL; } if (namep) *namep = filename; return fd; #else /* !USE_MKSTEMP */ do { filenum = (filenum << 1) ^ (unsigned long) time ((time_t *)0) ^ (unsigned long) dollar_dollar_pid ^ (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++); sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum); if (tmpnamelen > 0 && tmpnamelen < 32) filename[tdlen + 1 + tmpnamelen] = '\0'; fd = open (filename, BASEOPENFLAGS | ((flags & MT_READWRITE) ? O_RDWR : O_WRONLY), 0600); } while (fd < 0 && errno == EEXIST); if (namep) *namep = filename; else free (filename); return fd; #endif /* !USE_MKSTEMP */ } FILE * sh_mktmpfp (nameroot, flags, namep) char *nameroot; int flags; char **namep; { int fd; FILE *fp; fd = sh_mktmpfd (nameroot, flags, namep); if (fd < 0) return ((FILE *)NULL); fp = fdopen (fd, (flags & MT_READWRITE) ? "w+" : "w"); if (fp == 0) close (fd); return fp; } --- NEW FILE: vprint.c --- /* vprint.c -- v[fs]printf() for 4.[23] BSD systems. */ /* Copyright (C) 1987,1989 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. Bash 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. Bash 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 Bash; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ #include <config.h> #if defined (USE_VFPRINTF_EMULATION) #include <stdio.h> #if !defined (NULL) # if defined (__STDC__) # define NULL ((void *)0) # else # define NULL 0x0 # endif /* __STDC__ */ #endif /* !NULL */ /* * Beware! Don't trust the value returned by either of these functions; it * seems that pre-4.3-tahoe implementations of _doprnt () return the first * argument, i.e. a char *. */ #include <varargs.h> int vfprintf (iop, fmt, ap) FILE *iop; char *fmt; va_list ap; { int len; char localbuf[BUFSIZ]; if (iop->_flag & _IONBF) { iop->_flag &= ~_IONBF; iop->_ptr = iop->_base = localbuf; len = _doprnt (fmt, ap, iop); (void) fflush (iop); iop->_flag |= _IONBF; iop->_base = NULL; iop->_bufsiz = 0; iop->_cnt = 0; } else len = _doprnt (fmt, ap, iop); return (ferror (iop) ? EOF : len); } /* * Ditto for vsprintf */ int vsprintf (str, fmt, ap) char *str, *fmt; va_list ap; { FILE f; int len; f._flag = _IOWRT|_IOSTRG; f._ptr = str; f._cnt = 32767; len = _doprnt (fmt, ap, &f); *f._ptr = 0; return (len); } #endif /* USE_VFPRINTF_EMULATION */ --- NEW FILE: zread.c --- /* Copyright (C) 1999-2002 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. Bash 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. Bash 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 Bash; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ #include <config.h> #include <sys/types.h> #if defined (HAVE_UNISTD_H) # include <unistd.h> #endif #include <errno.h> #if !defined (errno) extern int errno; #endif #ifndef SEEK_CUR # define SEEK_CUR 1 #endif /* Read LEN bytes from FD into BUF. Retry the read on EINTR. Any other error causes the loop to break. */ ssize_t zread (fd, buf, len) int fd; char *buf; size_t len; { ssize_t r; while ((r = read (fd, buf, len)) < 0 && errno == EINTR) ; return r; } /* Read LEN bytes from FD into BUF. Retry the read on EINTR, up to three interrupts. Any other error causes the loop to break. */ #ifdef NUM_INTR # undef NUM_INTR #endif #define NUM_INTR 3 ssize_t zreadintr (fd, buf, len) int fd; char *buf; size_t len; { ssize_t r; int nintr; for (nintr = 0; ; ) { r = read (fd, buf, len); if (r >= 0) return r; if (r == -1 && errno == EINTR) { if (++nintr > NUM_INTR) return -1; continue; } return r; } } /* Read one character from FD and return it in CP. Return values are as in read(2). This does some local buffering to avoid many one-character calls to read(2), like those the `read' builtin performs. */ static char lbuf[128]; static size_t lind, lused; ssize_t zreadc (fd, cp) int fd; char *cp; { ssize_t nr; if (lind == lused || lused == 0) { nr = zread (fd, lbuf, sizeof (lbuf)); lind = 0; if (nr <= 0) { lused = 0; return nr; } lused = nr; } if (cp) *cp = lbuf[lind++]; return 1; } void zreset () { lind = lused = 0; } /* Sync the seek pointer for FD so that the kernel's idea of the last char read is the last char returned by zreadc. */ void zsyncfd (fd) int fd; { off_t off; off = lused - lind; if (off > 0) lseek (fd, -off, SEEK_CUR); lused = lind = 0; } --- NEW FILE: strftime.c --- /* * Modified slightly by Chet Ramey for inclusion in Bash */ /* * strftime.c * * Public-domain implementation of ISO C library routine. * * If you can't do prototypes, get GCC. * * The C99 standard now specifies just about all of the formats * that were additional in the earlier versions of this file. * * For extensions from SunOS, add SUNOS_EXT. * For extensions from HP/UX, add HPUX_EXT. * For VMS dates, add VMS_EXT. * For complete POSIX semantics, add POSIX_SEMANTICS. * * The code for %c, %x, and %X follows the C99 specification for * the "C" locale. * * This version ignores LOCALE information. * It also doesn't worry about multi-byte characters. * So there. * * This file is also shipped with GAWK (GNU Awk), gawk specific bits of * code are included if GAWK is defined. * * Arnold Robbins * January, February, March, 1991 * Updated March, April 1992 * Updated April, 1993 * Updated February, 1994 * Updated May, 1994 * Updated January, 1995 * Updated September, 1995 * Updated January, 1996 * Updated July, 1997 * Updated October, 1999 * Updated September, 2000 * * Fixes from ad...@el..., * February 1991, May 1992 * Fixes from Tor Lillqvist tm...@ti..., * May 1993 * Further fixes from ad...@el..., * February 1994 * %z code from ch...@ch..., * Applied September 1995 * %V code fixed (again) and %G, %g added, * January 1996 * %v code fixed, better configuration, * July 1997 * Moved to C99 specification. * September 2000 */ #include <config.h> #ifndef GAWK #include <stdio.h> #include <ctype.h> #include <time.h> #endif #if defined(TM_IN_SYS_TIME) #include <sys/types.h> #include <sys/time.h> #endif #include <stdlib.h> #include <string.h> /* defaults: season to taste */ #define SUNOS_EXT 1 /* stuff in SunOS strftime routine */ #define VMS_EXT 1 /* include %v for VMS date format */ #define HPUX_EXT 1 /* non-conflicting stuff in HP-UX date */ #ifndef GAWK #define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */ #endif #undef strchr /* avoid AIX weirdness */ extern void tzset(void); static int weeknumber(const struct tm *timeptr, int firstweekday); static int iso8601wknum(const struct tm *timeptr); #ifdef __GNUC__ #define inline __inline__ #else #define inline /**/ #endif #define range(low, item, hi) max(low, min(item, hi)) #if !defined(OS2) && !defined(MSDOS) && defined(HAVE_TZNAME) extern char *tzname[2]; extern int daylight; #if defined(SOLARIS) || defined(mips) extern long int timezone, altzone; #else extern int timezone, altzone; #endif #endif #undef min /* just in case */ /* min --- return minimum of two numbers */ static inline int min(int a, int b) { return (a < b ? a : b); } #undef max /* also, just in case */ /* max --- return maximum of two numbers */ static inline int max(int a, int b) { return (a > b ? a : b); } /* strftime --- produce formatted time */ size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr) { char *endp = s + maxsize; char *start = s; auto char tbuf[100]; long off; int i, w, y; static short first = 1; #ifdef POSIX_SEMANTICS static char *savetz = NULL; static int savetzlen = 0; char *tz; #endif /* POSIX_SEMANTICS */ #ifndef HAVE_TM_ZONE #ifndef HAVE_TM_NAME #ifndef HAVE_TZNAME extern char *timezone(); struct timeval tv; struct timezone zone; #endif /* HAVE_TZNAME */ #endif /* HAVE_TM_NAME */ #endif /* HAVE_TM_ZONE */ /* various tables, useful in North America */ static const char *days_a[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", }; static const char *days_l[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", }; static const char *months_a[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; static const char *months_l[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", }; static const char *ampm[] = { "AM", "PM", }; if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0) return 0; /* quick check if we even need to bother */ if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) return 0; #ifndef POSIX_SEMANTICS if (first) { tzset(); first = 0; } #else /* POSIX_SEMANTICS */ #if defined (SHELL) tz = get_string_value ("TZ"); #else tz = getenv("TZ"); #endif if (first) { if (tz != NULL) { int tzlen = strlen(tz); savetz = (char *) malloc(tzlen + 1); if (savetz != NULL) { savetzlen = tzlen + 1; strcpy(savetz, tz); } } tzset(); first = 0; } /* if we have a saved TZ, and it is different, recapture and reset */ if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) { i = strlen(tz) + 1; if (i > savetzlen) { savetz = (char *) realloc(savetz, i); if (savetz) { savetzlen = i; strcpy(savetz, tz); } } else strcpy(savetz, tz); tzset(); } #endif /* POSIX_SEMANTICS */ for (; *format && s < endp - 1; format++) { tbuf[0] = '\0'; if (*format != '%') { *s++ = *format; continue; } again: switch (*++format) { case '\0': *s++ = '%'; goto out; case '%': *s++ = '%'; continue; case 'a': /* abbreviated weekday name */ if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6) strcpy(tbuf, "?"); else strcpy(tbuf, days_a[timeptr->tm_wday]); break; case 'A': /* full weekday name */ if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6) strcpy(tbuf, "?"); else strcpy(tbuf, days_l[timeptr->tm_wday]); break; case 'b': /* abbreviated month name */ short_month: if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11) strcpy(tbuf, "?"); else strcpy(tbuf, months_a[timeptr->tm_mon]); break; case 'B': /* full month name */ if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11) strcpy(tbuf, "?"); else strcpy(tbuf, months_l[timeptr->tm_mon]); break; case 'c': /* appropriate date and time representation */ /* * This used to be: * * strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr); * * Now, per the ISO 1999 C standard, it this: */ strftime(tbuf, sizeof tbuf, "%A %B %d %T %Y", timeptr); break; case 'C': century: sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100); break; case 'd': /* day of the month, 01 - 31 */ i = range(1, timeptr->tm_mday, 31); sprintf(tbuf, "%02d", i); break; case 'D': /* date as %m/%d/%y */ strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr); break; case 'e': /* day of month, blank padded */ sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31)); break; case 'E': /* POSIX (now C99) locale extensions, ignored for now */ goto again; case 'F': /* ISO 8601 date representation */ strftime(tbuf, sizeof tbuf, "%Y-%m-%d", timeptr); break; case 'g': case 'G': /* * Year of ISO week. * * If it's December but the ISO week number is one, * that week is in next year. * If it's January but the ISO week number is 52 or * 53, that week is in last year. * Otherwise, it's this year. */ w = iso8601wknum(timeptr); if (timeptr->tm_mon == 11 && w == 1) y = 1900 + timeptr->tm_year + 1; else if (timeptr->tm_mon == 0 && w >= 52) y = 1900 + timeptr->tm_year - 1; else y = 1900 + timeptr->tm_year; if (*format == 'G') sprintf(tbuf, "%d", y); else sprintf(tbuf, "%02d", y % 100); break; case 'h': /* abbreviated month name */ goto short_month; case 'H': /* hour, 24-hour clock, 00 - 23 */ i = range(0, timeptr->tm_hour, 23); sprintf(tbuf, "%02d", i); break; case 'I': /* hour, 12-hour clock, 01 - 12 */ i = range(0, timeptr->tm_hour, 23); if (i == 0) i = 12; else if (i > 12) i -= 12; sprintf(tbuf, "%02d", i); break; case 'j': /* day of the year, 001 - 366 */ sprintf(tbuf, "%03d", timeptr->tm_yday + 1); break; case 'm': /* month, 01 - 12 */ i = range(0, timeptr->tm_mon, 11); sprintf(tbuf, "%02d", i + 1); break; case 'M': /* minute, 00 - 59 */ i = range(0, timeptr->tm_min, 59); sprintf(tbuf, "%02d", i); break; case 'n': /* same as \n */ tbuf[0] = '\n'; tbuf[1] = '\0'; break; case 'O': /* POSIX (now C99) locale extensions, ignored for now */ goto again; case 'p': /* am or pm based on 12-hour clock */ i = range(0, timeptr->tm_hour, 23); if (i < 12) strcpy(tbuf, ampm[0]); else strcpy(tbuf, ampm[1]); break; case 'r': /* time as %I:%M:%S %p */ strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr); break; case 'R': /* time as %H:%M */ strftime(tbuf, sizeof tbuf, "%H:%M", timeptr); break; #if defined(HAVE_MKTIME) || defined(GAWK) case 's': /* time as seconds since the Epoch */ { struct tm non_const_timeptr; non_const_timeptr = *timeptr; sprintf(tbuf, "%ld", mktime(& non_const_timeptr)); break; } #endif /* defined(HAVE_MKTIME) || defined(GAWK) */ case 'S': /* second, 00 - 60 */ i = range(0, timeptr->tm_sec, 60); sprintf(tbuf, "%02d", i); break; case 't': /* same as \t */ tbuf[0] = '\t'; tbuf[1] = '\0'; break; case 'T': /* time as %H:%M:%S */ the_time: strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr); break; case 'u': /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */ sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 : timeptr->tm_wday); break; case 'U': /* week of year, Sunday is first day of week */ sprintf(tbuf, "%02d", weeknumber(timeptr, 0)); break; case 'V': /* week of year according ISO 8601 */ sprintf(tbuf, "%02d", iso8601wknum(timeptr)); break; case 'w': /* weekday, Sunday == 0, 0 - 6 */ i = range(0, timeptr->tm_wday, 6); sprintf(tbuf, "%d", i); break; case 'W': /* week of year, Monday is first day of week */ sprintf(tbuf, "%02d", weeknumber(timeptr, 1)); break; case 'x': /* appropriate date representation */ strftime(tbuf, sizeof tbuf, "%A %B %d %Y", timeptr); break; case 'X': /* appropriate time representation */ goto the_time; break; case 'y': /* year without a century, 00 - 99 */ year: i = timeptr->tm_year % 100; sprintf(tbuf, "%02d", i); break; case 'Y': /* year with century */ fullyear: sprintf(tbuf, "%d", 1900 + timeptr->tm_year); break; /* * From: Chip Rosenthal <ch...@ch...> * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST) * * Warning: the %z [code] is implemented by inspecting the * timezone name conditional compile settings, and * inferring a method to get timezone offsets. I've tried * this code on a couple of machines, but I don't doubt * there is some system out there that won't like it. * Maybe the easiest thing to do would be to bracket this * with an #ifdef that can turn it off. The %z feature * would be an admittedly obscure one that most folks can * live without, but it would be a great help to those of * us that muck around with various message processors. */ case 'z': /* time zone offset east of GMT e.g. -0600 */ #ifdef HAVE_TM_NAME /* * Systems with tm_name probably have tm_tzadj as * secs west of GMT. Convert to mins east of GMT. */ off = -timeptr->tm_tzadj / 60; #else /* !HAVE_TM_NAME */ #ifdef HAVE_TM_ZONE /* * Systems with tm_zone probably have tm_gmtoff as * secs east of GMT. Convert to mins east of GMT. */ off = timeptr->tm_gmtoff / 60; #else /* !HAVE_TM_ZONE */ #if HAVE_TZNAME /* * Systems with tzname[] probably have timezone as * secs west of GMT. Convert to mins east of GMT. */ off = -(daylight ? timezone : altzone) / 60; #else /* !HAVE_TZNAME */ off = -zone.tz_minuteswest; #endif /* !HAVE_TZNAME */ #endif /* !HAVE_TM_ZONE */ #endif /* !HAVE_TM_NAME */ if (off < 0) { tbuf[0] = '-'; off = -off; } else { tbuf[0] = '+'; } sprintf(tbuf+1, "%02d%02d", off/60, off%60); break; case 'Z': /* time zone name or abbrevation */ #ifdef HAVE_TZNAME i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */ strcpy(tbuf, tzname[i]); #else #ifdef HAVE_TM_ZONE strcpy(tbuf, timeptr->tm_zone); #else #ifdef HAVE_TM_NAME strcpy(tbuf, timeptr->tm_name); #else gettimeofday(& tv, & zone); strcpy(tbuf, timezone(zone.tz_minuteswest, timeptr->tm_isdst > 0)); #endif /* HAVE_TM_NAME */ #endif /* HAVE_TM_ZONE */ #endif /* HAVE_TZNAME */ break; #ifdef SUNOS_EXT case 'k': /* hour, 24-hour clock, blank pad */ sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23)); break; case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */ i = range(0, timeptr->tm_hour, 23); if (i == 0) i = 12; else if (i > 12) i -= 12; sprintf(tbuf, "%2d", i); break; #endif #ifdef HPUX_EXT case 'N': /* Emperor/Era name */ /* this is essentially the same as the century */ goto century; /* %C */ case 'o': /* Emperor/Era year */ goto year; /* %y */ #endif /* HPUX_EXT */ #ifdef VMS_EXT case 'v': /* date as dd-bbb-YYYY */ sprintf(tbuf, "%2d-%3.3s-%4d", range(1, timeptr->tm_mday, 31), months_a[range(0, timeptr->tm_mon, 11)], timeptr->tm_year + 1900); for (i = 3; i < 6; i++) if (islower(tbuf[i])) tbuf[i] = toupper(tbuf[i]); break; #endif default: tbuf[0] = '%'; tbuf[1] = *format; tbuf[2] = '\0'; break; } i = strlen(tbuf); if (i) { if (s + i < endp - 1) { strcpy(s, tbuf); s += i; } else return 0; } } out: if (s < endp && *format == '\0') { *s = '\0'; return (s - start); } else return 0; } /* isleap --- is a year a leap year? */ static int isleap(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } /* iso8601wknum --- compute week number according to ISO 8601 */ static int iso8601wknum(const struct tm *timeptr) { /* * From 1003.2: * If the week (Monday to Sunday) containing January 1 * has four or more days in the new year, then it is week 1; * otherwise it is the highest numbered week of the previous * year (52 or 53), and the next week is week 1. * * ADR: This means if Jan 1 was Monday through Thursday, * it was week 1, otherwise week 52 or 53. * * XPG4 erroneously included POSIX.2 rationale text in the * main body of the standard. Thus it requires week 53. */ int weeknum, jan1day, diff; /* get week number, Monday as first day of the week */ weeknum = weeknumber(timeptr, 1); /* * With thanks and tip of the hatlo to tm...@ti... * * What day of the week does January 1 fall on? * We know that * (timeptr->tm_yday - jan1.tm_yday) MOD 7 == * (timeptr->tm_wday - jan1.tm_wday) MOD 7 * and that * jan1.tm_yday == 0 * and that * timeptr->tm_wday MOD 7 == timeptr->tm_wday * from which it follows that. . . */ jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7); if (jan1day < 0) jan1day += 7; /* * If Jan 1 was a Monday through Thursday, it was in * week 1. Otherwise it was last year's highest week, which is * this year's week 0. * * What does that mean? * If Jan 1 was Monday, the week number is exactly right, it can * never be 0. * If it was Tuesday through Thursday, the weeknumber is one * less than it should be, so we add one. * Otherwise, Friday, Saturday or Sunday, the week number is * OK, but if it is 0, it needs to be 52 or 53. */ switch (jan1day) { case 1: /* Monday */ break; case 2: /* Tuesday */ case 3: /* Wednesday */ case 4: /* Thursday */ weeknum++; break; case 5: /* Friday */ case 6: /* Saturday */ case 0: /* Sunday */ if (weeknum == 0) { #ifdef USE... [truncated message content] |