From 1ff9922df0f6e5f129299847d0f5eccbc8d6806c Mon Sep 17 00:00:00 2001 From: Chris Johns Date: Sat, 22 Dec 2007 08:27:18 +0000 Subject: 2007-12-22 Chris Johns * configure.ac: fixed bug that always enabled strict order mutexes. * score/inline/rtems/score/coremutex.inl: Fixed coding standard. * score/src/coremutex.c: Add the holder's thread to the lock_mutex list if the mutex is initialised locked. * libnetworking/rtems/rtems_glue.c: Changed semaphore error message to show the error is an rtems-net error. * libmisc/monitor/mon-network.c: Removed warnings. * telnetd/icmds.c: Changed shell_* to rtems_shell_*. * score/Makefile.am: Fixed typo that stopped 'make tags' working. * libmisc/shell/err.c, libmisc/shell/err.h, libmisc/shell/errx.c, libmisc/shell/extern-cp.h, libmisc/shell/fts.c, libmisc/shell/fts.h, libmisc/shell/main_cp.c, libmisc/shell/utils-cp.c, libmisc/shell/verr.c, libmisc/shell/verrx.c, libmisc/shell/vwarn.c, libmisc/shell/vwarnx.c, libmisc/shell/warn.c, libmisc/shell/warnx.c: New. Ported from BSD. * libmisc/shell/shellconfig.h: Add the cp command. * libmisc/Makefile.am: Add the new files to the shell. * libmisc/shell/shell.c, libmisc/shell/shell.h: Add scripting support. * libblock/src/flashdisk.c: Fixed disk drive count size setting bug. --- cpukit/ChangeLog | 26 + cpukit/configure.ac | 2 +- cpukit/libblock/src/flashdisk.c | 10 +- cpukit/libmisc/Makefile.am | 8 +- cpukit/libmisc/monitor/mon-network.c | 5 +- cpukit/libmisc/shell/err.c | 60 ++ cpukit/libmisc/shell/err.h | 84 ++ cpukit/libmisc/shell/errx.c | 60 ++ cpukit/libmisc/shell/extern-cp.h | 87 ++ cpukit/libmisc/shell/fts.c | 1193 +++++++++++++++++++++++++ cpukit/libmisc/shell/fts.h | 146 +++ cpukit/libmisc/shell/main_cp.c | 531 +++++++++++ cpukit/libmisc/shell/main_mount_nfs.c | 10 +- cpukit/libmisc/shell/shell.c | 253 ++++-- cpukit/libmisc/shell/shell.h | 27 + cpukit/libmisc/shell/shellconfig.h | 6 + cpukit/libmisc/shell/utils-cp.c | 350 ++++++++ cpukit/libmisc/shell/verr.c | 71 ++ cpukit/libmisc/shell/verrx.c | 64 ++ cpukit/libmisc/shell/vwarn.c | 70 ++ cpukit/libmisc/shell/vwarnx.c | 63 ++ cpukit/libmisc/shell/warn.c | 60 ++ cpukit/libmisc/shell/warnx.c | 60 ++ cpukit/libnetworking/rtems/rtems_glue.c | 11 +- cpukit/score/Makefile.am | 2 +- cpukit/score/inline/rtems/score/coremutex.inl | 3 +- cpukit/score/src/coremutex.c | 7 + cpukit/telnetd/icmds.c | 14 +- 28 files changed, 3192 insertions(+), 91 deletions(-) create mode 100644 cpukit/libmisc/shell/err.c create mode 100644 cpukit/libmisc/shell/err.h create mode 100644 cpukit/libmisc/shell/errx.c create mode 100644 cpukit/libmisc/shell/extern-cp.h create mode 100644 cpukit/libmisc/shell/fts.c create mode 100644 cpukit/libmisc/shell/fts.h create mode 100644 cpukit/libmisc/shell/main_cp.c create mode 100644 cpukit/libmisc/shell/utils-cp.c create mode 100644 cpukit/libmisc/shell/verr.c create mode 100644 cpukit/libmisc/shell/verrx.c create mode 100644 cpukit/libmisc/shell/vwarn.c create mode 100644 cpukit/libmisc/shell/vwarnx.c create mode 100644 cpukit/libmisc/shell/warn.c create mode 100644 cpukit/libmisc/shell/warnx.c (limited to 'cpukit') diff --git a/cpukit/ChangeLog b/cpukit/ChangeLog index 89e64c1f44..974030b991 100644 --- a/cpukit/ChangeLog +++ b/cpukit/ChangeLog @@ -1,3 +1,29 @@ +2007-12-22 Chris Johns + + * configure.ac: fixed bug that always enabled strict order + mutexes. + * score/inline/rtems/score/coremutex.inl: Fixed coding standard. + * score/src/coremutex.c: Add the holder's thread to the lock_mutex + list if the mutex is initialised locked. + * libnetworking/rtems/rtems_glue.c: Changed semaphore error + message to show the error is an rtems-net error. + * libmisc/monitor/mon-network.c: Removed warnings. + * telnetd/icmds.c: Changed shell_* to rtems_shell_*. + * score/Makefile.am: Fixed typo that stopped 'make tags' working. + * libmisc/shell/err.c, libmisc/shell/err.h, libmisc/shell/errx.c, + libmisc/shell/extern-cp.h, libmisc/shell/fts.c, + libmisc/shell/fts.h, libmisc/shell/main_cp.c, + libmisc/shell/utils-cp.c, libmisc/shell/verr.c, + libmisc/shell/verrx.c, libmisc/shell/vwarn.c, + libmisc/shell/vwarnx.c, libmisc/shell/warn.c, + libmisc/shell/warnx.c: New. Ported from BSD. + * libmisc/shell/shellconfig.h: Add the cp command. + * libmisc/Makefile.am: Add the new files to the shell. + * libmisc/shell/shell.c, libmisc/shell/shell.h: Add scripting + support. + * libblock/src/flashdisk.c: Fixed disk drive count size setting + bug. + 2007-12-21 Xi Yang * rtems/src/semtranslatereturncode.c: Add support for proper stacking diff --git a/cpukit/configure.ac b/cpukit/configure.ac index f2faabf915..c7b8545505 100644 --- a/cpukit/configure.ac +++ b/cpukit/configure.ac @@ -234,7 +234,7 @@ RTEMS_CPUOPT([__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__], ## This gives the same behavior as 4.8 and older RTEMS_CPUOPT([__STRICT_ORDER_MUTEX__], - [test x"${ENABLE_STRICT_ORDER_MUTEX}"=x"1"], + [test x"${ENABLE_STRICT_ORDER_MUTEX}"= x"1"], [1], [disable strict order mutex] ) diff --git a/cpukit/libblock/src/flashdisk.c b/cpukit/libblock/src/flashdisk.c index 94cbdb38e2..58bdabae8c 100644 --- a/cpukit/libblock/src/flashdisk.c +++ b/cpukit/libblock/src/flashdisk.c @@ -2002,7 +2002,7 @@ rtems_fdisk_read (rtems_flashdisk* fd, blkdev_request* req) /** * Flash disk WRITE request handler. This primitive copies data from - * supplied buffer to RAM disk and invoke the callout function to inform + * supplied buffer to flash disk and invoke the callout function to inform * upper layer that writing is completed. * * @param req Pointers to the WRITE block device request info. @@ -2340,6 +2340,8 @@ rtems_fdisk_ioctl (dev_t dev, uint32_t req, void* argp) /** * Flash disk device driver initialization. * + * @todo Memory clean up on error is really badly handled. + * * @param major Flash disk major device number. * @param minor Minor device number, not applicable. * @param arg Initialization argument, not applicable. @@ -2419,7 +2421,7 @@ rtems_fdisk_initialize (rtems_device_major_number major, if (!fd->blocks) return RTEMS_NO_MEMORY; - fd->block_count = blocks; + fd->block_count = blocks; fd->devices = calloc (c->device_count, sizeof (rtems_fdisk_device_ctl)); if (!fd->devices) @@ -2471,9 +2473,9 @@ rtems_fdisk_initialize (rtems_device_major_number major, if (ret) rtems_fdisk_error ("compacting of disk failed: %s (%d)", strerror (ret), ret); - - rtems_flashdisk_count = rtems_flashdisk_configuration_size; } + rtems_flashdisk_count = rtems_flashdisk_configuration_size; + return RTEMS_SUCCESSFUL; } diff --git a/cpukit/libmisc/Makefile.am b/cpukit/libmisc/Makefile.am index 19679c53d4..53c6e63964 100644 --- a/cpukit/libmisc/Makefile.am +++ b/cpukit/libmisc/Makefile.am @@ -68,10 +68,11 @@ if LIBNETWORKING shell_mount_fs = shell/main_mount_ftp.c shell/main_mount_nfs.c \ shell/main_mount_tftp.c endif +libshell_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shell libshell_a_SOURCES = shell/cat_file.c shell/cmds.c shell/internal.h \ shell/main_alias.c shell/main_cat.c shell/main_cd.c \ shell/main_chdir.c shell/main_chmod.c shell/main_chroot.c \ - shell/main_cpuuse.c shell/main_date.c shell/main_dir.c \ + shell/main_cp.c shell/main_cpuuse.c shell/main_date.c shell/main_dir.c \ shell/main_exit.c shell/main_help.c shell/main_id.c \ shell/main_logoff.c shell/main_ls.c shell/main_mallocinfo.c \ shell/main_mdump.c shell/main_medit.c shell/main_mfill.c \ @@ -82,7 +83,10 @@ libshell_a_SOURCES = shell/cat_file.c shell/cmds.c shell/internal.h \ shell/main_tty.c shell/main_umask.c shell/main_unmount.c \ shell/main_blksync.c shell/main_whoami.c shell/shell.c \ shell/shell_cmdset.c shell/shellconfig.c shell/shellconfig.h \ - shell/shell.h shell/shell_makeargs.c shell/str2int.c shell/write_file.c + shell/shell.h shell/shell_makeargs.c shell/str2int.c shell/write_file.c \ + shell/utils-cp.c shell/err.c shell/errx.c shell/verr.c shell/verrx.c \ + shell/vwarn.c shell/vwarnx.c shell/warn.c shell/warnx.c \ + shell/fts.c endif EXTRA_DIST += shell/README diff --git a/cpukit/libmisc/monitor/mon-network.c b/cpukit/libmisc/monitor/mon-network.c index 7aff60f539..fd53682e85 100644 --- a/cpukit/libmisc/monitor/mon-network.c +++ b/cpukit/libmisc/monitor/mon-network.c @@ -326,7 +326,10 @@ void mon_route(int argc, char *argv[], uint32_t command_arg, flags |= RTF_HOST; } - rc = rtems_bsdnet_rtrequest(cmd, &dst, &gw, &netmask, flags, NULL); + rc = rtems_bsdnet_rtrequest(cmd, + (struct sockaddr*) &dst, + (struct sockaddr*) &gw, + (struct sockaddr*) &netmask, flags, NULL); if (rc < 0) { printf("Error adding route\n"); } diff --git a/cpukit/libmisc/shell/err.c b/cpukit/libmisc/shell/err.c new file mode 100644 index 0000000000..e5b7156d43 --- /dev/null +++ b/cpukit/libmisc/shell/err.c @@ -0,0 +1,60 @@ +/* $NetBSD: err.c,v 1.25 2005/09/13 13:51:50 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: err.c,v 1.25 2005/09/13 13:51:50 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#ifdef __weak_alias +__weak_alias(err, _err) +#endif + +__dead void +err(jmp_buf* exit_jmp, int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verr(exit_jmp, eval, fmt, ap); + va_end(ap); +} diff --git a/cpukit/libmisc/shell/err.h b/cpukit/libmisc/shell/err.h new file mode 100644 index 0000000000..f028d3b151 --- /dev/null +++ b/cpukit/libmisc/shell/err.h @@ -0,0 +1,84 @@ +/* $NetBSD: err.h,v 1.14 2005/02/03 04:39:32 perry Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)err.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _ERR_H_ +#define _ERR_H_ + +/* + * Don't use va_list in the err/warn prototypes. Va_list is typedef'd in two + * places ( and ), so if we include one + * of them here we may collide with the utility's includes. It's unreasonable + * for utilities to have to include one of them to include err.h, so we get + * _BSD_VA_LIST_ from and use it. + */ +#include +#include + +#include +#define _BSD_VA_LIST_ va_list + +#define __dead + +#define err rtems_shell_err +#define verr rtems_shell_verr +#define errx rtems_shell_errx +#define verrx rtems_shell_verrx +#define warn rtems_shell_warn +#define vwarn rtems_shell_vwarn +#define warnx rtems_shell_warnx +#define vwarnx rtems_shell_vwarnx + +#include + +extern jmp_buf rtems_shell_bsd_exit_recover; + +__BEGIN_DECLS +__dead void err(jmp_buf*, int, const char *, ...) + __attribute__((__noreturn__, __format__(__printf__, 3, 4))); +__dead void verr(jmp_buf*, int, const char *, _BSD_VA_LIST_) + __attribute__((__noreturn__, __format__(__printf__, 3, 0))); +__dead void errx(jmp_buf*, int, const char *, ...) + __attribute__((__noreturn__, __format__(__printf__, 3, 4))); +__dead void verrx(jmp_buf*, int, const char *, _BSD_VA_LIST_) + __attribute__((__noreturn__, __format__(__printf__, 3, 0))); +void warn(const char *, ...) + __attribute__((__format__(__printf__, 1, 2))); +void vwarn(const char *, _BSD_VA_LIST_) + __attribute__((__format__(__printf__, 1, 0))); +void warnx(const char *, ...) + __attribute__((__format__(__printf__, 1, 2))); +void vwarnx(const char *, _BSD_VA_LIST_) + __attribute__((__format__(__printf__, 1, 0))); +__END_DECLS + +#endif /* !_ERR_H_ */ diff --git a/cpukit/libmisc/shell/errx.c b/cpukit/libmisc/shell/errx.c new file mode 100644 index 0000000000..f20d9d0bf4 --- /dev/null +++ b/cpukit/libmisc/shell/errx.c @@ -0,0 +1,60 @@ +/* $NetBSD: errx.c,v 1.13 2005/09/13 13:51:50 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: errx.c,v 1.13 2005/09/13 13:51:50 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#ifdef __weak_alias +__weak_alias(errx, _errx) +#endif + +__dead void +errx(jmp_buf* exit_jmp, int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verrx(exit_jmp, eval, fmt, ap); + va_end(ap); +} diff --git a/cpukit/libmisc/shell/extern-cp.h b/cpukit/libmisc/shell/extern-cp.h new file mode 100644 index 0000000000..614142aa9a --- /dev/null +++ b/cpukit/libmisc/shell/extern-cp.h @@ -0,0 +1,87 @@ +/* $NetBSD: extern.h,v 1.12 2005/10/15 18:22:18 christos Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)extern.h 8.2 (Berkeley) 4/1/94 + */ + +#ifndef _EXTERN_CP_H_ +#define _EXTERN_CP_H_ + +#define PATH_T RTEMS_SHELL_CP_PATH_T + +typedef struct { + char *p_end; /* pointer to NULL at end of path */ + char *target_end; /* pointer to end of target base */ + char p_path[MAXPATHLEN + 1]; /* pointer to the start of a path */ +} PATH_T; + +#include + +typedef struct { + PATH_T to; + uid_t myuid; + int Rflag, fflag, iflag, pflag, rflag, vflag, Nflag; + mode_t myumask; + jmp_buf exit_jmp; +} rtems_shell_cp_globals; + +#define to cp_globals->to +#define myuid cp_globals->myuid +#define Rflag cp_globals->Rflag +#define fflag cp_globals->fflag +#define iflag cp_globals->iflag +#define pflag cp_globals->pflag +#define rflag cp_globals->rflag +#define vflag cp_globals->vflag +#define Nflag cp_globals->Nflag +#define myumask cp_globals->myumask +#define exit_jump &(cp_globals->exit_jmp) + +#define copy_fifo rtems_shell_cp_copy_fifo +#define copy_file rtems_shell_cp_copy_file +#define copy_link rtems_shell_cp_copy_link +#define copy_special rtems_shell_cp_copy_special +#define set_utimes rtems_shell_cp_set_utimes +#define setfile rtems_shell_cp_setfile +#define usage rtems_shell_cp_usage + +#include + +__BEGIN_DECLS +int copy_fifo(rtems_shell_cp_globals* cp_globals, struct stat *, int); +int copy_file(rtems_shell_cp_globals* cp_globals, FTSENT *, int); +int copy_link(rtems_shell_cp_globals* cp_globals, FTSENT *, int); +int copy_special(rtems_shell_cp_globals* cp_globals, struct stat *, int); +int set_utimes(const char *, struct stat *); +int setfile(rtems_shell_cp_globals* cp_globals, struct stat *, int); +void usage(); +__END_DECLS + +#endif /* !_EXTERN_H_ */ diff --git a/cpukit/libmisc/shell/fts.c b/cpukit/libmisc/shell/fts.c new file mode 100644 index 0000000000..607806be42 --- /dev/null +++ b/cpukit/libmisc/shell/fts.c @@ -0,0 +1,1193 @@ +/* $NetBSD: fts.c,v 1.26 2005/10/22 20:55:13 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; +#else +__RCSID("$NetBSD: fts.c,v 1.26 2005/10/22 20:55:13 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#define _DIAGASSERT(a) +#undef FTS_WHITEOUT +#define dirfd(dp) __dirfd(dp) + +#if ! HAVE_NBTOOL_CONFIG_H +#define HAVE_STRUCT_DIRENT_D_NAMLEN 1 +#endif + +static FTSENT *fts_alloc __P((FTS *, const char *, size_t)); +static FTSENT *fts_build __P((FTS *, int)); +static void fts_lfree __P((FTSENT *)); +static void fts_load __P((FTS *, FTSENT *)); +static size_t fts_maxarglen __P((char * const *)); +static size_t fts_pow2 __P((size_t)); +static int fts_palloc __P((FTS *, size_t)); +static void fts_padjust __P((FTS *, FTSENT *)); +static FTSENT *fts_sort __P((FTS *, FTSENT *, size_t)); +static u_short fts_stat __P((FTS *, FTSENT *, int)); +static int fts_safe_changedir __P((const FTS *, const FTSENT *, int, + const char *)); + +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) + +#define CLR(opt) (sp->fts_options &= ~(opt)) +#define ISSET(opt) (sp->fts_options & (opt)) +#define SET(opt) (sp->fts_options |= (opt)) + +#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) + +/* fts_build flags */ +#define BCHILD 1 /* fts_children */ +#define BNAMES 2 /* fts_children, names only */ +#define BREAD 3 /* fts_read */ + +#ifndef DTF_HIDEW +#undef FTS_WHITEOUT +#endif + +FTS * +fts_open(argv, options, compar) + char * const *argv; + int options; + int (*compar) __P((const FTSENT **, const FTSENT **)); +{ + FTS *sp; + FTSENT *p, *root; + size_t nitems; + FTSENT *parent, *tmp = NULL; /* pacify gcc */ + size_t len; + + _DIAGASSERT(argv != NULL); + + /* Options check. */ + if (options & ~FTS_OPTIONMASK) { + errno = EINVAL; + return (NULL); + } + + /* Allocate/initialize the stream */ + if ((sp = malloc((u_int)sizeof(FTS))) == NULL) + return (NULL); + memset(sp, 0, sizeof(FTS)); + sp->fts_compar = compar; + sp->fts_options = options; + + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ + if (ISSET(FTS_LOGICAL)) + SET(FTS_NOCHDIR); + + /* + * Start out with 1K of path space, and enough, in any case, + * to hold the user's paths. + */ + if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) + goto mem1; + + /* Allocate/initialize root's parent. */ + if ((parent = fts_alloc(sp, "", 0)) == NULL) + goto mem2; + parent->fts_level = FTS_ROOTPARENTLEVEL; + + /* Allocate/initialize root(s). */ + for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { + /* Don't allow zero-length paths. */ + if ((len = strlen(*argv)) == 0) { + errno = ENOENT; + goto mem3; + } + + if ((p = fts_alloc(sp, *argv, len)) == NULL) + goto mem3; + p->fts_level = FTS_ROOTLEVEL; + p->fts_parent = parent; + p->fts_accpath = p->fts_name; + p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); + + /* Command-line "." and ".." are real directories. */ + if (p->fts_info == FTS_DOT) + p->fts_info = FTS_D; + + /* + * If comparison routine supplied, traverse in sorted + * order; otherwise traverse in the order specified. + */ + if (compar) { + p->fts_link = root; + root = p; + } else { + p->fts_link = NULL; + if (root == NULL) + tmp = root = p; + else { + tmp->fts_link = p; + tmp = p; + } + } + } + if (compar && nitems > 1) + root = fts_sort(sp, root, nitems); + + /* + * Allocate a dummy pointer and make fts_read think that we've just + * finished the node before the root(s); set p->fts_info to FTS_INIT + * so that everything about the "current" node is ignored. + */ + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) + goto mem3; + sp->fts_cur->fts_link = root; + sp->fts_cur->fts_info = FTS_INIT; + + /* + * If using chdir(2), grab a file descriptor pointing to dot to insure + * that we can get back here; this could be avoided for some paths, + * but almost certainly not worth the effort. Slashes, symbolic links, + * and ".." are all fairly nasty problems. Note, if we can't get the + * descriptor we run anyway, just more slowly. + */ + if (!ISSET(FTS_NOCHDIR)) { + if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1) + SET(FTS_NOCHDIR); + else if (fcntl(sp->fts_rfd, F_SETFD, FD_CLOEXEC) == -1) { + close(sp->fts_rfd); + SET(FTS_NOCHDIR); + } + } + + return (sp); + +mem3: fts_lfree(root); + free(parent); +mem2: free(sp->fts_path); +mem1: free(sp); + return (NULL); +} + +static void +fts_load(sp, p) + FTS *sp; + FTSENT *p; +{ + size_t len; + char *cp; + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(p != NULL); + + /* + * Load the stream structure for the next traversal. Since we don't + * actually enter the directory until after the preorder visit, set + * the fts_accpath field specially so the chdir gets done to the right + * place and the user can access the first node. From fts_open it's + * known that the path will fit. + */ + len = p->fts_pathlen = p->fts_namelen; + memmove(sp->fts_path, p->fts_name, len + 1); + if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { + len = strlen(++cp); + memmove(p->fts_name, cp, len + 1); + p->fts_namelen = len; + } + p->fts_accpath = p->fts_path = sp->fts_path; + sp->fts_dev = p->fts_dev; +} + +int +fts_close(sp) + FTS *sp; +{ + FTSENT *freep, *p; + int saved_errno = 0; + + _DIAGASSERT(sp != NULL); + + /* + * This still works if we haven't read anything -- the dummy structure + * points to the root list, so we step through to the end of the root + * list which has a valid parent pointer. + */ + if (sp->fts_cur) { + if (ISSET(FTS_SYMFOLLOW)) + (void)close(sp->fts_cur->fts_symfd); + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { + freep = p; + p = p->fts_link ? p->fts_link : p->fts_parent; + free(freep); + } + free(p); + } + + /* Free up child linked list, sort array, path buffer. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + if (sp->fts_array) + free(sp->fts_array); + free(sp->fts_path); + + /* Return to original directory, save errno if necessary. */ + if (!ISSET(FTS_NOCHDIR)) { + if (fchdir(sp->fts_rfd)) + saved_errno = errno; + (void)close(sp->fts_rfd); + } + + /* Free up the stream pointer. */ + free(sp); + /* ISSET() is illegal after this, since the macro touches sp */ + + /* Set errno and return. */ + if (saved_errno) { + errno = saved_errno; + return (-1); + } + return (0); +} + +/* + * Special case of "/" at the end of the path so that slashes aren't + * appended which would cause paths to be written as "....//foo". + */ +#define NAPPEND(p) \ + (p->fts_path[p->fts_pathlen - 1] == '/' \ + ? p->fts_pathlen - 1 : p->fts_pathlen) + +FTSENT * +fts_read(sp) + FTS *sp; +{ + FTSENT *p, *tmp; + int instr; + char *t; + int saved_errno; + + _DIAGASSERT(sp != NULL); + + /* If finished or unrecoverable error, return NULL. */ + if (sp->fts_cur == NULL || ISSET(FTS_STOP)) + return (NULL); + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* Save and zero out user instructions. */ + instr = p->fts_instr; + p->fts_instr = FTS_NOINSTR; + + /* Any type of file may be re-visited; re-stat and re-turn. */ + if (instr == FTS_AGAIN) { + p->fts_info = fts_stat(sp, p, 0); + return (p); + } + + /* + * Following a symlink -- SLNONE test allows application to see + * SLNONE and recover. If indirecting through a symlink, have + * keep a pointer to current location. If unable to get that + * pointer, follow fails. + */ + if (instr == FTS_FOLLOW && + (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + close(p->fts_symfd); + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + return (p); + } + + /* Directory in pre-order. */ + if (p->fts_info == FTS_D) { + /* If skipped or crossed mount point, do post-order visit. */ + if (instr == FTS_SKIP || + (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { + if (p->fts_flags & FTS_SYMFOLLOW) + (void)close(p->fts_symfd); + if (sp->fts_child) { + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + p->fts_info = FTS_DP; + return (p); + } + + /* Rebuild if only read the names and now traversing. */ + if (sp->fts_child && ISSET(FTS_NAMEONLY)) { + CLR(FTS_NAMEONLY); + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + + /* + * Cd to the subdirectory. + * + * If have already read and now fail to chdir, whack the list + * to make the names come out right, and set the parent errno + * so the application will eventually get an error condition. + * Set the FTS_DONTCHDIR flag so that when we logically change + * directories back to the parent we don't do a chdir. + * + * If haven't read do so. If the read fails, fts_build sets + * FTS_STOP or the fts_info field of the node. + */ + if (sp->fts_child) { + if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { + p->fts_errno = errno; + p->fts_flags |= FTS_DONTCHDIR; + for (p = sp->fts_child; p; p = p->fts_link) + p->fts_accpath = + p->fts_parent->fts_accpath; + } + } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { + if (ISSET(FTS_STOP)) + return (NULL); + return (p); + } + p = sp->fts_child; + sp->fts_child = NULL; + goto name; + } + + /* Move to the next node on this level. */ +next: tmp = p; + if ((p = p->fts_link) != NULL) { + free(tmp); + + /* + * If reached the top, return to the original directory, and + * load the paths for the next root. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + fts_load(sp, p); + return (sp->fts_cur = p); + } + + /* + * User may have called fts_set on the node. If skipped, + * ignore. If followed, get a file descriptor so we can + * get back if necessary. + */ + if (p->fts_instr == FTS_SKIP) + goto next; + if (p->fts_instr == FTS_FOLLOW) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = + open(".", O_RDONLY, 0)) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else if (fcntl(p->fts_symfd, F_SETFD, FD_CLOEXEC) == -1) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + close(p->fts_symfd); + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + p->fts_instr = FTS_NOINSTR; + } + +name: t = sp->fts_path + NAPPEND(p->fts_parent); + *t++ = '/'; + memmove(t, p->fts_name, (size_t)(p->fts_namelen + 1)); + return (sp->fts_cur = p); + } + + /* Move up to the parent node. */ + p = tmp->fts_parent; + free(tmp); + + if (p->fts_level == FTS_ROOTPARENTLEVEL) { + /* + * Done; free everything up and set errno to 0 so the user + * can distinguish between error and EOF. + */ + free(p); + errno = 0; + return (sp->fts_cur = NULL); + } + + /* Nul terminate the pathname. */ + sp->fts_path[p->fts_pathlen] = '\0'; + + /* + * Return to the parent directory. If at a root node or came through + * a symlink, go back through the file descriptor. Otherwise, cd up + * one directory. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + } else if (p->fts_flags & FTS_SYMFOLLOW) { + if (FCHDIR(sp, p->fts_symfd)) { + saved_errno = errno; + (void)close(p->fts_symfd); + errno = saved_errno; + SET(FTS_STOP); + return (NULL); + } + (void)close(p->fts_symfd); + } else if (!(p->fts_flags & FTS_DONTCHDIR) && + fts_safe_changedir(sp, p->fts_parent, -1, "..")) { + SET(FTS_STOP); + return (NULL); + } + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; + return (sp->fts_cur = p); +} + +/* + * Fts_set takes the stream as an argument although it's not used in this + * implementation; it would be necessary if anyone wanted to add global + * semantics to fts using fts_set. An error return is allowed for similar + * reasons. + */ +/* ARGSUSED */ +int +fts_set(sp, p, instr) + FTS *sp; + FTSENT *p; + int instr; +{ + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(p != NULL); + + if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && + instr != FTS_NOINSTR && instr != FTS_SKIP) { + errno = EINVAL; + return (1); + } + p->fts_instr = instr; + return (0); +} + +FTSENT * +fts_children(sp, instr) + FTS *sp; + int instr; +{ + FTSENT *p; + int fd; + + _DIAGASSERT(sp != NULL); + + if (instr && instr != FTS_NAMEONLY) { + errno = EINVAL; + return (NULL); + } + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* + * Errno set to 0 so user can distinguish empty directory from + * an error. + */ + errno = 0; + + /* Fatal errors stop here. */ + if (ISSET(FTS_STOP)) + return (NULL); + + /* Return logical hierarchy of user's arguments. */ + if (p->fts_info == FTS_INIT) + return (p->fts_link); + + /* + * If not a directory being visited in pre-order, stop here. Could + * allow FTS_DNR, assuming the user has fixed the problem, but the + * same effect is available with FTS_AGAIN. + */ + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) + return (NULL); + + /* Free up any previous child list. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + + if (instr == FTS_NAMEONLY) { + SET(FTS_NAMEONLY); + instr = BNAMES; + } else + instr = BCHILD; + + /* + * If using chdir on a relative path and called BEFORE fts_read does + * its chdir to the root of a traversal, we can lose -- we need to + * chdir into the subdirectory, and we don't know where the current + * directory is, so we can't get back so that the upcoming chdir by + * fts_read will work. + */ + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || + ISSET(FTS_NOCHDIR)) + return (sp->fts_child = fts_build(sp, instr)); + + if ((fd = open(".", O_RDONLY, 0)) == -1) + return (sp->fts_child = NULL); + sp->fts_child = fts_build(sp, instr); + if (fchdir(fd)) { + (void)close(fd); + return (NULL); + } + (void)close(fd); + return (sp->fts_child); +} + +/* + * This is the tricky part -- do not casually change *anything* in here. The + * idea is to build the linked list of entries that are used by fts_children + * and fts_read. There are lots of special cases. + * + * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is + * set and it's a physical walk (so that symbolic links can't be directories), + * we can do things quickly. First, if it's a 4.4BSD file system, the type + * of the file is in the directory entry. Otherwise, we assume that the number + * of subdirectories in a node is equal to the number of links to the parent. + * The former skips all stat calls. The latter skips stat calls in any leaf + * directories and for any files after the subdirectories in the directory have + * been found, cutting the stat calls by about 2/3. + */ +static FTSENT * +fts_build(sp, type) + FTS *sp; + int type; +{ + struct dirent *dp; + FTSENT *p, *head; + size_t nitems; + FTSENT *cur, *tail; + DIR *dirp; + int adjust, cderrno, descend, len, level, nlinks, saved_errno, nostat; + size_t maxlen; +#ifdef FTS_WHITEOUT + int oflag; +#endif + char *cp = NULL; /* pacify gcc */ + + _DIAGASSERT(sp != NULL); + + /* Set current node pointer. */ + cur = sp->fts_cur; + + /* + * Open the directory for reading. If this fails, we're done. + * If being called from fts_read, set the fts_info field. + */ +#ifdef FTS_WHITEOUT + if (ISSET(FTS_WHITEOUT)) + oflag = DTF_NODUP|DTF_REWIND; + else + oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; +#else +#define __opendir2(path, flag) opendir(path) +#endif + if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { + if (type == BREAD) { + cur->fts_info = FTS_DNR; + cur->fts_errno = errno; + } + return (NULL); + } + + /* + * Nlinks is the number of possible entries of type directory in the + * directory if we're cheating on stat calls, 0 if we're not doing + * any stat calls at all, -1 if we're doing stats on everything. + */ + if (type == BNAMES) { + nlinks = 0; + nostat = 1; + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); + nostat = 1; + } else { + nlinks = -1; + nostat = 0; + } + +#ifdef notdef + (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); + (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", + ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); +#endif + /* + * If we're going to need to stat anything or we want to descend + * and stay in the directory, chdir. If this fails we keep going, + * but set a flag so we don't chdir after the post-order visit. + * We won't be able to stat anything, but we can still return the + * names themselves. Note, that since fts_read won't be able to + * chdir into the directory, it will have to return different path + * names than before, i.e. "a/b" instead of "b". Since the node + * has already been visited in pre-order, have to wait until the + * post-order visit to return the error. There is a special case + * here, if there was nothing to stat then it's not an error to + * not be able to stat. This is all fairly nasty. If a program + * needed sorted entries or stat information, they had better be + * checking FTS_NS on the returned nodes. + */ + cderrno = 0; + if (nlinks || type == BREAD) { + if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) { + if (nlinks && type == BREAD) + cur->fts_errno = errno; + cur->fts_flags |= FTS_DONTCHDIR; + descend = 0; + cderrno = errno; + } else + descend = 1; + } else + descend = 0; + + /* + * Figure out the max file name length that can be stored in the + * current path -- the inner loop allocates more path as necessary. + * We really wouldn't have to do the maxlen calculations here, we + * could do them in fts_read before returning the path, but it's a + * lot easier here since the length is part of the dirent structure. + * + * If not changing directories set a pointer so that can just append + * each new name into the path. + */ + len = NAPPEND(cur); + if (ISSET(FTS_NOCHDIR)) { + cp = sp->fts_path + len; + *cp++ = '/'; + } + len++; + maxlen = sp->fts_pathlen - len; + + level = cur->fts_level + 1; + + /* Read the directory, attaching each entry to the `link' pointer. */ + adjust = 0; + for (head = tail = NULL, nitems = 0; (dp = readdir(dirp)) != NULL;) { + size_t dlen; + + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) + continue; + +#if HAVE_STRUCT_DIRENT_D_NAMLEN + dlen = dp->d_namlen; +#else + dlen = strlen(dp->d_name); +#endif + if ((p = fts_alloc(sp, dp->d_name, dlen)) == NULL) + goto mem1; + if (dlen >= maxlen) { /* include space for NUL */ + if (fts_palloc(sp, len + dlen + 1)) { + /* + * No more memory for path or structures. Save + * errno, free up the current structure and the + * structures already allocated. + */ +mem1: saved_errno = errno; + if (p) + free(p); + fts_lfree(head); + (void)closedir(dirp); + errno = saved_errno; + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + return (NULL); + } + adjust = 1; + if (ISSET(FTS_NOCHDIR)) + cp = sp->fts_path + len; + maxlen = sp->fts_pathlen - len; + } + + p->fts_pathlen = len + dlen; + p->fts_parent = sp->fts_cur; + p->fts_level = level; + +#ifdef FTS_WHITEOUT + if (dp->d_type == DT_WHT) + p->fts_flags |= FTS_ISW; +#endif + + if (cderrno) { + if (nlinks) { + p->fts_info = FTS_NS; + p->fts_errno = cderrno; + } else + p->fts_info = FTS_NSOK; + p->fts_accpath = cur->fts_accpath; + } else if (nlinks == 0 +#ifdef DT_DIR + || (nostat && + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) +#endif + ) { + p->fts_accpath = + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; + p->fts_info = FTS_NSOK; + } else { + /* Build a file name for fts_stat to stat. */ + if (ISSET(FTS_NOCHDIR)) { + p->fts_accpath = p->fts_path; + memmove(cp, p->fts_name, + (size_t)(p->fts_namelen + 1)); + } else + p->fts_accpath = p->fts_name; + /* Stat it. */ + p->fts_info = fts_stat(sp, p, 0); + + /* Decrement link count if applicable. */ + if (nlinks > 0 && (p->fts_info == FTS_D || + p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) + --nlinks; + } + + /* We walk in directory order so "ls -f" doesn't get upset. */ + p->fts_link = NULL; + if (head == NULL) + head = tail = p; + else { + tail->fts_link = p; + tail = p; + } + ++nitems; + } + (void)closedir(dirp); + + /* + * If had to realloc the path, adjust the addresses for the rest + * of the tree. + */ + if (adjust) + fts_padjust(sp, head); + + /* + * If not changing directories, reset the path back to original + * state. + */ + if (ISSET(FTS_NOCHDIR)) { + if (cp - 1 > sp->fts_path) + --cp; + *cp = '\0'; + } + + /* + * If descended after called from fts_children or after called from + * fts_read and nothing found, get back. At the root level we use + * the saved fd; if one of fts_open()'s arguments is a relative path + * to an empty directory, we wind up here with no other way back. If + * can't get back, we're done. + */ + if (descend && (type == BCHILD || !nitems) && + (cur->fts_level == FTS_ROOTLEVEL ? + FCHDIR(sp, sp->fts_rfd) : + fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + return (NULL); + } + + /* If didn't find anything, return NULL. */ + if (!nitems) { + if (type == BREAD) + cur->fts_info = FTS_DP; + return (NULL); + } + + /* Sort the entries. */ + if (sp->fts_compar && nitems > 1) + head = fts_sort(sp, head, nitems); + return (head); +} + +static u_short +fts_stat(sp, p, follow) + FTS *sp; + FTSENT *p; + int follow; +{ + FTSENT *t; + dev_t dev; + __fts_ino_t ino; + __fts_stat_t *sbp, sb; + int saved_errno; + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(p != NULL); + + /* If user needs stat info, stat buffer already allocated. */ + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; + +#ifdef FTS_WHITEOUT + /* check for whiteout */ + if (p->fts_flags & FTS_ISW) { + if (sbp != &sb) { + memset(sbp, '\0', sizeof (*sbp)); + sbp->st_mode = S_IFWHT; + } + return (FTS_W); + } +#endif + + /* + * If doing a logical walk, or application requested FTS_FOLLOW, do + * a stat(2). If that fails, check for a non-existent symlink. If + * fail, set the errno from the stat call. + */ + if (ISSET(FTS_LOGICAL) || follow) { + if (stat(p->fts_accpath, sbp)) { + saved_errno = errno; + if (!lstat(p->fts_accpath, sbp)) { + errno = 0; + return (FTS_SLNONE); + } + p->fts_errno = saved_errno; + goto err; + } + } else if (lstat(p->fts_accpath, sbp)) { + p->fts_errno = errno; +err: memset(sbp, 0, sizeof(*sbp)); + return (FTS_NS); + } + + if (S_ISDIR(sbp->st_mode)) { + /* + * Set the device/inode. Used to find cycles and check for + * crossing mount points. Also remember the link count, used + * in fts_build to limit the number of stat calls. It is + * understood that these fields are only referenced if fts_info + * is set to FTS_D. + */ + dev = p->fts_dev = sbp->st_dev; + ino = p->fts_ino = sbp->st_ino; + p->fts_nlink = sbp->st_nlink; + + if (ISDOT(p->fts_name)) + return (FTS_DOT); + + /* + * Cycle detection is done by brute force when the directory + * is first encountered. If the tree gets deep enough or the + * number of symbolic links to directories is high enough, + * something faster might be worthwhile. + */ + for (t = p->fts_parent; + t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) + if (ino == t->fts_ino && dev == t->fts_dev) { + p->fts_cycle = t; + return (FTS_DC); + } + return (FTS_D); + } + if (S_ISLNK(sbp->st_mode)) + return (FTS_SL); + if (S_ISREG(sbp->st_mode)) + return (FTS_F); + return (FTS_DEFAULT); +} + +static FTSENT * +fts_sort(sp, head, nitems) + FTS *sp; + FTSENT *head; + size_t nitems; +{ + FTSENT **ap, *p; + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(head != NULL); + + /* + * Construct an array of pointers to the structures and call qsort(3). + * Reassemble the array in the order returned by qsort. If unable to + * sort for memory reasons, return the directory entries in their + * current order. Allocate enough space for the current needs plus + * 40 so don't realloc one entry at a time. + */ + if (nitems > sp->fts_nitems) { + FTSENT **new; + + new = realloc(sp->fts_array, sizeof(FTSENT *) * (nitems + 40)); + if (new == 0) + return (head); + sp->fts_array = new; + sp->fts_nitems = nitems + 40; + } + for (ap = sp->fts_array, p = head; p; p = p->fts_link) + *ap++ = p; + qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), + (int (*) __P((const void *, const void *)))sp->fts_compar); + for (head = *(ap = sp->fts_array); --nitems; ++ap) + ap[0]->fts_link = ap[1]; + ap[0]->fts_link = NULL; + return (head); +} + +static FTSENT * +fts_alloc(sp, name, namelen) + FTS *sp; + const char *name; + size_t namelen; +{ + FTSENT *p; + size_t len; + + _DIAGASSERT(sp != NULL); + _DIAGASSERT(name != NULL); + +#if defined(ALIGNBYTES) && defined(ALIGN) + /* + * The file name is a variable length array and no stat structure is + * necessary if the user has set the nostat bit. Allocate the FTSENT + * structure, the file name and the stat structure in one chunk, but + * be careful that the stat structure is reasonably aligned. Since the + * fts_name field is declared to be of size 1, the fts_name pointer is + * namelen + 2 before the first possible address of the stat structure. + */ + len = sizeof(FTSENT) + namelen; + if (!ISSET(FTS_NOSTAT)) + len += sizeof(*(p->fts_statp)) + ALIGNBYTES; + if ((p = malloc(len)) == NULL) + return (NULL); + + if (!ISSET(FTS_NOSTAT)) + p->fts_statp = + (__fts_stat_t *)ALIGN((u_long)(p->fts_name + namelen + 2)); +#else + if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL) + return (NULL); + + if (!ISSET(FTS_NOSTAT)) + if ((p->fts_statp = malloc(sizeof(*(p->fts_statp)))) == NULL) { + free(p); + return (NULL); + } +#endif + + /* Copy the name plus the trailing NULL. */ + memmove(p->fts_name, name, namelen + 1); + + p->fts_namelen = namelen; + p->fts_path = sp->fts_path; + p->fts_errno = 0; + p->fts_flags = 0; + p->fts_instr = FTS_NOINSTR; + p->fts_number = 0; + p->fts_pointer = NULL; + return (p); +} + +static void +fts_lfree(head) + FTSENT *head; +{ + FTSENT *p; + + /* XXX: head may be NULL ? */ + + /* Free a linked list of structures. */ + while ((p = head) != NULL) { + head = head->fts_link; + +#if !defined(ALIGNBYTES) || !defined(ALIGN) + if (p->fts_statp) + free(p->fts_statp); +#endif + free(p); + } +} + +static size_t +fts_pow2(x) + size_t x; +{ + + x--; + x |= x>>1; + x |= x>>2; + x |= x>>4; + x |= x>>8; + x |= x>>16; +#if LONG_BIT > 32 + x |= x>>32; +#endif +#if LONG_BIT > 64 + x |= x>>64; +#endif + x++; + return (x); +} + +/* + * Allow essentially unlimited paths; find, rm, ls should all work on any tree. + * Most systems will allow creation of paths much longer than MAXPATHLEN, even + * though the kernel won't resolve them. Round up the new size to a power of 2, + * so we don't realloc the path 2 bytes at a time. + */ +static int +fts_palloc(sp, size) + FTS *sp; + size_t size; +{ + char *new; + + _DIAGASSERT(sp != NULL); + +#if 1 + /* Protect against fts_pathlen overflow. */ + if (size > USHRT_MAX + 1) { + errno = ENOMEM; + return (1); + } +#endif + size = fts_pow2(size); + new = realloc(sp->fts_path, size); + if (new == 0) + return (1); + sp->fts_path = new; + sp->fts_pathlen = size; + return (0); +} + +/* + * When the path is realloc'd, have to fix all of the pointers in structures + * already returned. + */ +static void +fts_padjust(sp, head) + FTS *sp; + FTSENT *head; +{ + FTSENT *p; + char *addr; + + _DIAGASSERT(sp != NULL); + +#define ADJUST(p) do { \ + if ((p)->fts_accpath != (p)->fts_name) \ + (p)->fts_accpath = \ + addr + ((p)->fts_accpath - (p)->fts_path); \ + (p)->fts_path = addr; \ +} while (/*CONSTCOND*/0) + + addr = sp->fts_path; + + /* Adjust the current set of children. */ + for (p = sp->fts_child; p; p = p->fts_link) + ADJUST(p); + + /* Adjust the rest of the tree, including the current level. */ + for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { + ADJUST(p); + p = p->fts_link ? p->fts_link : p->fts_parent; + } +} + +static size_t +fts_maxarglen(argv) + char * const *argv; +{ + size_t len, max; + + _DIAGASSERT(argv != NULL); + + for (max = 0; *argv; ++argv) + if ((len = strlen(*argv)) > max) + max = len; + return (max + 1); +} + +/* + * Change to dir specified by fd or p->fts_accpath without getting + * tricked by someone changing the world out from underneath us. + * Assumes p->fts_dev and p->fts_ino are filled in. + */ +static int +fts_safe_changedir(sp, p, fd, path) + const FTS *sp; + const FTSENT *p; + int fd; + const char *path; +{ + int oldfd = fd, ret = -1; + __fts_stat_t sb; + + if (ISSET(FTS_NOCHDIR)) + return 0; + + if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1) + return -1; + + if (fstat(fd, &sb) == -1) + goto bail; + + if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) { + errno = ENOENT; + goto bail; + } + + ret = fchdir(fd); + +bail: + if (oldfd < 0) { + int save_errno = errno; + (void)close(fd); + errno = save_errno; + } + return ret; +} diff --git a/cpukit/libmisc/shell/fts.h b/cpukit/libmisc/shell/fts.h new file mode 100644 index 0000000000..68e9d46c3e --- /dev/null +++ b/cpukit/libmisc/shell/fts.h @@ -0,0 +1,146 @@ +/* $NetBSD: fts.h,v 1.14 2005/09/13 01:44:32 christos Exp $ */ + +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)fts.h 8.3 (Berkeley) 8/14/94 + */ + +#ifndef _FTS_H_ +#define _FTS_H_ + +#ifndef __fts_stat_t +#define __fts_stat_t struct stat +#endif +#ifndef __fts_nlink_t +#define __fts_nlink_t nlink_t +#endif +#ifndef __fts_ino_t +#define __fts_ino_t ino_t +#endif + +typedef struct { + struct _ftsent *fts_cur; /* current node */ + struct _ftsent *fts_child; /* linked list of children */ + struct _ftsent **fts_array; /* sort array */ + dev_t fts_dev; /* starting device # */ + char *fts_path; /* path for this descent */ + int fts_rfd; /* fd for root */ + u_int fts_pathlen; /* sizeof(path) */ + u_int fts_nitems; /* elements in the sort array */ + int (*fts_compar) /* compare function */ + (const struct _ftsent **, const struct _ftsent **); + +#define FTS_COMFOLLOW 0x001 /* follow command line symlinks */ +#define FTS_LOGICAL 0x002 /* logical walk */ +#define FTS_NOCHDIR 0x004 /* don't change directories */ +#define FTS_NOSTAT 0x008 /* don't get stat info */ +#define FTS_PHYSICAL 0x010 /* physical walk */ +#define FTS_SEEDOT 0x020 /* return dot and dot-dot */ +#define FTS_XDEV 0x040 /* don't cross devices */ +#define FTS_WHITEOUT 0x080 /* return whiteout information */ +#define FTS_OPTIONMASK 0x0ff /* valid user option mask */ + +#define FTS_NAMEONLY 0x100 /* (private) child names only */ +#define FTS_STOP 0x200 /* (private) unrecoverable error */ + int fts_options; /* fts_open options, global flags */ +} FTS; + +typedef struct _ftsent { + struct _ftsent *fts_cycle; /* cycle node */ + struct _ftsent *fts_parent; /* parent directory */ + struct _ftsent *fts_link; /* next file in directory */ + long fts_number; /* local numeric value */ + void *fts_pointer; /* local address value */ + char *fts_accpath; /* access path */ + char *fts_path; /* root path */ + int fts_errno; /* errno for this node */ + int fts_symfd; /* fd for symlink */ + u_short fts_pathlen; /* strlen(fts_path) */ + u_short fts_namelen; /* strlen(fts_name) */ + + __fts_ino_t fts_ino; /* inode */ + dev_t fts_dev; /* device */ + __fts_nlink_t fts_nlink; /* link count */ + +#define FTS_ROOTPARENTLEVEL -1 +#define FTS_ROOTLEVEL 0 + short fts_level; /* depth (-1 to N) */ + +#define FTS_D 1 /* preorder directory */ +#define FTS_DC 2 /* directory that causes cycles */ +#define FTS_DEFAULT 3 /* none of the above */ +#define FTS_DNR 4 /* unreadable directory */ +#define FTS_DOT 5 /* dot or dot-dot */ +#define FTS_DP 6 /* postorder directory */ +#define FTS_ERR 7 /* error; errno is set */ +#define FTS_F 8 /* regular file */ +#define FTS_INIT 9 /* initialized only */ +#define FTS_NS 10 /* stat(2) failed */ +#define FTS_NSOK 11 /* no stat(2) requested */ +#define FTS_SL 12 /* symbolic link */ +#define FTS_SLNONE 13 /* symbolic link without target */ +#define FTS_W 14 /* whiteout object */ + u_short fts_info; /* user flags for FTSENT structure */ + +#define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */ +#define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */ +#define FTS_ISW 0x04 /* this is a whiteout object */ + u_short fts_flags; /* private flags for FTSENT structure */ + +#define FTS_AGAIN 1 /* read node again */ +#define FTS_FOLLOW 2 /* follow symbolic link */ +#define FTS_NOINSTR 3 /* no instructions */ +#define FTS_SKIP 4 /* discard node */ + u_short fts_instr; /* fts_set() instructions */ + + __fts_stat_t *fts_statp; /* stat(2) information */ + char fts_name[1]; /* file name */ +} FTSENT; + +#include + +#define __RENAME(n) +#define fts_children rtems_shell_fts_children +#define fts_close rtems_shell_fts_close +#define fts_open rtems_shell_fts_open +#define fts_read rtems_shell_fts_read +#define fts_set rtems_shell_fts_set + +__BEGIN_DECLS +#ifndef __LIBC12_SOURCE__ +FTSENT *fts_children(FTS *, int) __RENAME(__fts_children30); +int fts_close(FTS *) __RENAME(__fts_close30); +FTS *fts_open(char * const *, int, + int (*)(const FTSENT **, const FTSENT **)) __RENAME(__fts_open30); +FTSENT *fts_read(FTS *) __RENAME(__fts_read30); +int fts_set(FTS *, FTSENT *, int) __RENAME(__fts_set30); +#endif +__END_DECLS + +#endif /* !_FTS_H_ */ diff --git a/cpukit/libmisc/shell/main_cp.c b/cpukit/libmisc/shell/main_cp.c new file mode 100644 index 0000000000..f7e7347b66 --- /dev/null +++ b/cpukit/libmisc/shell/main_cp.c @@ -0,0 +1,531 @@ +/* $NetBSD: cp.c,v 1.39 2005/10/24 12:59:07 kleink Exp $ */ + +/* + * Copyright (c) 1988, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * David Hitz of Auspex Systems Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if 0 +#ifndef lint +__COPYRIGHT( +"@(#) Copyright (c) 1988, 1993, 1994\n\ + The Regents of the University of California. All rights reserved.\n"); +#endif /* not lint */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)cp.c 8.5 (Berkeley) 4/29/95"; +#else +__RCSID("$NetBSD: cp.c,v 1.39 2005/10/24 12:59:07 kleink Exp $"); +#endif +#endif /* not lint */ +#endif +/* + * Cp copies source files to target files. + * + * The global PATH_T structure "to" always contains the path to the + * current target file. Since fts(3) does not change directories, + * this path can be either absolute or dot-relative. + * + * The basic algorithm is to initialize "to" and use fts(3) to traverse + * the file hierarchy rooted in the argument list. A trivial case is the + * case of 'cp file1 file2'. The more interesting case is the case of + * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the + * path (relative to the root of the traversal) is appended to dir (stored + * in "to") to form the final target path. + */ + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "extern-cp.h" + +#define STRIP_TRAILING_SLASH(p) { \ + while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \ + *--(p).p_end = '\0'; \ + } + +static char empty[] = ""; + +enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE }; + +static int main_cp(rtems_shell_cp_globals* cp_globals, int, char *[]); +static int copy(rtems_shell_cp_globals* cp_globals, char *[], enum op, int); +static int mastercmp(const FTSENT **, const FTSENT **); + +void +rtems_shell_cp_exit (rtems_shell_cp_globals* cp_global, int code) +{ + longjmp (cp_global->exit_jmp, 1); +} + +int +rtems_shell_main_cp(int argc, char *argv[]) +{ + rtems_shell_cp_globals cp_globals; + memset (&cp_globals, 0, sizeof (cp_globals)); + if (setjmp (cp_globals.exit_jmp) == 0) + return main_cp (&cp_globals, argc, argv); + return 1; +} + +int +main_cp(rtems_shell_cp_globals* cp_globals, int argc, char *argv[]) +{ + struct stat to_stat, tmp_stat; + enum op type; + int Hflag, Lflag, Pflag, ch, fts_options, r; + char *target; + + to.p_end = to.p_path; + to.target_end = empty; + + (void)setlocale(LC_ALL, ""); + + Hflag = Lflag = Pflag = Rflag = 0; + while ((ch = getopt(argc, argv, "HLNPRfiprv")) != -1) + switch (ch) { + case 'H': + Hflag = 1; + Lflag = Pflag = 0; + break; + case 'L': + Lflag = 1; + Hflag = Pflag = 0; + break; + case 'N': + Nflag = 1; + break; + case 'P': + Pflag = 1; + Hflag = Lflag = 0; + break; + case 'R': + Rflag = 1; + break; + case 'f': + fflag = 1; + iflag = 0; + break; + case 'i': + iflag = isatty(fileno(stdin)); + fflag = 0; + break; + case 'p': + pflag = 1; + break; + case 'r': + rflag = 1; + break; + case 'v': + vflag = 1; + break; + case '?': + default: + usage(); + return 1; + } + argc -= optind; + argv += optind; + + if (argc < 2) + usage(); + + fts_options = FTS_NOCHDIR | FTS_PHYSICAL; + if (rflag) { + if (Rflag) { + errx(exit_jump, EXIT_FAILURE, + "the -R and -r options may not be specified together."); + /* NOTREACHED */ + } + if (Hflag || Lflag || Pflag) { + errx(exit_jump, EXIT_FAILURE, + "the -H, -L, and -P options may not be specified with the -r option."); + /* NOTREACHED */ + } + fts_options &= ~FTS_PHYSICAL; + fts_options |= FTS_LOGICAL; + } + if (Rflag) { + if (Hflag) + fts_options |= FTS_COMFOLLOW; + if (Lflag) { + fts_options &= ~FTS_PHYSICAL; + fts_options |= FTS_LOGICAL; + } + } else { + fts_options &= ~FTS_PHYSICAL; + fts_options |= FTS_LOGICAL | FTS_COMFOLLOW; + } + + myuid = getuid(); + + /* Copy the umask for explicit mode setting. */ + myumask = umask(0); + (void)umask(myumask); + + /* Save the target base in "to". */ + target = argv[--argc]; + if (strlen(target) > MAXPATHLEN) { + errx(exit_jump, EXIT_FAILURE, "%s: name too long", target); + /* NOTREACHED */ + } + (void)strcpy(to.p_path, target); + to.p_end = to.p_path + strlen(to.p_path); + STRIP_TRAILING_SLASH(to); + to.target_end = to.p_end; + + /* Set end of argument list for fts(3). */ + argv[argc] = NULL; + + /* + * Cp has two distinct cases: + * + * cp [-R] source target + * cp [-R] source1 ... sourceN directory + * + * In both cases, source can be either a file or a directory. + * + * In (1), the target becomes a copy of the source. That is, if the + * source is a file, the target will be a file, and likewise for + * directories. + * + * In (2), the real target is not directory, but "directory/source". + */ + r = stat(to.p_path, &to_stat); + if (r == -1 && errno != ENOENT) { + err(exit_jump, EXIT_FAILURE, "%s", to.p_path); + /* NOTREACHED */ + } + if (r == -1 || !S_ISDIR(to_stat.st_mode)) { + /* + * Case (1). Target is not a directory. + */ + if (argc > 1) + usage(); + /* + * Need to detect the case: + * cp -R dir foo + * Where dir is a directory and foo does not exist, where + * we want pathname concatenations turned on but not for + * the initial mkdir(). + */ + if (r == -1) { + if (rflag || (Rflag && (Lflag || Hflag))) + r = stat(*argv, &tmp_stat); + else + r = lstat(*argv, &tmp_stat); + if (r == -1) { + err(exit_jump, EXIT_FAILURE, "%s", *argv); + /* NOTREACHED */ + } + + if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag)) + type = DIR_TO_DNE; + else + type = FILE_TO_FILE; + } else + type = FILE_TO_FILE; + } else { + /* + * Case (2). Target is a directory. + */ + type = FILE_TO_DIR; + } + + return copy(cp_globals, argv, type, fts_options); +} + +int +copy(rtems_shell_cp_globals* cp_globals, + char *argv[], enum op type, int fts_options) +{ + struct stat to_stat; + FTS *ftsp; + FTSENT *curr; + int base, dne, rval; + size_t nlen; + char *p, *tmp; + + base = 0; /* XXX gcc -Wuninitialized (see comment below) */ + + if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL) + err(exit_jump, EXIT_FAILURE, "%s", argv[0]); + /* NOTREACHED */ + for (rval = 0; (curr = fts_read(ftsp)) != NULL;) { + switch (curr->fts_info) { + case FTS_NS: + case FTS_DNR: + case FTS_ERR: + warnx("%s: %s", curr->fts_path, + strerror(curr->fts_errno)); + rval = 1; + continue; + case FTS_DC: /* Warn, continue. */ + warnx("%s: directory causes a cycle", curr->fts_path); + rval = 1; + continue; + } + + /* + * If we are in case (2) or (3) above, we need to append the + * source name to the target name. + */ + if (type != FILE_TO_FILE) { + if ((curr->fts_namelen + + to.target_end - to.p_path + 1) > MAXPATHLEN) { + warnx("%s/%s: name too long (not copied)", + to.p_path, curr->fts_name); + rval = 1; + continue; + } + + /* + * Need to remember the roots of traversals to create + * correct pathnames. If there's a directory being + * copied to a non-existent directory, e.g. + * cp -R a/dir noexist + * the resulting path name should be noexist/foo, not + * noexist/dir/foo (where foo is a file in dir), which + * is the case where the target exists. + * + * Also, check for "..". This is for correct path + * concatentation for paths ending in "..", e.g. + * cp -R .. /tmp + * Paths ending in ".." are changed to ".". This is + * tricky, but seems the easiest way to fix the problem. + * + * XXX + * Since the first level MUST be FTS_ROOTLEVEL, base + * is always initialized. + */ + if (curr->fts_level == FTS_ROOTLEVEL) { + if (type != DIR_TO_DNE) { + p = strrchr(curr->fts_path, '/'); + base = (p == NULL) ? 0 : + (int)(p - curr->fts_path + 1); + + if (!strcmp(&curr->fts_path[base], + "..")) + base += 1; + } else + base = curr->fts_pathlen; + } + + p = &curr->fts_path[base]; + nlen = curr->fts_pathlen - base; + + tmp = to.target_end; + if (*p != '/' && *(tmp - 1) != '/') + *tmp++ = '/'; + *tmp = 0; + + (void)strncat(tmp, p, nlen); + to.p_end = tmp + nlen; + *to.p_end = 0; + STRIP_TRAILING_SLASH(to); + } + + /* Not an error but need to remember it happened */ + if (stat(to.p_path, &to_stat) == -1) + dne = 1; + else { + if (to_stat.st_dev == curr->fts_statp->st_dev && + to_stat.st_ino == curr->fts_statp->st_ino) { + warnx("%s and %s are identical (not copied).", + to.p_path, curr->fts_path); + rval = 1; + if (S_ISDIR(curr->fts_statp->st_mode)) + (void)fts_set(ftsp, curr, FTS_SKIP); + continue; + } + if (!S_ISDIR(curr->fts_statp->st_mode) && + S_ISDIR(to_stat.st_mode)) { + warnx("cannot overwrite directory %s with non-directory %s", + to.p_path, curr->fts_path); + rval = 1; + continue; + } + dne = 0; + } + + switch (curr->fts_statp->st_mode & S_IFMT) { + case S_IFLNK: + /* Catch special case of a non dangling symlink */ + if((fts_options & FTS_LOGICAL) || + ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) { + if (copy_file(cp_globals, curr, dne)) + rval = 1; + } else { + if (copy_link(cp_globals, curr, !dne)) + rval = 1; + } + break; + case S_IFDIR: + if (!Rflag && !rflag) { + if (curr->fts_info == FTS_D) + warnx("%s is a directory (not copied).", + curr->fts_path); + (void)fts_set(ftsp, curr, FTS_SKIP); + rval = 1; + break; + } + + /* + * Directories get noticed twice: + * In the first pass, create it if needed. + * In the second pass, after the children have been copied, + * set the permissions. + */ + if (curr->fts_info == FTS_D) /* First pass */ + { + /* + * If the directory doesn't exist, create the new + * one with the from file mode plus owner RWX bits, + * modified by the umask. Trade-off between being + * able to write the directory (if from directory is + * 555) and not causing a permissions race. If the + * umask blocks owner writes, we fail.. + */ + if (dne) { + if (mkdir(to.p_path, + curr->fts_statp->st_mode | S_IRWXU) < 0) + err(exit_jump, EXIT_FAILURE, "%s", + to.p_path); + /* NOTREACHED */ + } else if (!S_ISDIR(to_stat.st_mode)) { + errno = ENOTDIR; + err(exit_jump, EXIT_FAILURE, "%s", + to.p_path); + /* NOTREACHED */ + } + } + else if (curr->fts_info == FTS_DP) /* Second pass */ + { + /* + * If not -p and directory didn't exist, set it to be + * the same as the from directory, umodified by the + * umask; arguably wrong, but it's been that way + * forever. + */ + if (pflag && setfile(cp_globals, curr->fts_statp, 0)) + rval = 1; + else if (dne) + (void)chmod(to.p_path, + curr->fts_statp->st_mode); + } + else + { + warnx("directory %s encountered when not expected.", + curr->fts_path); + rval = 1; + break; + } + + break; + case S_IFBLK: + case S_IFCHR: + if (Rflag) { + if (copy_special(cp_globals, curr->fts_statp, !dne)) + rval = 1; + } else + if (copy_file(cp_globals, curr, dne)) + rval = 1; + break; + case S_IFIFO: + if (Rflag) { + if (copy_fifo(cp_globals, curr->fts_statp, !dne)) + rval = 1; + } else + if (copy_file(cp_globals, curr, dne)) + rval = 1; + break; + default: + if (copy_file(cp_globals, curr, dne)) + rval = 1; + break; + } + if (vflag && !rval) + (void)printf("%s -> %s\n", curr->fts_path, to.p_path); + } + if (errno) { + err(exit_jump, EXIT_FAILURE, "fts_read"); + /* NOTREACHED */ + } + return (rval); +} + +/* + * mastercmp -- + * The comparison function for the copy order. The order is to copy + * non-directory files before directory files. The reason for this + * is because files tend to be in the same cylinder group as their + * parent directory, whereas directories tend not to be. Copying the + * files first reduces seeking. + */ +int +mastercmp(const FTSENT **a, const FTSENT **b) +{ + int a_info, b_info; + + a_info = (*a)->fts_info; + if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR) + return (0); + b_info = (*b)->fts_info; + if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR) + return (0); + if (a_info == FTS_D) + return (-1); + if (b_info == FTS_D) + return (1); + return (0); +} + +rtems_shell_cmd_t rtems_shell_CP_Command = { + "cp", /* name */ + "cp [-R [-H | -L | -P]] [-f | -i] [-pv] src target # copy", /* usage */ + "files", /* topic */ + rtems_shell_main_cp, /* command */ + NULL, /* alias */ + NULL /* next */ +}; diff --git a/cpukit/libmisc/shell/main_mount_nfs.c b/cpukit/libmisc/shell/main_mount_nfs.c index 39b2218864..f09be8fa0f 100644 --- a/cpukit/libmisc/shell/main_mount_nfs.c +++ b/cpukit/libmisc/shell/main_mount_nfs.c @@ -43,6 +43,14 @@ rtems_shell_nfs_mounter (const char* device, return -1; } + if (rpcUdpInit () < 0) + { + fprintf (stdout, "error: initialising RPC\n"); + return -1; + } + + nfsInit (0, 0); + uidhost = strdup (device); path = strchr (uidhost, ':'); *path = '\0'; @@ -56,7 +64,7 @@ rtems_shell_nfs_mounter (const char* device, } rtems_shell_filesystems_t rtems_shell_Mount_NFS = { - name: "ftp", + name: "nfs", driver_needed: 1, fs_ops: NULL, mounter: rtems_shell_nfs_mounter diff --git a/cpukit/libmisc/shell/shell.c b/cpukit/libmisc/shell/shell.c index 4bbbec4033..267ce203fa 100644 --- a/cpukit/libmisc/shell/shell.c +++ b/cpukit/libmisc/shell/shell.c @@ -51,27 +51,28 @@ rtems_shell_env_t *rtems_shell_init_env( { rtems_shell_env_t *shell_env; + if (rtems_global_shell_env.magic != 0x600D600d) { + rtems_global_shell_env.magic = 0x600D600d; + rtems_global_shell_env.devname = ""; + rtems_global_shell_env.taskname = "GLOBAL"; + rtems_global_shell_env.tcflag = 0; + rtems_global_shell_env.exit_shell = 0; + rtems_global_shell_env.forever = TRUE; + rtems_global_shell_env.input = 0; + rtems_global_shell_env.output = 0; + rtems_global_shell_env.output_append = 0; + } + shell_env = shell_env_arg; if ( !shell_env ) { shell_env = malloc(sizeof(rtems_shell_env_t)); if ( !shell_env ) return NULL; + *shell_env = rtems_global_shell_env; + shell_env->taskname = NULL; } - if (rtems_global_shell_env.magic != 0x600D600d) { - rtems_global_shell_env.magic = 0x600D600d; - rtems_global_shell_env.devname = ""; - rtems_global_shell_env.taskname = "GLOBAL"; - rtems_global_shell_env.tcflag = 0; - rtems_global_shell_env.exit_shell = 0; - rtems_global_shell_env.forever = TRUE; - } - - *shell_env = rtems_global_shell_env; - shell_env->taskname = NULL; - shell_env->forever = FALSE; - return shell_env; } @@ -344,10 +345,12 @@ void rtems_shell_print_env( rtems_task rtems_shell_task(rtems_task_argument task_argument) { - rtems_shell_env_t * shell_env = (rtems_shell_env_t*) task_argument; - - rtems_shell_main_loop( shell_env ); - rtems_task_delete( RTEMS_SELF ); + rtems_shell_env_t *shell_env = (rtems_shell_env_t*) task_argument; + rtems_id wake_on_end = shell_env->wake_on_end; + rtems_shell_main_loop( shell_env ); + if (wake_on_end != RTEMS_INVALID_ID) + rtems_event_send (wake_on_end, RTEMS_EVENT_1); + rtems_task_delete( RTEMS_SELF ); } #define RTEMS_SHELL_MAXIMUM_ARGUMENTS 128 @@ -365,14 +368,20 @@ rtems_boolean rtems_shell_main_loop( char last_cmd[256]; /* to repeat 'r' */ int argc; char *argv[RTEMS_SHELL_MAXIMUM_ARGUMENTS]; + rtems_boolean result = TRUE; + rtems_boolean input_file = FALSE; + int line = 0; rtems_shell_initialize_command_set(); + shell_env = + rtems_current_shell_env = rtems_shell_init_env( shell_env_arg ); + /* * @todo chrisj - * Remove the use of task variables. Chnage to have a single + * Remove the use of task variables. Change to have a single * allocation per shell and then set into a notepad register - * in the TCP. Provide a function to return the pointer. + * in the TCB. Provide a function to return the pointer. * Task variables are a virus to embedded systems software. */ sc = rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_shell_env,free); @@ -381,76 +390,127 @@ rtems_boolean rtems_shell_main_loop( return FALSE; } - shell_env = - rtems_current_shell_env = rtems_shell_init_env( shell_env_arg ); - setuid(0); setgid(0); rtems_current_user_env->euid = rtems_current_user_env->egid = 0; - setvbuf(stdin,NULL,_IONBF,0); /* Not buffered*/ - /* make a raw terminal,Linux Manuals */ - if (tcgetattr(fileno(stdin), &term) >= 0) { - term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); - term.c_oflag &= ~OPOST; - term.c_oflag |= (OPOST|ONLCR); /* But with cr+nl on output */ - term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - if (shell_env->tcflag) - term.c_cflag = shell_env->tcflag; - term.c_cflag |= CLOCAL | CREAD; - term.c_cc[VMIN] = 1; - term.c_cc[VTIME] = 0; - if (tcsetattr (fileno(stdin), TCSADRAIN, &term) < 0) { - fprintf(stderr, - "shell:cannot set terminal attributes(%s)\n",shell_env->devname); + fileno(stdout); + + if (strcmp(shell_env->output, "stdout") != 0) { + if (strcmp(shell_env->output, "stderr") == 0) { + stdout = stderr; + } else if (strcmp(shell_env->output, "/dev/null") == 0) { + fclose (stdout); + } else { + FILE *output = fopen(shell_env_arg->output, + shell_env_arg->output_append ? "a" : "w"); + if (!output) { + fprintf(stderr, "shell: open output %s failed: %s\n", + shell_env_arg->output, strerror(errno)); + return FALSE; + } + stdout = output; + } + } + + if (strcmp(shell_env_arg->input, "stdin") != 0) { + FILE *input = fopen(shell_env_arg->input, "r"); + if (!input) { + fprintf(stderr, "shell: open input %s failed: %s\n", + shell_env_arg->input, strerror(errno)); + return FALSE; + } + stdin = input; + shell_env->forever = FALSE; + input_file = TRUE; + } + else { + /* make a raw terminal,Linux Manuals */ + if (tcgetattr(fileno(stdin), &term) >= 0) { + term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + term.c_oflag &= ~OPOST; + term.c_oflag |= (OPOST|ONLCR); /* But with cr+nl on output */ + term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + if (shell_env->tcflag) + term.c_cflag = shell_env->tcflag; + term.c_cflag |= CLOCAL | CREAD; + term.c_cc[VMIN] = 1; + term.c_cc[VTIME] = 0; + if (tcsetattr (fileno(stdin), TCSADRAIN, &term) < 0) { + fprintf(stderr, + "shell:cannot set terminal attributes(%s)\n",shell_env->devname); + } } - setvbuf(stdout,NULL,_IONBF,0); /* Not buffered*/ } + setvbuf(stdin,NULL,_IONBF,0); /* Not buffered*/ + setvbuf(stdout,NULL,_IONBF,0); /* Not buffered*/ + rtems_shell_initialize_command_set(); do { /* Set again root user and root filesystem, side effect of set_priv..*/ sc = rtems_libio_set_private_env(); if (sc != RTEMS_SUCCESSFUL) { rtems_error(sc,"rtems_libio_set_private_env():"); - return FALSE; + result = FALSE; + break; } - if (!rtems_shell_login(stdin,stdout)) { - rtems_shell_cat_file(stdout,"/etc/motd"); + if (input_file || !rtems_shell_login(stdin,stdout)) { + const char *c; strcpy(last_cmd,""); strcpy(cmd,""); - printf("\n" - "RTEMS SHELL (Ver.1.0-FRC):%s. "__DATE__". 'help' to list commands.\n", - shell_env->devname); + if (!input_file) { + rtems_shell_cat_file(stdout,"/etc/motd"); + fprintf(stdout, "\n" + "RTEMS SHELL (Ver.1.0-FRC):%s. " \ + __DATE__". 'help' to list commands.\n", + shell_env->devname); + } chdir("/"); /* XXX: chdir to getpwent homedir */ shell_env->exit_shell = FALSE; for (;;) { /* Prompt section */ - /* XXX: show_prompt user adjustable */ - getcwd(curdir,sizeof(curdir)); - printf( "%s%s[%s] %c ", - ((shell_env->taskname) ? shell_env->taskname : ""), - ((shell_env->taskname) ? " " : ""), - curdir, - geteuid()?'$':'#' - ); + if (!input_file) { + /* XXX: show_prompt user adjustable */ + getcwd(curdir,sizeof(curdir)); + fprintf(stdout, "%s%s[%s] %c ", + ((shell_env->taskname) ? shell_env->taskname : ""), + ((shell_env->taskname) ? " " : ""), + curdir, + geteuid()?'$':'#'); + } + /* getcmd section */ if (!rtems_shell_scanline(cmd,sizeof(cmd),stdin,stdout)) { break; /*EOF*/ } + line++; + /* evaluate cmd section */ + c = cmd; + while (*c) { + if (!isblank(*c)) + break; + c++; + } + + if (*c == '\0') /* empty line */ + continue; + if (*c == '#') /* comment character */ + continue; + if (!strcmp(cmd,"e")) { /* edit last command */ strcpy(cmd,last_cmd); continue; } else if (!strcmp(cmd,"r")) { /* repeat last command */ strcpy(cmd,last_cmd); - } else if (!strcmp(cmd,"bye")) { /* exit to telnetd */ - printf("Shell exiting\n" ); - return TRUE; - } else if (!strcmp(cmd,"exit")) { /* exit application */ - printf("System shutting down at user request\n" ); + } else if (!strcmp(cmd,"bye") || !strcmp(cmd,"exit")) { + fprintf(stdout, "Shell exiting\n" ); + break; + } else if (!strcmp(cmd,"shutdown")) { /* exit application */ + fprintf(stdout, "System shutting down at user request\n" ); exit(0); } else if (!strcmp(cmd,"")) { /* only for get a new prompt */ strcpy(last_cmd,cmd); @@ -469,7 +529,7 @@ rtems_boolean rtems_shell_main_loop( if ( argv[0] == NULL ) { shell_env->errorlevel = -1; } else if ( shell_cmd == NULL ) { - printf("shell:%s command not found\n", argv[0]); + fprintf(stdout, "shell:%s command not found\n", argv[0]); shell_env->errorlevel = -1; } else { shell_env->errorlevel = shell_cmd->command(argc, argv); @@ -482,22 +542,25 @@ rtems_boolean rtems_shell_main_loop( strcpy(last_cmd, cmd); cmd[0] = 0; } - printf("\nGoodbye from RTEMS SHELL :-(\n"); fflush( stdout ); fflush( stderr ); } - } while (shell_env->forever); - return TRUE; + } while (result && shell_env->forever); + return result; } /* ----------------------------------------------- */ -rtems_status_code rtems_shell_init ( +static rtems_status_code rtems_shell_run ( char *task_name, uint32_t task_stacksize, rtems_task_priority task_priority, char *devname, tcflag_t tcflag, - int forever + int forever, + const char* input, + const char* output, + int output_append, + rtems_id wake_on_end ) { rtems_id task_id; @@ -526,15 +589,67 @@ rtems_status_code rtems_shell_init ( shell_env = rtems_shell_init_env( NULL ); if ( !shell_env ) { - rtems_error(sc,"allocating shell_env %s in shell_init()",task_name); + rtems_error(RTEMS_NO_MEMORY, + "allocating shell_env %s in shell_init()",task_name); return RTEMS_NO_MEMORY; } - shell_env->devname = devname; - shell_env->taskname = task_name; - shell_env->tcflag = tcflag; - shell_env->exit_shell = FALSE; - shell_env->forever = forever; + shell_env->devname = devname; + shell_env->taskname = task_name; + shell_env->tcflag = tcflag; + shell_env->exit_shell = FALSE; + shell_env->forever = forever; + shell_env->input = strdup (input); + shell_env->output = strdup (output); + shell_env->output_append = output_append; + shell_env->wake_on_end = wake_on_end; return rtems_task_start(task_id, rtems_shell_task, (rtems_task_argument) shell_env); } + +rtems_status_code rtems_shell_init ( + char *task_name, + uint32_t task_stacksize, + rtems_task_priority task_priority, + char *devname, + tcflag_t tcflag, + int forever +) +{ + return rtems_shell_run (task_name, task_stacksize, task_priority, + devname, tcflag, forever, + "stdin", "stdout", 0, RTEMS_INVALID_ID); +} + +rtems_status_code rtems_shell_script ( + char *task_name, + uint32_t task_stacksize, + rtems_task_priority task_priority, + const char* input, + const char* output, + int output_append, + int wait +) +{ + rtems_id current_task = RTEMS_INVALID_ID; + rtems_status_code sc; + + if (wait) { + sc = rtems_task_ident (RTEMS_SELF, RTEMS_LOCAL, ¤t_task); + if (sc != RTEMS_SUCCESSFUL) + return sc; + } + + sc = rtems_shell_run (task_name, task_stacksize, task_priority, + NULL, 0, 0, input, output, output_append, + current_task); + if (sc != RTEMS_SUCCESSFUL) + return sc; + + if (wait) { + rtems_event_set out; + sc = rtems_event_receive (RTEMS_EVENT_1, RTEMS_WAIT, 0, &out); + } + + return sc; +} diff --git a/cpukit/libmisc/shell/shell.h b/cpukit/libmisc/shell/shell.h index d9a2a53fe7..d9312a4a6c 100644 --- a/cpukit/libmisc/shell/shell.h +++ b/cpukit/libmisc/shell/shell.h @@ -101,6 +101,29 @@ rtems_status_code rtems_shell_init( int forever ); +/** + * Run a shell script creating a shell tasks to execute the command under. + * + * @param task_name Name of the shell task. + * @param task_stacksize The size of the stack. If 0 the default size is used. + * @param task_priority The priority the shell runs at. + * @param input The file of commands. Can be 'stdin' to use stdin. + * @param output The output file to write commands to. Can be 'stdout', + * 'stderr' or '/dev/null'. + * @param output_append Append the output to the file or truncate the file. + * Create if it does not exist. + * @param wait Wait for the script to finish. + */ +rtems_status_code rtems_shell_script( + char *task_name, + uint32_t task_stacksize, /*0 default*/ + rtems_task_priority task_priority, + const char *input, + const char *output, + int output_append, + int wait +); + /* * Things that are useful to external entities developing commands and plugging * them in. @@ -117,6 +140,10 @@ typedef struct { int forever ; /* repeat login */ int errorlevel; uintptr_t mdump_addr; + const char* input; + const char* output; + int output_append; + rtems_id wake_on_end; } rtems_shell_env_t; rtems_boolean rtems_shell_main_loop( diff --git a/cpukit/libmisc/shell/shellconfig.h b/cpukit/libmisc/shell/shellconfig.h index 1596c10ca9..612d428efc 100644 --- a/cpukit/libmisc/shell/shellconfig.h +++ b/cpukit/libmisc/shell/shellconfig.h @@ -34,6 +34,7 @@ extern rtems_shell_cmd_t rtems_shell_ID_Command; extern rtems_shell_cmd_t rtems_shell_TTY_Command; extern rtems_shell_cmd_t rtems_shell_WHOAMI_Command; +extern rtems_shell_cmd_t rtems_shell_CP_Command; extern rtems_shell_cmd_t rtems_shell_PWD_Command; extern rtems_shell_cmd_t rtems_shell_LS_Command; extern rtems_shell_cmd_t rtems_shell_CHDIR_Command; @@ -192,6 +193,11 @@ extern rtems_shell_filesystems_t *rtems_shell_Mount_filesystems[]; /* * File and directory commands */ + #if (defined(CONFIGURE_SHELL_COMMANDS_ALL) && \ + !defined(CONFIGURE_SHELL_NO_COMMAND_CP)) || \ + defined(CONFIGURE_SHELL_COMMAND_CP) + &rtems_shell_CP_Command, + #endif #if (defined(CONFIGURE_SHELL_COMMANDS_ALL) && \ !defined(CONFIGURE_SHELL_NO_COMMAND_PWD)) || \ defined(CONFIGURE_SHELL_COMMAND_PWD) diff --git a/cpukit/libmisc/shell/utils-cp.c b/cpukit/libmisc/shell/utils-cp.c new file mode 100644 index 0000000000..52cbc63470 --- /dev/null +++ b/cpukit/libmisc/shell/utils-cp.c @@ -0,0 +1,350 @@ +/* $NetBSD: utils.c,v 1.29 2005/10/15 18:22:18 christos Exp $ */ + +/*- + * Copyright (c) 1991, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if 0 +#ifndef lint +#if 0 +static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94"; +#else +__RCSID("$NetBSD: utils.c,v 1.29 2005/10/15 18:22:18 christos Exp $"); +#endif +#endif /* not lint */ +#endif + +#if 0 +#include +#endif +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "extern-cp.h" + +#define lchmod chmod +#define lchown chown + +int +set_utimes(const char *file, struct stat *fs) +{ + struct utimbuf tv; + + tv.actime = fs->st_atime; + tv.modtime = fs->st_mtime; + + if (utime(file, &tv)) { + warn("lutimes: %s", file); + return (1); + } + return (0); +} + +int +copy_file(rtems_shell_cp_globals* cp_globals, FTSENT *entp, int dne) +{ + static char buf[MAXBSIZE]; + struct stat to_stat, *fs; + int ch, checkch, from_fd, rcount, rval, to_fd, wcount; + + if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) { + warn("%s", entp->fts_path); + return (1); + } + + fs = entp->fts_statp; + + /* + * If the file exists and we're interactive, verify with the user. + * If the file DNE, set the mode to be the from file, minus setuid + * bits, modified by the umask; arguably wrong, but it makes copying + * executables work right and it's been that way forever. (The + * other choice is 666 or'ed with the execute bits on the from file + * modified by the umask.) + */ + if (!dne) { + if (iflag) { + (void)fprintf(stderr, "overwrite %s? ", to.p_path); + checkch = ch = getchar(); + while (ch != '\n' && ch != EOF) + ch = getchar(); + if (checkch != 'y' && checkch != 'Y') { + (void)close(from_fd); + return (0); + } + } + /* overwrite existing destination file name */ + to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0); + } else + to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, + fs->st_mode & ~(S_ISUID | S_ISGID)); + + if (to_fd == -1 && fflag) { + /* + * attempt to remove existing destination file name and + * create a new file + */ + (void)unlink(to.p_path); + to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, + fs->st_mode & ~(S_ISUID | S_ISGID)); + } + + if (to_fd == -1) { + warn("%s", to.p_path); + (void)close(from_fd); + return (1); + } + + rval = 0; + + /* + * There's no reason to do anything other than close the file + * now if it's empty, so let's not bother. + */ + + if (fs->st_size) { + + /* + * Mmap and write if less than 8M (the limit is so + * we don't totally trash memory on big files). + * This is really a minor hack, but it wins some CPU back. + */ +#if 0 + if (fs->st_size <= 8 * 1048576) { + size_t fsize = (size_t)fs->st_size; + p = mmap(NULL, fsize, PROT_READ, MAP_FILE|MAP_SHARED, + from_fd, (off_t)0); + if (p == MAP_FAILED) { + goto mmap_failed; + } else { + (void) madvise(p, (size_t)fs->st_size, + MADV_SEQUENTIAL); + if (write(to_fd, p, fsize) != + fs->st_size) { + warn("%s", to.p_path); + rval = 1; + } + if (munmap(p, fsize) < 0) { + warn("%s", entp->fts_path); + rval = 1; + } + } + } else { +mmap_failed: +#endif + { + while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { + wcount = write(to_fd, buf, (size_t)rcount); + if (rcount != wcount || wcount == -1) { + warn("%s", to.p_path); + rval = 1; + break; + } + } + if (rcount < 0) { + warn("%s", entp->fts_path); + rval = 1; + } + } + } + + if (rval == 1) { + (void)close(from_fd); + (void)close(to_fd); + return (1); + } + + if (pflag && setfile(cp_globals, fs, to_fd)) + rval = 1; + /* + * If the source was setuid or setgid, lose the bits unless the + * copy is owned by the same user and group. + */ +#define RETAINBITS \ + (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) + else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) { + if (fstat(to_fd, &to_stat)) { + warn("%s", to.p_path); + rval = 1; + } else if (fs->st_gid == to_stat.st_gid && + fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) { + warn("%s", to.p_path); + rval = 1; + } + } + (void)close(from_fd); + if (close(to_fd)) { + warn("%s", to.p_path); + rval = 1; + } + /* set the mod/access times now after close of the fd */ + if (pflag && set_utimes(to.p_path, fs)) { + rval = 1; + } + return (rval); +} + +int +copy_link(rtems_shell_cp_globals* cp_globals, FTSENT *p, int exists) +{ + int len; + char target[MAXPATHLEN]; + + if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) { + warn("readlink: %s", p->fts_path); + return (1); + } + target[len] = '\0'; + if (exists && unlink(to.p_path)) { + warn("unlink: %s", to.p_path); + return (1); + } + if (symlink(target, to.p_path)) { + warn("symlink: %s", target); + return (1); + } + return (pflag ? setfile(cp_globals, p->fts_statp, 0) : 0); +} + +int +copy_fifo(rtems_shell_cp_globals* cp_globals, struct stat *from_stat, int exists) +{ + if (exists && unlink(to.p_path)) { + warn("unlink: %s", to.p_path); + return (1); + } + if (mkfifo(to.p_path, from_stat->st_mode)) { + warn("mkfifo: %s", to.p_path); + return (1); + } + return (pflag ? setfile(cp_globals, from_stat, 0) : 0); +} + +int +copy_special(rtems_shell_cp_globals* cp_globals, struct stat *from_stat, int exists) +{ + if (exists && unlink(to.p_path)) { + warn("unlink: %s", to.p_path); + return (1); + } + if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) { + warn("mknod: %s", to.p_path); + return (1); + } + return (pflag ? setfile(cp_globals, from_stat, 0) : 0); +} + + +/* + * Function: setfile + * + * Purpose: + * Set the owner/group/permissions for the "to" file to the information + * in the stat structure. If fd is zero, also call set_utimes() to set + * the mod/access times. If fd is non-zero, the caller must do a utimes + * itself after close(fd). + */ +#define st_flags st_mode + +int +setfile(rtems_shell_cp_globals* cp_globals, struct stat *fs, int fd) +{ + int rval, islink; + + rval = 0; + islink = S_ISLNK(fs->st_mode); + fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; + + /* + * Changing the ownership probably won't succeed, unless we're root + * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting + * the mode; current BSD behavior is to remove all setuid bits on + * chown. If chown fails, lose setuid/setgid bits. + */ +#if 0 + if (fd ? fchown(fd, fs->st_uid, fs->st_gid) : + lchown(to.p_path, fs->st_uid, fs->st_gid)) { +#endif + if (lchown(to.p_path, fs->st_uid, fs->st_gid)) { + if (errno != EPERM) { + warn("chown: %s", to.p_path); + rval = 1; + } + fs->st_mode &= ~(S_ISUID | S_ISGID); + } + if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) { + warn("chmod: %s", to.p_path); + rval = 1; + } + +#if 0 + if (!islink && !Nflag) { + unsigned long fflags = fs->st_flags; + /* + * XXX + * NFS doesn't support chflags; ignore errors unless + * there's reason to believe we're losing bits. + * (Note, this still won't be right if the server + * supports flags and we were trying to *remove* flags + * on a file that we copied, i.e., that we didn't create.) + */ + errno = 0; + if ((fd ? fchflags(fd, fflags) : + chflags(to.p_path, fflags)) == -1) + if (errno != EOPNOTSUPP || fs->st_flags != 0) { + warn("chflags: %s", to.p_path); + rval = 1; + } + } +#endif + /* if fd is non-zero, caller must call set_utimes() after close() */ + if (fd == 0 && set_utimes(to.p_path, fs)) + rval = 1; + return (rval); +} + +void +usage(void) +{ + (void)fprintf(stderr, + "usage: %s [-R [-H | -L | -P]] [-f | -i] [-pv] src target\n" + " %s [-R [-H | -L | -P]] [-f | -i] [-pv] src1 ... srcN directory\n", + "cp", "cp"); +} diff --git a/cpukit/libmisc/shell/verr.c b/cpukit/libmisc/shell/verr.c new file mode 100644 index 0000000000..fc6d82a9ec --- /dev/null +++ b/cpukit/libmisc/shell/verr.c @@ -0,0 +1,71 @@ +/* $NetBSD: verr.c,v 1.13 2005/09/13 01:44:09 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: verr.c,v 1.13 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(verr, _verr) +#endif + +__dead void +verr(jmp_buf* exit_jmp, int eval, const char *fmt, _BSD_VA_LIST_ ap) +{ + int sverrno; + + sverrno = errno; +#if 0 + (void)fprintf(stderr, "%s: ", getprogname()); +#endif + if (fmt != NULL) { + (void)vfprintf(stdout, fmt, ap); + (void)fprintf(stdout, ": "); + } + (void)fprintf(stdout, "%s\n", strerror(sverrno)); + longjmp (*exit_jmp, 1); +} diff --git a/cpukit/libmisc/shell/verrx.c b/cpukit/libmisc/shell/verrx.c new file mode 100644 index 0000000000..a9d57f4d28 --- /dev/null +++ b/cpukit/libmisc/shell/verrx.c @@ -0,0 +1,64 @@ +/* $NetBSD: verrx.c,v 1.13 2005/09/13 01:44:09 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: verrx.c,v 1.13 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(verrx, _verrx) +#endif + +__dead void +verrx(jmp_buf* exit_jmp, int eval, const char *fmt, _BSD_VA_LIST_ ap) +{ +#if 0 + (void)fprintf(stderr, "%s: ", getprogname()); +#endif + if (fmt != NULL) + (void)vfprintf(stdout, fmt, ap); + (void)fprintf(stdout, "\n"); + longjmp (*exit_jmp, 1); +} diff --git a/cpukit/libmisc/shell/vwarn.c b/cpukit/libmisc/shell/vwarn.c new file mode 100644 index 0000000000..0d5928a685 --- /dev/null +++ b/cpukit/libmisc/shell/vwarn.c @@ -0,0 +1,70 @@ +/* $NetBSD: vwarn.c,v 1.13 2005/09/13 01:44:09 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: vwarn.c,v 1.13 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(vwarn, _vwarn) +#endif + +void +vwarn(const char *fmt, _BSD_VA_LIST_ ap) +{ + int sverrno; + + sverrno = errno; +#if 0 + (void)fprintf(stderr, "%s: ", getprogname()); +#endif + if (fmt != NULL) { + (void)vfprintf(stdout, fmt, ap); + (void)fprintf(stdout, ": "); + } + (void)fprintf(stdout, "%s\n", strerror(sverrno)); +} diff --git a/cpukit/libmisc/shell/vwarnx.c b/cpukit/libmisc/shell/vwarnx.c new file mode 100644 index 0000000000..4d70c21891 --- /dev/null +++ b/cpukit/libmisc/shell/vwarnx.c @@ -0,0 +1,63 @@ +/* $NetBSD: vwarnx.c,v 1.13 2005/09/13 01:44:09 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: vwarnx.c,v 1.13 2005/09/13 01:44:09 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(vwarnx, _vwarnx) +#endif + +void +vwarnx(const char *fmt, _BSD_VA_LIST_ ap) +{ +#if 0 + (void)fprintf(stderr, "%s: ", getprogname()); +#endif + if (fmt != NULL) + (void)vfprintf(stdout, fmt, ap); + (void)fprintf(stdout, "\n"); +} diff --git a/cpukit/libmisc/shell/warn.c b/cpukit/libmisc/shell/warn.c new file mode 100644 index 0000000000..dfadcd8fa2 --- /dev/null +++ b/cpukit/libmisc/shell/warn.c @@ -0,0 +1,60 @@ +/* $NetBSD: warn.c,v 1.13 2005/09/13 13:51:50 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: warn.c,v 1.13 2005/09/13 13:51:50 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#ifdef __weak_alias +__weak_alias(warn, _warn) +#endif + +void +warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); +} diff --git a/cpukit/libmisc/shell/warnx.c b/cpukit/libmisc/shell/warnx.c new file mode 100644 index 0000000000..1c67eac786 --- /dev/null +++ b/cpukit/libmisc/shell/warnx.c @@ -0,0 +1,60 @@ +/* $NetBSD: warnx.c,v 1.13 2005/09/13 13:51:50 christos Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: warnx.c,v 1.13 2005/09/13 13:51:50 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#ifdef __weak_alias +__weak_alias(warnx, _warnx) +#endif + +void +warnx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} diff --git a/cpukit/libnetworking/rtems/rtems_glue.c b/cpukit/libnetworking/rtems/rtems_glue.c index ad254dfca1..1313738def 100644 --- a/cpukit/libnetworking/rtems/rtems_glue.c +++ b/cpukit/libnetworking/rtems/rtems_glue.c @@ -311,13 +311,15 @@ rtems_bsdnet_semaphore_obtain (void) level ); if (_Thread_Executing->Wait.return_code) - rtems_panic ("Can't obtain network semaphore\n"); + rtems_panic ("rtems-net: can't obtain network sema: %d\n", + _Thread_Executing->Wait.return_code); #else rtems_status_code sc; sc = rtems_semaphore_obtain (networkSemaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT); if (sc != RTEMS_SUCCESSFUL) - rtems_panic ("Can't obtain network semaphore: `%s'\n", rtems_status_text (sc)); + rtems_panic ("rtems-net: can't obtain network semaphore: `%s'\n", + rtems_status_text (sc)); #endif } @@ -338,13 +340,14 @@ rtems_bsdnet_semaphore_release (void) ); _Thread_Enable_dispatch(); if (i) - rtems_panic ("Can't release network semaphore\n"); + rtems_panic ("rtems-net: can't release network sema: %i\n"); #else rtems_status_code sc; sc = rtems_semaphore_release (networkSemaphore); if (sc != RTEMS_SUCCESSFUL) - rtems_panic ("Can't release network semaphore: `%s'\n", rtems_status_text (sc)); + rtems_panic ("rtems-net: can't release network semaphore: `%s'\n", + rtems_status_text (sc)); #endif } diff --git a/cpukit/score/Makefile.am b/cpukit/score/Makefile.am index 0152e138b3..9e412863f7 100644 --- a/cpukit/score/Makefile.am +++ b/cpukit/score/Makefile.am @@ -113,7 +113,7 @@ libscore_a_SOURCES += src/heap.c src/heapallocate.c src/heapextend.c \ libscore_a_SOURCES += src/object.c src/objectallocate.c \ src/objectclearname.c src/objectcomparenameraw.c \ src/objectcomparenamestring.c src/objectcopynameraw.c \ - src/objectcopynamestring.c \ src/objectextendinformation.c \ + src/objectcopynamestring.c src/objectextendinformation.c \ src/objectfree.c src/objectget.c src/objectgetisr.c src/objectgetnext.c \ src/objectinitializeinformation.c src/objectnametoid.c \ src/objectshrinkinformation.c src/objectgetnoprotection.c \ diff --git a/cpukit/score/inline/rtems/score/coremutex.inl b/cpukit/score/inline/rtems/score/coremutex.inl index 9311cd11a5..8d417568e9 100644 --- a/cpukit/score/inline/rtems/score/coremutex.inl +++ b/cpukit/score/inline/rtems/score/coremutex.inl @@ -141,7 +141,8 @@ RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body( _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ){ #ifdef __STRICT_ORDER_MUTEX__ - _Chain_Prepend_unprotected(&executing->lock_mutex,&the_mutex->queue.lock_queue); + _Chain_Prepend_unprotected( &executing->lock_mutex, + &the_mutex->queue.lock_queue ); the_mutex->queue.priority_before = executing->current_priority; #endif diff --git a/cpukit/score/src/coremutex.c b/cpukit/score/src/coremutex.c index 173418ffc0..f6e6c80177 100644 --- a/cpukit/score/src/coremutex.c +++ b/cpukit/score/src/coremutex.c @@ -64,6 +64,13 @@ void _CORE_mutex_Initialize( the_mutex->holder_id = _Thread_Executing->Object.id; if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) + +#ifdef __STRICT_ORDER_MUTEX__ + _Chain_Prepend_unprotected( &executing->lock_mutex, + &the_mutex->queue.lock_queue ); + the_mutex->queue.priority_before = executing->current_priority; +#endif + _Thread_Executing->resource_count++; } else { the_mutex->nest_count = 0; diff --git a/cpukit/telnetd/icmds.c b/cpukit/telnetd/icmds.c index 3f4cd0743e..2963c376d0 100644 --- a/cpukit/telnetd/icmds.c +++ b/cpukit/telnetd/icmds.c @@ -54,11 +54,11 @@ static int main_udp(int argc,char * argv[]) { /*+++++++++++++++++++++++++++++++++++++++++++++*/ void _rtems_telnetd_register_icmds(void) { - shell_add_cmd("inet" ,"net","inet routes" ,main_inet); - shell_add_cmd("mbuf" ,"net","mbuf stats" ,main_mbuf); - shell_add_cmd("if" ,"net","if stats" ,main_if ); - shell_add_cmd("ip" ,"net","ip stats" ,main_ip ); - shell_add_cmd("icmp" ,"net","icmp stats" ,main_icmp); - shell_add_cmd("tcp" ,"net","tcp stats" ,main_tcp ); - shell_add_cmd("udp" ,"net","udp stats" ,main_udp ); + rtems_shell_add_cmd("inet" ,"net","inet routes" ,main_inet); + rtems_shell_add_cmd("mbuf" ,"net","mbuf stats" ,main_mbuf); + rtems_shell_add_cmd("if" ,"net","if stats" ,main_if ); + rtems_shell_add_cmd("ip" ,"net","ip stats" ,main_ip ); + rtems_shell_add_cmd("icmp" ,"net","icmp stats" ,main_icmp); + rtems_shell_add_cmd("tcp" ,"net","tcp stats" ,main_tcp ); + rtems_shell_add_cmd("udp" ,"net","udp stats" ,main_udp ); } -- cgit v1.2.3