示例#1
0
int main(int argc, char *argv[])
{
	if(argc != 5)
	{
		printf("Siga o padrao:\n./tp3 [entrada] [saida] [Numero de fitas] [Tamanho da memoria disponivel]\n");
		return 1;
	}
	
	FILE *entrada, *saida;
	entrada = fopen(argv[1], "r");
	if(entrada == NULL)
	{
		printf("Erro ao abrir o arquivo de entrada. O arquivo existe mesmo?\n");
		return 1;
	}
	
	saida = fopen(argv[2], "w");
	
	sort(entrada, saida, stringtoint(argv[4]), stringtoint(argv[3]));
	
	fclose(entrada);
	fclose(saida);
	
	entrada = NULL;
	saida = NULL;
	
	return 0;
}
示例#2
0
int main()
{
    char str1[10] = "1101";
    char str2[10] = "1100";
    char *str3;
    int len1, len2, len3, i, d1, d2;
    len1 = strlen(str1);
    len2 = strlen(str2);
    len3 = ((len1 >= len2) ? len1:len2); 
    str3 = (char *)malloc((len3+1)*sizeof(char));
    d1 = stringtoint(str1);
    d2 = stringtoint(str2);
    printf("\n%d\t%d\n", d1, d2);
    inttostring((d1+d2), str3);
    reverse(str3, strlen(str3));
    printf("\n\t%s\n", str3);
}
TreeNode* deserialize(string data)
{
    if (data.size()==0)
        return NULL;
    queue<TreeNode*> q;
    istringstream ss(data);
    vector<string> sv;

    int tmp;
    do
    {
        string s;
        ss>>s;
        sv.push_back(s);
    } while(ss);
    TreeNode* root = new TreeNode(stringtoint(sv[0]));
    q.push(root);
    int k=0;
    while (k<sv.size()-2)
    {
        TreeNode* tm = q.front();
        q.pop();
        if (sv[++k]!="#")
        {
            tmp = stringtoint(sv[k]);
            tm->left = new TreeNode(tmp);
            q.push(tm->left);
        }
        if (k+1<sv.size()-1)
            if (sv[++k]!="#")
            {
                tmp = stringtoint(sv[k]);
                tm->right = new TreeNode(tmp);
                q.push(tm->right);
            }

    }
    return root;
}
示例#4
0
/* Gracefully get an integer value */
int getint (const char *display)
{
	char *buffer;
	int value;
	int rtn;

	while (1)
	{
		buffer = getstring (display);
		rtn = stringtoint (buffer, &value);
		free (buffer);

		if (rtn == EXIT_SUCCESS)
			return value;
	}
}
示例#5
0
Real
AB2PredictorCorrector::estimateTimeError(NumericVector<Number> & solution)
{
 _pred1 = _fe_problem.getNonlinearSystemBase().getPredictor()->solutionPredictor();
  TimeIntegrator * ti = _fe_problem.getNonlinearSystemBase().getTimeIntegrator();
  std::string scheme = ti->name();
  Real dt_old = _my_dt_old;
  if (dt_old == 0)
    dt_old = _dt;

  switch (stringtoint(scheme))
  {
  case 1:
  {
    // NOTE: this is never called, since stringtoint does not return 1 - EVER!
    //I am not sure this is actually correct.
    _pred1 *= -1;
    _pred1 += solution;
    Real calc = _dt * _dt * .5;
    _pred1 *= calc;
    return _pred1.l2_norm();
  }
  case 2:
  {
    // Crank Nicolson
    _pred1 -= solution;
    _pred1 *= (_dt) / (3.0 * (_dt + dt_old));
    return _pred1.l2_norm();
  }
  case 3:
  {
    // BDF2
    _pred1 *= -1.0;
    _pred1 += solution;
    Real topcalc = 2.0 * (_dt + dt_old) * (_dt + dt_old);
    Real bottomcalc = 6.0 * _dt * _dt + 12.0 * _dt * dt_old + 5.0 * dt_old * dt_old;
    _pred1 *= topcalc / bottomcalc;

    return _pred1.l2_norm();
  }
  default:
    break;
  }
  return -1;
}
示例#6
0
int db_getsrvid()
{
	char query[QUERYLEN];
	char *hn;
	char *safe_hn;
	MYSQL_RES *res;
	MYSQL_ROW data;
	int sid;

	if (!DBhandle) { return(-1); }

	hn = (char *)malloc(HOSTNAMELEN);
	if (!hn)
	{
		if (dlvl(1) && rundaemon) { syslog(LOG_WARNING, "malloc(hn): %s", strerror(errno)); }
		else if (dlvl(1)) { fprintf(stdout, "malloc(hn): %s\n", strerror(errno)); }
		return(-1);
	}
	memset(hn, 0, HOSTNAMELEN);
	gethostname(hn, HOSTNAMELEN);
	if ((hn[0] == '\0') || strlen(hn) < 6)
	{
		if (dlvl(1) && rundaemon) { syslog(LOG_WARNING, "Invalid system hostname: %s", hn); }
		else if (dlvl(1)) { fprintf(stdout, "Invalid system hostname: %s\n", hn); }
		return(-1);
	}

	safe_hn = (char *)malloc((strlen(hn) * 2) + 1);
	if (!safe_hn)
	{
		return(-1);
	}
	memset(safe_hn, 0, ((strlen(hn) * 2) + 1));
	mysql_real_escape_string(DBhandle, safe_hn, hn, strlen(hn));

	memset(query, 0, QUERYLEN);
	snprintf(query, QUERYLEN, "select srvid from unix_srv where servername=\"%s\"", safe_hn);
	if (dlvl(5) && !rundaemon) { fprintf(stdout, "QUERY: %s\n", query); }
	free(safe_hn);

	if (mysql_query(DBhandle, query) != 0)
	{
		if (dlvl(1) && rundaemon) { syslog(LOG_WARNING, "Error finding myself in the database: %s", mysql_error(DBhandle)); }
		else if (dlvl(1)) { fprintf(stdout, "Error finding myself in the database: %s\n", mysql_error(DBhandle)); }
		return(-1);
	}
	res = mysql_store_result(DBhandle);
	if (!res)
	{
		if (dlvl(1) && rundaemon) { syslog(LOG_WARNING, "Error finding myself in the database(2): %s", mysql_error(DBhandle)); }
		else if (dlvl(1)) { fprintf(stdout, "Error finding myself in the database(2): %s\n", mysql_error(DBhandle)); }
		return(-1);
	}
	if (mysql_num_rows(res) < 1)
	{
		if (dlvl(1) && rundaemon) { syslog(LOG_WARNING, "Error finding myself in the database(3): %s is not listed.", hn); }
		else if (dlvl(1)) { fprintf(stdout, "Error finding myself in the database(3): %s is not listed.\n", hn); }
		mysql_free_result(res);
		return(-1);
	}
	data = mysql_fetch_row(res);

	sid = stringtoint(data[0]);

	mysql_free_result(res);
	if (dlvl(5) && rundaemon) { syslog(LOG_INFO, "Found myself in the unix_srv database: %s=%i", hn, sid); }
	else if (dlvl(5)) { fprintf(stdout, "Found myself in the unix_srv database: %s=%i\n", hn, sid); }
	free(hn);
	return(sid);
}
示例#7
0
int main()
{
    int status;
    // command variable
    char *args[20];
    int i;
    for (i = 0; i < 20; ++i)
    {
        args[i] = NULL;
    }

    // background variable
    int bg;

    // history variables
    int historynbr = 1;
    int *toBeSaved = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
    struct cmd **history =  mmap(NULL, sizeof(struct cmd), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
    int notInitialized = 1;
    while(notInitialized) {
        int i;
        for (i = 0; i < 10; i++) {
            history[i] = (struct cmd*) malloc(sizeof(struct cmd));
        }
        notInitialized = 0;
    }

    // job variable
    struct job * head = NULL;

    
    printf("\n-----------------------------------------\nWelcome!\nThis is a simple shell. \nEnter 'help' for more info.\n-----------------------------------------\n\n");

    while (1) {
        int cnt = getcmd("\n>> ", args, &bg); 

        // int i;
        // for (i = 0; i < cnt; i++)
        //     printf("\nArg[%d] = %s", i, args[i]);

        // if (bg)
        //     printf("\nBackground enabled..\n");
        // else
        //     printf("\nBackground not enabled \n");

        printf("\n");



        /**********************************/
        /******** INTERNAL COMMAND ********/
        /**********************************/

        // HISTORY
        if (cnt != 0 && isnumber(args[0])) {
                int nbr =  stringtoint(args[0]);
                *toBeSaved = 0;
                // find the index of the command in the history, if it is there
                int index = searchhistory(history, nbr);
                if (index == -1){
                    printf("***ERROR  command ID '%s' does not exist\n\n", args[0]);
                }
                else {                
                    int j;
                    for (j = 0; j < 20; j++) {
                        args[j] = history[index]->args[j];
                    }

                    //check is it was in backgorund
                    int bgIndex;
                    if ((bgIndex = isbg(args)) > 0) {
                        bg = 1;
                        args[bgIndex] = NULL;
                    }
                }
        }

        // if nothing is entered
        if(cnt == 0){
            // do nothing
        }

        // HELP
        else if (strcmp(args[0], "help") == 0){
            printf("This is a simple shell brought to you by Felix Dube\n\nIt keeps the last 10 commands in HISTORY.\nEnter 'history' to see the list of commands in history.\n\nProcess can be run un BACKGROUND using the '&' argument.\nEnter 'jobs' to see the list of process running in background\nEnter 'fg' and the process job number to bring a process in the forground.\n\n");
        }

        // PRINT HISTORY
        else if (strcmp(args[0], "history") == 0) {
            printhistory(history);
            initargs(args);
        }
         // PRESENT WORKING DIRECTORY
        else if( strcmp(args[0], "pwd") == 0) {
            char* cwd;
            char buff[PATH_MAX + 1];

            cwd = getcwd( buff, PATH_MAX + 1 );
            if( cwd != NULL ) {
                printf( "My working directory is %s.\n", cwd );
            }
            addhistory(history, args, historynbr);
            historynbr++;
            initargs(args);
        }
        
        // CHANGE DIRECTORY
        else if( strcmp(args[0], "cd") == 0 ){
            chdir(args[1]);
            addhistory(history, args, historynbr);
            historynbr++;
            initargs(args);
       }

        // JOBS
        else if( strcmp(args[0], "jobs") == 0) {
            printjobs(&head);
            addhistory(history, args, historynbr);
            historynbr++;
            initargs(args);
        }

        // FOREGROUND
        else if( strcmp(args[0], "fg") == 0) {
            addhistory(history, args, historynbr);
            historynbr++;
            if (jobexist(head, stringtoint(args[1]))){
                waitpid(stringtoint(args[1]), &status, 0);
            }
            else{
                printf("***ERROR  job PID '%s' does not exist\n\n", args[1]);
            }
            initargs(args);
        }

        // EXIT
        else if ( strcmp(args[0], "exit") == 0){
            exit(0);

        }



        /**********************************/
        /******** EXTERNAL COMMAND ********/
        /**********************************/

        else if (cnt != 0) {
            *toBeSaved = 1;

            pid_t pid = fork();
            


            /**** PARENT ****/
            // the parent process either wait for the child process or not 
            // depending if the command is executed in background or not

            if ( pid != 0 ) {
                if (bg) {
                    pushjob(&head, pid, args);
                    // save the cmd if it was valid and not already in the history
                    if(*toBeSaved){
                        int i = 0;
                        while(args[i] != NULL){
                            i++;
                        }
                        args[i] = "&";
                        addhistory(history, args, historynbr);
                        historynbr++;
                    }
                    initargs(args);
                }
                else {
                    waitpid(pid, &status, 0);
                    // save the cmd if it was valid and not already in the history
                    if(*toBeSaved){
                        addhistory(history, args, historynbr);
                        historynbr++;
                    }
                    initargs(args);
                }
            }



            /**** CHILD ****/
            // the command is exucuted in the child process

            else {

                if (cnt != 0 && isnumber(args[0])){
                    exit(0);
                }

                // change the output of the process when specified (eg. ls > out.txt)
                int argNbr;
                if ((argNbr = isredirected(args)) > 0){
                    freopen(args[argNbr+1], "w", stdout);
                    args[argNbr] = NULL;
                    args[argNbr+1] = NULL;
                }

                // execute the command and make sure it is valid
                if (execvp(args[0], args) == -1) {
                    *toBeSaved = 0;
                    printf("***ERROR  invalid command\n");
                }
            }
        }
    }
}