pax_global_header00006660000000000000000000000064144457637250014533gustar00rootroot0000000000000052 comment=d357705f8678f5299d90fc33245b8783cfd0d29c s2argv-execs-1.4/000077500000000000000000000000001444576372500137105ustar00rootroot00000000000000s2argv-execs-1.4/CMakeLists.txt000066400000000000000000000031121444576372500164450ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.12) project(s2argv-execs VERSION 1.4 DESCRIPTION "execute a file taking its arguments from a string" HOMEPAGE_URL "https://github.com/rd235/s2argv-execs" LANGUAGES C) set(LIB_VERSION 1.4.0) set(LIB_SOVERSION 1) include(GNUInstallDirs) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic") include_directories(${CMAKE_CURRENT_SOURCE_DIR}) add_library(execs SHARED execs.c noshell.c) set_target_properties(execs PROPERTIES VERSION ${LIB_VERSION} SOVERSION ${LIB_SOVERSION}) add_library(execs-embedded SHARED execs.c) target_compile_options(execs-embedded PUBLIC -DEEXECS) set_target_properties(execs-embedded PROPERTIES VERSION ${LIB_VERSION} SOVERSION ${LIB_SOVERSION}) add_library(execs_static STATIC execs.c noshell.c) set_target_properties(execs_static PROPERTIES OUTPUT_NAME execs) add_library(execs-embedded_static STATIC execs.c) set_target_properties(execs-embedded_static PROPERTIES OUTPUT_NAME execs-embedded) target_compile_options(execs-embedded_static PUBLIC -DEEXECS) install(TARGETS execs LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(TARGETS execs-embedded LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(TARGETS execs_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(TARGETS execs-embedded_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES execs.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) add_executable(exectest execstest.c) target_link_libraries(exectest execs) add_subdirectory(man) add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${PROJECT_SOURCE_DIR}/Uninstall.cmake") s2argv-execs-1.4/COPYING000066400000000000000000000636421444576372500147560ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! s2argv-execs-1.4/README.md000066400000000000000000000007211444576372500151670ustar00rootroot00000000000000s2argv-execs ============ s2argv converts a command string into an argv array for execv, execvp, execvpe. execs is like execv taking a string instead of an argv. Similarly execsp and execspe are the counterpart of execvp and execvpe, respectively, using command strings. Use cases are included in the man pages. To install this library: ``` $ mkdir build $ cd build $ cmake .. $ make $ sudo make install ``` Copyright Renzo Davoli 2014-2020, renzo@cs.unibo.it s2argv-execs-1.4/Uninstall.cmake000066400000000000000000000010361444576372500166630ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.13) set(MANIFEST "${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt") if(NOT EXISTS ${MANIFEST}) message(FATAL_ERROR "Cannot find install manifest: '${MANIFEST}'") endif() file(STRINGS ${MANIFEST} files) foreach(file ${files}) if(EXISTS ${file} OR IS_SYMLINK ${file}) message(STATUS "Removing: ${file}") execute_process( COMMAND rm -f ${file} RESULT_VARIABLE retcode ) if(NOT "${retcode}" STREQUAL "0") message(WARNING "Failed to remove: ${file}") endif() endif() endforeach(file) s2argv-execs-1.4/execs.c000066400000000000000000000146431444576372500151730ustar00rootroot00000000000000/* * s2argv: convert strings to argv * Copyright (C) 2014 Renzo Davoli. University of Bologna. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #define _GNU_SOURCE #include #include #include #include #include #include //char *sn[] = { "END", "SPACE", "CHAR", "SGLQ", "DBLQ", "ESCAPE", "SEMIC", "VAR", "ESCVAR", "DBLESC" }; #define END 0 #define SPACE 1 #define CHAR 2 #define SGLQ 3 //single quote #define DBLQ 4 //double quote #define ESCAPE 5 // escape '\' #define SEMIC 6 // semicolon ; #define VAR 7 // $name #define ESCVAR 8 // '\' in variable name #define DBLESC 9 // '\' in double quoted text #define NSTATES (DBLESC+1) #define NEWARG 0x1 // beginning of a new argument #define CHCOPY 0x2 // copy the char in current argv #define ENDARG 0x4 // end of an argument #define ENDVAR 0x8 // end of a variable #define ENDCMD 0x10 // end of a command /* This is the FSA used to get the lexical items of the command line */ static char nextstate[NSTATES][NSTATES-1]= { {END, 0, 0, 0, 0, 0, 0, 0}, // END {END,SPACE,CHAR,SGLQ,DBLQ,ESCAPE,SEMIC, VAR}, // SPACE {END,SPACE,CHAR,SGLQ,DBLQ,ESCAPE,SEMIC,CHAR}, // CHAR {END, SGLQ,SGLQ,CHAR,SGLQ, SGLQ, SGLQ,SGLQ}, // SGLQ {END, DBLQ,DBLQ,DBLQ,CHAR,DBLESC, DBLQ,DBLQ}, // DBLQ {END, CHAR,CHAR,CHAR,CHAR, CHAR, CHAR,CHAR}, // ESCAPE {END,SEMIC,CHAR,SGLQ,DBLQ,ESCAPE,SEMIC, VAR}, // SEMIC {END,SPACE, VAR, VAR, VAR, VAR,SEMIC, VAR}, // VAR {END, VAR, VAR, VAR, VAR, VAR, VAR, VAR}, // ESCVAR {END, DBLQ,DBLQ,DBLQ,DBLQ, DBLQ, DBLQ,DBLQ}}; // DBLESC static char action[NSTATES][NSTATES-1]= { {ENDCMD| 0, 0, 0, 0, 0, 0, 0, 0}, //END {ENDCMD| 0, 0,NEWARG|CHCOPY,NEWARG,NEWARG,NEWARG, ENDCMD,NEWARG}, //SPACE {ENDCMD|ENDARG,ENDARG, CHCOPY, 0, 0, 0,ENDCMD|ENDARG, 0}, //CHAR {ENDCMD|ENDARG,CHCOPY, CHCOPY, 0,CHCOPY,CHCOPY, CHCOPY,CHCOPY}, //SNGQ {ENDCMD|ENDARG,CHCOPY, CHCOPY,CHCOPY, 0,CHCOPY, CHCOPY,CHCOPY}, //DBLQ {ENDCMD|ENDARG,CHCOPY, CHCOPY,CHCOPY,CHCOPY,CHCOPY, CHCOPY,CHCOPY}, //ESCAPE {ENDCMD| 0, 0,NEWARG|CHCOPY,NEWARG,NEWARG,NEWARG, 0,NEWARG}, //SEMIC {ENDCMD|ENDVAR,ENDVAR, CHCOPY, 0, 0, 0,ENDCMD|ENDVAR, 0}, //VAR {ENDCMD|ENDVAR,CHCOPY, CHCOPY,CHCOPY,CHCOPY,CHCOPY, CHCOPY,CHCOPY}, //ESCVAR {ENDCMD|ENDARG,CHCOPY, CHCOPY,CHCOPY,CHCOPY,CHCOPY, CHCOPY,CHCOPY}}; //DBLESC s2argv_getvar_t s2argv_getvar=NULL; int (* execs_fork_security)(void *execs_fork_security_arg); void *execs_fork_security_arg; static int args_fsa(const char *args, char **argv, char *buf, int flags) { int state=SPACE; int argc=0; char *thisarg=NULL; for (;state != END;args++) { int this; switch (*args) { case 0: this=END; break; case ' ': case '\t': case '\n': this=SPACE; break; case '\'': this=SGLQ; break; case '"': this=DBLQ; break; case '\\': this=ESCAPE; break; case ';': this=SEMIC; break; case '$': this=VAR; break; default: this=CHAR; } if (argv) { if (action[state][this] & NEWARG) thisarg=buf; if (action[state][this] & CHCOPY) *buf++=*args; if (action[state][this] & ENDARG) { *buf++=0; *argv++=thisarg; } if (action[state][this] & ENDVAR) { *buf++=0; if (s2argv_getvar) { *argv=s2argv_getvar(thisarg); if (*argv == NULL) *argv=""; } else *argv=""; argv++; } if (action[state][this] & ENDCMD) *argv++=0; } if (action[state][this] & (ENDARG|ENDVAR)) argc++; if (action[state][this] & ENDCMD) argc++; //printf("%c %s+%s=%s %x\n",*args,sn[state],sn[this],sn[nextstate[state][this]],action[state][this]); //printf("%s %d->%d\n",args,state,nextstate[state][this]); state=nextstate[state][this]; switch (state) { case VAR: if (flags & EXECS_NOVAR) return errno = EINVAL, -1; break; case SEMIC: if (flags & EXECS_NOSEQ) return errno = EINVAL, -1; break; } } if (argv) *argv=0; return argc; } #ifndef EEXECS char **s2argv(const char *args) { int argc=args_fsa(args,NULL,NULL,0); char buf[strlen(args)+1]; char **argv=calloc(argc+1,sizeof(char *)); if (argv) { int i; args_fsa(args,argv,buf,0); for (i=0; i * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef S2ARGV_H #define S2ARGV_H #include #include #include #include #include #define EXECS_SOVERSION 1 extern char **environ; /* This header file declares all the functions defined in the libexecs and libeexecs libraries. libeexecs is a minimal subset of the libexecs library designed for embedded systems with strict memory requirements. It implements only the execs* functions. Programs using libexecs can also use the esystem (a.k.a. system_eexecsp) inline function */ /* fork security: if defined this function gets called for each child process created by this library. If it returns a non zero value it fails and exec is aborted */ /* it can be used to drop privileges such as capabilities */ extern int (* execs_fork_security)(void *execs_fork_security_arg); extern void *execs_fork_security_arg; /***************** library functions defined both in libexecs and in libeexecs ********/ /* execs is like execv: argv is computed by parsing args */ /* execsp is like execvp: argv is computed by parsing args, argv[0] is the executable file to be searched for along $PATH */ /* execse and execspe permit the specification of the environment (as in execve or execvpe) */ /* execs, execse, execsp and execspe do not require dynamic allocation *but* require an extra copy of args on the stack */ /* in all eexecs* functions, the string args is modified (no extra copies on the stack, args is parsed on itself): */ #define EXECS_NOSEQ 0x1 #define EXECS_NOVAR 0x2 int _execs_common(const char *path, const char *args, char *const envp[], char *buf, int flags); #define execs(path, args) _execs_common((path),(args),environ,NULL,EXECS_NOSEQ) #define execse(path, args, env) _execs_common((path),(args),(env),NULL,EXECS_NOSEQ) #define execsp(args) _execs_common(NULL,(args),environ,NULL,EXECS_NOSEQ) #define execspe(args,env) _execs_common(NULL,(args),(env),NULL,EXECS_NOSEQ) #define eexecs(path, args) _execs_common((path),(args),environ,(args),EXECS_NOSEQ) #define eexecse(path, args, env) _execs_common((path),(args),(env),(args),EXECS_NOSEQ) #define eexecsp(args) _execs_common(NULL,(args),environ,(args),EXECS_NOSEQ) #define eexecspe(args,env) _execs_common(NULL,(args),(env),(args),EXECS_NOSEQ) static inline int system_eexecsp(const char *command) { int status; pid_t pid; switch (pid=fork()) { case -1: return -1; case 0: if (__builtin_expect(execs_fork_security == NULL || execs_fork_security(execs_fork_security_arg) == 0, 1)) _execs_common(NULL, (char *) command, environ, (char *) command, 0); _exit(127); default: waitpid(pid,&status,0); return status; } } #define esystem(cmd) system_eexecsp(cmd) /******** library functions defined in libexecs only (not in libeexec) ********/ int _system_common(const char *path, const char *command, int redir[3], int flags); /* system_safe requires the absolute path of the command */ /* system_execs executes the program whose path has been passed as its first arg. */ #define system_safe(cmd) _system_common("",(cmd),NULL,EXECS_NOSEQ | EXECS_NOVAR) #define system_execs(path,cmd) _system_common((path),(cmd),NULL,EXECS_NOSEQ) #define system_execsp(cmd) _system_common(NULL,(cmd),NULL,EXECS_NOSEQ) #define system_execsa(cmd) _system_common("",(cmd),NULL,EXECS_NOSEQ) #define system_execsr(path,command,redir) _system_common((path),(cmd),(redir),EXECS_NOSEQ) #define system_execsrp(cmd,redir) _system_common(NULL,(cmd),(redir),EXECS_NOSEQ) #define system_execsra(cmd,redir) _system_common("",(cmd),(redir),EXECS_NOSEQ) /* system_nosh is an "almost" drop in replacement for system(3). it does not start a shell but it parses the arguments and runs the command */ /* system_execsq* support colon separated sequences of commands */ #define system_nosh(cmd) _system_common(NULL,(cmd),NULL,0) #define system_execsqp(cmd) _system_common(NULL,(cmd),NULL,0) #define system_execsqa(cmd) _system_common("",(cmd),NULL,0) #define system_execsqrp(cmd,redir) _system_common(NULL,(cmd),(redir),0) #define system_execsqra(cmd,redir) _system_common("",(cmd),(redir),0) FILE *_popen_common(const char *path, const char *command, const char *type, int flags); /* popen_execs/pclose_execs do not use $PATH to search the executable file*/ int pclose_execs(FILE *stream); /* popen_nosh is an "almost" drop in replacement for popen(3), and pclose_nosh is its counterpart for pclose(3). */ #define popen_nosh(cmd, type) _popen_common(NULL, (cmd), (type)) #define pclose_nosh(stream) _popen_common(stream) #define popen_execs(path, cmd, type) _popen_common(path, (cmd), (type), EXECS_NOSEQ) #define popen_execsp(cmd, type) _popen_common(NULL, (cmd), (type), EXECS_NOSEQ) #define pclose_execsp(stream) pclose_execs(stream) /* run a command in coprocessing mode */ pid_t _coprocess_common(const char *path, const char *command, char *const argv[], char *const envp[], int pipefd[2], int flags); #define coprocv(path, argv, pfd) _coprocess_common((path),NULL,(argv), environ, pfd, EXECS_NOSEQ) #define coprocve(path, argv, env, pfd) _coprocess_common((path),NULL,(argv), (env), pfd, EXECS_NOSEQ) #define coprocvp(file, argv, pfd) _coprocess_common(NULL,(file),(argv), environ, pfd, EXECS_NOSEQ) #define coprocvpe(file, argv, env, pfd) _coprocess_common(NULL,(file),(argv), (env), pfd, EXECS_NOSEQ) #define coprocs(path, cmd, pfd) _coprocess_common((path),(cmd),NULL, environ, pfd, EXECS_NOSEQ) #define coprocse(path, cmd, env, pfd) _coprocess_common((path),(cmd),NULL, (env), pfd, EXECS_NOSEQ) #define coprocsp(cmd, pfd) _coprocess_common(NULL,(cmd),NULL, environ, pfd, EXECS_NOSEQ) #define coprocspe(cmd, env, pfd) _coprocess_common(NULL,(cmd),NULL, (env), pfd, EXECS_NOSEQ) /* Low level argc management functions */ /* s2argv parses args. It allocates, initializes and returns an argv array, ready for execv. s2argv is able to parse several commands separated by semicolons (;). The return value is the sequence of all the corresponding argv (each one has a NULL element as its terminator) and one further NULL element terminates the whole sequence. (i.e. this multi-argv has two NULLs in a row at its end). This format is compatible with the standard argv. */ char **s2argv(const char *args); /* s2argv_free deallocates an argv returned by s2argv */ void s2argv_free(char **argv); /* the sum of s2argc for all commands, including the NULLs */ size_t s2argvlen(char **argv); /* argc of the (first) command */ /* argv=argv+s2argc(argv)+1 is the next argv */ size_t s2argc(char **argv); /* var definition function (e.g. s2argv_getvar=getenv)*/ typedef char * (* s2argv_getvar_t) (const char *name); extern s2argv_getvar_t s2argv_getvar; /* multi argv. Args can contain several commands semicolon (;) separated. This function parses args and calls f for each command/argv in args. If f returns 0 s2multiargv calls f for the following argv, otherwise returns the non-zero value. */ int s2multiargv(const char *args, int (*f)(char **argv, void *opaque), void *opaque, int flags); #endif s2argv-execs-1.4/execstest.c000066400000000000000000000033071444576372500160660ustar00rootroot00000000000000/* * s2argv: convert strings to argv * Copyright (C) 2014 Renzo Davoli. University of Bologna. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #define _GNU_SOURCE #include #include #include #include #include static void printargv(char **argv) { for(;*argv!=0;argv++) { int argc=0; for(;*argv!=0;argv++,argc++) printf("argv[%d]=\"%s\"\n",argc,*argv); argc++; } } static int print1argv(char **argv, void *useless) { int argc=0; for(;*argv!=0;argv++,argc++) printf("argv[%d]=\"%s\"\n",argc,*argv); return 0; } int main() { char buf[1024]; s2argv_getvar=getenv; while (1) { char **myargv; if (fgets(buf,1024,stdin) == NULL) return 0; buf[strlen(buf)-1]=0; myargv=s2argv(buf); printargv(myargv); printf("len %zd argc %zd\n", s2argvlen(myargv), s2argc(myargv)); s2argv_free(myargv); s2multiargv(buf, print1argv, NULL, 0); if (fork()==0) { eexecsp(buf); exit(-1); } else { int status; wait(&status); } } } s2argv-execs-1.4/man/000077500000000000000000000000001444576372500144635ustar00rootroot00000000000000s2argv-execs-1.4/man/CMakeLists.txt000066400000000000000000000017421444576372500172270ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.7) set(PANDOC_ORG "VirtualSquare") # ### pandoc pages file(GLOB VU_PANDOC_PAGES ${CMAKE_CURRENT_SOURCE_DIR}/*.[1-8].md) set(VU_MAN_FILES) foreach(VU_PANDOC_PATH IN LISTS VU_PANDOC_PAGES) # VU_PANDOCPAGE: basename of VU_PANDOC_PATH get_filename_component(VU_PANDOCPAGE ${VU_PANDOC_PATH} NAME) # VU_MANPAGE: VU_PANDOCPAGE without the suffix string(REGEX REPLACE "\.md$" "" VU_MANPAGE ${VU_PANDOCPAGE}) list(APPEND VU_MAN_FILES ${VU_MANPAGE}) endforeach(VU_PANDOC_PATH) add_custom_target(${PROJECT_NAME}_manpages ALL make PANDOC_ORG="${PANDOC_ORG}" ${VU_MAN_FILES} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) ### man pages file(GLOB VU_MAN_PAGES ${CMAKE_CURRENT_SOURCE_DIR}/*.[1-8]) foreach(VU_MAN_PATH IN LISTS VU_MAN_PAGES) get_filename_component(VU_MANPAGE ${VU_MAN_PATH} NAME) string(REGEX REPLACE ".*\\." "" MAN_CHAPTER ${VU_MANPAGE}) install(FILES ${VU_MAN_PATH} DESTINATION ${CMAKE_INSTALL_MANDIR}/man${MAN_CHAPTER}) endforeach(VU_MAN_PATH) s2argv-execs-1.4/man/Makefile000066400000000000000000000013341444576372500161240ustar00rootroot00000000000000PANDOC=pandoc PANDOCOK := $(shell command -v ${PANDOC} 2> /dev/null) none: % : %.md ifdef PANDOCOK # copy copyright notice grep "^\.\\\\\"" $< > $@ || true # run pandoc $(eval SECTION := $(subst .,,$(suffix $@))) $(eval BASENAME := $(basename $@)) $(eval TITLE := $(shell echo "${BASENAME}\(${SECTION}\)" | tr [:lower:] [:upper:])) $(eval HEADER := "$(shell man ${SECTION} intro | head -1 | sed -e 's/^[^[:blank:]]*[[:blank:]]*//' -e 's/[[:blank:]]*[^[:blank:]]*$$//' )") $(PANDOC) -standalone -M title=${TITLE} -M section=${SECTION} -M header=${HEADER} -M footer=${PANDOC_ORG} -M "date=`date +\"%B %Y\"`" --to man $< >> $@ else echo "${PANDOC} is not available. Manpage $@ cannot be updated" >/dev/stderr >&2 endif s2argv-execs-1.4/man/coprocess.3000066400000000000000000000061111444576372500165460ustar00rootroot00000000000000.\"* coprocess: exec extensions for coprocesing .\" Copyright (C) 2014 Renzo Davoli. University of Bologna. .\" .\" This library is free software; you can redistribute it and/or .\" modify it under the terms of the GNU Lesser General Public .\" License as published by the Free Software Foundation; either .\" version 2.1 of the License, or (at your option) any later version. .\" .\" This library is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" Lesser General Public License for more details. .\" .\" You should have received a copy of the GNU Lesser General Public .\" License along with this library; if not, write to the Free Software .\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .TH coprocess 3 2014-05-27 "VirtualSquare" "Linux Programmer's Manual" .SH NAME coprocv, coprocvp, coprocvpe, coprocs, coprocsp \- execute a command in coprocessing mode .SH SYNOPSIS .B #include .sp .BI "pid_t coprocv(const char *" path ", char *const " argv "[], int " pipefd "[2]);" .br .BI "pid_t coprocve(const char *" path ", char *const " argv "[]," .br .BI " char *const " envp "[], int " pipefd "[2]);" .br .BI "pid_t coprocvp(const char *" file ", char *const " argv "[], int " pipefd "[2]); .br .BI "pid_t coprocvpe(const char *" file ", char *const " argv "[]," .br .BI " char *const " envp "[], int " pipefd "[2]); .sp .BI "pid_t coprocs(const char *" path ", const char *" command ", int " pipefd "[2]); .br .BI "pid_t coprocse(const char *" path ", const char *" command "," .br .BI " char *const " envp "[], int " pipefd "[2]); .br .BI "pid_t coprocsp(const char *" command ", int " pipefd "[2]); .br .BI "pid_t coprocspe(const char *" command ", char *const " envp "[]," .br .BI " int " pipefd "[2]); .sp These functions are provided by libexecs. Link with \fI-lexecs\fR. .SH DESCRIPTION These functions run commands in coprocessing mode. They differs in the way to specify the executable file to run and its parameters. All of them define two pipes (by \fBpipe\fR(2)), create a new process by \fBfork\fR(2), redirect the standard input and output of the new process in the two pipes, and exec a program by one of the \fBexec\fR(3) functions. .sp The calling program controls both the standard input and the standard output of the called process: writing to \fBpipefd[1]\fR provides input to the coprocess while the coprocess output can be retrieved from \fBpipefd[0]\fR. .sp \fBcoprocv\fR, \fBcoprocve\fR, \fBcoprocvp\fR, \fBcoprocvpe\fR, \fBcoprocs\fR, \fBcoprocse\fR, \fBcoprocsp\fR, and \fBcoprocsp\fR differ as they define the executable file to run and its arguments as \fBexecv\fR, \fBexecve\fR, \fBexecvp\fR, \fBexecvpe\fR, \fBexecs\fR, \fBexecse\fR, \fBexecsp\fR, and \fBexecsp\fR, respectively. .SH BUGS Bug reports should be addressed to .SH AUTHOR Renzo Davoli s2argv-execs-1.4/man/coprocs.3000077700000000000000000000000001444576372500202762coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/coprocse.3000077700000000000000000000000001444576372500204432coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/coprocsp.3000077700000000000000000000000001444576372500204562coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/coprocspe.3000077700000000000000000000000001444576372500206232coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/coprocv.3000077700000000000000000000000001444576372500203012coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/coprocve.3000077700000000000000000000000001444576372500204462coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/coprocvp.3000077700000000000000000000000001444576372500204612coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/coprocvpe.3000077700000000000000000000000001444576372500206262coprocess.3ustar00rootroot00000000000000s2argv-execs-1.4/man/eexecs.3000077700000000000000000000000001444576372500172112execs.3ustar00rootroot00000000000000s2argv-execs-1.4/man/eexecse.3000077700000000000000000000000001444576372500173562execs.3ustar00rootroot00000000000000s2argv-execs-1.4/man/eexecsp.3000077700000000000000000000000001444576372500173712execs.3ustar00rootroot00000000000000s2argv-execs-1.4/man/eexecspe.3000077700000000000000000000000001444576372500175362execs.3ustar00rootroot00000000000000s2argv-execs-1.4/man/esystem.3000066400000000000000000000051711444576372500162440ustar00rootroot00000000000000.\"* esystem: system replacement not using any shell (iversion for embedded systems) .\" Copyright (C) 2014 Renzo Davoli. University of Bologna. .\" .\" This library is free software; you can redistribute it and/or .\" modify it under the terms of the GNU Lesser General Public .\" License as published by the Free Software Foundation; either .\" version 2.1 of the License, or (at your option) any later version. .\" .\" This library is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" Lesser General Public License for more details. .\" .\" You should have received a copy of the GNU Lesser General Public .\" License along with this library; if not, write to the Free Software .\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .TH system_nosh 3 2014-05-27 "VirtualSquare" "Linux Programmer's Manual" .SH NAME esystem, system_eexecsp \- execute a command with its arguments from a string without using a shell .SH SYNOPSIS .B #include .br .B #include .sp .BI "int esystem(const char *" command ");" .br .BI "int system_eexecsp(const char *" command ");" .sp These functions are provided by libexecs and libeexecs. Link with \fI-lexecs\fR or \fI-leexecs\fR. .SH DESCRIPTION \fBesystem\fR is an almost drop in replacement for \fBsystem\fR(3) provided by the libc. \fBesystem\fR parses the command string and runs the command directly, without using a shell. (\fBsystem_eexecsp\fR and \fBesystem\fR are synonyms). .br Command arguments in \fIargs\fR are delimited by space characters (blank, tabs or new lines). Single or double quotes can be used to delimitate command arguments including spaces and a non quoted backslash (\fB\e\fP) is the escape character to protect the next char. The executable file is sought using the PATH environment variable as explained for \fBexeclp\fR(3). .br .SH RETURN VALUE These functions have the same return values of \fBsystem\fR(3). .SH EXAMPLE The following program shows the usage of \fBesystem\fR: .BR .sp \& .nf #include #include #include #define BUFLEN 1024 int main(int argc, char *argv) { char buf[BUFLEN]; printf("type in a command and its arguments, e.g. 'ls -l'\\n"); while (fgets(buf, BUFLEN, stdin) != NULL) { printf("Command: '%s' \\n",buf); esystem(buf); printf("Command done\\n"); } } .fi .SH SEE ALSO .BR system (3), execs (3), s2argv(3), system_nosh(3) .SH BUGS Bug reports should be addressed to .SH AUTHOR Renzo Davoli s2argv-execs-1.4/man/execs.3000066400000000000000000000067001444576372500156610ustar00rootroot00000000000000.\"* execs: convert strings to argv .\" Copyright (C) 2014 Renzo Davoli. University of Bologna. .\" .\" This library is free software; you can redistribute it and/or .\" modify it under the terms of the GNU Lesser General Public .\" License as published by the Free Software Foundation; either .\" version 2.1 of the License, or (at your option) any later version. .\" .\" This library is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" Lesser General Public License for more details. .\" .\" You should have received a copy of the GNU Lesser General Public .\" License along with this library; if not, write to the Free Software .\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .TH execs 3 2014-05-27 "VirtualSquare" "Linux Programmer's Manual" .SH NAME execs, execsp, execspe \- execute a file taking its arguments from a string .SH SYNOPSIS .B #include .br .B #include .sp .BI "int execs(const char *" path ", const char *" args ");" .br .BI "int execse(const char *" path ", const char *" args ", char *const " envp "[]);" .br .BI "int execsp(const char *" args ");" .br .BI "int execspe(const char *" args ", char *const " envp "[]);" .sp .sp .sp .BI "int eexecs(const char *" path ", char *" args ");" .br .BI "int eexecse(const char *" path ", char *" args ", char *const " envp "[]);" .br .BI "int eexecsp(char *" args ");" .br .BI "int eexecspe(char *" args ", char *const " envp "[]);" .sp These functions are provided by libexecs and libeexecs. Link with \fI-lexecs\fR or \fI-leexecs\fR. .SH DESCRIPTION This group of functions extends the family of \fBexec\fR(3) provided by the libc. .br \fBexecs\fR, \fBexecse\fR, \fBexecsp\fR and \fBexecspe\fR are similar to \fBexecv\fR(3), \fBexecve\fR(2), \fBexecvp\fR(3) and \fBexecvpe\fR(3), respectively, but take the command line arguments for the file to execute (and the also the command name for \fBexecsp\fR(3) and \fBexecspe\fR(3)) by parsing a command string \fIargs\fR. .br Command arguments in \fIargs\fR are delimited by space characters (blank, tabs or new lines). Single or double quotes can be used to delimitate command arguments including spaces and a non quoted backslash (\fB\e\fP) is the escape character to protect the next char. .br \fBexecs\fR, \fBexecse\fR, \fBexecsp\fR and \fBexecspe\fR do not use dynamic allocation but require space on the stack to store an entire copy of \fIargs\fR. \fBeexecs\fR, \fBeexecse\fR, \fBeexecsp\fR and \fBeexecspe\fR do not use extra stack space but modify \fIargs\fR. In case the same argv should be used for several exec command, use \fBs2argv\fR(3) to parse the args just once. .SH RETURN VALUE These functions return only if an error has occurred. The return value is always \-1. The failure cases and errno values are those specified for \fBexecve\fR(2). .SH EXAMPLE The following program demonstrates the use of \fBexecs\fR: .BR .sp \& .nf #include #include #include #define BUFLEN 1024 int main(int argc, char *argv) { char buf[BUFLEN]; printf("type in a command and its arguments, e.g. 'ls -l'\\n"); if (fgets(buf, BUFLEN, stdin) != NULL) { execsp(buf); printf("exec error\\n"); } } .fi .SH SEE ALSO .BR exec (3), s2argv (3) .SH BUGS Bug reports should be addressed to .SH AUTHOR Renzo Davoli s2argv-execs-1.4/man/execse.3000077700000000000000000000000001444576372500172112execs.3ustar00rootroot00000000000000s2argv-execs-1.4/man/execsp.3000077700000000000000000000000001444576372500172242execs.3ustar00rootroot00000000000000s2argv-execs-1.4/man/execspe.3000077700000000000000000000000001444576372500173712execs.3ustar00rootroot00000000000000s2argv-execs-1.4/man/pclose_execs.3000077700000000000000000000000001444576372500214522popen_nosh.3ustar00rootroot00000000000000s2argv-execs-1.4/man/pclose_execsp.3000077700000000000000000000000001444576372500216322popen_nosh.3ustar00rootroot00000000000000s2argv-execs-1.4/man/pclose_nosh.3000077700000000000000000000000001444576372500213122popen_nosh.3ustar00rootroot00000000000000s2argv-execs-1.4/man/popen_execs.3000077700000000000000000000000001444576372500213062popen_nosh.3ustar00rootroot00000000000000s2argv-execs-1.4/man/popen_execsp.3000077700000000000000000000000001444576372500214662popen_nosh.3ustar00rootroot00000000000000s2argv-execs-1.4/man/popen_nosh.3000066400000000000000000000051461444576372500167250ustar00rootroot00000000000000.\"* popen_nosh: system replacement not using any shell .\" Copyright (C) 2014 Renzo Davoli. University of Bologna. .\" .\" This library is free software; you can redistribute it and/or .\" modify it under the terms of the GNU Lesser General Public .\" License as published by the Free Software Foundation; either .\" version 2.1 of the License, or (at your option) any later version. .\" .\" This library is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" Lesser General Public License for more details. .\" .\" You should have received a copy of the GNU Lesser General Public .\" License along with this library; if not, write to the Free Software .\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .TH popen_nosh 3 2014-05-27 "VirtualSquare" "Linux Programmer's Manual" .SH NAME popen_nosh, pclose_nosh, popen_execs, pclose_execs, \- pipe stream to or from a process without using a shell .SH SYNOPSIS .B #include .br .B #include .sp .BI "FILE *popen_nosh(const char *" command ", const char *" type ");" .br .BI "int pclose_nosh(FILE *" stream ");" .sp .BI "FILE *popen_execsp(const char *" command ", const char *" type ");" .br .BI "int pclose_execsp(FILE *" stream ");" .sp .BI "FILE *popen_execs(const char *" path ",const char *" command ", const char *" type ");" .br .BI "int pclose_execs(FILE *" stream ");" .sp These functions are provided by libexecs. Link with \fI-lexecs\fR. .SH DESCRIPTION \fBpopen_nosh\fR, \fBpopen_execsp\fR and \fBpclose_nosh\fR are almost drop in replacement for \fBpopen\fR(3) and \fBpclose\fR(3) provided by the libc. \fBpopen_nosh\fR and \fBpopen_execsp\fR are synonyms: they parse the command string and run the command directly, without using a shell. .br Command arguments in \fIargs\fR are delimited by space characters (blank, tabs or new lines). Single or double quotes can be used to delimitate command arguments including spaces and a non quoted backslash (\fB\e\fP) is the escape character to protect the next char. The executable file is sought using the PATH environment variable as explained for \fBexeclp\fR(3). .br \fBpclose_nosh\fR closes a stream opened by \fBpopen_nosh\fR. .br \fBpopen_execs\fR requires the path of the executable to be specified as its first parameter so it does not use the PATH environment variable. \fBpclose_execs\fR closes a stream opened by \fBpopen_execs\fR. .SH RETURN VALUE These functions have the same return values of \fBpopen\fR(3) and \fBpclose\fR(3). s2argv-execs-1.4/man/s2argv.3000066400000000000000000000071161444576372500157600ustar00rootroot00000000000000.\"* s2argv: convert strings to argv .\" Copyright (C) 2014 Renzo Davoli. University of Bologna. .\" .\" This library is free software; you can redistribute it and/or .\" modify it under the terms of the GNU Lesser General Public .\" License as published by the Free Software Foundation; either .\" version 2.1 of the License, or (at your option) any later version. .\" .\" This library is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" Lesser General Public License for more details. .\" .\" You should have received a copy of the GNU Lesser General Public .\" License along with this library; if not, write to the Free Software .\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .TH S2ARGV 3 2014-05-27 "VirtualSquare" "Linux Programmer's Manual" .SH NAME s2argv \- convert a command string in an argv array .SH SYNOPSIS .B #include .br .B #include .br .B #include .sp .BI "char **s2argv(const char *" args ");" .br .BI "void s2argv_free(char **" argv ");" .sp .br .BI "size_t s2argvlen(char **" argv ");" .sp .br .BI "size_t s2argc(char **" argv ");" .sp .br .BI "typedef char * (* s2argv_getvar_t) (const char *name);" .br .BI "extern s2argv_getvar_t s2argv_getvar;" .sp These functions are provided by libexecs and libeexecs. Link with \fI-lexecs\fR or \fI-leexecs\fR. .sp .SH DESCRIPTION .BR s2argv convert a command string in an argv array for \fBexecv\fR(3), \fBexecvp\fR(3) or \fBexecvpe\fR(3). Single or double quotes can be used to delimitate command arguments including spaces and a non quoted backslash (\fB\e\fP) is the escape character to protect the next char. .br \fBs2argv\fR can parse several commands separated by semicolons (\fB;\fR). The argv of each command is terminated by a NULL element, one further NULL element tags the end of the array returned by s2argv. .br \fBs2argv\fR supports variables as arguments. When an argument of a command is a dollar sign followed by a name (e.g. $USER) \fBs2argv\fR puts the output of the \fBs2argv_getvar\fR function instead. (It is possible for example to assign \fBs2argv_getvar=getenv\fR. For security reasons, the function is NULL by default and all variables get replaced with an empty string. Programmers can use their own custom function instead). .sp .BR s2argv_free frees the memory that was allocated by \fBs2argv\fR. .sp .BR s2argvlen returns the sum of the \fBs2argc\fR for all the commands, including the trailing NULLs that separate them. .sp .BR s2argc returns the number of arguemnts of the (first) command returned by \fBs2argv\fR. (The beginning of the next argv is \fBargv+s2argc(argv)+1\fR). .SH RETURN VALUE .BR s2argv returns a dynamically allocated argv, ready to be used as an argument to \fBexecv\fR(3), \fBexecvp\fR(3) or \fBexecvpe\fR(3). The return value of .BR s2argv should be freed by .BR s2argv_free in case the exec command does not succeed. .SH EXAMPLE The following program demonstrates the use of \fBs2argv\fR: .BR .sp \& .nf #include #include #include #include #define BUFLEN 1024 int main(int argc, char *argv) { char buf[BUFLEN]; printf("type in a command and its arguments, e.g. 'ls -l'\\n"); if (fgets(buf, BUFLEN, stdin) != NULL) { char **argv=s2argv(buf); execvp(argv[0], argv); s2argv_free(argv); printf("exec error\\n"); } } .fi .SH SEE ALSO .BR exec (3) .SH BUGS Bug reports should be addressed to .SH AUTHOR Renzo Davoli s2argv-execs-1.4/man/system_eexecsp.3000077700000000000000000000000001444576372500213572esystem.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execs.3000077700000000000000000000000001444576372500216632system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsa.3000077700000000000000000000000001444576372500220242system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsp.3000077700000000000000000000000001444576372500220432system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsqa.3000077700000000000000000000000001444576372500222052system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsqp.3000077700000000000000000000000001444576372500222242system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsqra.3000077700000000000000000000000001444576372500223672system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsqrp.3000077700000000000000000000000001444576372500224062system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsr.3000077700000000000000000000000001444576372500220452system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsra.3000077700000000000000000000000001444576372500222062system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_execsrp.3000077700000000000000000000000001444576372500222252system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_nosh.3000077700000000000000000000000001444576372500215232system_safe.3ustar00rootroot00000000000000s2argv-execs-1.4/man/system_safe.3000066400000000000000000000121161444576372500170720ustar00rootroot00000000000000.\"* system_safe: system replacement not using any shell .\" Copyright (C) 2014 Renzo Davoli. University of Bologna. .\" .\" This library is free software; you can redistribute it and/or .\" modify it under the terms of the GNU Lesser General Public .\" License as published by the Free Software Foundation; either .\" version 2.1 of the License, or (at your option) any later version. .\" .\" This library is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" Lesser General Public License for more details. .\" .\" You should have received a copy of the GNU Lesser General Public .\" License along with this library; if not, write to the Free Software .\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .TH system_safe 3 2014-05-27 "VirtualSquare" "Linux Programmer's Manual" .SH NAME system_safe, system_execs, system_nosh \- execute a command with its arguments from a string without using a shell .SH SYNOPSIS .B #include .br .B #include .sp .BI "int system_safe(const char *" command ");" .br .BI "int system_execsp(const char *" command ");" .br .BI "int system_execsa(const char *" command ");" .br .BI "int system_execs(const char *" path ", char const *" command ");" .sp .BI "int system_execsrp(const char *" command ", int " redir "[3]);" .br .BI "int system_execsra(const char *" command ", int " redir "[3]);" .br .BI "int system_execsr(const char *" path ", char const *" command ", int " redir "[3]);" .sp .BI "int system_nosh(const char *" command ");" .br .BI "int system_execsqp(const char *" command ");" .br .BI "int system_execsqa(const char *" command ");" .sp .BI "int system_execsqrp(const char *" command ", int " redir "[3]);" .br .BI "int system_execsqra(const char *" command ", int " redir "[3]);" .sp These functions are provided by libexecs. Link with \fI-lexecs\fR. .SH DESCRIPTION \fBsystem_safe\fR is a safe replacement for \fBsystem\fR(3) provided by the libc. \fBsystem_safe\fR parses the command string and runs the command directly, without using a shell. The command must be specified as an absolute pathname. \fBsystem_safe\fR does not support variables as argument. .br Command arguments in \fIargs\fR are delimited by space characters (blank, tabs or new lines). Single or double quotes can be used to delimitate command arguments including spaces and a non quoted backslash (\fB\e\fP) is the escape character to protect the next char. .br \fBsystem_execsa\fR is like \fBsystem_safe\fR supporting also variables as arguments. When an argument of a command is a dollar sign followed by a name (e.g. $USER) s2argv puts the output of the s2argv_getvar function instead. (It is possible for example to assign s2argv_getvar=getenv. For security reasons, the function is NULL by default and all variables get replaced with an empty string. Programmers can use their own custom function instead) .br In \fBsystem_execsp\fR the executable file is sought using the PATH environment variable as explained for \fBexeclp\fR(3). .br \fBsystem_execs\fR requires the path of the executable to be specified as its first parameter so it does not use the PATH environment variable. .br \fBsystem_execsr\fR, \fBsystem_execsrp\fR and \fBsystem_execsra\fR works as their couterparts without the 'r', but they permit the redirection of standard input, output and error streams. Their last parameter is an array of three integers. The standard input of the command will be redirected to \fBredir[0]\fR if it is positive, the standard output to \fBredir[1]\fR if it is not negative and different from 1, the standard error to \fBredir[2]\fR if it is not negative and different from 2. .br \fBsystem_execsra\fR does not use the PATH variable, argv[0] must be specified as a full pathname. .br \fBsystem_nosh\fR, \fBsystem_execsqp\fR, \fBsystem_execsqa\fR, \fBsystem_execsqrp\fR and \fBsystem_execsqra\fR can run sequences of commands separated by semicolons (\fB;\fR). The first command returning a non-zero exit status breaks the sequence. .br \fBsystem_nosh\fR is an almost drop in replacement for \fBsystem\fR(3) provided by the libc. (\fBsystem_execsqp\fR and \fBsystem_nosh\fR are synonyms). .SH RETURN VALUE These functions have the same return values of \fBsystem\fR(3). When running a sequence of commands, it returns the "wait status" of the first command returning a non-zero value. If the return value is zero it means that all the commands of the sequence succeeded. .SH EXAMPLE The following program shows the usage of \fBsystem_nosh\fR: .BR .sp \& .nf #include #include #include #define BUFLEN 1024 int main(int argc, char *argv) { char buf[BUFLEN]; printf("type in a command and its arguments, e.g. 'ls -l'\\n"); while (fgets(buf, BUFLEN, stdin) != NULL) { printf("Command: '%s' \\n",buf); system_nosh(buf); printf("Command done\\n"); } } .fi .SH SEE ALSO .BR system (3), execs (3), s2argv (3) .SH BUGS Bug reports should be addressed to .SH AUTHOR Renzo Davoli s2argv-execs-1.4/noshell.c000066400000000000000000000117651444576372500155320ustar00rootroot00000000000000/* * noshell: system(3) safe and effficient replacements * Copyright (C) 2014-2023 Renzo Davoli. University of Bologna. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #define _GNU_SOURCE #include #include #include #include #include #include #include struct system_execsq_t { const char *path; int *redir; int flags; }; static int system_execsq_f(char **argv, void *arg) { struct system_execsq_t *v=arg; int status; pid_t pid; switch (pid=fork()) { case -1: return -1; case 0: if (__builtin_expect(execs_fork_security && execs_fork_security(execs_fork_security_arg) != 0, 0)) _exit(127); if (v->redir) { int i; for (i=0; i<3; i++) { if (v->redir[i] >= 0 && v->redir[i] != i) { dup2(v->redir[i],i); close(v->redir[i]); } } } if (v->path) { if (*v->path) execv(v->path, argv); else if (argv[0][0] == '/') execv(argv[0], argv); else _exit(127); } else execvp(argv[0], argv); _exit(127); default: waitpid(pid,&status,0); return status; } } int _system_common(const char *path, const char *command, int redir[3], int flags) { struct system_execsq_t seqexec_var={path, redir, flags}; if (command) { int rv = s2multiargv(command, system_execsq_f, &seqexec_var, flags); return (rv == -1) ? W_EXITCODE(127, 0) : rv; } else return 1; } pid_t _coprocess_common(const char *path, const char *command, char *const argv[], char *const envp[], int pipefd[2], int flags) { if (command) { int pfd_in[2]; int pfd_out[2]; pid_t pid; if (pipe2(pfd_in, O_CLOEXEC) == -1 || pipe2(pfd_out, O_CLOEXEC) == -1) return -1; switch (pid=fork()) { case -1: return -1; case 0: if (__builtin_expect(execs_fork_security && execs_fork_security(execs_fork_security_arg) != 0, 0)) _exit(127); if (dup2(pfd_in[0],0) == -1 || dup2(pfd_out[1],1) == -1) _exit(127); close(pfd_in[0]); close(pfd_in[1]); close(pfd_out[0]); close(pfd_out[1]); if (argv) { if (path) _execs_common(path, (const char *) argv, envp, NULL, flags); else _execs_common(command, (const char *) argv, envp, NULL, flags); } else _execs_common(path, command, envp, NULL, flags); _exit(127); default: pipefd[0]=pfd_out[0]; pipefd[1]=pfd_in[1]; close(pfd_in[0]); close(pfd_out[1]); return pid; } } else return 1; } struct popen_info { FILE *stream; pid_t pid; struct popen_info *next; }; static struct popen_info *popen_list; FILE *_popen_common(const char *path, const char *command, const char *type, int flags) { if ((type[0] == 'r' || type[0] == 'w') && (type[1] == 0 || type[1] == 'e')) { int fd[2]; struct popen_info *new; int streamno = (type[0] == 'r') ? STDIN_FILENO : STDOUT_FILENO; if (pipe2(fd, O_CLOEXEC)) return NULL; if ((new=malloc(sizeof(struct popen_info)))==NULL) { errno = ENOMEM; return NULL; } switch (new->pid = fork()) { case -1: close(fd[0]); close(fd[1]); free(new); return NULL; case 0: if (__builtin_expect(execs_fork_security && execs_fork_security(execs_fork_security_arg) != 0, 0)) _exit(127); dup2(fd[1-streamno],1-streamno); close(fd[0]); close(fd[1]); if (path) _execs_common(path,(char *) command,environ,(char *) command, flags); else _execs_common(NULL, (char *) command,environ,(char *) command, flags); _exit(127); default: close(fd[1-streamno]); if (type[1] == 'e') fcntl(fd[streamno], F_SETFD, FD_CLOEXEC); if ((new->stream = fdopen(fd[streamno], type)) == NULL) { close(streamno); free(new); return NULL; } else { new->next = popen_list; popen_list = new; return new->stream; } } } else { errno = EINVAL; return NULL; } } int pclose_execs(FILE *stream) { struct popen_info **pprev; for (pprev = &popen_list; *pprev != NULL; pprev = &((*pprev)->next)) { struct popen_info *curr = *pprev; if (curr->stream == stream) { int status; pid_t waitrv; *pprev = curr->next; fclose(curr->stream); while ((waitrv = waitpid(curr->pid, &status, 0)) == -1 && errno == EINTR) ; free(curr); if (waitrv != -1) return status; else { errno = ECHILD; return -1; } } } errno = EINVAL; return -1; }