Exemple #1
0
int main(int argc, const char * argv[]) {
	char test[420];
	printf("Enter a smallish number: ");
	scanf("%[^\n]%*c", test);
	if (is_valid_int(test))
		modtest(to_int(test));
	printf("\n%d\t%d\n",is_valid_int(test),to_int(test));
	printf("%d\t%g\n",is_valid_number(test),to_double(test));
	return 420;
}
Exemple #2
0
int main(){

	char egn[12];
	int egn_int[10], i;
	
	scanf("%s", egn);

	if(strlen(egn) != 10){
	
		printf("0\n");
	
	}else{
	
		for(i = 0; i < 10; i++){
		
			egn_int[i] = is_valid_int(egn[i]);
		
			if(egn_int[i] == -1){
			
				printf("0\n");
				return 0;
			
			}
		
		}
	
	}

	printf("%d\n", is_valid(egn_int) );

	return 0;

}
Exemple #3
0
Fichier : type.c Projet : ryvnf/zc1
struct type *type_new_from_ast(struct ast *ast) {
	switch (ast->type) {
	case SYN_VOID:
		assert(is_valid_void(ast));
		return type_new_void();
	case SYN_BOOL:
		assert(is_valid_bool(ast));
		return type_new_bool();
	case SYN_BYTE:
		assert(is_valid_byte(ast));
		return type_new_byte();
	case SYN_SHORT:
		assert(is_valid_short(ast));
		return type_new_short();
	case SYN_INT:
		assert(is_valid_int(ast));
		return type_new_int();
	case SYN_LONG:
		assert(is_valid_long(ast));
		return type_new_long();
	case SYN_UBYTE:
		assert(is_valid_ubyte(ast));
		return type_new_ubyte();
	case SYN_USHORT:
		assert(is_valid_ushort(ast));
		return type_new_ushort();
	case SYN_UINT:
		assert(is_valid_uint(ast));
		return type_new_uint();
	case SYN_ULONG:
		assert(is_valid_ulong(ast));
		return type_new_ulong();
	case SYN_HALF:
		assert(is_valid_half(ast));
		return type_new_half();
	case SYN_FLOAT:
		assert(is_valid_float(ast));
		return type_new_float();
	case SYN_DOUBLE:
		assert(is_valid_double(ast));
		return type_new_double();
	case SYN_POINTER:
		assert(is_valid_pointer(ast));
		return pointer_ast(&ast->val.op);
	case SYN_ARRAY:
		assert(is_valid_array(ast));
		return array_ast(&ast->val.op);
	case SYN_ARRAY_UNDEFINED_SIZE:
		assert(is_valid_array_undefined_size(ast));
		return array_undefined_size_ast(&ast->val.op);
	case SYN_FUNCTION:
		assert(is_valid_function(ast));
		return function_ast(&ast->val.op);
	default:
		ERROR_HERE;
	}
}
Exemple #4
0
int		check_args(int ac, char **av, t_env *e)
{
	int	nbarg;
	int	i;

	i = 1;
	while (i < ac && is_option(av[i], e))
		i++;
	while (--ac >= i)
	{
		nbarg = ac;
		if (!is_valid_int(av[ac]))
			return (0);
		while (--nbarg)
		{
			if (ft_atoi(av[ac]) == ft_atoi(av[nbarg]))
				return (0);
		}
	}
	ac++;
	return (ac);
}
Exemple #5
0
void nomi_proc(char *path, char** array_nomi_proc) {

    int i = 0;
    DIR * d;
    struct dirent *dir;
    d = opendir(path);
         if (d == NULL){ 
            syslog(LOG_ERR, "non riesco ad aprire /proc/");         //exit se d è nullo
            exit(EXIT_FAILURE);
        }
    if (d != NULL) {
        while ((dir = readdir(d)) != NULL) {
            if (strcmp(".", dir -> d_name) == 0 || strcmp("..", dir -> d_name) == 0) {
                continue;
            }
            if (!(is_valid_int(dir -> d_name)))
                continue;
            strcpy(array_nomi_proc[i], dir -> d_name);            //salva nell'array di nomi dei processi il nome della cartella in cui sta leggendo
            i++;
        }
        closedir(d);
    }
}
Exemple #6
0
int num_proc(char *path) {

    int i = 0;
    DIR * d;
    struct dirent *dir;
    d = opendir(path);
    if (d == NULL){ 
            syslog(LOG_ERR, "non riesco ad aprire /proc/");           //exit se d è nullo
            exit(EXIT_FAILURE);
        }
    if (d != NULL) {
        while ((dir = readdir(d)) != NULL) {
            if (strcmp(".", dir -> d_name) == 0 || strcmp("..", dir -> d_name) == 0) {
                continue;
            }
            if (!(is_valid_int(dir -> d_name)))        
                continue;
            i++;                        //se il nome della cartella è un numero aumenta il contatore
        }
        closedir(d);
    }
    return i;                    //ritorna il numero di cartelle che hanno per nome un numero (ossia le cartelle dei processi)
}
Exemple #7
0
bool is_valid_shelf(char *str)
{
  if (isalpha(str[0]) && strlen(str) == 3 && is_valid_int(str+1))
    return true; 
  return false;
}
Exemple #8
0
int parse_file_into_processes(char *filename, int *num_processes, cpu_process **processes){
    FILE *fd = NULL;
    char buffer[1024];
    char *fgets_rtn = NULL;
    
    /* Open File */
    fd = fopen(filename, "r");
    if(NULL == fd){
        fprintf(stderr, "Error: Cannot open the file %s for reading!\n", filename);
        return -1;
    }
    
    /* Read File */
    int i = -1;
    while(0 == feof(fd) && i != *num_processes){
        char * str_ptr  = NULL;
        fgets_rtn = fgets(buffer, 1024, fd);
        
        if( NULL == fgets_rtn) {
            break;
        }
        
        /* Strip off the new line */
        if( '\n' == buffer[ strlen(buffer) - 1] ) {
            buffer[ strlen(buffer) - 1] = '\0';
        }
        
        /* If this is the first line in the file it contains the process count */
        if(i == -1){
            str_ptr = strtok(buffer, " ");
            /* Check for valid integer before we call strtol()*/
            if(is_valid_int(str_ptr) != 0){
                return -1;
            }
            *num_processes = strtol(str_ptr, NULL, 10);
            /* Add correct amount of space in the processes array */
            (*processes) = (cpu_process *)realloc((*processes), (sizeof(cpu_process) * (*num_processes)));
            if( NULL == (*processes) ) {
                fprintf(stderr, "Error: Failed to allocate memory! Critical failure on %d!", __LINE__);
                exit(-1);
            }
        }    
        /* All other lines should be treated as a process */
        else{
            int j = 0;
            for( str_ptr = strtok(buffer, " ");
                NULL   != str_ptr && *str_ptr != '\n';
                str_ptr = strtok(NULL, " ") ) {
                switch(j){
                    case 0:
                        /* Check for valid integer before we call strtol()*/
                        if(is_valid_int(str_ptr) != 0){
                            return -1;
                        }
                        (*processes)[i].identifier = strtol(str_ptr, NULL, 10);
                        break;
                    case 1:
                        /* Check for valid integer before we call strtol()*/
                        if(is_valid_int(str_ptr) != 0){
                            return -1;
                        }
                        (*processes)[i].burst_length = strtol(str_ptr, NULL, 10);
                        break;
                    case 2:
                        /* Check for valid integer before we call strtol()*/
                        if(is_valid_int(str_ptr) != 0){
                            return -1;
                        }
                        (*processes)[i].priority = strtol(str_ptr, NULL, 10);
                        /* Initialize these for later use */
                        (*processes)[i].waiting = -1;
                        (*processes)[i].turnaround = -1;
                        break;
                    default:
                        break;
                }
                j++;
            }
        }
        i++;
    }
    return 0;
}
Exemple #9
0
int parse_command_line_arguments(int argc, char *argv[], char *filename, algorithm_type *algorithm, int *quantum){
    int i;    
    int filename_found = 1;
    int algorithm_found = 1;
    int quantum_found = 1;
    
    for(i = 1; i < argc; i++){
        /* Check for alrogithm type flag */
        if(strncmp("-s", argv[i], strlen("-s")) == 0){
            /* Check to see if there is another argument and assign that value to algorithm_type */
            if(i + 1 < argc){
                /* Check for valid integer before we call strtol()*/
                if(is_valid_int(argv[i + 1]) != 0){
                    return -1;
                }
               int algorithm_identifier = strtol(argv[i + 1], NULL, 10);
               switch(algorithm_identifier){
                   case 1:
                       *algorithm = FCFS;
                       algorithm_found = 0;
                       break;
                   case 2:
                       *algorithm = SJF;
                       algorithm_found = 0;
                       break;
                   case 3:
                       *algorithm = PRIORITY;
                       algorithm_found = 0;
                       break;
                   case 4:
                       *algorithm = RR;
                       algorithm_found = 0;
                       break;
                   default:
                       *algorithm = NONE;
                       break;
                }
                /* Skip the next argument since we just used it */
                i++;                
            }
        }                
        /* Check for quantum type flag */
        else if(strncmp("-q", argv[i], strlen("-q")) == 0){
            /* Check to see if there is another argument and assign that value to quantum */
            if(i + 1 < argc){
                /* Check for valid integer before we call strtol()*/
                if(is_valid_int(argv[i + 1]) != 0){
                    return -1;
                }
                *quantum = strtol(argv[i + 1], NULL, 10);
                quantum_found = 0;
                /* Skip the next argument since we just used it */
                i++;
            }
        }
        /* Treat everything else as a filename, but only assign 1 filename. */
        else if(filename_found != 0){
            strcpy(filename, argv[i]);
            filename_found = 0;
        }
    }
    
    /* Check that filename and algorithm were found */
    if(filename_found != 0 || algorithm_found != 0){
        return -1;
    }
    
    /* Check that quantum was found if using Round-Robin */
    if(*algorithm == RR){
        if(quantum_found != 0){
            return -1;
        }
    }
    /* If we aren't using Round-Robin, make sure that quantum is 0 */
    else{
        *quantum = 0;
    }
    
    return 0;
}
Exemple #10
0
int main()
{
   FILE *fp, *fpd, *fpo;
   char c;
   char buffer[MAXS];
   char outbuffer[MAXS2] = "";
   char dictionary[DICS];
   char* token;
	double flag;
	int isreplaced=0;
	int numcount=1;

   /* Open file for both reading and writing */
   fprintf(stderr, "Opening files ...\n");
   fp = fopen("input.txt", "r+");
   fpd = fopen("dictionary.txt", "r+");
   fpo = fopen("output.txt", "w+");

   /* Write data to the file */
   //fwrite(c, strlen(c) + 1, 1, fp);

   /* Seek to the beginning of the file */
   //fseek(fp, SEEK_SET, 0);

   /* Read and display data */
   //fread(buffer, sizeof(buffer)+1, 1, fp);
   fgets(buffer, sizeof(buffer), fp);
   remove_newline_ch(buffer);
   printf("Input paragraph;\n%s\n", buffer);
   
  /* while(!feof(fpd))
   {
   fgets(dictionary, sizeof(dictionary), fpd);	
   	printf("%s\n", dictionary);
   }*/
   
 token = strtok(buffer, " ");
while (token) {
		remove_punct_and_make_lower_case(token);
   // printf("\n\ntoken: %s, lenght: %d\n\n\n", token, strlen(token));

    // checking for numbers
    if(is_valid_int(token)){
    	printf("\nNumber %d: %s",numcount, token);
    	numcount++;
    	convert_to_words(token);
		strcat(outbuffer, token);
        strcat(outbuffer, " ");	
    } else {

	if(strlen(token) >= 4){


    // checking for spelling
    fseek(fpd, SEEK_SET, 0);
    	isreplaced = 0;
       while(!feof(fpd))
   {

   	flag = 0.0;
   fgets(dictionary, sizeof(dictionary), fpd);	
   remove_punct_and_make_lower_case(dictionary);
   remove_newline_ch(dictionary);
  // 	printf("%s\n", dictionary);
   	
   	   		   	if(strlen(token) >= 4){
   	flag = is_similar(token, dictionary);

   	/*{
   		strcat(outbuffer, token);
        strcat(outbuffer, " ");	
   	}if(flag == 0.0){
   	strcat(outbuffer, token);
        strcat(outbuffer, " ");		
   	} else */

	if(isreplaced != 1){
   	if(flag > 0.6){
   		strcat(outbuffer, dictionary);
        strcat(outbuffer, " ");	
        isreplaced = 1;
   	} else if(flag == 1.0){	
   	strcat(outbuffer, token);
        strcat(outbuffer, " ");	
		isreplaced = 1;	
   	}
	}
	   	} 
   }
   	if(isreplaced == 0){
	strcat(outbuffer, token);
        strcat(outbuffer, " ");		
	   	}
    } else {
		strcat(outbuffer, token);
        strcat(outbuffer, " ");	
	}
	} 
    token = strtok(NULL, " ");
}
   
   fwrite(outbuffer, strlen(outbuffer) + 1, 1, fpo);
   
   fclose(fp);
   fclose(fpd);
   fclose(fpo);
   
   return(0);
}
Exemple #11
0
int check_command_line(int argc, char * argv[], int *pirates_number, int* ninjas_number, int* time_to_run)
{
    switch(argc)
    {
        case 2:
        /* time to run */
        if( !is_valid_int(argv[1]))
            return 0;
        else
        {
            int convert_int = strtol(argv[1],NULL,10);
            if(convert_int < 0)
                return 0;
            else
            {
                *time_to_run = convert_int;
                *pirates_number = 5;
                *ninjas_number = 5;
                return 1;
            }
        }
        break;

        case 3:
        if( is_valid_int(argv[1]) && is_valid_int(argv[2]) )
        {
            int convert_int1 = strtol(argv[1],NULL,10);
            int convert_int2 = strtol(argv[2],NULL,10);
            if( convert_int1 > 0 && convert_int2 >0)
            {
                *time_to_run = convert_int1;
                *pirates_number = convert_int2;
                return 1;

            }else
                return 0;
        }else
            return 0;
        break;
        
        case 4:
        if( is_valid_int(argv[1]) && is_valid_int(argv[2]) && is_valid_int(argv[3]) )
        {
            int convert_int1 = strtol(argv[1],NULL,10);
            int convert_int2 = strtol(argv[2],NULL,10);
            int convert_int3 = strtol(argv[3],NULL,10);
            if( convert_int1 > 0 && convert_int2 >0 && convert_int3 > 0)
            {
                *time_to_run = convert_int1;
                *pirates_number = convert_int2;
                *ninjas_number = convert_int3;
                return 1;

            }else
                return 0;
        }else
            return 0;
        break;
        
        default:
        return 0;
        break;
    }
}
Exemple #12
0
void execnice(Cmd c, int inPipeId, int outPipeId)
{
  int newfd;
  int i=0;
  char* str;
  int which = PRIO_PROCESS;
  id_t pid;

  if(c->next == NULL)
  {
  //if(fork() == 0)
  //  {  
      redirection(c, inPipeId, outPipeId);

      if(c->args[1] == NULL)
      {
        pid = getpid();
        if(setpriority(which, pid, 4) == -1)
          perror("Cannnot set priority");
        //fprintf(stderr, "The priority set is: %d\n", getpriority(which, pid));
      }
      else if(is_valid_int(c->args[1]) == 0)
      {
        pid = getpid();
        if(setpriority(which, pid, 4) == -1)
          perror("Cannnot set priority");
        //fprintf(stderr, "The priority set is: %d\n", getpriority(which, pid));
        if(c->args[2] != NULL)
        {
          c->args+=1;
          c->nargs-=1;
          //fprintf(stderr, "The priority set is: %d for command: %s\n", getpriority(which, pid),c->args[0]);
          prCmd(c, inPipeId, outPipeId);
        }
      }
      else{

        pid = getpid();
        if(setpriority(which, pid, atoi(c->args[1])) == -1)
          perror("Cannnot set priority");
        //fprintf(stderr, "The priority set is: %d\n", getpriority(which, pid));
        if(c->args[2] != NULL)
        {
          c->args+=2;
          c->nargs-=2;
          //fprintf(stderr, "The priority set is: %d for command: %s\n", getpriority(which, pid),c->args[0]);
          prCmd(c, inPipeId, outPipeId);
        }
        

      }

    //  exit(0);
    //}
    //else
    //{
    //  wait(); 
    //}


  }
  else
  {


    if(fork() == 0)
    {  
      redirection(c, inPipeId, outPipeId);

      if(c->args[1] == NULL)
      {
        pid = getpid();
        if(setpriority(which, pid, 4) == -1)
          perror("Cannnot set priority");
        //fprintf(stderr, "The priority set is: %d\n", getpriority(which, pid));
      }
      else if(is_valid_int(c->args[1]) == 0)
      {
        pid = getpid();
        if(setpriority(which, pid, 4) == -1)
          perror("Cannnot set priority");
        //fprintf(stderr, "The priority set is: %d\n", getpriority(which, pid));
        if(c->args[2] != NULL)
        {
          c->args+=1;
          c->nargs-=1;
          //fprintf(stderr, "The priority set is: %d for command: %s\n", getpriority(which, pid),c->args[0]);
          prCmd(c, inPipeId, outPipeId);
        }
      }
      else{

        pid = getpid();
        if(setpriority(which, pid, atoi(c->args[1])) == -1)
          perror("Cannnot set priority");
        //fprintf(stderr, "The priority set is: %d\n", getpriority(which, pid));
        if(c->args[2] != NULL)
        {
          c->args+=2;
          c->nargs-=2;
          //fprintf(stderr, "The priority set is: %d for command: %s\n", getpriority(which, pid),c->args[0]);
          prCmd(c, inPipeId, outPipeId);
        }
        

      }

      exit(0);
    }
    else
    {
      close((pipes+(2*outPipeId))[1]);
      wait(); 
    }

  }

}
Exemple #13
0
int main(int argc, char * argv[]) {    
    int N;
    int max_N = 1024;
    double time;
    mtype_t *matrix = NULL;
    mtype_t scalar = 1;
    
    // Initialize the support library
    support_init();
    
    /* Command line argument handling */
    if(argc >= 2) {
        if(is_valid_int(argv[1]) == 0){
            max_N = (int)strtol(argv[1], NULL, 10);
        }
    }
    
    /* run_experiment_ij */
    printf("\n---------------------------\n\n");
    printf("Executing: run_experiment_ij()\n\n");
    for(N = 2; N <= max_N;){
        allocate_matrix(&matrix, N);
        time = run_experiment_ij(matrix, scalar, N);
        printf("Matrix Size in bytes: %lu\n", sizeof(mtype_t) * N * N);
        printf("megaFLOPS = %f\n\n", (1 * N * N) / time / 1000000);
        N = N*2;
        
        /* Cleanup */
        if(matrix != NULL){
            free(matrix);
            matrix = NULL;
        }
    }
    
    /* run_experiment_ji */
    printf("\n---------------------------\n\n");
    printf("Executing: run_experiment_ji()\n\n");
    for(N = 2; N <= max_N;){
        allocate_matrix(&matrix, N);
        time = run_experiment_ji(matrix, scalar, N);
        printf("Matrix Size in bytes: %lu\n", sizeof(mtype_t) * N * N);
        printf("megaFLOPS = %f\n\n", (1 * N * N) / time / 1000000);
        N = N*2;
        
        /* Cleanup */
        if(matrix != NULL){
            free(matrix);
            matrix = NULL;
        }
    }
    
    /* Cleanup */
    if(matrix != NULL){
        free(matrix);
        matrix = NULL;
    }

    // Finalize the support library
    support_finalize();
    
    return 0;
}