Example #1
0
/**
 * Prints the environment to stdout.
 */
void print_environment() {
    char **pt = environ;

    do {
        putz(*pt);
    } while (*++pt);
}
Example #2
0
// mostra os comandos do jogo
void ajuda_comandos(){
    char buffer[255] = {'\0'};
    FILE *fd = fopen("comandos.txt", "r");
    zenter_cls(AJUDA_NOME);
    putz("[Comandos]", 11);
    
    if(fd == NULL) return;
    while(fgets(buffer, 255, fd) != NULL) {
        if(buffer[0] == ' ') printz(7, "%s", buffer);
        else printz(15, "%s", buffer);
    }
    fclose(fd);
    pausa();
 }
Example #3
0
/**
 * Start a process in the foreground and wait for the process to finish.
 */
void foreground(char *args[ARGS_SIZE]) {

    char *command = args[0];

    pid_t group_id = getpid();
    pid_t pid;
    pid = fork();
    if (pid == 0) {
        /* Child */
        setpgid(0, group_id);
        run_child(args, command);

    } else if (pid > 0) {
        /* Parent */
        int status = 0;
        struct rusage before;
        struct rusage after;

        setpgid(pid, group_id);

        if (getrusage(RUSAGE_CHILDREN, &before) == -1) {
            perror("Catastrophic failure");
            exit(EXIT_FAILURE);
        }

        sighold(SIGCHLD);

        if (waitpid(pid, &status, 0) == -1) {
            printf("waitpid failed %d\n", errno);
        } else {
            getrusage(RUSAGE_CHILDREN, &after);

            print_time(&before, &after);

            if (WIFEXITED(status)) {
                printf("Exited with status %d\n", WEXITSTATUS(status));
            } else if (WIFSIGNALED(status)) {
                printf("Exited through signal %d\n", WTERMSIG(status));
            } else {
                putz("Exited abnormally");
            }
        }
        sigrelse(SIGCHLD);

    } else {
        /* System fork err */
        printf("Fork failed");
        exit(0xCC);
    }
}
Example #4
0
File: line.c Project: erukiti/ma
char	_getestr(char *s,size_t l)
{
	char	i;

	s[0]=ed.c;
	s[1]='\0';

	putz(s); /* ガイドラインがないから、ここで表示する必要がある。 */
	i=_ma_getstr(s,l-1,F_normal+F_seqedit+F_esc,__retedcode);
	msgout("");
/*prt("getestr[",s,"]:",lpu(strlen(s),0),"\n",NULL);*/



	return i;
}
Example #5
0
/**
 * Get arguments from standard in and execute the commands. e
 */
int main(int argc, char *argv[]) {

    char command[CMD_LEN];
    int len;

    if (SIGDET)
        setup_signal_handler();

    setup_sigterm_handler();

    while (is_running) {
        char *args[ARGS_SIZE] = {0};
        memset(command, 0, CMD_LEN);


        if(!SIGDET) poll_background_children();

        setup_interrupt_signal_handler();

        /* Command prompt */
        if (SAY_TURTLE) system("say \"turtle\"");
        printf(" \xF0\x9F\x90\xA2  " ANSI_COLOR_GREEN);
        fgets(command, CMD_LEN, stdin);

        remove_trailing_nl(command);
        tokenize(command, args);

        putz(ANSI_COLOR_RESET);

        /* Now the fun stuff */

        len = count_non_null(args);

        if (*(args[len - 1]) == '&') {
            args[len - 1] = 0;
            background(args);


        } else if (!handle_builtin(args) && strlen(args[0]) != 0) {
            foreground(args);
        }

    }
    
    return 0;
}
Example #6
0
/**
 * Execute the ls command.
 */
void cmd_ls() {
    DIR *dir;
    struct dirent *ep;
    dir = opendir("./");
    putz("");
    if (dir != NULL) {
        while ((ep = readdir(dir))) {
            if(ep->d_type == 4) {
                printf(" \xF0\x9F\x8D\xB0  %s \n", ep->d_name);
            } else {
                printf(" \xF0\x9F\x8D\x93  %s \n", ep->d_name);
            }
        }
        closedir(dir);
    } else {
        perror("Failed to read dir\n");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}
Example #7
0
/**
 * Print the exit status from the specified status var. Also print
 * the system and user time for the terminated process.
 */
void handle_status(struct rusage *before, struct rusage *after, int *status) {
    if (*status != 0) {
        if (WIFEXITED((*status))) {
            print_time(before, after);
            printf("Exited with status %d\n", WEXITSTATUS((*status)));


        } else if (WIFSIGNALED((*status))) {
            print_time(before, after);
            printf("Exited through signal %d\n", WTERMSIG((*status)));


        } else {
            print_time(before, after);
            putz("Exited abnormally");
        }
    } else {
        print_time(before, after);
        printf("Exited with status %d\n", *status);
    }

}
Example #8
0
/**
 * Execute the cd command.
 */
void cmd_cd(char *args[ARGS_SIZE]) {
    char current[PATH_MAX];
    getcwd((char *) &current, PATH_MAX);

    if (args[1] == NULL) {
        if (chdir(getenv("HOME")) == -1) {
            perror("Failed to chdir");
        }
        return;
    } else if (args[1][0] == '-' && last_dir[0] != 0) {
        chdir((const char *) &last_dir);
        return;
    } else if (args[1][0] == '-') {
        putz("No previous dir");
        return;
    }

    strcpy(last_dir, current);

    if (chdir(args[1]) == -1) {
        perror("Failed to chdir");
    }
}
Example #9
0
File: line.c Project: erukiti/ma
SHELL	uint	editor()
{
	char	buf[80+1];
	uint	retcode;

/* dbmsgout("ed_start"); */
	ed.c='\0';
	ed.fpe=op_temp("w+");
	if (ed.fpe ==NULL)
		error("");
	disp_text("editline");
	retcode=TRUE;

	if (iscall())
		{
		 ch.frug.monitor=0;
		 hcputs("--- 唯今 書き込み中 ---\r\n");
		}

	for(;;)
		{
/* dbprt("ed_main:",lpu(ftell(ed.fpe),0),"\n",NULL); */
/*dbmsgout("ed_main");*/
		 ed.c=getestr(buf);
/* dbprintf("Editor:[%s]\n",buf);*/
		 if (iscntrl(ed.c))
		 	ed.c='\0';
		 if (strchr(buf,'<')!=NULL)
		 	{
		 	 sprt(where.param,"<verb>",buf,NULL);
		 	 ed_write();
		 	 continue;
		 	}
		 switch(*buf)
		 	{
		  case '.':
		  	 if (buf[1]=='\0' ||buf[1]=='.')
		  	 	{
		  	 	 fclose(ed.fpe);
		  	 	 goto ret;
		  	 	}
		  	 continue;
		  case '/':
			 if (strlen(buf)<4)
			 	{
			  	 switch(buf[1])
			  	 	{
				  case 'b':
/* アーティクルにバイナリを付属する。*/
					 msgout(art.file!=ERROR?IC_err 
					 	"一つのアーティクルに複数のファイルを付加できません。":
						pool_upload()?
			 				IC_ok  "バイナリファイルをアップロードしました。" :
			 				IC_stop"バイナリの添付を中止しました。");
				 	 continue;
			  	  case 'u':
				 	 if (file_upload(ed.fpe))
				 	 	msgout(IC_ok "テキストアップロードしました。");
					 fflush(ed.fpe);
			  	  	 continue;
			  	  case '/':
			  	 	 fclose(ed.fpe);
			  	 	 goto ret;
			  	  case 'a':
			  	  case 'q':
/*dbmsgout("ed_abort");*/
					 fclose(ed.fpe);
					 retcode=FALSE;
			  	  	 goto ret;
			  	  case 't':
					 msgout("最初から読みだします。");

					 rewind(ed.fpe);
					 while(fgets(buf,sizeof(buf),ed.fpe))
					 	putz(buf);
					 clearerr(ed.fpe);
			  	  	 continue;
			  	  case '?':
			  	  	 disp_text("ed.hlp");
			  	  	 continue;
			  	  	}
				}
		  case '\\':
		  case '^':
		  case '*':
		 	 sprt(where.param,"<verb>",buf,NULL);
		 	 ed_write();
		 	 continue;
		  	}
		 strcpy(where.param,buf);
		 ed_write();
		}
ret:
	if (iscall())
		ch.frug.monitor=1;
	return retcode;
}
Example #10
0
EXTERN_ENV

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#include "decs.h"

/* perform multigrid (w cycles)                                     */
void multig(long my_id)
{
    long iter;
    double wu;
    double errp;
    long m;
    long flag;
    long k;
    long my_num;
    double wmax;
    double local_err;
    double red_local_err;
    double black_local_err;
    double g_error;

    flag = 0;
    iter = 0;
    m = numlev - 1;
    wmax = maxwork;
    my_num = my_id;
    wu = 0.0;

    k = m;
    g_error = 1.0e30;
    while (!flag) {
        errp = g_error;
        iter++;
        if (my_num == MASTER) {
            multi->err_multi = 0.0;
        }

/* barrier to make sure all procs have finished intadd or rescal   */
/* before proceeding with relaxation                               */
#if defined(MULTIPLE_BARRIERS)
        BARRIER(bars->error_barrier, nprocs)
#else
        BARRIER(bars->barrier, nprocs)
#endif
        copy_black(k, my_num);

        relax(k, &red_local_err, RED_ITER, my_num);

/* barrier to make sure all red computations have been performed   */
#if defined(MULTIPLE_BARRIERS)
        BARRIER(bars->error_barrier, nprocs)
#else
        BARRIER(bars->barrier, nprocs)
#endif
        copy_red(k, my_num);

        relax(k, &black_local_err, BLACK_ITER, my_num);

/* compute max local error from red_local_err and black_local_err  */

        if (red_local_err > black_local_err) {
            local_err = red_local_err;
        } else {
            local_err = black_local_err;
        }

/* update the global error if necessary                         */

        LOCK(locks->error_lock)
            if (local_err > multi->err_multi) {
            multi->err_multi = local_err;
        }
        UNLOCK(locks->error_lock)

/* a single relaxation sweep at the finest level is one unit of    */
/* work                                                            */
        wu += pow((double) 4.0, (double) k - m);

/* barrier to make sure all processors have checked local error    */
#if defined(MULTIPLE_BARRIERS)
        BARRIER(bars->error_barrier, nprocs)
#else
        BARRIER(bars->barrier, nprocs)
#endif
        g_error = multi->err_multi;

/* barrier to make sure master does not cycle back to top of loop  */
/* and reset global->err before we read it and decide what to do   */
#if defined(MULTIPLE_BARRIERS)
        BARRIER(bars->error_barrier, nprocs)
#else
        BARRIER(bars->barrier, nprocs)
#endif
        if (g_error >= lev_tol[k]) {
            if (wu > wmax) {
/* max work exceeded                                               */
                fprintf(stderr, "ERROR: Maximum work limit %0.5f exceeded\n", wmax);
                exit(-1);
            } else {
/* if we have not converged                                        */
                if ((k != 0) && (g_error / errp >= 0.6) && (k > minlevel)) {
/* if need to go to coarser grid                                   */

                    copy_borders(k, my_num);
                    copy_rhs_borders(k, my_num);

/* This bar is needed because the routine rescal uses the neighbor's
   border points to compute s4.  We must ensure that the neighbor's
   border points have been written before we try computing the new
   rescal values                                                   */

#if defined(MULTIPLE_BARRIERS)
                    BARRIER(bars->error_barrier, nprocs)
#else
                    BARRIER(bars->barrier, nprocs)
#endif
                    rescal(k, my_num);

/* transfer residual to rhs of coarser grid                        */
                    lev_tol[k - 1] = 0.3 * g_error;
                    k = k - 1;
                    putz(k, my_num);
/* make initial guess on coarser grid zero                         */
                    g_error = 1.0e30;
                }
            }
        } else {
/* if we have converged at this level                              */
            if (k == m) {
/* if finest grid, we are done                                     */
                flag = 1;
            } else {
/* else go to next finest grid                                     */

                copy_borders(k, my_num);

                intadd(k, my_num);
/* changes the grid values at the finer level.  rhs at finer level */
/* remains what it already is                                      */
                k++;
                g_error = 1.0e30;
            }
        }
    }
    if (do_output) {
        if (my_num == MASTER) {
            printf("iter %ld, level %ld, residual norm %12.8e, work = %7.3f\n", iter, k, multi->err_multi, wu);
        }
    }
}