tiny-shell 0.2
A mini shell project aiming to gain knowledge about Win32 and Linux API
Loading...
Searching...
No Matches
add_path.c File Reference
#include "add_path.h"
#include "../core/io_wrap.h"
#include "../os/operations.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for add_path.c:

Go to the source code of this file.

Functions

char * strtok_r_custom (char *str, const char *delim, char **saveptr)
 
bool in_path_env (const os_char *path_env, const os_char *new_path)
 
bool add_path (const os_char *new_path)
 
enum run_result run_add_path (const os_char *new_path)
 

Function Documentation

◆ add_path()

bool add_path ( const os_char * new_path)

Definition at line 74 of file add_path.c.

74 {
75 char buffer[SHRT_MAX];
76 unsigned int bufferSize = sizeof(buffer) / sizeof(char);
77
78 if(!get_shell_env("PATH", bufferSize, buffer)) {
79 format_error("Fail to get PATH environment variable!\n");
80 return false;
81 }
82
83 if(in_path_env(buffer, new_path)) {
84 format_error("The path %s is already in the PATH environment variable.\n", new_path);
85 return false;
86 }
87
88 os_char *f_path = formatted_path(new_path);
89 strncat(buffer, f_path, strlen(f_path));
90
91 if(!set_shell_env("PATH", buffer)) {
92 format_error("Failed to set the PATH environment variable!\n");
93 free(f_path);
94 return false;
95 }
96 format_success("PATH environment variable is updated successfully.\n");
97 free(f_path);
98 return true;
99}
bool in_path_env(const os_char *path_env, const os_char *new_path)
Definition add_path.c:57
void format_success(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:69
void format_error(char *fmt,...)
Used format_xxx instead of printf and such for uniform output.
Definition io_wrap.c:89
bool get_shell_env(const os_char *var, unsigned int buffer_size, os_char *buffer)
Get value of an an environment variable.
bool set_shell_env(const os_char *name, const os_char *val)
Set environment variable of the shell process.

References format_error(), format_success(), get_shell_env(), in_path_env(), and set_shell_env().

Referenced by run_add_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ in_path_env()

bool in_path_env ( const os_char * path_env,
const os_char * new_path )

Definition at line 57 of file add_path.c.

57 {
58 size_t len = strlen(path_env);
59 os_char tmp_path_env[SHRT_MAX];
60 strncpy(tmp_path_env, path_env, len);
61 tmp_path_env[len] = '\0';
62
63 char *rest = NULL;
64 char *token = strtok_r_custom(tmp_path_env, SEPARATOR, &rest);
65 while(token != NULL) {
66 if(!strcmp(token, new_path)) {
67 return true;
68 }
69 token = strtok_r_custom(NULL, SEPARATOR, &rest);
70 }
71 return false;
72}
char * strtok_r_custom(char *str, const char *delim, char **saveptr)
Definition add_path.c:33

References strtok_r_custom().

Referenced by add_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_add_path()

enum run_result run_add_path ( const os_char * new_path)

Definition at line 101 of file add_path.c.

101 {
102 return add_path(new_path) ? RUN_OK : RUN_FAILED;
103}
bool add_path(const os_char *new_path)
Definition add_path.c:74
@ RUN_OK
Definition res.h:7
@ RUN_FAILED
Definition res.h:9

References add_path(), RUN_FAILED, and RUN_OK.

Referenced by invoke_runner().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strtok_r_custom()

char * strtok_r_custom ( char * str,
const char * delim,
char ** saveptr )

Definition at line 33 of file add_path.c.

33 {
34 char *token = NULL;
35 if(str == NULL) {
36 str = *saveptr;
37 }
38 // Skip leading delimiters
39 str += strspn(str, delim);
40 if(*str == '\0') {
41 return NULL;
42 }
43 // Find the end of the token
44 token = str;
45 str = strpbrk(token, delim);
46 if(str == NULL) {
47 // This token finishes the string
48 *saveptr = strchr(token, '\0');
49 } else {
50 // Terminate the token and update the save pointer
51 *str = '\0';
52 *saveptr = str + 1;
53 }
54 return token;
55}

Referenced by in_path_env().

Here is the caller graph for this function: