tgrekov-pipex
HIVE pipex May 2024
Loading...
Searching...
No Matches
get_cmd.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* get_cmd.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/04/22 13:47:02 by tgrekov #+# #+# */
9/* Updated: 2024/05/20 12:27:30 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <unistd.h>
21#include <stdlib.h>
22#include "../utils/utils.h"
23#include <libft.h>
24
33char *get_cmd(char **paths, char *name)
34{
35 char *cmd;
36
37 if (!name)
38 return (0);
39 cmd = ft_strdup(name);
40 if (!cmd)
41 return (err("malloc() failed while assembling path to cmd", 0));
42 if (!access(cmd, F_OK))
43 return (cmd);
44 free(cmd);
45 if (!paths)
46 return (0);
47 while (*paths)
48 {
49 cmd = ft_strjoin(*paths, name);
50 if (!cmd)
51 return (err("malloc() failed while assembling path to cmd", 0));
52 if (!access(cmd, F_OK))
53 return (cmd);
54 free(cmd);
55 paths++;
56 }
57 return (0);
58}
void * err(const char *str, void *retval)
Wrapper around perror() that always returns retval.
Definition err.c:29
char * ft_strdup(const char *s1)
Allocates memory for, copies to, and returns a duplicate of s1.
Definition ft_strdup.c:28
char * ft_strjoin(char const *s1, char const *s2)
Allocates enough space for and appends string s2 to string s1 and returns the new string.
Definition ft_strjoin.c:30
char * get_cmd(char **paths, char *name)
Find and return the first existing file with called name from the array of paths.
Definition get_cmd.c:33