Exemplo n.º 1
0
int main (int argc , char* argv[], char* envp[])
{
	printf("%s\n","lab 5" );



	while(1){
		char curDir[2048];
		getcwd (curDir,2048);
		printf("\n%s $ ", curDir);
		char readLine [2048]="";
		printf("%s\n","here" );
		fgets(readLine,2048,stdin);
		//printf("%s\n",readLine );
		if(strncmp(readLine, "quit", 4)==0){
	      printf("%s\n","bye" );
	      exit(0);
	    }
		while (readLine[0]==10){
			printf("%s $ ",curDir );
			fgets(readLine,2048,stdin);
		}
		cmdLine line = *parseCmdLines (readLine);
 
		if(execute (&line)==-1) exit(0);



	}
	printf("%s\n","done" );

	return 0;

}
Exemplo n.º 2
0
/*	patch_mem targetfile tgt_location mem_location length	*/
void Copy_from_mem(char* file) {
	char* line = malloc(256);
	char input[10];

	strcpy(line, "./Operations/bin/patch_mem");
	strcat(line, " ");

	printf("The target file is %s\n", file);
	strcat(line, file);
	strcat(line, " ");

	printf("Where would you like to write ? ");
	gets(input);
	strcat(line, input);
	strcat(line, " ");

	printf("Where would you like to copy from ? ");
	gets(input);
	strcat(line, input);
	strcat(line, " ");

	printf("How many bytes would you like to copy ? ");
	gets(input);
	strcat(line, input);

	printf("%s\n", line);
	execute(parseCmdLines(line));
}
Exemplo n.º 3
0
int main (int argc , char* argv[], char* envp[]) {
    
    /*Parameters*/
    char *buf[PATH_MAX];
    char *buf1[PATH_MAX];
    char str[STR_MAX];
    cmdLine *command_struct;

    /*Displaying a prompt - current directory
     * savig the current directory         */
    *buf1 = getwd(buf);

    while (1)
    {
    
        printf("%s > ", *buf1);
        fgets(str,STR_MAX,stdin);

        /*quiting*/
        if( strcmp(str, "quit\n") == 0)
            break;

        /*Parsing string line*/
        command_struct = parseCmdLines(str); 
        execute(command_struct);
    }
    return 0; 
}
Exemplo n.º 4
0
void invoke(cmdList list,char *s){
	int index =atoi(s+1);
	if(list->size <= index)
		printf("Error: no such index in history!\n");
	else{
		cmdLine *redo;
		redo = parseCmdLines(list->cmds[index]);
		add_cmdLine(list,list->cmds[index]);
		if(strcmp(redo->arguments[0],"history") == 0)
			history(list);
		else 
			execute(list,redo);
		freeCmdLines(redo);
	}
}
Exemplo n.º 5
0
int Params::getopt(int argc, char* const argv[])
{ 
    int rc = Util::Getopt::getopt(argc, argv, optstring_);
    // Further consistency checks
    if (help_ || version_) return 0;
    if (action_ == Action::none) {
        // This shouldn't happen since print is taken as default action
        std::cerr << progname() << ": An action must be specified\n";
        rc = 1;
    }
    if (action_ == Action::adjust && !adjust_) {
        std::cerr << progname() 
                  << ": Adjust action requires option -a time\n";
        rc = 1;
    }
    if (action_ == Action::modify && cmdFiles_.empty() && cmdLines_.empty()) {
        std::cerr << progname() 
                  << ": Modify action requires at least one -m or -M option\n";
        rc = 1;
    }
    if (0 == files_.size()) {
        std::cerr << progname() << ": At least one file is required\n";
        rc = 1;
    }
    if (rc == 0 && action_ == Action::modify) {
        // Parse command files
        if (!parseCmdFiles(modifyCmds_, cmdFiles_)) {
            std::cerr << progname() << ": Error parsing -m option arguments\n"; 
            rc = 1;
        }
    }
    if (rc ==0 && action_ == Action::modify) {
        // Parse command lines
        if (!parseCmdLines(modifyCmds_, cmdLines_)) {
            std::cerr << progname() << ": Error parsing -M option arguments\n";
            rc = 1;
        }
    }
    if (!directory_.empty() && !(action_ == Action::insert || action_ == Action::extract)) {
        std::cerr << progname() << ": -l option can only be used with extract or insert actions\n"; 
        rc = 1; 
    }
    return rc;
} // Params::getopt
Exemplo n.º 6
0
/*	display_ascii filename location length	*/
void display_ascii(char* file) {
	char* line = malloc(256);
	char input[10];

	strcpy(line, "./Operations/bin/display_ascii");
	strcat(line, " ");
	strcat(line, file);
	strcat(line, " ");

	printf("Which position would you like to present from ? ");
	gets(input);
	strcat(line, input);
	strcat(line, " ");

	printf("How many would you like to present ? ");
	gets(input);
	strcat(line, input);

	execute(parseCmdLines(line));
}
Exemplo n.º 7
0
/*	modify file location value	*/
void modify(char* file) {
	char* line = malloc(256);
	char input[10];

	strcpy(line, "./Operations/bin/modify");
	strcat(line, " ");
	strcat(line, file);
	strcat(line, " ");

	printf("Which position would you like to write into ? ");
	gets(input);
	strcat(line, input);
	strcat(line, " ");

	printf("Which volue would you like to write ? ");
	gets(input);
	strcat(line, input);

	execute(parseCmdLines(line));
}
Exemplo n.º 8
0
int main(int argc, char **argv){
	cmdList list = make_cmdList();
	while(1){
		char buf[PATH_MAX];
		char s[linelen];
		getcwd(buf,PATH_MAX);
		printf("current path: %s >\n",buf);
		fgets(s,linelen,stdin);
		cmdLine *cmdline;
		cmdline = parseCmdLines(s);
		if((cmdline)!= NULL){
			/*testFunc(cmdline);*/
			if(strcmp(cmdline->arguments[0],"quit")==0){
				freeCmdLines(cmdline);
				break;
			}
			if (strcmp(cmdline->arguments[0],"cd")==0){
				changeDir(cmdline);
			}
			else {
				if (s[0]=='!'){
					invoke(list,s);
				}
				else{
					add_cmdLine(list, s);
					if(strcmp(cmdline->arguments[0],"history") == 0){
						history(list);
					}
					else {
						execute(list,cmdline);
					}
				}
			}	
		}
		freeCmdLines(cmdline);
	}
	freeCmdList(list);
	return 0;
}
Exemplo n.º 9
0
int Params::getopt(int argc, char* const argv[])
{
    int rc = Util::Getopt::getopt(argc, argv, optstring_);
    // Further consistency checks
    if (help_ || version_) return 0;
    if (action_ == Action::none) {
        // This shouldn't happen since print is taken as default action
        std::cerr << progname() << ": " << _("An action must be specified\n");
        rc = 1;
    }
    if (   action_ == Action::adjust
        && !adjust_
        && !yodAdjust_[yodYear].flag_
        && !yodAdjust_[yodMonth].flag_
        && !yodAdjust_[yodDay].flag_) {
        std::cerr << progname() << ": "
                  << _("Adjust action requires at least one -a, -Y, -O or -D option\n");
        rc = 1;
    }
    if (   action_ == Action::modify
        && cmdFiles_.empty() && cmdLines_.empty() && jpegComment_.empty()) {
        std::cerr << progname() << ": "
                  << _("Modify action requires at least one -c, -m or -M option\n");
        rc = 1;
    }
    if (0 == files_.size()) {
        std::cerr << progname() << ": " << _("At least one file is required\n");
        rc = 1;
    }
    if (rc == 0 && !cmdFiles_.empty()) {
        // Parse command files
        if (!parseCmdFiles(modifyCmds_, cmdFiles_)) {
            std::cerr << progname() << ": " << _("Error parsing -m option arguments\n");
            rc = 1;
        }
    }
    if (rc == 0 && !cmdLines_.empty()) {
        // Parse command lines
        if (!parseCmdLines(modifyCmds_, cmdLines_)) {
            std::cerr << progname() << ": " << _("Error parsing -M option arguments\n");
            rc = 1;
        }
    }
    if (   !directory_.empty()
        && !(action_ == Action::insert || action_ == Action::extract)) {
        std::cerr << progname() << ": "
                  << _("-l option can only be used with extract or insert actions\n");
        rc = 1;
    }
    if (!suffix_.empty() && !(action_ == Action::insert)) {
        std::cerr << progname() << ": "
                  << _("-S option can only be used with insert action\n");
        rc = 1;
    }
    if (timestamp_ && !(action_ == Action::rename)) {
        std::cerr << progname() << ": "
                  << _("-t option can only be used with rename action\n");
        rc = 1;
    }
    if (timestampOnly_ && !(action_ == Action::rename)) {
        std::cerr << progname() << ": "
                  << _("-T option can only be used with rename action\n");
        rc = 1;
    }
    return rc;
} // Params::getopt
Exemplo n.º 10
0
Arquivo: task2.c Projeto: liranr23/C
int main (int argc , char* argv[]){
  char dir[PATH_MAX];
  char BUFFER[LINE_LENGTH];
  int commands_counter = 0;
  linked_list *head = 0;
  var_list *var_head = 0;
  cmdLine *command;
  while(1){
    getcwd(dir, PATH_MAX);
    printf("The current dir is: %s \n", dir);
    fgets(BUFFER, LINE_LENGTH, stdin);
    command = parseCmdLines(BUFFER);
    if(strncmp(command->arguments[0], "!", 1) == 0){
      int place = atoi(&command->arguments[0][1]);
      if(place < 0 || place > commands_counter){
	perror("Error, out of bounds");
      }else{
	freeCmdLines(command);
	char *repeat = getCommand(head, place);
	command = parseCmdLines(repeat);
	strcpy(BUFFER, repeat);
      }
    }
    int i;
    for(i = 0; i<command->argCount; i++){
      if(strncmp(command->arguments[i], "$", 1) == 0){
	char *change = &command->arguments[i][1];
	char *fut_change = getArgu(var_head, change);
	replaceCmdArg(command, i, fut_change);
      }
    }
    if(strcmp(command->arguments[0], "set") == 0){
      var_list *newVar = malloc(sizeof(var_list));
      char *str1 = calloc(strlen(command->arguments[1])+1, sizeof(char));
      char *str2 = calloc(strlen(command->arguments[2])+1, sizeof(char));
      strcpy(str1, command->arguments[1]);
      strcpy(str2, command->arguments[2]);
      newVar->name = str1;
      newVar->value = str2;
      newVar->next = 0;
      var_head = setValue(var_head, newVar);
    }
    else if(strcmp(command->arguments[0], "delete") == 0){
      var_head = delete_var(var_head, command->arguments[1]);
    }
    else if((choose_action(command, head, var_head)) == 1){
      freeCmdLines(command);
      break;
    }
    linked_list *newNode = malloc(sizeof(linked_list));
    char *str = calloc(strlen(BUFFER)+1, sizeof(char));
    strcpy(str, BUFFER);
    newNode->command = str;
    newNode->next = 0;
    head = list_append(head, newNode);
    freeCmdLines(command);
    commands_counter++;
  }
  list_free(head);
  var_free(var_head);
  return 0;
}
Exemplo n.º 11
0
int Params::getopt(int argc, char* const Argv[])
{
	char** argv = new char* [argc+1];
	argv[argc] = NULL;
	long_t longs;

	longs["--adjust"   ] = "-a";
	longs["--binary"   ] = "-b";
	longs["--comment"  ] = "-c";
	longs["--delete"   ] = "-d";
	longs["--days"     ] = "-D";
	longs["--force"    ] = "-f";
	longs["--Force"    ] = "-F";
	longs["--grep"     ] = "-g";
	longs["--help"     ] = "-h";
	longs["--insert"   ] = "-i";
	longs["--keep"     ] = "-k";
	longs["--key"      ] = "-K";
	longs["--location" ] = "-l";
	longs["--modify"   ] = "-m";
	longs["--Modify"   ] = "-M";
	longs["--encode"   ] = "-n";
	longs["--months"   ] = "-O";
	longs["--print"    ] = "-p";
	longs["--Print"    ] = "-P";
	longs["--quiet"    ] = "-q";
	longs["--log"      ] = "-Q";
	longs["--rename"   ] = "-r";
	longs["--suffix"   ] = "-S";
	longs["--timestamp"] = "-t";
	longs["--Timestamp"] = "-T";
	longs["--unknown"  ] = "-u";
	longs["--verbose"  ] = "-v";
	longs["--Version"  ] = "-V";
	longs["--version"  ] = "-V";
	longs["--years"    ] = "-Y";

	for ( int i = 0 ; i < argc ; i++ ) {
		std::string* arg = new std::string(Argv[i]);
		if (longs.find(*arg) != longs.end() ) {
			argv[i] = ::strdup(longs[*arg].c_str());
		} else {
			argv[i] = ::strdup(Argv[i]);
		}
		delete arg;
	}

    int rc = Util::Getopt::getopt(argc, argv, optstring_);
    // Further consistency checks
    if (help_ || version_) return 0;
    if (action_ == Action::none) {
        // This shouldn't happen since print is taken as default action
        std::cerr << progname() << ": " << _("An action must be specified\n");
        rc = 1;
    }
    if (   action_ == Action::adjust
        && !adjust_
        && !yodAdjust_[yodYear].flag_
        && !yodAdjust_[yodMonth].flag_
        && !yodAdjust_[yodDay].flag_) {
        std::cerr << progname() << ": "
                  << _("Adjust action requires at least one -a, -Y, -O or -D option\n");
        rc = 1;
    }
    if (   action_ == Action::modify
        && cmdFiles_.empty() && cmdLines_.empty() && jpegComment_.empty()) {
        std::cerr << progname() << ": "
                  << _("Modify action requires at least one -c, -m or -M option\n");
        rc = 1;
    }
    if (0 == files_.size()) {
        std::cerr << progname() << ": " << _("At least one file is required\n");
        rc = 1;
    }
    if (rc == 0 && !cmdFiles_.empty()) {
        // Parse command files
        if (!parseCmdFiles(modifyCmds_, cmdFiles_)) {
            std::cerr << progname() << ": " << _("Error parsing -m option arguments\n");
            rc = 1;
        }
    }
    if (rc == 0 && !cmdLines_.empty()) {
        // Parse command lines
        if (!parseCmdLines(modifyCmds_, cmdLines_)) {
            std::cerr << progname() << ": " << _("Error parsing -M option arguments\n");
            rc = 1;
        }
    }
    if (rc == 0 && (!cmdFiles_.empty() || !cmdLines_.empty())) {
        // We'll set them again, after reading the file
        Exiv2::XmpProperties::unregisterNs();
    }
    if (   !directory_.empty()
        && !(action_ == Action::insert || action_ == Action::extract)) {
        std::cerr << progname() << ": "
                  << _("-l option can only be used with extract or insert actions\n");
        rc = 1;
    }
    if (!suffix_.empty() && !(action_ == Action::insert)) {
        std::cerr << progname() << ": "
                  << _("-S option can only be used with insert action\n");
        rc = 1;
    }
    if (timestamp_ && !(action_ == Action::rename)) {
        std::cerr << progname() << ": "
                  << _("-t option can only be used with rename action\n");
        rc = 1;
    }
    if (timestampOnly_ && !(action_ == Action::rename)) {
        std::cerr << progname() << ": "
                  << _("-T option can only be used with rename action\n");
        rc = 1;
    }

	// cleanup the argument vector
	for ( int i = 0 ; i < argc ; i++ ) ::free((void*)argv[i]);
    delete [] argv;

    return rc;
} // Params::getopt
Exemplo n.º 12
0
List* execString(cmdLog* cLog, List* env, char* incoming) {
    cmdLine* line;
    cmdLine* firstLine;
    cmdLine* currLine;
    int pipeCounter = 0;
    int * pid;
    int pid2;
    int i;
    int j;
    int **pipefd;
    line = parseCmdLines(incoming);
    currLine = line;
    firstLine = line;
    if (line==NULL || !strcmp(line->arguments[0], "quit")) {
        freeList(env);
        freeCmdLines(line);
        return (List*)-1;
    }
    line= cmdEnvReplace(env, line);
    if (!strcmp(line->arguments[0], "assign")) {
        env= cmdAssign(env, line);
    }
    else if (!strcmp(line->arguments[0], "unassign")) {
        env= cmdUnassign(env, line);
    }
    else if (!strcmp(line->arguments[0], "cd")) {
        cmdCD(cLog, line, incoming);
    }
    else if (line->arguments[0][0]=='!') {
        env= cmdReadLog(cLog, env, line);
    }
    else {
        j = 0;
        pushArray(cLog, incoming);
        if(line->next) {
            while(currLine != NULL) {
                currLine = currLine->next;
                pipeCounter++;
            }
            pipefd= createPipes(pipeCounter-1);
            pid = (int *)malloc(pipeCounter * sizeof(int));
            if (!(pid[j] =fork())) { /* first son*/
                close(STDOUT_FILENO);          /* Close unused write end */
                dup(rightPipe(pipefd, line)[1]);
                close(rightPipe(pipefd, line)[1]);
                coreExec(cLog, env, line);
            }
            close(rightPipe(pipefd, line)[1]);
            j++;
            line = line->next;
            for (i=0; i<pipeCounter-2; i++) {
                if (!(pid[j] =fork())) { /* first son*/
                    close(STDOUT_FILENO);          /* Close unused write end */
                    dup(rightPipe(pipefd, line)[1]);
                    close(rightPipe(pipefd, line)[1]);
                    close(STDIN_FILENO);
                    dup(leftPipe(pipefd, line)[0]);
                    close(leftPipe(pipefd, line)[0]);
                    coreExec(cLog, env, line);
                }
                close(leftPipe(pipefd, line)[0]);
                close(rightPipe(pipefd, line)[1]);
                j++;
                line = line->next;
            }

            if (!(pid[j] =fork())) { /* first son*/
                close(STDIN_FILENO);          /* Close unused write end */
                dup(leftPipe(pipefd, line)[0]);
                close(leftPipe(pipefd, line)[0]);
                coreExec(cLog, env, line);
            }
            close(leftPipe(pipefd, line)[0]);
            for(i=0; i<=j; i++) {
                if(line->blocking!=0) {
                    waitpid(pid[i],0,0);
                }
            }
            releasePipes(pipefd, pipeCounter-1);
        } else {
            if (!(pid2=fork())) {
                coreExec(cLog, env, line);
            }
        }
        if(line->blocking!=0) {
            waitpid(pid2,0,0);
        }
    }
    freeCmdLines(firstLine);
    free(pid);
    return env;
}