Exemplo n.º 1
0
int main(void){
	//a variable to store the result of the exponentiation
	int answer;

	//call a function by writing its name followed by ()
        //provide arguments between the () if the function expects them
        welcome_message();
        //eventually our program will do some stuff

	//call the power function with the correct number of arguments
	//assign (store the value) of the result in the "answer" variable
	//ex. 2 raised to the power of 3
	answer = power(2,3);
	printf("Two raised to the power of three is: %d\n", answer);

	//print our goodbye message 
	goodbye_message(5);
	
}
Exemplo n.º 2
0
int shell() {
  int err = 0;
  extern PCBDLL **PRIORITY_QUEUES;

  // Makin ur queues PCBDLL_CREATION
  PRIORITY_QUEUES = PCBDLL_creation( TDF_QUEUES );
  
  buffer_size = BUFFER_SIZE;
  EXIT_PROMPT_LOOP = 0;
  strcpy(EXIT_STRING, "exit\0");

  welcome_message();

  // Debugging check
  // givemeready();

  while (!EXIT_PROMPT_LOOP) {
    display_prompt(); // `tdf:$ ' currently
    
    SYS_REQ_ERR = sys_req(READ, TERMINAL, BUFFER, &buffer_size);
    err = append_history(BUFFER);
    PARSER_ERROR = parse_buffer(BUFFER, PARSED_CMD, ARGUMENTS);
    // The most trivial case
    if (!strcmp(BUFFER,"\n\0")) { //Just a New Line
      continue;
    }

    switch (PARSER_ERROR) {
    case PARSER_FAIL_WHITESPACE:
      continue;
      break;
    case PARSER_FAIL_LONGNAME:
      printf("Invalid command name entered. Must be less than 9 characters.\n");
      continue;
      break;
    case PARSER_WIN_SINGLETON: //This is linked to PARSER_WIN_HAS_ARGS which follows
    case PARSER_WIN_HAS_ARGS:
      if (!strcmp(PARSED_CMD,EXIT_STRING)) { //Equal to EXIT_STRING
	if(exit_confirmation() == SHELL_EXIT_CONFIRM) {
	  EXIT_PROMPT_LOOP = !EXIT_PROMPT_LOOP;
	  break;
	} else {
	  continue;
	}
      }
      break;
    default:
      printf("I have no idea how you got here. File a bug report!\n");
      printf("Parser exited wth status of: %d\n", PARSER_ERROR);
      continue;
      break;
    }

    if (!strcmp(PARSED_CMD, "date")) {
      date(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "dispatch")) {
      dispatch ( );
    } else if (!strcmp(PARSED_CMD, "display") || !strcmp(PARSED_CMD, "ls")) {
      display(ARGUMENTS);
      continue;
    } else if (!strcmp(PARSED_CMD, "hossgive")){
      givemeready();
    } else if (!strcmp(PARSED_CMD, "load")) {
      load( ARGUMENTS );
    } else if (!strcmp(PARSED_CMD, "loadr3")) {
      loadr3 ( );
    } else if (!strcmp(PARSED_CMD, "history")) {
      history(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "pblock")) {
      pblock(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "pcreate")) {
      pcreate(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "pdelete")) {
      pdelete(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "presume")) {
      presume(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "psetprio")) {
      psetprio(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "pshow")) {
      pshow(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "pshowall")) {
      pshowall();
    } else if (!strcmp(PARSED_CMD, "pshowblk")) {
      pshowblk();
    } else if (!strcmp(PARSED_CMD, "pshowrd")) {
      pshowrd();
    } else if (!strcmp(PARSED_CMD, "psuspend")) {
      psuspend(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "punblock")) {
      punblock(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "version")) {
      version(ARGUMENTS);
    } else if (!strcmp(PARSED_CMD, "help")) {
      help(ARGUMENTS);
    } else {
      //printf("Command not found.\n");
      continue;
    }
  }
  departing_message();

  // Clean up our queues
  PCBDLL_freeall( );

  return err;
}
Exemplo n.º 3
0
/******************************************************************************
 * main
 *****************************************************************************/
int main (int argc, char *argv[])
{
  int listenSfd;        // Listen for control connections on this sfd.
  int *csfd;            // An accepted control socket file descriptor.
  pthread_t thread;     // The handle for a new thread.
  pthread_attr_t attr;  // pthread attribute, to set detached state on creation.
  
  char *rootTemp;

  //Retrieve the name of the root directory from the config file.
  if ((rootTemp = get_config_value ("ROOT_PATH_CONFIG", FTP_CONFIG_FILE)) == NULL)
    return -1;

  /* Append the relative path from the server executable to the server root directory
   * to the absolute path of the server executable. */
  if ((rootdir = get_config_path (rootTemp)) == NULL) {
    free (rootTemp);
    return -1;
  }
  free (rootTemp);

  rootTemp = rootdir;
  /* Canonicalize the path to the server root directory 
   * (eg. resolve all "..", ".", excessive "/" and symbolic links). */
  if ((rootdir = canonicalize_file_name (rootTemp)) == NULL) {
    fprintf (stderr, "%s: canonicalize_file_name: %s\n", __FUNCTION__, strerror (errno));
  }
  free (rootTemp);

  //Initialize the pthread attributes.
  if (pthread_attr_init (&attr) != 0) {
    fprintf (stderr, "%s: pthread_attr_init: %s\n", __FUNCTION__, strerror (errno));
    return -1;
  }

  //Set the detach state attribute.
  if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0) {
    fprintf (stderr, "%s: pthread_attr_init: %s\n", __FUNCTION__, strerror (errno));
    return -1;
  }

  //Create a socket to listen for control connections.
  if ((listenSfd = get_control_sock ()) == -1)
    return -1;

  //Display usage instructions to the server operator, and connection information.
  if (welcome_message() == -1) {
    return -1;
  }

  /* This loop does the following:
   *   -Accepts a control connection from a client.
   *   -Starts a thread for the accepted control connection.
   *   OR
   *   -Read a command from stdin entered on the server console.
   *   -Call server_cmd_handler to perform the command.
   *
   * This loop will exit when:
   *    -malloc cannot allocate memory for an integer.
   *    -The functions pthread_mutex_lock or pthread_mutex_unlock return error.
   *    -The command "shutdown" is entered on the server console. */ 
  while (1) {
    if ((csfd = malloc (sizeof(*csfd))) == NULL) {
      fprintf (stderr, "%s: malloc of %lu bytes failed\n", __FUNCTION__, sizeof(*csfd));
      break;
    }

    //Accept a connection from the client, or read a server command on stdin.
    if ((*csfd = accept_connection (listenSfd, ACCEPT_CONTROL, NULL)) == -1) {
      free (csfd);
      continue;
    } else if (*csfd == STDIN_READY) {   //There is something to read on stdin.
      if (read_server_cmd () == SHUTDOWN_SERVER) {
	shutdownServer = true;
	free (csfd);
	break;
      } else {
	free (csfd);
	continue;
      }
    }

    //Create a new thread for this control connection.
    if (pthread_create (&thread, &attr, &control_thread, csfd) != 0) {
      fprintf (stderr, "%s: pthread_create: %s\n", __FUNCTION__, strerror (errno));
      free (csfd);
      continue;
    }

    //Increment the control connection thread count.
    if (modify_cthread_count (1) == -1)
      break;
  }

  free (rootdir);

  if (activeControlThreads > 0)
    printf ("waiting on threads to resolve...\n");

  //Wait for the control threads to shutdown.
  while (activeControlThreads > 0) {
    sleep (1);
  }

  if (pthread_attr_destroy (&attr) == -1)
    fprintf (stderr, "%s: pthread_attr_destroy: %s\n", __FUNCTION__, strerror (errno));

  printf ("All threads have terminated, exiting the program.\n");
  return 0;
}
Exemplo n.º 4
0
int main(int argc, char *argv[]) {
	char *word = NULL;
	char *ptr = NULL;
	char line[MAXLINE];
	char delim = ".,:;'/\"+-_(){}[]<>*&^%$#@!?|=~/|\\=1234567890 \t\n";
	FILE *fp;
	
	int opt;
	int slice = 2;
		
	if(argc == 1) {
		printf("Incorrect!!");
		return 1;
	}
	//Add a welcome function i.e., help function. 
	// I have no idea what ngram function does. 
	while((opt = getopt(argc,argv,"wn: ucil: \n")) != -1) {
		switch(opt) {
			case 'w':
				welcome_message();
				return 0;
			case 'n':
				if(strchr(optarg,'-') == NULL) {
					slice = atoi(optarg);
					if(slice < 1 || slice > 10) {
						fprintf(stderr, "%s: Error: Value too low or too high. Select n in range of 1 to 10 \n",PACKAGE);
						return 1;
					}
					else {
						/* We can use this in case a range is given instead of a number */
					}
				}
				break;
			case 'u':
				u_flag = 1;
				break;
			case 'c':
				c_flag = 1;
			case 'i':
				i_flag = 1;
				break;
			case 'l':
				l_flag = 1;
				break;
			case ':':
				fprintf(stderr,"%s: Error: Option needs to be an argument ",PACKAGE);
				welcome_message();
				break;
			case '?':
				fprintf(stderr,"%s: Error: Option needs to be an argument ",PACKAGE);
				welcome_message();
				break;
		}
	}
	
	while ((fgets(line, MAXLINE, stdin)) != NULL) {
		if(strlen(line)>1) {
			if(l_flag == 1)
				for(ptr = line; *ptr;ptr++)
					if(isupper(*ptr)) *ptr = tolower(*ptr)
			word = strtok(line,delim);
			while(word != NULL) {
				if(i_flag == 1 && strlen(word) < slice) {
					word = strtok(NULL,delim);
					continue;
				}
				if(r_flag == 0) 
					ngram(word,slice);
				
				word = strtok(NULL,delim);
			}
		}
	}
	if(fp == NULL) 
		printTree(root,stdout);
	else
		printTree(root,fp), pclose(fp);
	return 0;
}
Exemplo n.º 5
0
int main(void){
	//call a function by writing its name followed by ()
	//provide arguments between the () if the function expects them
	welcome_message();	

}