Commit a32f559e authored by jan.koester's avatar jan.koester
Browse files

test

parent 877aa6a9
Loading
Loading
Loading
Loading
+48 −75
Changes for commands/system/cpio.cpp: 48 added lines, 75 removed lines.
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <getopt.h>

#include <unistd.h>
#include <fcntl.h>
@@ -39,6 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sys/sysmacros.h>

#include "../filesystem/copyutil.h"
#include "../text/textutil.h"
#include "shell.h"

namespace fresh { namespace cmd {
@@ -298,88 +298,61 @@ namespace fresh { namespace cmd {
            bool help=false;
            bool createDirs=false,unconditional=false,preserveTimes=false;
            std::vector<std::string> operands;
            static const struct option longOptions[]={
                {"create",no_argument,nullptr,'o'},
                {"extract",no_argument,nullptr,'i'},
                {"unextract",no_argument,nullptr,'i'},
                {"pass-through",no_argument,nullptr,'p'},
                {"list",no_argument,nullptr,'t'},
                {"format",required_argument,nullptr,'H'},
                {"verbose",no_argument,nullptr,'v'},
                {"quiet",no_argument,nullptr,'q'},
                {"null",no_argument,nullptr,'0'},
                {"make-directories",no_argument,nullptr,'d'},
                {"unconditional",no_argument,nullptr,'u'},
                {"preserve-modification-time",no_argument,nullptr,'m'},
                {"help",no_argument,nullptr,1000},
                {"usage",no_argument,nullptr,1001},
                {nullptr,0,nullptr,0}
            };
            int previousOpterr=opterr;
            opterr=0;
            optind=0;
            int option;
            while((option=getopt_long(argc,const_cast<char *const *>(argv),"-oitpH:vqc0dum?",longOptions,nullptr))!=-1){
                char selectedMode=0;
                switch(option){
                    case 'o': case 'i': case 'p': case 't':
                        selectedMode=(char)option;
            bool usage=false;
            auto selectMode=[&](char selectedMode){
                if(mode && mode!=selectedMode){
                            opterr=previousOpterr;
                    std::cerr << "cpio: conflicting operation modes" << std::endl;
                            return 1;
                    return false;
                }
                mode=selectedMode;
                        break;
                    case 'H':
                        format=optarg;
                        break;
                    case 'c':
                        format="newc";
                        break;
                    case 'v': case 'q':
                        break;
                    case '0':
                        inputDelimiter='\0';
                        break;
                    case 'd':
                        createDirs=true;
                        break;
                    case 'u':
                        unconditional=true;
                        break;
                    case 'm':
                        preserveTimes=true;
                        break;
                    case 1:
                        operands.emplace_back(optarg);
                        break;
                    case 1000:
                        help=true;
                        break;
                    case 1001:
                        std::cout << "Usage: cpio -o [-v] [-0] [-H FORMAT]" << std::endl;
                        opterr=previousOpterr;
                return true;
            };
            text::Opts parsed=text::parseOpts(argc,argv,"H",
                [&](char option,const std::string &value)->bool{
                    switch(option){
                        case 'o': case 'i': case 'p': case 't': return selectMode(option);
                        case 'H': format=value; return true;
                        case 'c': format="newc"; return true;
                        case 'v': case 'q': return true;
                        case '0': inputDelimiter='\0'; return true;
                        case 'd': createDirs=true; return true;
                        case 'u': unconditional=true; return true;
                        case 'm': preserveTimes=true; return true;
                        case '?': help=true; return true;
                        default: return text::badOpt("cpio",option);
                    }
                },
                [&](const std::string &option,const std::string &value,bool hasValue)->int{
                    if(option=="create") return selectMode('o') ? 0 : -1;
                    if(option=="extract" || option=="unextract") return selectMode('i') ? 0 : -1;
                    if(option=="pass-through") return selectMode('p') ? 0 : -1;
                    if(option=="list") return selectMode('t') ? 0 : -1;
                    if(option=="format"){
                        if(!hasValue) return 1;
                        format=value;
                        return 0;
                    case '?':
                        if(optopt=='H')
                            std::cerr << "cpio: option requires an argument: -H/--format" << std::endl;
                        else if(optind>0 && std::string(argv[optind-1])=="-?")
                            help=true;
                        else
                            std::cerr << "cpio: unrecognized or incomplete option" << std::endl;
                        if(!help){
                            opterr=previousOpterr;
                            return 1;
                        }
                        break;
                    default:
                        opterr=previousOpterr;
                        return 1;
                    }
                    if(option=="verbose" || option=="quiet"){}
                    else if(option=="null") inputDelimiter='\0';
                    else if(option=="make-directories") createDirs=true;
                    else if(option=="unconditional") unconditional=true;
                    else if(option=="preserve-modification-time") preserveTimes=true;
                    else if(option=="help") help=true;
                    else if(option=="usage") usage=true;
                    else return text::badLong("cpio",option);
                    if(hasValue){
                        std::cerr << "cpio: option '--" << option << "' doesn't allow an argument" << std::endl;
                        return -1;
                    }
            opterr=previousOpterr;
                    return 0;
                });
            if(!parsed.ok) return 1;
            operands=parsed.operands;

            if(usage){
                std::cout << "Usage: cpio -o [-v] [-0] [-H FORMAT] | cpio -p [-d] [-u] [-m] DIRECTORY" << std::endl;
                return 0;
            }
            if(help){
                std::cout << "Usage: cpio -o [-v] [-0] [-H FORMAT]\n"
                             "       cpio -p [-d] [-u] [-m] [--null] DIRECTORY\n"