Esempio n. 1
0
int
main(void)
{
	initarray();
	sort(A, SIZE);
	check();
	return 0;
}
Esempio n. 2
0
int
main(void)
{
	initarray();
	sort(A, SIZE);
	int result = check();
	if(result)
		return result;
	return 0;
}
/*
 * main function
 */
void main(){
    int x = 0;
    char *ptr = NULL;
    char *index = NULL;
    int index2 = 0;
    /*
     * initialize the array
     */
    initarray();
    /*
     * Allocate memory for the input text
     */
    ptr = (char *)malloc(sizeof(char) * MAX_TEXT_LEN);
    if(ptr == NULL){
        printf("Failed to allocate memory\n");
        return;
    }
    get_input_from_console(ptr);
    printf("%s\n", ptr);
    /*
     * count the occurences of each alphabet in the inout text
     */
    index = ptr;
    while(*index != '\0'){
        if(isalpha(*index)){
            index2 = (int)(tolower(*index)) - (int)('a');
            occurences[index2].count++;
        }
        index++;
    }
    /*
     * Print the output
     */
    for(x = 0;x < 26;x++){
        printf("Alphabet: %c, Count: %d\n", occurences[x].alphabet, occurences[x].count);
    }

}
Esempio n. 4
0
void cbzone_main(
     int argc,
     const char* const argv[])
{
#ifdef DEVELOPER
  int passes = 0;
  struct timeval game_start;
  struct timeval game_end;
#endif //DEVELOPER

  gprinqconfig(&argc, argv);
  limit = opt->delay * 1.2e4;

  /* now that we have parsed the options, we know how large to */
  /* make the world.  Use calloc here as most of the array     */
  /* should start off 0.                                       */

  pl = o = (Genericp) calloc(opt->mobjects,sizeof(Generic));

  if (o == NULL) {
    printf("Malloc failed...trying to create too many objects?\n");
#ifdef WIN32
     return;
#else //X11
     myexit(1);
#endif
  }

  initarray(o);                         /* prepare the main array */
  srandom(time((long *) 0));            /* start things off randomly */
  screeninit();
  updatedisplay(missilerun, lander, score, opt->numleft, sens, False);
  xhairs(aligned);
  gprinqcursor((Position_t *)position);
  event = gprcondeventwait(&key, (Position_t *)position);
  joystick(position, sens, pl);

  /* place the objects out there to start the game.  if the player is */
  /* is_new, then the objects are placed at a random distance, else   */
  /* the objects get placed on the horizon.                           */

  placeobjects(o, missilerun, score);
  pl->attr &= ~IS_NEW;                  /* now the objects can be */
                                        /* placed at the horizon. */

  /* now calculate ranges to all the objects and translate them */
  /* into a player-centric coordinate system                    */

  pl->ca = cos(pl->azm);
  pl->sa = sin(pl->azm);
  for (g=o+opt->estart; g<o+opt->mobjects; g++)
    if (g->attr & IS_ALIVE) {
      dx = g->x - pl->x;
      dy = g->y - pl->y;
      g->range = sqrt(dx*dx + dy*dy);
      g->proy = -dx * pl->sa + dy * pl->ca;
      g->prox = dx * pl->ca + dy * pl->sa;
    }

  scanner(o);
  drawhorizon(pl->azm);

  /* now the work really starts....we just iterate through */
  /* the following loop until the player dies or quits     */
#ifdef DEVELOPER
  gettimeofday(&game_start, 0);
#endif
}
Esempio n. 5
0
int main(int argc, char *argv[]) {
    //Initialize the shell
    init_shell();

    int numofpipes[MAXCMD];

    //Initialize jobs array
    init_jobsarray();

    //Holds the command
    char wholecommand[MAXNUM];
    char dupwholecommand[MAXNUM];
    char *cmdarr[MAXCMD][MAXOP];	//The rows hold commands separated by ;
    //while the columns hold the command and its options separated by " " or \t
    char *pipecmdarr[MAXCMD][MAXCMD][MAXOP];

    char maincmd[MAXNUM];

    //For prompt
    char username[MAXNUM],sysname[MAXNUM],cwdir[MAXNUM];
    char homedir[MAXNUM];
    getcwd(homedir,MAXNUM);
    char duphomedir[MAXNUM];
    duphomedir[0] = '\0';
    strcat(duphomedir,homedir);

    //For tokenizing
    char *str1 = NULL;
    char *token = NULL;

    //Counters
    int i;

    //Initializing background process array
    for(i=0; i<(MAXNUM*MAXOP); i++) {
        bgrounds[i].used = 0;
    }
    //Initializing ends

    //Initialize to NULL
    initarray(cmdarr);

    char *hdir[MAXNUM];
    //Tokenize home directory
    tokenize(str1,hdir,token,duphomedir);
    //Tokenize home directory done

    while(1) {

        if(signal(SIGINT,signal_handler)==SIG_ERR)
            perror("Signal not caught!!");
        if(signal(SIGCHLD,signal_handler)==SIG_ERR)
            perror("Signal not caught!!");

        for(i=0; i<MAXCMD; i++)
            numofpipes[i] = 0;

        rectifyjobsarray();

        //Printing message on background process completion
        for(i=0; i<(MAXNUM*MAXOP); i++) {
            if(bgrounds[i].used == 0)
                continue;
            bgrounds[i].used = 0;
            if(bgrounds[i].status == 1) {
                //printf("Process %s with pid %d terminated normally\n",bgrounds[i].name,bgrounds[i].pid);
            }
            else {
                //printf("Process %s with pid %d terminated with status %d\n",bgrounds[i].name,bgrounds[i].pid,bgrounds[i].status);
            }
        }

        //Renitializing background process array
        for(i=0; i<(MAXNUM*MAXOP); i++) {
            bgrounds[i].used = 0;
        }
        //Renitializing ends

        //Initialize to NULL
        initarray(cmdarr);
        initpipecmd(pipecmdarr);

        //For prompt
        username[0] = '\0';
        cwdir[0] = '\0';
        strcat(username,getlogin());
        gethostname(sysname,MAXNUM);
        getcwd(cwdir,MAXNUM);
        char dupcwdir[MAXNUM];
        dupcwdir[0] = '\0';
        strcat(dupcwdir,cwdir);

        directorysettings(cwdir,homedir,dupcwdir,hdir);
        //Prompt settings done

        //Take commands
        printf("<%s@%s:%s>",username,sysname,cwdir);
        wholecommand[0] = '\0';
        dupwholecommand[0] = '\0';
        readLine(wholecommand);
        wholecommand[strlen(wholecommand)-1]='\0';
        strcpy(dupwholecommand,wholecommand);
        //Done taking commands

        //Tokenize
        maintokenize(cmdarr,dupwholecommand);
        //tokentest(cmdarr);
        pipetokenize(cmdarr,pipecmdarr,numofpipes);
        //pipetokenizetest(cmdarr,pipecmdarr,numofpipes);
        //exit(EXIT_SUCCESS);
        //continue;
        //Tokenize done


        //Executing each ; separated command
        for(i = 0; i< MAXCMD; i++) {
            if(cmdarr[i][0] == NULL)
                continue;
            maincmd[0] = '\0';
            strcat(maincmd,cmdarr[i][0]);

            strcpy(infile,"\0");
            strcpy(outfile,"\0");


            if(numofpipes[i] != 0) {		//Execute piped commands
                pipesexecute(cmdarr, pipecmdarr, numofpipes, i);
                continue;
            }


            if((strcmp(maincmd,"echo") == 0) || (strcmp(maincmd,"pwd") == 0) || (strcmp(maincmd,"cd") == 0)\
                    || (strcmp(maincmd,"clear") == 0) || (strcmp(maincmd,"quit") == 0) || (strcmp(maincmd,"pinfo") == 0) ||
                    strcmp(maincmd,"jobs") == 0 || strcmp(maincmd,"overkill") == 0 || strcmp(maincmd,"kjob") == 0
                    || strcmp(maincmd,"fg") == 0 ) {

                int dupstdout = dup(1);
                int dupstdin = dup(0);

                if(checkinfile(cmdarr,i) == 1) {

                    if(strcmp(infile,"\0") != 0) {
                        int in=open(infile,O_RDONLY);
                        dup2(in,0);
                        close(in);
                    }
                }

                int outretval = checkoutfile(cmdarr,i);
                if(outretval == 1 || outretval == 2) {

                    if(strcmp(outfile,"\0") != 0) {
                        if(outretval == 1) {
                            int out=open(outfile,O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
                            dup2(out,1);
                            close(out);
                        }
                        else {
                            if(fopen(outfile,"r") == NULL) {
                                int out=open(outfile,O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
                                dup2(out,1);
                                close(out);
                            }
                            else {
                                int out=open(outfile,O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
                                dup2(out,1);
                                close(out);
                            }
                        }
                    }
                }


                //echo execution
                if(strcmp (maincmd,"echo") == 0) {
                    exececho(wholecommand,cmdarr,i);
                }
                //echo done

                //pwd execution
                if(strcmp (maincmd,"pwd") == 0) {
                    execpwd();
                }
                //pwd done

                //chdir execution
                if(strcmp (maincmd,"cd") == 0) {
                    execcd(cmdarr,homedir,i);
                }
                //chdir done

                //clear execution
                if(strcmp (maincmd,"clear") == 0) {
                    printf("\e[1;1H\e[2J");
                }
                //clear done

                //terminal quit
                if(strcmp (maincmd,"quit") == 0) {
                    exit(EXIT_SUCCESS);
                }
                //terminal quit done

                //jobs
                if(strcmp (maincmd,"jobs") == 0) {
                    execjobs();
                }
                //jobs done

                //overkill
                if(strcmp (maincmd,"overkill") == 0) {
                    execoverkill();
                }
                //overkill done

                //kjob
                if(strcmp (maincmd,"kjob") == 0) {
                    execkjob(cmdarr,i);
                }
                //kjob done

                //fg
                if(strcmp (maincmd,"fg") == 0) {
                    execfg(cmdarr,i);
                }
                //fg done

                //pinfo
                if(strcmp (maincmd,"pinfo") == 0) {
                    pid_t proid;
                    int flag = 0;
                    if(cmdarr[i][1] == NULL)	//pinfo of shell
                        proid = getpid();
                    else {	//pinfo of given pid
                        char temps[MAXNUM];
                        strcpy(temps,cmdarr[i][1]);
                        proid = atoi(temps);
                        if(kill(proid,0) == (-1)) {
                            printf("No such process exists\n");
                            continue;
                        }
                        flag = 1;
                    }
                    getpinfo(proid, flag, homedir);
                }
                //pinfo done

                dup2(dupstdin,0);
                dup2(dupstdout,1);

            }
            else {