コード例 #1
0
ファイル: webgetter.cpp プロジェクト: sasq64/apone
void HttpSession::parseHeader() {
	istream is(&impl->data);
	string line;
	getline(is, line);
	auto rc = split(line, " ");
	impl->code = stol(rc[1]);
	LOGD(">>%s", line);
	while(!is.eof()) {
		getline(is, line);
		if(line == "" || line == "\r")
			break;
		LOGD("%s", line);
		auto parts = split(line, ": ", 2);
		if(parts.size() == 2) {
			parts[1] = rstrip(rstrip(parts[1], '\n'), '\r');
			impl->header[parts[0]] = parts[1];
		}
	}
	auto cl = impl->header["Content-Length"];
	impl->cntSize = cl != "" ? stol(impl->header["Content-Length"]) : 0;
	if(isRedirect()) {
		LOGD("Should redirect to '%s'", impl->header["Location"]);
		//sleepms(500);
	}
}
コード例 #2
0
ファイル: webgetter.cpp プロジェクト: sasq64/apone
void WebGetter::update() {

	lock_guard<mutex>{m};

	Connection::run();
	auto it = sessions.begin();
	while(it != sessions.end()) {
		auto s = *it;
		if(s->gotHeaders() && s->isRedirect()) {
			auto location = s->getHeader("Location");
			if(!startsWith(location, "http://")) {
				location = format("http://%s:%d%s", s->getHost(), s->getPort(), urlencode(location, ":\\?;+ "));
			}
			LOGD("Redirecting to '%s'", location);
			auto callback = s->getCallback();
			it = sessions.erase(it);
			auto s = make_shared<HttpSession>(location);
			sessions.push_back(s);
			s->connect();
			s->stream(callback);
			it = sessions.end();
			continue;
		}
		if(s->error()) {
			errorCallback(0, "");
		}
		if(s->done()) {
			it = sessions.erase(it);
			LOGD("Session done");
		} else
			it++;
	}

}
コード例 #3
0
bool NetworkJob::shouldNotifyClientFinished()
{
    if (m_redirectCount >= s_redirectMaximum)
        return true;

    if (m_needsRetryAsFTPDirectory && retryAsFTPDirectory())
        return false;

    if (isRedirect(m_extendedStatusCode) && handleRedirect())
        return false;

    return true;
}
コード例 #4
0
void NetworkJob::handleNotifyClose(int status)
{
#ifndef NDEBUG
    m_isRunning = false;
#endif
    if (!m_cancelled) {
        if (!m_statusReceived) {
            // Connection failed before sending notifyStatusReceived: use generic NetworkError.
            notifyStatusReceived(BlackBerry::Platform::FilterStream::StatusNetworkError, 0);
        }

        // If an HTTP authentication-enabled request is successful, save
        // the credentials for later reuse. If the request fails, delete
        // the saved credentials.
        if (!isError(m_extendedStatusCode))
            storeCredentials();
        else if (isUnauthorized(m_extendedStatusCode))
            purgeCredentials();

        if (shouldNotifyClientFinished()) {
            if (isRedirect(m_extendedStatusCode) && (m_redirectCount >= s_redirectMaximum))
                m_extendedStatusCode = BlackBerry::Platform::FilterStream::StatusTooManyRedirects;

            sendResponseIfNeeded();
            if (isClientAvailable()) {

                RecursionGuard guard(m_callingClient);
                if (isError(m_extendedStatusCode) && !m_dataReceived) {
                    String domain = m_extendedStatusCode < 0 ? ResourceError::platformErrorDomain : ResourceError::httpErrorDomain;
                    ResourceError error(domain, m_extendedStatusCode, m_response.url().string(), m_response.httpStatusText());
                    m_handle->client()->didFail(m_handle.get(), error);
                } else
                    m_handle->client()->didFinishLoading(m_handle.get(), 0);
            }
        }
    }

    // Whoever called notifyClose still have a reference to the job, so
    // schedule the deletion with a timer.
    m_deleteJobTimer.startOneShot(0);

    // Detach from the ResourceHandle in any case.
    m_handle = 0;
    m_multipartResponse = nullptr;
}
コード例 #5
0
ファイル: wiki2eng.cpp プロジェクト: rti7743/wiki2dic
static void AnalizeEng(const std::wstring& titleW,const std::wstring& innerW)
{
	if ( isRedirect(innerW) )
	{//リダイレクトは処理しない.
		return;
	}

	const std::wstring _TitleW = cleaningInnerText(titleW);
	//曖昧さの解決のための()があったら消します.
	const std::wstring clipTitleW = snipAimaiKako(titleW);
	if (clipTitleW.empty())
	{
		return;
	}

	std::wstring engwordW;
	std::wstring yomiW;
	
	if ( isKatakanaOnly(clipTitleW) )
	{//カタカナだけのページにしか興味はないね
		yomiW = convertPlainYomi(clipTitleW);
		engwordW = findEng(clipTitleW,innerW);
	}
	else if ( isAplhaOnly(clipTitleW) )
	{//英語だけのページにも興味ありました
		yomiW = convertPlainYomi(findYomi(clipTitleW,innerW));
		engwordW = clipTitleW;

		if ( checkGobaku(yomiW,L"",engwordW,engwordW) )
		{
			yomiW = L"";
		}
	}
	else
	{
		return ;
	}

	//結果の表示.

	if ( Option::m()->getShow() == Option::TypeShow_NoEmpty)
	{//空ならば表示しない?
		if (engwordW.empty() || yomiW.empty() )
		{
			return ;
		}
	}
	else if ( Option::m()->getShow() == Option::TypeShow_Empty)
	{//空だけ表示する
		if ( (!engwordW.empty() || !yomiW.empty() ) )
		{
			return;
		}
	}

	//カタカナひらがな変換.
	switch ( Option::m()->getCase())
	{
	case Option::TypeCase_Kana:	yomiW = XLWStringUtil::mb_convert_kana(yomiW,L"Hc"); break;
	case Option::TypeCase_Kata:	yomiW = XLWStringUtil::mb_convert_kana(yomiW,L"KC"); break;
	}

	//tofuに注意しながらMultiByteに修正します.
	if ( Option::m()->getAimai() == Option::TypeAimai_Del)
	{
		wprintf(L"%ls	%ls\n",yomiW.c_str(),engwordW.c_str() );
	}
	else
	{//消す消さない以前に処理的に存在しないかなあ
		wprintf(L"%ls	%ls\n",yomiW.c_str(),engwordW.c_str() );
	}
}
コード例 #6
0
ファイル: webgetter.cpp プロジェクト: sasq64/apone
void HttpSession::readContent() {
	int size = impl->cntSize - impl->data.in_avail();
	if(size <= 0) {

		if(impl->data.in_avail() > 0) {
			LOGD("ALL IN BUFFER");
			vector<uint8_t> buffer(impl->data.size());
			auto bufs = impl->data.data();
			copy(asio::buffers_begin(bufs), asio::buffers_end(bufs), buffer.begin());
			impl->total = impl->data.size();
			impl->state = DONE;
			impl->callback(*this, buffer);
		} else {
			LOGD("EMPTY");
			impl->state = EMPTY;
			if(!isRedirect()) {
				vector<uint8_t> v;
				impl->callback(*this, v);
			}
		}
		return;
	}

	if(!impl->callback || impl->oneShot) {
		LOGD("ALL AT ONCE");
		asio::async_read(impl->c.getSocket(), impl->data, asio::transfer_exactly(size), [&](const asio::error_code &e, size_t n) {
			impl->state = DONE;
			if(impl->oneShot) {
				auto v8 = getContent();
				impl->callback(*this, v8);
			}
		});
		return;
	}

	//filled = data.in_avail();
	impl->total = 0;
	//buffer.reserve(chunkSize);

	impl->handler = [=](const asio::error_code &e, size_t n) {
		//asio::deadline_timer t(io, boost::posix_time::seconds(5));
		//t->async_wait(

		impl->data.commit(n);
		//LOGD("Read %d bytes, avail %d", n, impl->data.size());
		//n += filled;
		vector<uint8_t> buffer(impl->data.size());
		auto bufs = impl->data.data();
		copy(asio::buffers_begin(bufs), asio::buffers_end(bufs), buffer.begin());
		impl->total += impl->data.size();
		//LOGD("Total %d", impl->total);

		if(n == 0 || impl->total == impl->cntSize) {
			impl->state = DONE;
			LOGD("Done reading");
		}

		impl->callback(*this, buffer);

		if(impl->state == DONE) {
			LOGD("Request stop");
			return;
		}

		impl->data.consume(impl->data.size());
		auto mutableBuffer = impl->data.prepare(16384);

		impl->c.getSocket().async_read_some(asio::buffer(mutableBuffer), impl->handler);
	};

	auto mutableBuffer = impl->data.prepare(16384);
	impl->c.getSocket().async_read_some(asio::buffer(mutableBuffer), impl->handler);
}
コード例 #7
0
ファイル: main.c プロジェクト: justinposerio/OperatingSystems
int main(int argc, char *argv[]) {
	FILE *fp = stdin;
	if (argc > 1) {
		printf("Running as a shell!\n");
		fp = fopen(argv[1],"r");
		if (!fp) {
			fprintf(stderr,"Error opening script %s: %s\n", argv[1], strerror(errno));
			return -1;
		}
	}
	while(1) {
		char *command, *token, *args[1024], *path, *line = NULL;
		size_t inputSize;
		if (getline(&line, &inputSize, fp) == -1) {
			fprintf(stderr, "end of file\n");
			return -1;
		}
		struct Redirect rd;
		int argCount = 0, redirectCharCount = 0, numRedirects = 0;
		command = strtok(line, " \t\n\r");
		if (command != NULL && command[0] != '#') {
			args[0] = command;
			argCount++;
			for (token = strtok(NULL, " \t\n\r"); token != NULL; token = strtok(NULL, " \t\n\r")) {
				redirectCharCount = isRedirect(token, &rd);
				if (redirectCharCount == 0) {
					args[argCount] = token;
					argCount++;
				} else {
					break;
				}
			}
			printf("\n");
			int pid, status;
			struct rusage rusage;
			struct timeval tv1, tv2;
			float t1s, t2s, t1u, t2u;
			gettimeofday(&tv1, NULL);
			switch (pid = fork()) {
				case -1:
					fprintf(stderr," Error with fork!");
					return -1;
					break;
				case 0:
					if (argCount > 1) {
						printf("Executing command %s with arguments: ", command);
						int i;
						for (i = 1; i < argCount; i++) {
							printf("\"%s\"", args[i]);
							if (i != argCount - 1) {
								printf(", ");
							} else {
								printf("\n");
							}
						}
					} else {
						printf("Executing command %s\n", command);
					}
					int fd;
					for (;token != NULL; token = strtok(NULL, " \t\n\r")) {
						path = token + redirectCharCount;
						isRedirect(token, &rd);
						switch (rd.rdtype) {
							case RD_IN:
								fd = open(path,O_RDWR,0666);
								break;
							case RD_TRUNC:
								fd = open(path,O_RDWR,0666);
								break;
							case RD_APPEND:
								fd = open(path,O_RDWR,0666);
								break;
							default:
								break;
						}
						if (fd == -1) {
							fprintf(stderr, "Error opening file \"%s\": %s\n",path,strerror(errno));
							exit(-1);
						}
						if (dup2(fd, rd.fd) == -1) {
							fprintf(stderr, "Error using dup2: %s\n", strerror(errno));
						}
						close(fd);
					}
					args[argCount] = NULL;
					if (argc > 1) {
						fclose(fp);
					}
					if (execvp(command, args) == -1) {
						fprintf(stderr, "Error with execvp: %s\n", strerror(errno));
						exit(-1);
					}
					break;
				default:
					t1s = (float) tv1.tv_sec;
					t1u = ((float) tv1.tv_usec);
					int d = wait3(&status, 0, &rusage);
					if (d == -1) {
						fprintf(stderr, "Error with wait: %s\n", strerror(errno));
						return -1;
					}
					printf("Command returned with return code %d\n", status);
					gettimeofday(&tv2, NULL);
					t2s = (float) tv2.tv_sec;
					t2u = ((float) tv2.tv_usec);
					float ts = (float) (t2u - t1u)/1000000.0;
					float tt = (float) (t2s - t2s);
					printf("Consuming %f real seconds, %f user, %f system\n", tt + ts, (float) rusage.ru_utime.tv_sec + (float) rusage.ru_utime.tv_usec/1000000, (float) rusage.ru_stime.tv_sec + (float) rusage.ru_stime.tv_usec/1000000);
					break;
			}
		}
		free(line);
	}
	return -1;
}
コード例 #8
0
ファイル: shell.c プロジェクト: SaumyaRawat/Shell
int main(int argc, char *argv[])
{
    initShell();
    //signal(SIGCHLD, SIG_IGN);
    //Declarations
    int bytes_read,no_of_tokens,no_of_commands=0,redirectFlag=0;
    char uname[80],homedir[256],input[1024],hostname[80],tempstr[256],cwd[256];
    char *cmdline, *sentence, *line, *token,**savedTokens, **command, *cmd, *mcptr, *com;
    size_t length,homedirlen;
    pid_t childPid;
    //Change Shell home dir to ~
    getcwd(homedir , sizeof(homedir) );
    getusername(uname);
    homedirlen=strlen(homedir);
    strcpy(cwd,homedir);

/*    int ptr=0;
    for(ptr=0;ptr<=1000;ptr++)
    {
        jobs[ptr]=NULL;
    }*/
    int exit_flag=0;
    while (1)
    {
        sigset_t mask, prevmask;
        //Initialize mask with just the SIGCHLD signal
        sigemptyset(&mask);
        sigaddset(&mask, SIGCHLD);
        sigprocmask(SIG_BLOCK, &mask, &prevmask); /*block SIGCHLD, get previous mask*/

        no_of_tokens=0;
        command=malloc ( 200 * sizeof(char)); //Number of commands there can be will be stored in 2D Array
        cmdline = (char *) malloc (1025 * sizeof(char));
        line = (char *) malloc (1025 * sizeof(char));
        cmd = (char *) malloc (1025 * sizeof(char));
        savedTokens=malloc ( 100 * sizeof(char)); //Number of tokens there can be
        strcpy(line,"\n");
        gethostname(hostname, sizeof(hostname));
        getcwd(cwd , sizeof(cwd) );
        //printf("PRINT THIS : %s\n",cwd+homedirlen );
        //printf("CWD: %d HOMEDIR: %d\n",strlen(cwd),strlen(homedir) );
        if( strncmp( cwd, homedir, homedirlen-1) == 0) // && strncmp( cwd, homedir, homedirlen-1)!=0) //If the current working directory is not ~
        {
            strcpy(tempstr,"~");
            //printf("HOME DIR IS: %s\n",tempstr );
            strcat(tempstr,cwd+homedirlen);
            strcpy(cwd, tempstr);
        }
        int jumper=setjmp(env);
        printf("<%s@%s:%s>",uname,hostname,cwd ); //PROMPT
        getline (&line, &length+1, stdin);
        //PARSING:
        //Stage 1: Handling multiple commands:

        int k=0;
            token = strtok (line, ";");
            command[k]=token;
            while ( token!=NULL )
            {
                command[k]=token;
                token = strtok (NULL,";");
                k++;
            }
            no_of_commands=k-1;
            if(no_of_commands==-1)
            {
                printf("Exiting main shell!\n");
                printf("\n");
                return 0;
            }
            else if(command[no_of_commands]!=NULL)
            {
                int len=strlen(command[no_of_commands]);
                command[no_of_commands][len-1]=0; //Last token gets an extra \n .. therefore removed here.
            }

        //STAGE 2:
        for(k=0;k<=no_of_commands;k++)
        {
/*            sigset_t mask, prevmask;
            //Initialize mask with just the SIGCHLD signal
            sigemptyset(&mask);
            sigaddset(&mask, SIGCHLD);
            sigprocmask(SIG_BLOCK, &mask, &prevmask); /*block SIGCHLD, get previous mask*/
            cmdline = command[k];
            com = (char *) malloc (1025 * sizeof(char));
            if(command[k]!=NULL)
                strcpy(com,command[k]); //com stores the whole command to be executed
            else
                com=NULL;

            //Stage 3: Piping
            int no_of_pipes=0;
            if(com!=NULL)
            {
                redirectFlag=isRedirect(com);
                no_of_pipes=isPipeJob(com);
            }
            if(no_of_pipes!=0)
            {
                int status;
                pid_t procid=fork();
                if(procid==0)
                {
                    executePipe(no_of_pipes,com,redirectFlag,homedir);
                }
                else
                {
                    sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking
                    wait(&status);
                }
            }
            else
            {
                int i=0;
                token = strtok(cmdline,">");
                token = strtok(cmdline,"<");
                token = strtok(cmdline," \t\n");
                if(token==NULL)
                {
                    no_of_commands=-1;
                }
                while(token != NULL)
                {
                    savedTokens[i]=token;
                    i++;
                    token = strtok (NULL, " \t\n");
                }
                if(i!=0)
                {
                    no_of_tokens=i-1;
                    cmd=savedTokens[0];
                }
                else
                {
                    no_of_tokens=0;
                    cmd=NULL;
                }

                int len=0;

                if(savedTokens[no_of_tokens]!=NULL)
                {
                    len=strlen(savedTokens[no_of_tokens]);
                }
                //savedTokens[no_of_tokens][len-1]=0; //Last token gets an extra \n .. therefore removed here.
                //if ((cmd!=NULL) && ((strcmp("exit",cmd)==0) ||  (strcmp("Exit",cmd)==0) || (strcmp("exit ",cmd)==0) || (strcmp("Exit ",cmd)==0)))
                if ((cmd!=NULL) && ((strcmp("quit",cmd)==0) || (strcmp("quit ",cmd)==0) || (strcmp(" quit",cmd)==0)))
                {
                    //exit(1);
                    exit_flag=1;
                    break;
                }

              /*int j=0;
                while(j<=no_of_tokens)
                {
                    printf("TOKEN %d: %s\n",j,savedTokens[j]);
                    j++;
                } */
        		//record command in history list (GNU readline history ?)
                int std_out;
                if(no_of_commands!=-1)
                {
            		if ( (cmd!=NULL) && isBuiltInCommand(cmd)==1 )
                    {
                            if(redirectFlag!=0)
                            {
                                executeRedirect(cmd,com,redirectFlag);
                            }
                            if(outfile!=0)
                            {
                                int fd1;
                                if(outfile==1)
                                {
                                    fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666);
                                    lseek(fd1, 0, SEEK_SET);
                                }
                                else if(outfile==2)
                                {
                                    fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666);
                                }
                                if(fd1==-1)
                                {
                                    fprintf(stderr, "Can't open file %s for output!\n",output);
                                    memset(output, 0, 10);
                                    outfile=0;
                                    continue;
                                }
                                std_out=dup(1);
                                dup2(fd1,STDOUT_FILENO);
                                close(fd1);
                                memset(output, 0, 10);
                                outfile=0;
                            }
                            if(infile==3)
                            {
                                int fd2;
                                fd2=open(inputfile,O_RDWR,00666);
                                if(fd2==-1)
                                {
                                    fprintf(stderr, "Can't open file for input! 4\n");
                                    memset(inputfile, 0, 10);
                                    infile=0;
                                    continue;
                                }
                                dup2(fd2,STDIN_FILENO);
                                close(fd2);
                                memset(inputfile, 0, 10);
                                infile=0;
                            }
                            job_no++;
                            addJob(0,cmd,0,job_no);
                            executeBuiltInCommand(cmd,savedTokens,no_of_tokens,homedir);
                            dup2(std_out,1);
                    }
                    else
                    {
                        if((com!=NULL) && isBackgroundJob(com)==1)
                        {
                            savedTokens[no_of_tokens]=NULL;
                        }
                        int status;
            		    childPid = fork();
                        switch (childPid)
                        {
                            case 0: //Child Process
                                //setpgid(0,0);  //make the current process the group leader
                                //tcsetpgrp(0,getpid());
                                if(redirectFlag!=0)
                                {
                                    executeRedirect(cmd,com,redirectFlag);
                                }
                                if(outfile!=0)
                                {
                                    int fd1;
                                    if(outfile==1)
                                    {
                                        fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666);
                                        lseek(fd1, 0, SEEK_SET);
                                    }
                                    else if(outfile==2)
                                    {
                                        fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666);
                                    }
                                    if(fd1==-1)
                                    {
                                        fprintf(stderr, "Can't open file for output 6!\n");
                                        memset(output, 0, 10);
                                        outfile=0;
                                        continue;
                                    }
                                    dup2(fd1,STDOUT_FILENO);
                                    close(fd1);
                                    memset(output, 0, 10);
                                    outfile=0;
                                }
                                if(infile==3)
                                {
                                    int fd2;
                                    printf("%s\n",inputfile);
                                    fd2=open(inputfile,O_RDWR,00666);
                                    if(fd2==-1)
                                    {
                                        fprintf(stderr, "Can't open file for input! 5\n");
                                        memset(inputfile, 0, 10);
                                        infile=0;
                                        continue;
                                    }
                                    dup2(fd2,STDIN_FILENO);
                                    close(fd2);
                                    memset(inputfile, 0, 10);
                                    infile=0;
                                }
                                executeCommand(cmd,savedTokens,no_of_tokens); //calls execvp
                                /* if exec returns there was an error. */
                                perror(savedTokens[0]);
                                exit(-1);

                            case -1:
                                perror("Fork");
                                return -1;

                            default: //In Parent
                                sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking
                                //handler(childPid,cmd,job_no,jobs); //Check if any of the childs exited
                                if (isBackgroundJob(com)==1)
                                {
                                    setpgid(childPid,childPid); //added the background process to its own group
                                    //tcsetpgrp(0,childPid);
                                    savedTokens[no_of_tokens]=NULL;
                                //    add pid to some list to track jobs
                                    job_no++;
                                    printf("[%d][proc %d started]\n",job_no, childPid);
                                    addJob(childPid,cmd,1,job_no);
    //                                sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking
                                }
                                else
                                {
                                    //Add foreground jobs to list:
                                    job_no++;
                                    //printf("Parent: Here total jobs are %d \n",job_no );
                                    addJob(childPid,cmd,0,job_no);
                                    curpid=childPid;
                                //    printf("jobs[%d]->cmd: %s\n",job_no,jobs[job_no]->cmd);
                                    sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking
                                    pid_t wpid;
                                    do
                                    {
                                        wpid = waitpid(childPid, &status, WUNTRACED); //WUNTRACED->status of child processes will be reported here!
                                    } while (!WIFEXITED(status) && !WIFSIGNALED(status)); //WIFEXITED reports normal termination and //WIFSIGNALED not 0  status if child process stopped but wasnt caught!
                                    removeJob(wpid);
                                    curpid=getpid();
                                    //printf("I am removing the fg job with pid %d\n",wpid );
                                    //waitpid (childPid);
                                    //printf("HERE! 2\n" );
                                }
                        }
                    }
                }
            }
        }//end of k loop
        if(exit_flag==1)
            break;

        //free(line); //
    } //End of while loop
    return 0;
}
コード例 #9
0
ファイル: shell.c プロジェクト: SaumyaRawat/Shell
int executePipe( int no_of_pipes, char *com, int redirectFlag,char *homedir)
{
    pc *pcmd=malloc(100 * sizeof(pc));
    int fd[2];
    int isFirstPipe=1;
    int count = 0;
    char *commandline=malloc(1024*sizeof(char));
    strcpy(commandline,com);

    char *command=malloc(1024*sizeof(char));
    char **argv=malloc ( 100 * sizeof(char)); //Number of tokens there can be
    char *token=malloc(1024*sizeof(char));
    int i=0,j=0;
    command = strtok ( com,"|");  //first command
    while( command!=NULL)
    {
        pcmd[i].argv=command;
        pcmd[i].redirectFlag=isRedirect(command);
        command = strtok (NULL, "|");
        i++;
    }
    //Tokenise command for execution
    //parseCommand(pcmd[0].argv,argv);

    int in=0;
    for(i=0;i<no_of_pipes;i++)
    {
        pipe(fd);
        spawn_proc(in,fd[1],pcmd[i],pcmd[i].redirectFlag,homedir);
        close(fd[1]);
        in=fd[0];
    }
    if(in!=0)
        dup2(in,0);
    //last command
    infile=0;
    outfile=0;
    int random=isRedirect(pcmd[i].argv);
    int no_of_tokens=parseCommand(pcmd[i].argv,argv,pcmd[i].redirectFlag);
    int std_out=dup(1);
    if(outfile!=0)
    {
        int fd1;
        if(outfile==1)
        {
            fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666);
            lseek(fd1, 0, SEEK_SET);
        }
        else if(outfile==2)
        {
            fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666);
        }
        if(fd1==-1)
        {
            fprintf(stderr, "Can't open file for output 1!\n");
            memset(output, 0, 10);
            outfile=0;
            return;
        }
        dup2(fd1,STDOUT_FILENO);
        close(fd1);
        memset(output, 0, 10);
        outfile=0;
        //redirectFlag=0;
    }
    if(infile==3)
    {
        int fd2;
        fd2=open(inputfile,O_RDWR,00666);
        if(fd2==-1)
        {
            fprintf(stderr, "Can't open file for input! 3\n");
            memset(inputfile, 0, 10);
            infile=0;
            return;
        }
        dup2(fd2,STDIN_FILENO);
        close(fd2);
        memset(inputfile, 0, 10);
        infile=0;
        //redirectFlag=0;
    }

    if(isBuiltInCommand(argv[0])==1)
    {
        job_no++;
        addJob(0,argv[0],0,job_no);
        executeBuiltInCommand(argv[0],argv,no_of_tokens,homedir);
        _exit(1);
        dup2(std_out,1);
        return(0);
    }
    else
        return(execvp(argv[0],argv));
}
コード例 #10
0
ファイル: shell.c プロジェクト: SaumyaRawat/Shell
int spawn_proc(int in,int out,pc node,int redirectFlag,char *homedir)
{
    int no_of_tokens=0,std_out;
    pid_t pid;
    char **argv=malloc ( 100 * sizeof(char)); //Number of tokens there can be
    pid=fork();
    if(pid<0)
    {
        fprintf(stderr, "Error in Piping!\n" );
    }
    else if(pid==0) /* Child */
    {
        infile=0;
        outfile=0;
        int random=isRedirect(node.argv);
        no_of_tokens=parseCommand(node.argv,argv,redirectFlag);
        if(in!=0)
        {
            dup2(in,0);
            close(in);
        }
        if(out!=1)
        {
            dup2(out,1);
            close(out);
        }
        if(outfile!=0)
        {
            int fd1;
            if(outfile==1)
            {
                fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666);
                lseek(fd1, 0, SEEK_SET);
            }
            else if(outfile==2)
            {
                fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666);
            }
            if(fd1==-1)
            {
                fprintf(stderr, "Can't open file for output!\n");
                memset(output, 0, 10);
                outfile=0;
                return;
            }
            int std_out=dup(1);
            dup2(fd1,STDOUT_FILENO);
            close(fd1);
            memset(output, 0, 10);
            outfile=0;
            //redirectFlag=0;
        }
        if(infile==3)
        {
            int fd2;
            fd2=open(inputfile,O_RDWR,00666);
            if(fd2==-1)
            {
                fprintf(stderr, "Can't open file for input! 2\n");
                memset(inputfile, 0, 10);
                infile=0;
                return;
            }
            dup2(fd2,STDIN_FILENO);
            close(fd2);
            memset(inputfile, 0, 10);
            infile=0;
            //redirectFlag=0;
        }
        if(isBuiltInCommand(argv[0])==1)
        {
            job_no++;
            addJob(0,argv[0],0,job_no);
            executeBuiltInCommand(argv[0],argv,no_of_tokens,homedir);
            dup2(std_out,1);
            _exit(1);
        }
        else
            return (execvp(argv[0],argv));
    }
    return pid;
}