Beispiel #1
0
Datei: fs.c Projekt: asac/procd
int add_path_and_deps(const char *path, int readonly, int error, int lib)
{
	assert(path != NULL);

	if (lib == 0 && path[0] != '/') {
		ERROR("%s is not an absolute path\n", path);
		return error;
	}

	char *map = NULL;
	int fd, ret = -1;
	if (path[0] == '/') {
		if (avl_find(&mounts, path))
			return 0;
		fd = open(path, O_RDONLY|O_CLOEXEC);
		if (fd == -1)
			return error;
		add_mount(path, readonly, error);
	} else {
		if (avl_find(&libraries, path))
			return 0;
		char *fullpath;
		fd = lib_open(&fullpath, path);
		if (fd == -1)
			return error;
		if (fullpath) {
			alloc_library(fullpath, path);
			free(fullpath);
		}
	}

	struct stat s;
	if (fstat(fd, &s) == -1) {
		ERROR("fstat(%s) failed: %s\n", path, strerror(errno));
		ret = error;
		goto out;
	}

	if (!S_ISREG(s.st_mode)) {
		ret = 0;
		goto out;
	}

	/* too small to be an ELF or a script -> "normal" file */
	if (s.st_size < 4) {
		ret = 0;
		goto out;
	}

	map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (map == MAP_FAILED) {
		ERROR("failed to mmap %s\n", path);
		ret = -1;
		goto out;
	}

	if (map[0] == '#' && map[1] == '!') {
		ret = add_script_interp(path, map, s.st_size);
		goto out;
	}

	if (map[0] == ELFMAG0 && map[1] == ELFMAG1 && map[2] == ELFMAG2 && map[3] == ELFMAG3) {
		ret = elf_load_deps(path, map);
		goto out;
	}

	ret = 0;

out:
	if (fd >= 0)
		close(fd);
	if (map)
		munmap(map, s.st_size);

	return ret;
}
Beispiel #2
0
//int* adduser(int booknum);
//int userexist ();
int main()
{
	struct library *library = alloc_library();
	if (!library){
		printf("Failed to allocate library");
		return -1;
	}
	char input[80];
	char input2[80];
	int exit = 0;
	int ret;
	int menu;
	//i=0,booknum=0;
	while (!exit){
		printf("CHOOSE AN OPTION\n");
		scanf("%d",&menu);
		switch (menu)
		{
			case 1:
			scanf("%s", input);
			ret = add_user(library, input);
			if (ret < 0){
				printf("Failed to allocate memory\n");
				exit = 1;
				break;
			}
			if (ret == 1){
				printf("ERROR\n");
				break;
			}
			printf("User added\n");
			break;
		case 2:
			scanf("%s", input);
			ret = add_book(library, input);
			if (ret < 0){
				printf("Failed to allocate memory\n");
				exit = 1;
				break;
			}
			if (ret == 1){
				printf("ERROR\n");
				break;
			}
			printf("Book added\n");
			break;
		case 3:
			scanf("%s", input);
			ret = remove_book(library, input);
			if (ret == 1){
				printf("error\n");
				break;
			}
			printf("removed\n");
			break;
		case 4:
			scanf("%s", input);
			scanf("%s", input2);
			ret = take_book(library, input, input2);
			switch (ret){
				case -1:
					printf("Failed to allocate memory\n");
					exit = 1;
					break;
				case 1:
				case 2:
					printf("ERROR\n");
					break;
				case 3:
					printf("IN USE\n");
					break;
				default:
					printf("SUCESS\n");
					break;

			}
			break;
		case 5:
			scanf("%s", input);
			scanf("%s", input2);
			ret = return_book(library, input, input2);
			switch (ret){
				case 1:
				case 2:
					printf("ERROR\n");
					break;
				case 3:
					printf("NOT IN YOUR USE\n");
					break;
				default:
					printf("SUCESS\n");
			}
			break;
		case 6:
			scanf("%s", input);
			ret = print_user_info(library, input);
			if (ret == 1){
				printf("ERROR\n");
			}
			break;
		case 7:
			exit = 1;
			printf("BYE\n");
			break;
		}
	}
	free_library(library);
	return 0;
}