2
3
4
5
6
7
8
30void ABEND(
char *,
int,
char *);
36static char doc[] =
"whence locate files in path";
37static char args_doc[] =
"";
39static struct argp_option options[] = {
40 {
"all",
'a', 0, 0,
"list all matches", 0},
41 {
"verbose",
'v', 0, 0,
"verbose messages", 0},
49static error_t parse_opt(
int key,
char *arg,
struct argp_state *state) {
50 struct wh_opts *wh_opts = state->input;
63 wh_opts->argv[i] = arg;
70 return ARGP_ERR_UNKNOWN;
74static struct argp argp = {options, parse_opt, args_doc, doc,
77int main(
int argc,
char **argv) {
78 struct wh_opts wh_opts = {0};
82 argp_parse(&argp, argc, argv, 0, 0, &wh_opts);
85 ABEND(argv[0]
, 0
, "PATH environment variable not set");
88 while (optind < argc) {
89 whence(wh_opts.argv[optind++]
, wh_opts.flags
);
94
95
96
97
98
99
100
101
102
103void whence(
char *file_spec_p,
int flags) {
104 char file_spec[PATH_MAX];
105 char file_dir[PATH_MAX];
106 char file_name[PATH_MAX];
107 char try_spec[PATH_MAX];
108 char try_dir[PATH_MAX];
110 struct stat stat_struct;
117 while (path_l != 0) {
119 if (try_spec[path_l] !=
'/')
123 if (stat(try_spec, &stat_struct) == -1)
124 printf(
"- %s\n", try_spec);
126 printf(
"found %s\n", try_spec);
127 }
else if (stat(try_spec, &stat_struct) == 0) {
128 printf(
"%s\n", try_spec);
136
137
138
139
140
141
142
143
144
150 getcwd(dp, PATH_MAX);
154 while (**sp !=
'\0' && **sp !=
'\n' && **sp !=
'\r' && **sp !=
':') {
159 if (**sp ==
':' && *++(*sp) ==
'\0')
165
166
167
168
169
170
171
172
173
176 char tmp_file_spec[PATH_MAX];
178 struct stat stat_struct;
180 if (stat(file_spec, &stat_struct) != -1)
181 if ((stat_struct.st_mode & S_IFMT) == S_IFDIR) {
182 if (file_spec[strlen(file_path)] !=
'/')
188 if (strlen(file_spec) == 0) {
195 file_spec_l = strlen(tmp_file_spec);
196 if (file_spec_l > 0) {
198 while (i < file_spec_l && tmp_file_spec[i] !=
'\0') {
199 if (tmp_file_spec[i] ==
'/') {
206 if (last_slash < 0) {
208 if (strcmp(file_spec,
".") == 0)
215 tmp_file_spec[last_slash] =
'\0';
218 if (last_slash < file_spec_l)
225
226
229
230
231
232
233
234
235void ABEND(
char *pgmid,
int rc,
char *err_msg) {
236 fprintf(stderr,
"%s; error %d; %s\n", pgmid, rc, err_msg);
char * file_name[MAXLEN+1]
void normalend()
Exit the program successfully.
void whence(char *, int)
Find the full path of a file in the directories specified by the PATH environment variable.
int file_spec_parts(char *, char *, char *)
Split a file specification into directory and file name components.
int next_path(char *, char **)
Extract the next directory path from the PATH string.
const char * argp_program_version
const char * argp_program_bug_address
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat