00001 // more/system.h -- a more/signal.h consistent system command 00002 // Copyright (C) 1999--2001 Petter Urkedal (petter.urkedal@matfys.lth.se) 00003 00004 // This file is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 00009 // This file is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 // As a special exception, you may use this file as part of a free 00019 // software library without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to 00022 // produce an executable, this file does not by itself cause the 00023 // resulting executable to be covered by the GNU General Public 00024 // License. This exception does not however invalidate any other 00025 // reasons why the executable file might be covered by the GNU General 00026 // Public License. 00027 00028 // $Id: system.h,v 1.1 2002/05/30 18:01:40 petter_urkedal Exp $ 00029 00030 00031 #ifndef MORE_SYS_SYSTEM_H 00032 #define MORE_SYS_SYSTEM_H 00033 00034 #include <string> 00035 #include <memory> 00036 #include <more/io/redirection.h> 00037 00038 namespace more { 00039 namespace sys { 00040 00041 inline char const* c_str(char const* s) { return s; } 00042 inline char const* c_str(std::string const& s) { return s.c_str(); } 00043 00044 /** Executes a system specific interpreted command. Be careful if 00045 you use in a secure program, as the string is interpreted by the 00046 shell. Better, use one of the other overloads of this name if 00047 you don't need the shell-functionality. */ 00048 int system(std::string); 00049 00050 /** Executes the program in argv[0] with arguments from the remaining 00051 of the null-terminated array. Check out the overloads with more 00052 arguments for a higher level iterface. */ 00053 int system(char const* const* argv, io::redirection const& redir); 00054 00055 /** Executes a program prgname with arguments given by the iterator 00056 range. The value type of the Iterator type must be std::string 00057 or char const*. If prgname does not contain a '/', the program 00058 is search for in an OS dependent way (e.g. using $PATH on Unix). 00059 For secure applications, use absolute path names (starting with 00060 '/') if possible. The program name shall not be included in the 00061 argument list. */ 00062 template <typename Iterator> 00063 inline int 00064 system(std::string prgname, Iterator args_beg, Iterator args_end, 00065 io::redirection const& redir = io::redirection()) 00066 { 00067 int n = std::distance(args_beg, args_end) + 2; 00068 std::auto_ptr<char const*> 00069 argv(new (char const*)[n]); 00070 char const** it = argv.get(); 00071 *it++ = prgname.c_str(); 00072 while (args_beg != args_end) { 00073 *it++ = c_str(*args_beg); 00074 ++args_beg; 00075 } 00076 *it++ = 0; 00077 return system(argv.get(), redir); 00078 } 00079 00080 }} // more::sys 00081 00082 #endif