Beispiel #1
0
char		*get_exec_path(t_env env, char *cmd)
{
	char	path[6000];
	char	*bin;
	char	*colon;

	if (!cmd)
		shell_perror("get_exec_path recieved NULL command string");
	if (!*cmd)
		return (shell_pwarning("not a valid command: ", "(Empty)"));
	if (isdir(cmd))
		return (shell_pwarning("is a directory: ", cmd));
	if (!access(cmd, X_OK))
		return (ft_strdup(cmd));
	if (!(bin = fetch_key_val(env, "PATH")))
		return (NULL);
	while ((colon = ft_strchr(bin, ':')))
	{
		ft_strncpy(path, bin, (colon - bin) + 1);
		ft_strncpy(ft_strchr(path, ':'), "/", 2);
		ft_strcat(path, cmd);
		if (!access(path, X_OK))
			return (ft_strdup(path));
		bin = colon + 1;
	}
	return (parse_exec_string(path, bin, cmd));
}
Beispiel #2
0
int		main(int argc, char **argv)
{
	int	sock;

	if (argc != 2)
		shell_perror("USAGE: ./serveur [port]");
	sock = create_server(ft_atoi(argv[1]));
	server(sock);
	close(sock);
	return (0);
}
Beispiel #3
0
int		create_server(int port)
{
	int					sock;
	struct protoent		*proto;
	struct sockaddr_in	sin;

	if (proto = getprotobyname("tcp"), (proto) == 0)
		shell_perror("getprotobyname(\"tcp\") returned 0");
	if (sock = socket(PF_INET, SOCK_STREAM, proto->p_proto), sock < 0)
		shell_perror("socket creation failed");
	sin.sin_family = AF_INET;
	sin.sin_port = htons(port);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
	{
		shell_perror("socket bind failed");
	}
	if (listen(sock, 1) < 0)
		shell_perror("socket listen failed");
	printf("Server started at address %d port %d\n", INADDR_ANY, port);
	return (sock);
}
Beispiel #4
0
void	handle_signals(int signum)
{
	static int	sock = -1;

	if (sock == -1)
	{
		sock = signum;
		return ;
	}
	if (sock == 0)
		shell_perror("Server encountered an issue. Need a tissue?");
	server_sendbuf(sock, "quit");
	write(1, "\n", 1);
	shell_pwarning("", "Server connection ended");
	ft_putstr(GOODBYE);
	exit(0);
}
Beispiel #5
0
void	server(int sock)
{
	int					pid;
	int					cs;
	struct sockaddr_in	csin;
	unsigned int		cslen;

	while (1)
	{
		cs = accept(sock, (struct sockaddr *)&csin, &cslen);
		printf("Client connected:\n\tfamily: %i\n\tport: %u\n\taddr: %s\n",
			csin.sin_family, csin.sin_port, inet_ntoa(csin.sin_addr));
		if (pid = fork(), pid == 0)
		{
			close(sock);
			while (chdir(SERVER_DIR) == -1)
				mkdir(SERVER_DIR, 0777) ? shell_perror("mkdir failed") : 1;
			server_root_dir();
			handle_child(cs, csin.sin_port);
		}
		close(cs);
	}
}
Beispiel #6
0
/* execute_external
 *
 * takes a subcmd and executes the command by forking and then exec'ing
 */
void execute_external(char *cmd, char **args) {
	get_and_check_command(&cmd);
	execv(cmd, args);
	shell_perror(cmd, errno);
	exit(1);
}