source: mainline/uspace/app/bdsh/exec.c@ 1ef0fc3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1ef0fc3 was 2721a75, checked in by Martin Decky <martin@…>, 15 years ago

ping overhaul:

  • coding style
  • shorter and better readable main()
  • tool output is much similar to GNU's ping, demonstrating what functionality is still missing

related changes:

  • rename strerror.h → str_error.h for consistency
  • replace network apps' specific command-line parsing functions with a generalized libc versions
  • Property mode set to 100644
File size: 3.9 KB
Line 
1/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of the original program's authors nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* The VERY basics of execute in place support. These are buggy, leaky
32 * and not nearly done. Only here for beta testing!! You were warned!!
33 * TODO:
34 * Hash command lookups to save time
35 * Create a running pointer to **path and advance/rewind it as we go */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <str.h>
41#include <fcntl.h>
42#include <str_error.h>
43
44#include "config.h"
45#include "util.h"
46#include "exec.h"
47#include "errors.h"
48
49/* FIXME: Just have find_command() return an allocated string */
50static char *found;
51
52static char *find_command(char *);
53static int try_access(const char *);
54
55/* work-around for access() */
56static int try_access(const char *f)
57{
58 int fd;
59
60 fd = open(f, O_RDONLY);
61 if (fd > -1) {
62 close(fd);
63 return 0;
64 } else
65 return -1;
66}
67
68/* Returns the full path of "cmd" if cmd is found, else just hand back
69 * cmd as it was presented */
70static char *find_command(char *cmd)
71{
72 char *path_tok;
73 char *path[PATH_MAX];
74 int n = 0, i = 0;
75 size_t x = str_size(cmd) + 2;
76
77 found = (char *)malloc(PATH_MAX);
78
79 /* The user has specified a full or relative path, just give it back. */
80 if (-1 != try_access(cmd)) {
81 return (char *) cmd;
82 }
83
84 path_tok = str_dup(PATH);
85
86 /* Extract the PATH env to a path[] array */
87 path[n] = strtok(path_tok, PATH_DELIM);
88 while (NULL != path[n]) {
89 if ((str_size(path[n]) + x ) > PATH_MAX) {
90 cli_error(CL_ENOTSUP,
91 "Segment %d of path is too large, search ends at segment %d",
92 n, n-1);
93 break;
94 }
95 path[++n] = strtok(NULL, PATH_DELIM);
96 }
97
98 /* We now have n places to look for the command */
99 for (i=0; path[i]; i++) {
100 memset(found, 0, sizeof(found));
101 snprintf(found, PATH_MAX, "%s/%s", path[i], cmd);
102 if (-1 != try_access(found)) {
103 free(path_tok);
104 return (char *) found;
105 }
106 }
107
108 /* We didn't find it, just give it back as-is. */
109 free(path_tok);
110 return (char *) cmd;
111}
112
113unsigned int try_exec(char *cmd, char **argv)
114{
115 task_id_t tid;
116 task_exit_t texit;
117 char *tmp;
118 int retval;
119
120 tmp = str_dup(find_command(cmd));
121 free(found);
122
123 tid = task_spawn(tmp, (const char **) argv);
124 free(tmp);
125
126 if (tid == 0) {
127 cli_error(CL_EEXEC, "%s: Cannot spawn `%s'", progname, cmd);
128 return 1;
129 }
130
131 task_wait(tid, &texit, &retval);
132 if (texit != TASK_EXIT_NORMAL) {
133 printf("%s: Command failed (unexpectedly terminated)\n", progname);
134 } else if (retval != 0) {
135 printf("%s: Command failed (%s)\n",
136 progname, str_error(retval));
137 }
138
139 return 0;
140}
Note: See TracBrowser for help on using the repository browser.