1
0

more cleaning

This commit is contained in:
dtb
2022-09-18 10:44:47 -04:00
parent 49b9f21067
commit 9189124f2d
26 changed files with 13 additions and 410 deletions

52
libshell/getpaths.3 Normal file
View File

@@ -0,0 +1,52 @@
.TH GETPATHS 3
.SH NAME
getpaths \- get an array of the current executable PATHs
.SH LIBRARY
libshell
.SH SYNOPSIS
.In stdlib.h
.In string.h
.In libshell.h
.Ft char **
.Fn getpaths void
.SH DESCRIPTION
.Fn getpaths
returns the system PATHs as an array of null-terminated strings ordered from highest to lowest priority.
The last address in the array is NULL.
The first address in the array, and the array address itself, should be passed to
.Xr free 3
to release the allocated storage when the returned array is no longer needed.
.SH EXAMPLES
To print the current PATHs, from highest to lowest priority:
.Bd -literal -offset indent
#include <stdlib.h>
#include <string.h>
#include <libshell.h>
int main(){
char **paths;
int i;
paths = getpaths();
if(paths == NULL)
exit(EXIT_FAILURE);
for(i = 0; paths[i] != NULL; ++i)
printf("%s\n", paths[i]);
free(*paths);
free(paths);
return EXIT_SUCCESS;
}
.Ed
.SH SEE ALSO
.Xr getenv 3
.SH STANDARDS
.Fn getpaths
is not part of any current standard.
.SH BUGS
Certainly existent but not yet known.

59
libshell/libshell.c Normal file
View File

@@ -0,0 +1,59 @@
#include "libshell.h"
char **
getpaths(void)
{
char *p;
char **q;
char *path;
size_t path_s;
char *path_temp;
char **paths;
size_t paths_s;
if((path_temp = getenv(PATH_NAME)) == NULL){
errno = ENOMEM; /* Cannot allocate memory */
return NULL;
}
/* How long is path? */
for(path_s = 0; path_temp[path_s] != '\0'; ++path_s);
/* path_s has the size of the path string, not including the null
* terminator */
/* getenv's return value mustn't be modified so copy it into memory we
* control */
if((path = malloc(sizeof(char) * (path_s + 1))) == NULL){
errno = ENOMEM; /* Cannot allocate memory */
return NULL;
}
memcpy(path, path_temp, path_s + 1);
path_temp = NULL;
path_s = 0; /* This shouldn't be necessary anymore */
/* How many paths in $PATH? */
for(paths_s = 1, p = path; *p != '\0'; paths_s += *p++ == PATH_DELIMITER);
if((paths = malloc(sizeof(char *) * (paths_s + 1))) == NULL){
free(path);
errno = ENOMEM; /* Cannot allocate memory */
return NULL;
}
/* Replace all instances of the path delimiter with 0, and then put the
* next path beginning into paths. */
/* This way we can get multiple strings out of one span of memory. */
for(*paths = p = path, q = paths + 1; *p != '\0';)
if(*p++ == PATH_DELIMITER){
*(p - 1) = '\0';
*q++ = p;
}
paths[path_s] = NULL;
/* Because paths[0] will always be path, we can just free(*paths) at
* the end and discard path */
path = NULL;
return paths;
}

11
libshell/libshell.h Normal file
View File

@@ -0,0 +1,11 @@
#include <errno.h>
#include <stdlib.h> /* getenv(3), malloc(3) */
#include <string.h> /* memcpy(3) */
/* For the shell variable $PATH */
#define PATH_NAME "PATH"
#define PATH_DELIMITER ':'
/* Returns a list of the current paths ordered from first to last priority.
* output must be freed and *output must be freed! */
char **getpaths(void);