示例#1
0
static char *
locate_next_exec(const char *path, const char *first_dir, const char *name, const char* ignore_prefix)
{
	struct buffer buf[1];
	size_t first_dir_len, ignore_prefix_len;
	const char *dir_start;
	bool more, found;
	enum { BEFORE_FIRST_DIR, SEARCH_SECOND_DIR } state;
	
	first_dir_len = strlen(first_dir);
	if (ignore_prefix != NULL) {
		ignore_prefix_len = strlen(ignore_prefix);
	} else {
		ignore_prefix_len = 0;
	}
	buffer_init(buf);

	dir_start = path;
	more = true;
	found = false;
	state = BEFORE_FIRST_DIR;
	for (;;) {
		const char *dir_end = strchr(dir_start, ':');
		size_t dir_len;
		if (dir_end == NULL) {
			more = false;
			dir_end = strchr(path, '\0');
		}
		dir_len = dir_end - dir_start;

		if (dir_start[0] != '/' 
		    && !(dir_len == 1 && *dir_start == '.')) 
		{
			build_location(dir_start, dir_len, NULL, buf);
			warning("PATH component is not absolute path: %s",
				buf->ptr);
				
		} else if (state == BEFORE_FIRST_DIR) {
			bool found = false;
			if (first_dir_len == dir_len) {
				if (0 == memcmp(dir_start, first_dir, 
				                first_dir_len)) 
				{
					found = true;
				}
			}
			if (found) {
				build_location(dir_start, dir_len, name, buf);
				if (!is_executable(buf->ptr)) {
					fatal_error("Failed to locate executable regular file '%s' in %s", name, first_dir);
				}
				state = SEARCH_SECOND_DIR;
			}
			
		} else if (state == SEARCH_SECOND_DIR) {
			bool skip = false;
			if (ignore_prefix != NULL 
			    && dir_len >= ignore_prefix_len)
			{
				if (0 == memcmp(dir_start, ignore_prefix, 
				                ignore_prefix_len))
				{
					skip = (dir_len == ignore_prefix_len)
						|| (dir_start[ignore_prefix_len]
					            == '/');
				}
			}
			if (!skip) {
				build_location(dir_start, dir_len, name, buf);
				if (is_executable(buf->ptr)) {
					found = true;
					break;
				}
			}
		
		} else {
			assert(false);
		}
		
		if (more) {
			dir_start = dir_end + 1;
			continue;
		}
		break;
	}
	
	if (!found) {
		fatal_error("Failed to locate real executable '%s' in %s", name, path);
	}
	
	return buffer_forget(buf, strlen(buf->ptr) + 1);
}
示例#2
0
int main(){
	struct sockaddr_in serv;
	memset(&serv, 0, sizeof(serv));
	int option = 0, sokk = 0, ran_loc = 0, id = 0, indic = 0;
	char location[101] = "", query[50] = "", answer[250] = "";
	struct info arr[6];
	FILE *idfile = NULL;
	socklen_t temp_size;

	// create array of different locations
	if(create_list(arr, 6) != 0){
		printf("An error happened while creating the locations\n");
		return -1;
	}

	// pick a location randomly to use as current location
	srand(time(NULL));
	ran_loc = rand() % 6;

	// open a socket, declare what kind it is and save IP and port into sockaddr_in structure
	sokk = socket(AF_INET, SOCK_DGRAM, 0);
	serv.sin_family = AF_INET;
	serv.sin_port = htons(7000);
	inet_pton(AF_INET, "127.0.0.1" /*"80.239.229.217"*/, &serv.sin_addr);

	// check if this client has already an ID assigned
	idfile = fopen("ID_file", "r");
	// no ID assigned, fopens fails because the file doesn't exist
	if(idfile == NULL){
		perror("This client has not yet assigned an ID:");

		// build a string containing the location information
		if(build_location(location, arr[ran_loc], id, indic, 0, NULL) != 0){
			printf("Error while building the string to be sent to the server, idfile = NULL\n");
			return -1;
		}

		// send the string to the server containing zero as ID
		if(strlen(location) != sendto(sokk, location, strlen(location), 0, (struct sockaddr *) &serv, sizeof(serv))){
			perror("some error happened when sending zero as ID, the location information could not be sent properly (id is 0):");
			return -1;
		}

		// read from the server what ID it assigns to this client and save it into a file
		temp_size = sizeof(serv);
		if(recvfrom(sokk, &id, sizeof(int), 0, (struct sockaddr*)&serv, &temp_size) == -1){	// modify this in order to decode properly the answer from server
			perror("some error happened at reading the ID sent by the server:");
			return -1;
		}
		idfile = fopen("ID_file", "w");
		if(idfile == NULL){
			perror("An error happened when creating the ID_file in order to write the newly assigned ID:");
			return -1;
		}
		if(fwrite(&id, sizeof(id), 1, idfile) != 1){
			printf("The newly assigned ID could not be written into the file\n");
			return -1;
		}
	}
	// this client has already an ID, read it from the file and store it in variable id
	else{
		if(fread(&id, sizeof(int), 1, idfile) != 1){
			printf("some error happened at reading the ID stored in the file\n");
			return -1;
		}
		printf("The id read from the file is: %d\n", id);

		// indicator to specify that an ID has been assigned
		indic++;

		// build a string containing the location information
		if(build_location(location, arr[ran_loc], id, indic, 0, NULL) != 0){
			printf("Error while building the string to be sent to the server, ID_file exists\n");
			return -1;
		}
		printf("The string to be sent to the server as initial location is (with exiting idfile): %s\n", location);

		// send location information to the server
		ssize_t first_sent = sendto(sokk, location, strlen(location), 0, (struct sockaddr *) &serv, sizeof(serv));
		if(strlen(location) != first_sent){
			perror("some error happened while sending location info to the server, the location information could not be sent properly:");
			return -1;
		}
		printf("the bytes sent to the server, idfile exists is: %d\n", first_sent);
	}

	// greet the user and ask what operation s/he wants
	printf("Please select the number from the following options that best suits your request\n");
	printf("1) I have a name and want to get the rest of the information\n");
	printf("2) I have an email address and want to get the rest of the information\n");
	for( ; ; ){
		scanf("%d", &option);
		if(option == 1){
			// ask the user to input the information s/he has
			printf("Please write the name from which you want to get information and press enter\n");
			scanf("%s", query);

			// build the query string with name as parameter
			if(build_location(location, arr[ran_loc], id, indic, option, query) != 0){
				printf("Error while creating string for query by name\n");
				return -1;
			}
			printf("The string just built is (for, option 1): %s", location);
			break;
		}
		else if(option == 2){
			// ask the user to input the information s/he has
			printf("Please write the email address from which you want to get information and press enter\n");
			scanf("%s", query);

			// build the query string with email as parameter
			if(build_location(location, arr[ran_loc], id, indic, option, query) != 0){
				printf("Error while creating string for query by email\n");
				return -1;
			}
			break;
		}
		else
			printf("The option you provided is invalid, please indicate the desired option by its number (1 or 2)");
	}

	// send the information to the server
	if(strlen(location) != sendto(sokk, location, strlen(location), 0, (struct sockaddr *) &serv, sizeof(serv))){
		printf("some error happened, the query could not be sent to the server\n");
		return -1;
	}
	printf("All good, waiting for the server query answer\n");

	// read the answer from the server
	temp_size = sizeof(serv);
	if(recvfrom(sokk, answer, sizeof(answer), 0, (struct sockaddr *) &serv, &temp_size) == -1){
		perror("some error happened at reading the answer of the query\n");
		return -1;
	}

	// display the info of the query to the user
	printf("%s\n", answer);

	// close everything and exit
	close(sokk);
	return 0;

}