Exemple #1
0
static enum ParserResult parseSimpleCommand(struct Parser* parser,
        struct SimpleCommand* command) {
    command->redirections = NULL;
    command->numRedirections = 0;
    command->words = NULL;
    command->numWords = 0;

    enum ParserResult result;

    struct Token* token = getToken(parser);
    assert(token);

    while (true) {
        if (token->type == IO_NUMBER || token->type == OPERATOR) {
            int fd = -1;
            if (token->type == IO_NUMBER) {
                fd = strtol(token->text, NULL, 10);
                parser->offset++;
            }

            struct Redirection redirection;
            result = parseIoRedirect(parser, fd, &redirection);

            if (result == PARSER_BACKTRACK) {
                if (command->numWords > 0 || command->numRedirections > 0) {
                    return PARSER_MATCH;
                }
                result = PARSER_SYNTAX;
                goto fail;
            }

            if (result != PARSER_MATCH) goto fail;

            if (!addToArray((void**) &command->redirections,
                    &command->numRedirections, &redirection,
                    sizeof(redirection))) {
                result = PARSER_ERROR;
                goto fail;
            }
        } else {
            assert(token->type == TOKEN);
            if (!addToArray((void**) &command->words, &command->numWords,
                    &token->text, sizeof(char*))) {
                result = PARSER_ERROR;
                goto fail;
            }
            parser->offset++;
        }

        token = getToken(parser);
        if (!token) return PARSER_MATCH;
    }

    return PARSER_MATCH;

fail:
    freeSimpleCommand(command);
    return result;
}
Exemple #2
0
	void test(size_t numberTests) {
		stack.reserve(numberTests);
		for (size_t i = 0; i < numberTests; ++i) {
			Operation operation = static_cast<Operation>(rand() % OPERATION_TYPE_NUMBER);
			switch (operation) {
				case CREATE_OBJECT_STACK:
					stack.push_back(A());
					break;
				case CREATE_OBJECT_HEAP:
					heap.push_back(new A());
					break;
				case CREATE_ARRAY: {
					size_t size = rand() % 20;
					arrays.emplace_back(new A[size], size);
					break;
				}
				case ADD_STACK_TO_STACK:
					add(stack, stack, [this](size_t from, size_t to) { stack[to].add(&stack[from]); });
					break;
				case ADD_STACK_TO_HEAP:
					add(stack, heap, [this](size_t from, size_t to) { heap[to]->add(&stack[from]); });
					break;
				case ADD_HEAP_TO_STACK:
					add(heap, stack, [this](size_t from, size_t to) { stack[to].add(heap[from]); });
					break;
				case ADD_HEAP_TO_HEAP:
					add(heap, heap, [this](size_t from, size_t to) { heap[to]->add(heap[from]); });
					break;
				case ADD_ARRAY_TO_ARRAY:
					addToArray(arrays, [this](size_t from, A &to) { to.add(arrays[from]); });
					break;
				case ADD_ARRAY_TO_STACK:
					add(arrays, stack, [this](size_t from, size_t to) { stack[to].add(arrays[from]); });
					break;
				case ADD_ARRAY_TO_HEAP:
					add(arrays, heap, [this](size_t from, size_t to) { heap[to]->add(arrays[from]); });
					break;
				case ADD_STACK_TO_ARRAY:
					addToArray(stack, [this](size_t from, A &to) { to.add(&stack[from]); });
					break;
				case ADD_HEAP_TO_ARRAY:
					addToArray(heap, [this](size_t from, A &to) { to.add(heap[from]); });
					break;
				case OPERATION_TYPE_NUMBER:
					assert(0);
					break;
			}
			if (rand() % 100 == 0) {
				GarbageCollector::getInstance().gc();
			}
		}
	}
Exemple #3
0
Proto arrayInsertObjAt(Proto destarray, Proto obj, int index)
{
    Proto tmp;
    Proto blob;
    int total, i;

    // Ok, loop from index +1 to end
    // Set all of the slots lower

    i = objectGetSlot(destarray, ATOMsize, &tmp);
    assert(i);			// It can only work if this is a array
    
    i = objectGetSlot(destarray, ATOM_array, &blob);
    assert(i);			// It can only work if this is a array

    total = objectIntegerValue(tmp);
    i = index;

    // Catch the invalid case
    if ((i < 0) || (i > (total - 1))) {
	assert(0);
    }
    
    // Handle the simple case
    if ((total) == i) {
	// Create the space
	addToArray(destarray, obj);
	return destarray;
    } else {
	// Create the space
	addToArray(destarray, nil);
    }

    i = total - 1;
    
    while (1) {

	if (i < index) {
	    break;
	}
	
	tmp = objectBlobAtGet(blob, i);
	objectBlobAtSet(blob, (i+1), tmp);
	i--;
    }

    // Finally, place the new obj, buff1 should still be set right
    objectBlobAtSet(blob, (i+1), obj);
    
    return destarray;
}
int alt_sep_test()
{
    int* __CREST_p8 = (int*)malloc(sizeof(int)*10);
    addToArray((void**)&__CREST_p8,8);
    bool enabled, tcas_equipped, intent_not_known;
    bool need_upward_RA, need_downward_RA;
    int alt_sep;

    enabled = High_Confidence && (Own_Tracked_Alt_Rate <= OLEV) && (Cur_Vertical_Sep > MAXALTDIFF);
    tcas_equipped = Other_Capability == TCAS_TA;
    intent_not_known = Two_of_Three_Reports_Valid && Other_RAC == NO_INTENT;
    
    alt_sep = UNRESOLVED;
    
    if (enabled && ((tcas_equipped && intent_not_known) || !tcas_equipped))
    {
	need_upward_RA = Non_Crossing_Biased_Climb() && Own_Below_Threat();
	need_downward_RA = Non_Crossing_Biased_Descend() && Own_Above_Threat();
	if (need_upward_RA && need_downward_RA)
        /* unreachable: requires Own_Below_Threat and Own_Above_Threat
           to both be true - that requires Own_Tracked_Alt < Other_Tracked_Alt
           and Other_Tracked_Alt < Own_Tracked_Alt, which isn't possible */
	    alt_sep = UNRESOLVED;
	else if (need_upward_RA)
	    alt_sep = UPWARD_RA;
	else if (need_downward_RA)
	    alt_sep = DOWNWARD_RA;
	else
	    alt_sep = UNRESOLVED;
    }
    
    return alt_sep;
}
Exemple #5
0
static enum ParserResult parsePipeline(struct Parser* parser,
        struct Pipeline* pipeline) {
    pipeline->commands = NULL;
    pipeline->numCommands = 0;
    pipeline->bang = false;

    enum ParserResult result;

    struct Token* token = getToken(parser);
    if (!token) return PARSER_SYNTAX;

    while (token->type == TOKEN && strcmp(token->text, "!") == 0) {
        pipeline->bang = !pipeline->bang;
        parser->offset++;
        token = getToken(parser);
        if (!token) return PARSER_SYNTAX;
    }

    while (true) {
        struct SimpleCommand command;
        result = parseSimpleCommand(parser, &command);
        if (result != PARSER_MATCH) goto fail;

        if (!addToArray((void**) &pipeline->commands, &pipeline->numCommands,
                &command, sizeof(command))) {
            result = PARSER_ERROR;
            goto fail;
        }

        token = getToken(parser);
        if (!token) return PARSER_MATCH;
        if (token->type != OPERATOR || strcmp(token->text, "|") != 0) {
            return PARSER_MATCH;
        }

        parser->offset++;

        token = getToken(parser);
        if (!token) {
            result = PARSER_SYNTAX;
            goto fail;
        }

        while (token->type == OPERATOR && strcmp(token->text, "\n") == 0) {
            parser->offset++;
            token = getToken(parser);
            if (!token) {
                result = PARSER_NEWLINE;
                goto fail;
            }
        }
    }

    return PARSER_MATCH;

fail:
    freePipeline(pipeline);
    return result;
}
void initialize()
{
    int* __CREST_p1 = (int*)malloc(sizeof(int)*10);
    addToArray((void**)&__CREST_p1,1);
    Positive_RA_Alt_Thresh[0] = 400;
    Positive_RA_Alt_Thresh[1] = 500;
    Positive_RA_Alt_Thresh[2] = 640;
    Positive_RA_Alt_Thresh[3] = 740;
}
Exemple #7
0
/*
* commandSplit: splits the string to an array of sub string
* ignores eampty strings.
*
* @param string the string.
* @param size the new array size.
* @param value the split character.
*
* @return
* 	NULL if string is null or malloc failed; else returns the matrix
 */
static bool splitString(char* string, int *size, char*** result_ptr) {
	int actual_size = 8;
	int logical_size = 0;
	int start_index = 0;
	char** result = malloc(sizeof(char*) * actual_size);
	int index = 0;
	while ((string[index] != '\0') && (string[index] != '\n')) {
		if ((string[index] == COMMAND_SAPARATOR_1) ||
			(string[index] == COMMAND_SAPARATOR_2)) {
			if (start_index < index) {
				if (!addToArray(&result, &logical_size, &actual_size,
						string, start_index, index)) {
					return false;
				}
			}
			start_index = index + 1;
		}
		index++;
	}
	if (start_index < index) {
		if (!addToArray(&result, &logical_size, &actual_size,
				string, start_index, index)) {
			return false;
		}
	}
	if (logical_size == 0) {
		free(result);
		return true;
	}
	if (logical_size != actual_size) {
		char** new_array =
			realloc(result, logical_size * sizeof(*new_array));
		if (new_array == NULL) {
			matrixDestroy(result, logical_size);
			return false;
		}
		result = new_array;
	}

	*size = logical_size;
	*result_ptr = logical_size == 0 ? NULL : result;

    return true;
}
bool Non_Crossing_Biased_Descend()
{
    int* __CREST_p5 = (int*)malloc(sizeof(int)*10);
    addToArray((void**)&__CREST_p5,5);
    int upward_preferred;
    int upward_crossing_situation;
    bool result;

    upward_preferred = Inhibit_Biased_Climb() > Down_Separation;
    if (upward_preferred)
    {
	result = Own_Below_Threat() && (Cur_Vertical_Sep >= MINSEP) && (Down_Separation >= ALIM());
    }
    else
    {
	result = !(Own_Above_Threat()) || ((Own_Above_Threat()) && (Up_Separation >= ALIM()));
    }
    return result;
}
int main()
{
	srand(time(NULL));

	for (int x = 0; x < std::rand() % 10 + 5; x++)//Crea un arreglo de tamaño aleatorio entre 5 y 10
	{
		arreglo[x] = std::rand() % 100 + 1; //De 1 a 100 al azar
		currSize++;
	}
	std::cout << "Initialized new array of size " << currSize << std::endl;

	printArray(arreglo);//Imprime el arreglo

	for (int x = 0; x < addAmount; x++)
		addToArray(std::rand() % 100 + 1, std::rand() % 10, arreglo);//Agrega numero entre 1 y 100 a posicion entre 0 y 9

	for (int x = 0; x < removeAmount; x++)
		removeFromArray(std::rand() % 10, arreglo);//Borra numero de posicion entre 0 y 9

	return 0;
}
Exemple #10
0
Proto appendArrayToArray(Proto src, Proto dest)
{
    Proto tmp;
    Proto blob;
    int i,j;
    
    
    i = objectGetSlot(src, ATOMsize, &tmp);
    assert(i);

    i = objectGetSlot(src, ATOM_array, &blob);
    assert(i);

    
    i = objectIntegerValue(tmp);

    for (j = 0; j < i; j++ ) {
	tmp = objectBlobAtGet(blob, j);
	addToArray(dest, tmp);
    }

    return dest;
}
Exemple #11
0
Proto whileObj(int isTrueLoop)
{

    Proto codeObj;
    Proto assembly;
    Proto tmp;


    codeObj = objectNew(0);

    // Setup the arguments

    // This points to the target
    objectSetSlot(codeObj, stringToAtom("a"), PROTO_ACTION_GETFRAME, (uint32) objectNewInteger(6));
    // This one points to arg0
    objectSetSlot(codeObj, stringToAtom("b"), PROTO_ACTION_GETFRAME, (uint32) objectNewInteger(8));
    
    assembly = createArray( (Proto) stringToAtom("Ignore This Slot"));
    objectSetSlot(codeObj, stringToAtom("_assemble"), PROTO_ACTION_GET, (uint32) assembly);
    
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("slotName"));
    if (isTrueLoop) {
	objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) stringToAtom("whileTrue:"));
    } else {
	objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) stringToAtom("whileFalse:"));
    }
    addToArray(assembly, tmp);
    
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("setupFrame"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(0));
    addToArray(assembly, tmp);

    // Push value
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) stringToAtom("value"));
    addToArray(assembly, tmp);
    

    // Now discover 'a'
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) stringToAtom("a"));
    addToArray(assembly, tmp);

    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushSelf"));
    addToArray(assembly, tmp);
    
    // Push the arg count
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(2));
    addToArray(assembly, tmp);
    
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("call"));
    addToArray(assembly, tmp);
    


    // With 'a''s value on the stack and 'value', evaluate that
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(2));
    addToArray(assembly, tmp);
    
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("call"));
    addToArray(assembly, tmp);


    // Now do the branch
    tmp = objectNew(0);
    if (isTrueLoop) {
	objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("branchIfFalse"));
	objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(29));
    } else {
	objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("branchIfTrue"));
	objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(29));
    }
    addToArray(assembly, tmp);





    // If branch not taken...

    // Push value
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) stringToAtom("value"));
    addToArray(assembly, tmp);
    
    // Now discover 'b'
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) stringToAtom("b"));
    addToArray(assembly, tmp);

    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushSelf"));
    addToArray(assembly, tmp);
    
    // Push the arg count
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(2));
    addToArray(assembly, tmp);
    
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("call"));
    addToArray(assembly, tmp);
    
    // With 'b''s value on the stack and 'value', evaluate that
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pushimmediate"));
    objectSetSlot(tmp, stringToAtom("type"), PROTO_ACTION_GET, (uint32) stringToAtom("object"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(2));
    addToArray(assembly, tmp);
    
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("call"));
    addToArray(assembly, tmp);

    // Remove the result

    //// In the future we will use this result to do 'continue/break'
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("pop"));
    addToArray(assembly, tmp);

    // Now do the branch to the top, to do it all over again
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("branch"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(-57));
    addToArray(assembly, tmp);





    // The end
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("restoreFrame"));
    addToArray(assembly, tmp);
    
    tmp = objectNew(0);
    objectSetSlot(tmp, stringToAtom("op"), PROTO_ACTION_GET, (uint32) stringToAtom("return"));
    objectSetSlot(tmp, stringToAtom("operand"), PROTO_ACTION_GET, (uint32) objectNewInteger(2));
    addToArray(assembly, tmp);


    return codeObj;
}
Exemple #12
0
int main(){

	int paycode;

	// parse values for arithmetic in switch
	int hours;
	float wage;
	float sales;
	float total;
	int quantity;
	float price;
	
	promptCode();
	scanf("%d", &paycode);

	while(paycode!=-1){
		// Check user input and respond
		switch(paycode){
			Employee emp;
			case 1: // manager
				// Set employees pay code
				emp.paycode=paycode;
				
				// Prompt for user First name
				printf(fname);
				scanf("%s", &emp.name);

				// Manager specific: Get weekly salary
				printf("How much is their weekly salary? (X.XX): ");
				scanf("%f", &emp.total);

				// Display paycheck to user
				printf(totalPaycheck,&emp.name,emp.total);

				// Add Employee to array
				c=addToArray(emp);
				break;
			case 2: // hourly
				// Set employees pay code
				emp.paycode=paycode;
				
				// Prompt for user First name
				printf(fname);
				scanf("%s", &emp.name);

				// Get hours worked 
				printf("Enter the number of hours worked (integer): ");
				scanf("%d", &hours);
				
				// Get hourly wage
				printf("Enter hourly wage (X.XX):");
				scanf("%f", &wage);
			
				// Calculate total and display to user
				emp.total=wage*respHrs(hours);
				printf(totalPaycheck,&emp.name,emp.total);

				// Add Employee to array
				c=addToArray(emp);
				break;
			case 3: // commission
				// Set employees pay code
				emp.paycode=paycode;
				
				// Prompt for user First name
				printf(fname);
				scanf("%s", &emp.name);

				// Get total sales 
				printf("Enter total sales (X.XX):");
				scanf("%f", &sales);
				
				// Calculate total and display to user
				emp.total=sales+250.00;
				printf(totalPaycheck,&emp.name,emp.total);

				// Add Employee to array
				c=addToArray(emp);
				break;
			case 4: // pieceworker
				// Set employees pay code
				emp.paycode=paycode;
				
				// Prompt for user First name
				printf(fname);
				scanf("%s", &emp.name);

				// Pieceworker: Get items sold
				printf("Enter number of items sold (integer): ");
				scanf("%d",&quantity);

				// Pieceworker: Get items selling price
				printf("Enter the price of the item (X.XX): ");
				scanf("%f",&price);

				// Calculate total and display to user
				emp.total=quantity*price;
				printf(totalPaycheck,&emp.name,emp.total);

				// Add Employee to array
				c=addToArray(emp);
				break;
			case 5: // test case
				printf("Successfully parsed paycode as %d\n",paycode);
				break;
			default:
				printf("Value \"%d\" does not relate to any action\n",paycode);
				break;
		}
		promptCode();
		scanf("%d", &paycode);
	}

	// Display employees input into the system
	printf("\nThe following are the employees input for this month:\n");
	printf("%s%s%s%s%s%s%s%s\n",top,top,top,top,top,top,top,top);
	for(int x=0; x<c;x++){
		char* empType;
		switch(payroll[x].paycode){
			case 1:
				strcpy(empType,"Manager");
				break;
			case 2:
				strcpy(empType,"Hourly");
				break;
			case 3:
				strcpy(empType,"Commission");
				break;
			case 4:
				strcpy(empType,"Pieceworker");
				break;
		}
		printf("|  Employee: %12s  |  Type: %12s  |  Total Payment: %9.2f  |\n",&payroll[x].name,empType,payroll[x].total);
	}
	printf("%s%s%s%s%s%s%s%s\n",top,top,top,top,top,top,top,top);
} 
  int main(int argc, char* argv[])
{

  char* recvBuff = malloc(512);

  if(argc<3)
    {
      perror("Incorrect number of arguments\n");
      exit(0);
    }



  struct stat s;

  int err = stat("images",&s);

  if(-1==err)
    {
      if(ENOENT == errno)
	{
	  printf("Directory for images does not exist, creating it\n");
	  mkdir("images",0777);
	  printf("Directory created\n");
	}
    }
  else
    {
      if(S_ISDIR(s.st_mode))
	{
	  printf("Directory for images exists\n");
	}
    }

  struct stat st = {0};
  
  if(stat("images/catalog.csv",&st)!=-1)
    {
      perror("Catalog exists. Deleting it..\n");
      remove("images/catalog.csv");
    }
 


  htmlpath = malloc(200);

  strcpy(htmlpath,"images/");
  strcat(htmlpath,"download.html");

    
  

  int sockfd;
  
  struct sockaddr_in serv_addr;
  
  if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
      perror("Could not create socket connection\n");
      exit(0);
    }

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(atoi(argv[2]));
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

  if(connect(sockfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
    {
      perror("Connection failed\n");
      exit(0);
    }

  char* logpath = malloc(200);

  strcpy(logpath,"images/catalog.csv");

  
  write_to_image(logpath,sockfd);


  FILE* fp;

  fp = fopen(logpath,"r");
  
  char ch;

  int lines=0;
  while(!feof(fp))
    {
      ch = fgetc(fp);
      if(ch == '\n')    //Get the number of lines in log file.
	{
	  lines++;
	}
    }
  
  fclose(fp);

  char* line = NULL;
  
  size_t len = 0;

  fp = fopen(logpath,"r");
  getline(&line,&len,fp);             //Read log file line by line.
  

  printf("Dumping catalog.csv content\n");

  int i = 1;
  int j;
  int k;
  int m;
  int n;
  for(k = 0;k<lines-1;k++)
    {
      strcpy(file_array[file_count],"images/");
      printf("[%d] ",i);
      getline(&line,&len,fp);

      for(j=0;line[j]!=',';j++)
	{
	  
	  file_array[file_count][j+7] = line[j];
	  printf("%c",line[j]);
	}

      char* name = strtok(line,",");
      char* size = strtok(NULL,",");
      char* sum = strtok(NULL,",");
      strcpy(checksums[file_count],sum);
      //printf("%s\n",checksums[file_count]);
     
    
	  

      
      
      file_count++;
      i++;
      printf(" \n");
    }
      
  fclose(fp);
  

  for(i=0;i<file_count;i++)
    {
      
      addToArray(file_array[i],i);
    }





  if(argv[3]!=NULL)                  //Arg[3] present. Passive mode.
     {
      printf("Passive mode\n");
      
    

      write(sockfd,argv[3],11);

      write(sockfd,"ready",5);
  
      if(is_jpg(argv[3]))                 
	{                               //Write jpg images
	  for(i=0;i<jpg_count;i++)
	    write_to_image(jpg_array[i],sockfd);
	}

      if(is_png(argv[3]))
	{
	  for(i=0;i<png_count;i++)          //Write png images.
	    write_to_image(png_array[i],sockfd);
	}
      
       if(is_gif(argv[3]))
	 {                                //Write gif files.
	  for(i=0;i<gif_count;i++)
	    write_to_image(gif_array[i],sockfd);
	}

       if(is_tiff(argv[3]))
	 {                                 //Write tiff files.
	   for(i=0;i<tiff_count;i++)
	     write_to_image(tiff_array[i],sockfd);
	 }
	  
    }

   else
     {                              //argv[3] not present. interactive mode.
      inter = 1;
      printf("Interactive mode\n");
      write(sockfd,"interactive",11);

      char input[1];
      while(1)
	{

	  printf("Enter the file number to download\n");
	  scanf("%s",input);
	  
	  if(atoi(input)==0)
	    {
	      write(sockfd,input,2);
	      printf("Entered 0. Exiting...\n");
	      close(sockfd);
	      break;
	    }
	  printf("File number entered:%d\n",atoi(input));

	  write(sockfd,input,2);
	  write_to_image(file_array[atoi(input)-1],sockfd);
	}
      
    }

    printf("Done\n");


    //Create html file.
    FILE* html;
    
    html = fopen("./images/download.html","w+");
    
    fprintf(html,"<html><head>Downloaded Images</head>");
    fprintf(html,"<title>Downloaded Images</title>\n");
    fprintf(html,"<p>");


    /*
      For each file in the downloaded(images) folder, look for the
      file in the file_array,get the index, see if checksums 
      are equal. Accordingly write to html.
    */

    DIR* dir;
    struct dirent* file;

    dir = opendir("./images/");
    
    while((file = readdir(dir))!=NULL)
      {
	int i;
	int j;
	if(file->d_name[0] == '.') continue;
	if(strcmp(file->d_name,"catalog.csv")==0) continue;
	char* path = malloc(200);
	strcpy(path,"images/");
	strcat(path,file->d_name);
	unsigned char sum[MD5_DIGEST_LENGTH];
	char convertedsum[(MD5_DIGEST_LENGTH*2)+1];
	md5sum(path,sum);

	/**stackoverflow.com/questions/7627723/how-to-create-a-md5-hash-of-a-string-in-c**/
	for(j=0;j<MD5_DIGEST_LENGTH;j++)
	  {
	    sprintf(convertedsum+(j*2),"%02x",sum[j]);
	  }
	convertedsum[2*MD5_DIGEST_LENGTH]='\0';
	char* other_name = malloc(200);

	for(i=0;i<file_count;i++)
	  {
	    
	    if(strstr(path,file_array[i]))
	      {
		
		if(strncmp(convertedsum,checksums[i],MD5_DIGEST_LENGTH)==0)
		  {
		    
		    fprintf(html,"Checksum match!\n");
		    fprintf(html, "      ");
		    fprintf(html, "<a href = \"");
		    
		    fprintf(html,"%s\">",file->d_name);
		    fprintf(html,"%s",file->d_name);
		    fprintf(html,"</a></p>\n");

		  }
		else
		  {
		    
		    fprintf(html,"Checksum mismatch");
		    fprintf(html,"       ");
		    fprintf(html,"%s",file->d_name);
		    fprintf(html,"</p>");
		  }
	      }
	  }
      }

     
	    


	fprintf(html,"</body></html>");
	fclose(html);


  return 0;
    
     

}
Exemple #14
0
int Inhibit_Biased_Climb ()
{
    int* __CREST_p3 = (int*)malloc(sizeof(int)*10);
    addToArray((void**)&__CREST_p3,3);
    return (Climb_Inhibit ? Up_Separation + NOZCROSS : Up_Separation);
}
Exemple #15
0
int ALIM ()
{
 int* __CREST_p2 = (int*)malloc(sizeof(int)*10);
 addToArray((void**)&__CREST_p2,2);
 return Positive_RA_Alt_Thresh[Alt_Layer_Value];
}