C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
whence.c File Reference

Find the full path of a file in the directories specified by the. More...

#include <argp.h>
#include <cm.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Include dependency graph for whence.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE

Enumerations

enum  WhenceFlags { WH_ALL = 1 , WH_VERBOSE = 2 , WH_EXECUTABLE = 4 , WH_SETUID = 8 }

Functions

void ABEND (char *pgmid, int rc, char *err_msg)
 Exit the program with an error message.
int file_spec_parts (char *file_spec, char *file_path, char *file_name)
 Split a file specification into directory and file name components.
int main (int argc, char **argv)
int next_path (char *dp, char **pp)
 Get the next directory path from a colon-separated list of paths.
size_t strnz__cat (char *, const char *, size_t)
 safer alternative to strncat
size_t strnz__cat (char *d, const char *s, size_t max_len)
 safer alternative to strncat
size_t strnz__cpy (char *, const char *, size_t)
 safer alternative to strncpy
size_t strnz__cpy (char *d, const char *s, size_t max_len)
 safer alternative to strncpy
int whence (char *file_spec_p, int flags)
 Find the full path of a file in the directories specified by the PATH environment variable.

Variables

const char * argp_program_bug_address = "billxwaller@gmail.com"
const char * argp_program_version = CM_VERSION
char * path_p
char path_s [MAXLEN]
int wh_flags = 0

Detailed Description

Find the full path of a file in the directories specified by the.

Author
Bill Waller Copyright (c) 2025 MIT License billx.nosp@m.wall.nosp@m.er@gm.nosp@m.ail..nosp@m.com
Date
2026-02-09

Definition in file whence.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 10 of file whence.c.

Enumeration Type Documentation

◆ WhenceFlags

Enumerator
WH_ALL 
WH_VERBOSE 
WH_EXECUTABLE 
WH_SETUID 

Definition at line 34 of file whence.c.

34 { WH_ALL = 1,
35 WH_VERBOSE = 2,
36 WH_EXECUTABLE = 4,
WhenceFlags
Definition whence.c:34
@ WH_ALL
Definition whence.c:34
@ WH_EXECUTABLE
Definition whence.c:36
@ WH_SETUID
Definition whence.c:37
@ WH_VERBOSE
Definition whence.c:35

Function Documentation

◆ ABEND()

void ABEND ( char * pgmid,
int rc,
char * err_msg )

Exit the program with an error message.

Parameters
pgmidThe name of the program
rcThe return code to exit with
err_msgThe error message to display

This function is called to exit the program with an error status. It prints the program name, return code, and error message to the standard error stream, and then exits with the specified return code.

Definition at line 266 of file whence.c.

266 {
267 fprintf(stderr, "%s; error %d; %s\n", pgmid, rc, err_msg);
268 exit(EXIT_FAILURE);
269}
char err_msg[MAXLEN]

◆ file_spec_parts()

int file_spec_parts ( char * file_spec,
char * file_path,
char * file_name )

Split a file specification into directory and file name components.

Parameters
file_specThe full file specification to split
file_pathA buffer to store the extracted directory path
file_nameA buffer to store the extracted file name
Returns
0 on success

This function takes a file specification, checks if it is a directory, and if so, it sets the file path accordingly. If the file specification is empty, it defaults to the current directory. Otherwise, it splits the file specification into the directory and file name components based on the last occurrence of a slash ('/').

Definition at line 209 of file whence.c.

209 {
210 int i, last_slash;
211 char tmp_file_spec[PATH_MAX];
212 int file_spec_l;
213 struct stat stat_struct;
214
215 if (stat(file_spec, &stat_struct) != -1)
216 if ((stat_struct.st_mode & S_IFMT) == S_IFDIR) {
217 if (file_spec[strlen(file_path)] != '/')
218 strnz__cat(file_spec, "/", MAXLEN - 1);
219 strnz__cpy(file_path, file_spec, MAXLEN - 1);
220 file_name[0] = '\0';
221 return (0);
222 }
223 if (strlen(file_spec) == 0) {
224 strnz__cpy(file_spec, "./", MAXLEN - 1);
225 file_name[0] = '\0';
226 return (0);
227 }
228 strnz__cpy(tmp_file_spec, file_spec, MAXLEN - 1);
229 last_slash = -1;
230 file_spec_l = strlen(tmp_file_spec);
231 if (file_spec_l > 0) {
232 i = 0;
233 while (i < file_spec_l && tmp_file_spec[i] != '\0') {
234 if (tmp_file_spec[i] == '/') {
235 last_slash = i;
236 break;
237 }
238 i++;
239 }
240 }
241 if (last_slash < 0) {
242 strnz__cpy(file_path, "./", MAXLEN - 1);
243 if (strcmp(file_spec, ".") == 0)
244 file_name[0] = '\0';
245 else
246 strnz__cpy(file_name, tmp_file_spec, MAXLEN - 1);
247 strnz__cpy(file_spec, file_path, MAXLEN - 1);
248 strnz__cat(file_spec, file_name, MAXLEN - 1);
249 } else {
250 tmp_file_spec[last_slash] = '\0';
251 strnz__cpy(file_path, tmp_file_spec, MAXLEN - 1);
252 strnz__cat(file_path, "/", MAXLEN - 1);
253 if (last_slash < file_spec_l)
254 last_slash++;
255 strnz__cpy(file_name, tmp_file_spec + last_slash, MAXLEN - 1);
256 }
257 return (0);
258}
#define MAXLEN
Definition curskeys.c:15
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition futil.c:544
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition futil.c:573

References strnz__cat(), and strnz__cpy().

Referenced by whence().

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

◆ main()

int main ( int argc,
char ** argv )

Definition at line 90 of file whence.c.

90 {
91 struct wh_opts wh_opts = {0};
92 wh_opts.flags = 0;
93 wh_opts.argv[0] = nullptr;
94 int i = 0;
95 int found = 0;
96 argp_parse(&argp, argc, argv, 0, 0, &wh_opts);
97 path_p = getenv("PATH");
98 if (path_p == nullptr)
99 ABEND(argv[0], 0, "PATH environment variable not set");
100 if (wh_opts.flags & WH_VERBOSE)
101 printf("%s\n", path_p);
102 while (i < wh_opts.argc) {
103 found = whence(wh_opts.argv[i++], wh_opts.flags);
104 }
105 if (found == 0)
106 exit(1);
107 exit(0);
108}
int whence(char *, int)
Find the full path of a file in the directories specified by the PATH environment variable.
Definition whence.c:119
void ABEND(char *, int, char *)
Exit the program with an error message.
Definition whence.c:266
char * path_p
Definition whence.c:24

References ABEND(), path_p, WH_VERBOSE, and whence().

Here is the call graph for this function:

◆ next_path()

int next_path ( char * dp,
char ** pp )

Get the next directory path from a colon-separated list of paths.

Parameters
dpA buffer to store the extracted directory path
ppA pointer to the current position in the path string
Returns
The length of the extracted directory path

This function takes a pointer to a colon-separated list of paths and extracts the next directory path from it. If the current character is a colon, it retrieves the current working directory instead. The extracted directory path is stored in the provided buffer, and the function returns the length of the extracted path.

Definition at line 180 of file whence.c.

180 {
181 int dl;
182
183 if (**pp == ':') {
184 (*pp)++;
185 getcwd(dp, PATH_MAX);
186 return (strlen(dp));
187 } else {
188 dl = 0;
189 while (**pp != '\0' && **pp != '\n' && **pp != '\r' && **pp != ':') {
190 *dp++ = *(*pp)++;
191 dl++;
192 }
193 *dp = '\0';
194 if (**pp == ':' && *++(*pp) == '\0')
195 (*pp)--;
196 return (dl);
197 }
198}

Referenced by whence().

Here is the caller graph for this function:

◆ whence()

int whence ( char * file_spec_p,
int flags )

Find the full path of a file in the directories specified by the PATH environment variable.

Parameters
file_spec_pThe file specification to search for
flagsFlags to control the behavior of the search (e.g., verbose mode, list all matches)

This function takes a file specification, extracts the directory and file name components, and searches through the directories specified in the PATH environment variable to find matches. It prints the full path of each match found, and if verbose mode is enabled, it also indicates whether each attempted path was found or not.

Definition at line 119 of file whence.c.

119 {
120 char file_spec[PATH_MAX];
121 char file_dir[PATH_MAX];
122 char file_name[PATH_MAX];
123 char try_spec[PATH_MAX];
124 char try_dir[PATH_MAX];
125 int path_l;
126 int found = 0;
127 struct stat stat_struct;
128
129 strnz__cpy(file_spec, file_spec_p, MAXLEN - 1);
131 file_spec_parts(file_spec, file_dir, file_name);
132 path_p = path_s;
133 path_l = next_path(try_dir, &path_p);
134 while (path_l != 0) {
135 strnz__cpy(try_spec, try_dir, MAXLEN - 1);
136 if (try_spec[path_l] != '/')
137 strnz__cat(try_spec, "/", MAXLEN - 1);
138 strnz__cat(try_spec, file_name, MAXLEN - 1);
139 if (stat(try_spec, &stat_struct) != 0) {
140 if (flags & WH_VERBOSE)
141 printf("- %s\n", try_spec);
142 path_l = next_path(try_dir, &path_p);
143 continue;
144 }
145 if (flags & WH_SETUID)
146 if (!(stat_struct.st_mode & S_ISUID)) {
147 path_l = next_path(try_dir, &path_p);
148 continue;
149 }
150 if (flags & WH_EXECUTABLE)
151 if (!(stat_struct.st_mode & S_ISUID)) {
152 path_l = next_path(try_dir, &path_p);
153 continue;
154 }
155 if (!(stat_struct.st_mode & S_IXUSR)) {
156 path_l = next_path(try_dir, &path_p);
157 continue;
158 }
159 found++;
160 if (flags & WH_VERBOSE)
161 printf("found %s\n", try_spec);
162 else {
163 printf("%s\n", try_spec);
164 if (!(flags & WH_ALL))
165 return found;
166 }
167 path_l = next_path(try_dir, &path_p);
168 }
169 return found;
170}
int next_path(char *, char **)
Get the next directory path from a colon-separated list of paths.
Definition whence.c:180
char path_s[MAXLEN]
Definition whence.c:25
int file_spec_parts(char *, char *, char *)
Split a file specification into directory and file name components.
Definition whence.c:209

References file_spec_parts(), next_path(), path_p, path_s, strnz__cat(), strnz__cpy(), WH_ALL, WH_EXECUTABLE, WH_SETUID, and WH_VERBOSE.

Referenced by main().

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

Variable Documentation

◆ argp_program_bug_address

const char* argp_program_bug_address = "billxwaller@gmail.com"

Definition at line 40 of file whence.c.

◆ argp_program_version

const char* argp_program_version = CM_VERSION

Definition at line 39 of file whence.c.

◆ path_p

char* path_p

Definition at line 24 of file whence.c.

Referenced by main(), and whence().

◆ path_s

char path_s[MAXLEN]

Definition at line 25 of file whence.c.

Referenced by whence().

◆ wh_flags

int wh_flags = 0

Definition at line 38 of file whence.c.