Mercurial > hg > toybox
annotate lib/functions.c @ 49:bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
need 3. Doing varargs for this doesn't really appeal to me (bugs in waiting)
so I made an xcreate() that takes 3 args, and had xopen() call it with 0 for
the third argument. That way, if we feed O_CREAT to xopen() the permission
000 result should be easy to spot.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 07 Jan 2007 22:51:12 -0500 |
parents | e74aa8b8660a |
children | 63c168b65fa6 |
rev | line source |
---|---|
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
1 /* vi: set sw=4 ts=4 :*/ |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
2 /* functions.c - reusable stuff. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
3 * |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
4 * Functions with the x prefix are wrappers for library functions. They either |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
5 * succeed or kill the program with an error message, but never return failure. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
6 * They usually have the same arguments and return value as the function they |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
7 * wrap. |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
8 * |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
9 * Copyright 2006 Rob Landley <rob@landley.net> |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
10 */ |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
11 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
12 #include "toys.h" |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
13 |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
14 void verror_msg(char *msg, int err, va_list va) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
15 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
16 fprintf(stderr, "%s: ", toys.which->name); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
17 vfprintf(stderr, msg, va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
18 if (err) fprintf(stderr, ": %s", strerror(err)); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
19 putc('\n', stderr); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
20 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
21 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
22 void error_msg(char *msg, ...) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
23 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
24 va_list va; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
25 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
26 va_start(va, msg); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
27 verror_msg(msg, 0, va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
28 va_end(va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
29 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
30 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
31 void perror_msg(char *msg, ...) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
32 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
33 va_list va; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
34 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
35 va_start(va, msg); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
36 verror_msg(msg, errno, va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
37 va_end(va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
38 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
39 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
40 // Die with an error message. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
41 void error_exit(char *msg, ...) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
42 { |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
43 va_list va; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
44 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
45 va_start(va, msg); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
46 verror_msg(msg, 0, va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
47 va_end(va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
48 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
49 exit(toys.exitval); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
50 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
51 |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
52 // Die with an error message and strerror(errno) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
53 void perror_exit(char *msg, ...) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
54 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
55 va_list va; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
56 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
57 va_start(va, msg); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
58 verror_msg(msg, errno, va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
59 va_end(va); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
60 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
61 exit(toys.exitval); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
62 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
63 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
64 // Like strncpy but always null terminated. |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
65 void strlcpy(char *dest, char *src, size_t size) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
66 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
67 strncpy(dest,src,size); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
68 dest[size-1] = 0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
69 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
70 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
71 // Die unless we can allocate memory. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
72 void *xmalloc(size_t size) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
73 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
74 void *ret = malloc(size); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
75 if (!ret) error_exit("xmalloc"); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
76 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
77 return ret; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
78 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
79 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
80 // Die unless we can allocate prezeroed memory. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
81 void *xzalloc(size_t size) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
82 { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
83 void *ret = xmalloc(size); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
84 bzero(ret,size); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
85 return ret; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
86 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
87 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
88 // Die unless we can change the size of an existing allocation, possibly |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
89 // moving it. (Notice different arguments from libc function.) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
90 void xrealloc(void **ptr, size_t size) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
91 { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
92 *ptr = realloc(*ptr, size); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
93 if (!*ptr) error_exit("xrealloc"); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
94 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
95 |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
96 // Die unless we can allocate a copy of this many bytes of string. |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
97 void *xstrndup(char *s, size_t n) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
98 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
99 void *ret = xmalloc(++n); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
100 strlcpy(ret, s, n); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
101 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
102 return ret; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
103 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
104 |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
105 // Die unless we can allocate a copy of this string. |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
106 void *xstrdup(char *s) |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
107 { |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
108 return xstrndup(s,strlen(s)); |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
109 } |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
110 |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
111 // Die unless we can allocate enough space to sprintf() into. |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
112 char *xmsprintf(char *format, ...) |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
113 { |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
114 va_list va; |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
115 int len; |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
116 char *ret; |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
117 |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
118 // How long is it? |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
119 |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
120 va_start(va, format); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
121 len = vsnprintf(0, 0, format, va); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
122 len++; |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
123 va_end(va); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
124 |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
125 // Allocate and do the sprintf() |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
126 ret = xmalloc(len); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
127 va_start(va, format); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
128 vsnprintf(ret, len, format, va); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
129 va_end(va); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
130 |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
131 return ret; |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
132 } |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
133 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
134 // Die unless we can exec argv[] (or run builtin command). Note that anything |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
135 // with a path isn't a builtin, so /bin/sh won't match the builtin sh. |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
136 void xexec(char **argv) |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
137 { |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
138 toy_exec(argv); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
139 execvp(argv[0], argv); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
140 error_exit("No %s", argv[0]); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
141 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
142 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
143 // Die unless we can open/create a file, returning file descriptor. |
49
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
44
diff
changeset
|
144 int xcreate(char *path, int flags, int mode) |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
145 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
146 int fd = open(path, flags, mode); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
147 if (fd == -1) error_exit("No file %s\n", path); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
148 return fd; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
149 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
150 |
49
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
44
diff
changeset
|
151 // Die unless we can open a file, returning file descriptor. |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
44
diff
changeset
|
152 int xopen(char *path, int flags) |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
44
diff
changeset
|
153 { |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
44
diff
changeset
|
154 return xcreate(path, flags, 0); |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
44
diff
changeset
|
155 } |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
44
diff
changeset
|
156 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
157 // Die unless we can open/create a file, returning FILE *. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
158 FILE *xfopen(char *path, char *mode) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
159 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
160 FILE *f = fopen(path, mode); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
161 if (!f) error_exit("No file %s\n", path); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
162 return f; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
163 } |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
164 |
8
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
165 // Read from file handle, retrying if interrupted. |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
166 ssize_t reread(int fd, void *buf, size_t count) |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
167 { |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
168 ssize_t len; |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
169 for (;;) { |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
170 len = read(fd, buf, count); |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
171 if (len >= 0 || errno != EINTR) return len; |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
172 } |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
173 } |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
174 |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
175 // Keep reading until full or EOF |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
176 ssize_t readall(int fd, void *buf, size_t count) |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
177 { |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
178 size_t len = 0; |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
179 while (len<count) { |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
180 int i = reread(fd, buf, count); |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
181 if (!i) return len; |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
182 if (i<0) return i; |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
183 count += i; |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
184 } |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
185 |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
186 return count; |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
187 } |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
188 |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
189 // Die if we can't fill a buffer |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
190 void xread(int fd, char *buf, size_t count) |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
191 { |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
192 if (count != readall(fd, buf, count)) perror_exit("xread"); |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
193 } |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
194 |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
195 char *xgetcwd(void) |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
196 { |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
197 char *buf = getcwd(NULL, 0); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
198 if (!buf) error_exit("xgetcwd"); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
199 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
200 return buf; |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
201 } |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
202 |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
203 // Cannonicalizes path by removing ".", "..", and "//" elements. This is not |
44 | 204 // the same as realpath(), where "dir/.." could wind up somewhere else by |
205 // following symlinks. | |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
206 char *xabspath(char *path) |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
207 { |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
208 char *from, *to; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
209 |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
210 // If this isn't an absolute path, make it one with cwd. |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
211 if (path[0]!='/') { |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
212 char *cwd=xgetcwd(); |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
213 path = xmsprintf("%s/%s",cwd,path); |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
214 free(cwd); |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
215 } else path = xstrdup(path); |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
216 |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
217 // Loop through path elements |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
218 from = to = path; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
219 while (*from) { |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
220 |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
221 // Continue any current path component. |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
222 if (*from!='/') { |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
223 *(to++) = *(from++); |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
224 continue; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
225 } |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
226 |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
227 // Skip duplicate slashes. |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
228 while (*from=='/') from++; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
229 |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
230 // Start of a new filename. Handle . and .. |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
231 while (*from=='.') { |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
232 // Skip . |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
233 if (from[1]=='/') from += 2; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
234 else if (!from[1]) from++; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
235 // Back up for .. |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
236 else if (from[1]=='.') { |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
237 if (from[2]=='/') from +=3; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
238 else if(!from[2]) from+=2; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
239 else break; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
240 while (to>path && *(--to)!='/'); |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
241 } else break; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
242 } |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
243 // Add directory separator slash. |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
244 *(to++) = '/'; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
245 } |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
246 *to = 0; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
247 |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
248 return path; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
249 } |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
250 |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
251 // Find all file in a colon-separated path with access type "type" (generally |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
252 // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
253 // order. |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
254 |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
255 struct string_list *find_in_path(char *path, char *filename) |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
256 { |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
257 struct string_list *rlist = NULL; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
258 char *cwd = xgetcwd(); |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
259 |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
260 for (;;) { |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
261 char *next = path ? index(path, ':') : NULL; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
262 int len = next ? next-path : strlen(path); |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
263 struct string_list *rnext; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
264 struct stat st; |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
265 |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
266 rnext = xmalloc(sizeof(void *) + strlen(filename) |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
267 + (len ? len : strlen(cwd)) + 2); |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
268 if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
269 else { |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
270 char *res = rnext->str; |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
271 strncpy(res, path, len); |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
272 res += len; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
273 *(res++) = '/'; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
274 strcpy(res, filename); |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
275 } |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
276 |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
277 // Confirm it's not a directory. |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
278 if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
279 rnext->next = rlist; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
280 rlist = rnext; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
281 } else free(rnext); |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
282 |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
283 if (!next) break; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
284 path += len; |
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
285 path++; |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
286 } |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
287 free(cwd); |
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
288 |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
289 return rlist; |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
290 |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
291 } |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
292 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
293 // Convert unsigned int to ascii, writing into supplied buffer. A truncated |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
294 // result contains the first few digits of the result ala strncpy, and is |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
295 // always null terminated (unless buflen is 0). |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
296 void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
297 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
298 int i, out = 0; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
299 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
300 if (buflen) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
301 for (i=1000000000; i; i/=10) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
302 int res = n/i; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
303 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
304 if ((res || out || i == 1) && --buflen>0) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
305 out++; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
306 n -= res*i; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
307 *buf++ = '0' + res; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
308 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
309 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
310 *buf = 0; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
311 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
312 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
313 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
314 // Convert signed integer to ascii, using utoa_to_buf() |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
315 void itoa_to_buf(int n, char *buf, unsigned buflen) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
316 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
317 if (buflen && n<0) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
318 n = -n; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
319 *buf++ = '-'; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
320 buflen--; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
321 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
322 utoa_to_buf((unsigned)n, buf, buflen); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
323 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
324 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
325 // This static buffer is used by both utoa() and itoa(), calling either one a |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
326 // second time will overwrite the previous results. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
327 // |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
328 // The longest 32 bit integer is -2 billion plus a null terminator: 12 bytes. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
329 // Note that int is always 32 bits on any remotely unix-like system, see |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
330 // http://www.unix.org/whitepapers/64bit.html for details. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
331 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
332 static char itoa_buf[12]; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
333 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
334 // Convert unsigned integer to ascii, returning a static buffer. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
335 char *utoa(unsigned n) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
336 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
337 utoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
338 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
339 return itoa_buf; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
340 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
341 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
342 char *itoa(int n) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
343 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
344 itoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
345 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
346 return itoa_buf; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
347 } |