annotate lib/lib.c @ 615:43489d673976

Add NOP b (byte) suffix to atolx() since od needs it.
author Rob Landley <rob@landley.net>
date Sat, 30 Jun 2012 16:31:37 -0500
parents 90d6026c36c3
children f51beec92738
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 :*/
250
101a71a76c24 Fix filename in header
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 245
diff changeset
2 /* lib.c - reusable stuff.
2
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
167
f16c8e5e9435 Replace strlcpy() with xstrcpy(), which exits if the string won't fit.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
14 // Strcpy with size checking: exit if there's not enough space for the string.
f16c8e5e9435 Replace strlcpy() with xstrcpy(), which exits if the string won't fit.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
15 void xstrcpy(char *dest, char *src, size_t size)
124
ef2bc92d5fb0 Work around uClibc weirdness.
Rob Landley <rob@landley.net>
parents: 115
diff changeset
16 {
167
f16c8e5e9435 Replace strlcpy() with xstrcpy(), which exits if the string won't fit.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
17 if (strlen(src)+1 > size) error_exit("xstrcpy");
f16c8e5e9435 Replace strlcpy() with xstrcpy(), which exits if the string won't fit.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
18 strcpy(dest, src);
124
ef2bc92d5fb0 Work around uClibc weirdness.
Rob Landley <rob@landley.net>
parents: 115
diff changeset
19 }
ef2bc92d5fb0 Work around uClibc weirdness.
Rob Landley <rob@landley.net>
parents: 115
diff changeset
20
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
21 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
22 {
245
67a0839bda77 Teach perror_exit() to take a NULL argument when we just want "command: error".
Rob Landley <rob@landley.net>
parents: 234
diff changeset
23 char *s = ": %s";
67a0839bda77 Teach perror_exit() to take a NULL argument when we just want "command: error".
Rob Landley <rob@landley.net>
parents: 234
diff changeset
24
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
25 fprintf(stderr, "%s: ", toys.which->name);
245
67a0839bda77 Teach perror_exit() to take a NULL argument when we just want "command: error".
Rob Landley <rob@landley.net>
parents: 234
diff changeset
26 if (msg) vfprintf(stderr, msg, va);
67a0839bda77 Teach perror_exit() to take a NULL argument when we just want "command: error".
Rob Landley <rob@landley.net>
parents: 234
diff changeset
27 else s+=2;
67a0839bda77 Teach perror_exit() to take a NULL argument when we just want "command: error".
Rob Landley <rob@landley.net>
parents: 234
diff changeset
28 if (err) fprintf(stderr, s, strerror(err));
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
29 putc('\n', stderr);
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
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
32 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
33 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
34 va_list va;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
35
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
36 va_start(va, msg);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
37 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
38 va_end(va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
39 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
40
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
41 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
42 {
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, errno, 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
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
50 // Die with an error message.
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
51 void error_exit(char *msg, ...)
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
52 {
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
53 va_list va;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
54
144
1fbc50374a30 Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents: 143
diff changeset
55 if (CFG_HELP && toys.exithelp) {
1fbc50374a30 Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents: 143
diff changeset
56 *toys.optargs=*toys.argv;
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 220
diff changeset
57 USE_HELP(help_main();) // dear gcc: shut up.
144
1fbc50374a30 Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents: 143
diff changeset
58 fprintf(stderr,"\n");
1fbc50374a30 Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents: 143
diff changeset
59 }
1fbc50374a30 Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents: 143
diff changeset
60
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
61 va_start(va, msg);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
62 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
63 va_end(va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
64
197
571623c6765d Changeset 186 assumed that toys.exitval defaults to 0. Actually change the
Rob Landley <rob@landley.net>
parents: 187
diff changeset
65 exit(!toys.exitval ? 1 : toys.exitval);
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
66 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
67
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
68
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
69 // 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
70 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
71 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
72 va_list va;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
73
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
74 va_start(va, msg);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
75 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
76 va_end(va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
77
197
571623c6765d Changeset 186 assumed that toys.exitval defaults to 0. Actually change the
Rob Landley <rob@landley.net>
parents: 187
diff changeset
78 exit(!toys.exitval ? 1 : toys.exitval);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
79 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
80
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
81 // 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
82 void *xmalloc(size_t size)
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
83 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
84 void *ret = malloc(size);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
85 if (!ret) error_exit("xmalloc");
3
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 return ret;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
88 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
89
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
90 // Die unless we can allocate prezeroed memory.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
91 void *xzalloc(size_t size)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
92 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
93 void *ret = xmalloc(size);
480
f558dce66095 Nathan McSween convinced me compilers that inline memset() can optimize the bzero case pretty well.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
94 memset(ret, 0, size);
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
95 return ret;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
96 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
97
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
98 // 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
99 // moving it. (Notice different arguments from libc function.)
115
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
100 void *xrealloc(void *ptr, size_t size)
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
101 {
115
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
102 ptr = realloc(ptr, size);
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
103 if (!ptr) error_exit("xrealloc");
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
104
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
105 return ptr;
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
106 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
107
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
108 // Die unless we can allocate a copy of this many bytes of string.
369
5715eed39575 Correct return types of xstrdup() and xstrndup()
Rob Landley <rob@landley.net>
parents: 354
diff changeset
109 char *xstrndup(char *s, size_t n)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
110 {
284
603275a05524 Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents: 252
diff changeset
111 char *ret = xmalloc(++n);
603275a05524 Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents: 252
diff changeset
112 strncpy(ret, s, n);
603275a05524 Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents: 252
diff changeset
113 ret[--n]=0;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
114
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
115 return ret;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
116 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
117
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
118 // Die unless we can allocate a copy of this string.
369
5715eed39575 Correct return types of xstrdup() and xstrndup()
Rob Landley <rob@landley.net>
parents: 354
diff changeset
119 char *xstrdup(char *s)
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
120 {
252
8d751063a563 Add spaces after some commas (from Charlie Shepherd).
Rob Landley <rob@landley.net>
parents: 251
diff changeset
121 return xstrndup(s, strlen(s));
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
122 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
123
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
124 // Die unless we can allocate enough space to sprintf() into.
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
125 char *xmsprintf(char *format, ...)
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
126 {
126
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
127 va_list va, va2;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
128 int len;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
129 char *ret;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
130
126
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
131 va_start(va, format);
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
132 va_copy(va2, va);
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
133
126
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
134 // How long is it?
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
135 len = vsnprintf(0, 0, format, va);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
136 len++;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
137 va_end(va);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
138
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
139 // Allocate and do the sprintf()
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
140 ret = xmalloc(len);
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
141 vsnprintf(ret, len, format, va2);
126
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
142 va_end(va2);
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
143
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
144 return ret;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
145 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
146
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
147 void xprintf(char *format, ...)
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
148 {
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
149 va_list va;
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
150 va_start(va, format);
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
151
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
152 vprintf(format, va);
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
153 if (ferror(stdout)) perror_exit("write");
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
154 }
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
155
128
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
156 void xputs(char *s)
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
157 {
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
158 if (EOF == puts(s)) perror_exit("write");
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
159 }
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
160
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
161 void xputc(char c)
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
162 {
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
163 if (EOF == fputc(c, stdout)) perror_exit("write");
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
164 }
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
165
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
166 void xflush(void)
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
167 {
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
168 if (fflush(stdout)) perror_exit("write");;
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
169 }
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
170
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
171 // 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
172 // 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
173 void xexec(char **argv)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
174 {
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
175 toy_exec(argv);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
176 execvp(argv[0], argv);
419
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
177
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
178 perror_exit("exec %s", argv[0]);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
179 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
180
51
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
181 void xaccess(char *path, int flags)
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
182 {
251
d9ddba2c7acf Remove extra newlines on error_paths
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 250
diff changeset
183 if (access(path, flags)) perror_exit("Can't access '%s'", path);
51
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
184 }
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
185
214
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
186 // Die unless we can delete a file. (File must exist to be deleted.)
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
187 void xunlink(char *path)
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
188 {
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
189 if (unlink(path)) perror_exit("unlink '%s'", path);
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
190 }
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
191
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
192 // 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
193 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
194 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
195 int fd = open(path, flags, mode);
148
88dd003ccdf4 xcreate(): perror already prints error name and newline, remove redundancy.
Rob Landley <rob@landley.net>
parents: 144
diff changeset
196 if (fd == -1) perror_exit("%s", path);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
197 return fd;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
198 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
199
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
200 // 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
201 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
202 {
bb4c38853a7d xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents: 44
diff changeset
203 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
204 }
bb4c38853a7d xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents: 44
diff changeset
205
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
206 void xclose(int fd)
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
207 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
208 if (close(fd)) perror_exit("xclose");
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
209 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
210
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
211 int xdup(int fd)
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
212 {
592
be4b2d3796eb Feeding -1 to xdup() isn't an error.
Rob Landley <rob@landley.net>
parents: 590
diff changeset
213 if (fd != -1) {
be4b2d3796eb Feeding -1 to xdup() isn't an error.
Rob Landley <rob@landley.net>
parents: 590
diff changeset
214 fd = dup(fd);
be4b2d3796eb Feeding -1 to xdup() isn't an error.
Rob Landley <rob@landley.net>
parents: 590
diff changeset
215 if (fd == -1) perror_exit("xdup");
be4b2d3796eb Feeding -1 to xdup() isn't an error.
Rob Landley <rob@landley.net>
parents: 590
diff changeset
216 }
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
217 return fd;
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
218 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
219
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
220 // 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
221 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
222 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
223 FILE *f = fopen(path, mode);
251
d9ddba2c7acf Remove extra newlines on error_paths
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 250
diff changeset
224 if (!f) perror_exit("No file %s", path);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
225 return f;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
226 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
227
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
228 // Keep reading until full or EOF
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
229 ssize_t readall(int fd, void *buf, size_t len)
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
230 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
231 size_t count = 0;
309
79a61cd58596 Bug spotted by Roberto Foglietta: at EOF readall() should return count, not len.
Rob Landley <rob@landley.net>
parents: 308
diff changeset
232
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
233 while (count<len) {
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
234 int i = read(fd, buf+count, len-count);
309
79a61cd58596 Bug spotted by Roberto Foglietta: at EOF readall() should return count, not len.
Rob Landley <rob@landley.net>
parents: 308
diff changeset
235 if (!i) break;
8
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
236 if (i<0) return i;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
237 count += i;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
238 }
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
239
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
240 return count;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
241 }
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
242
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
243 // Keep writing until done or EOF
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
244 ssize_t writeall(int fd, void *buf, size_t len)
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
245 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
246 size_t count = 0;
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
247 while (count<len) {
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
248 int i = write(fd, buf+count, len-count);
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
249 if (i<1) return i;
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
250 count += i;
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
251 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
252
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
253 return count;
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
254 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
255
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
256 // Die if there's an error other than EOF.
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
257 size_t xread(int fd, void *buf, size_t len)
8
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
258 {
399
7a5b70965e0e Bugfix (spotted by Nathan McSween): xread can't detect <0 if the return type is stored in an unsigned variable.
Rob Landley <rob@landley.net>
parents: 397
diff changeset
259 ssize_t ret = read(fd, buf, len);
7a5b70965e0e Bugfix (spotted by Nathan McSween): xread can't detect <0 if the return type is stored in an unsigned variable.
Rob Landley <rob@landley.net>
parents: 397
diff changeset
260 if (ret < 0) perror_exit("xread");
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
261
399
7a5b70965e0e Bugfix (spotted by Nathan McSween): xread can't detect <0 if the return type is stored in an unsigned variable.
Rob Landley <rob@landley.net>
parents: 397
diff changeset
262 return ret;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
263 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
264
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
265 void xreadall(int fd, void *buf, size_t len)
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
266 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
267 if (len != readall(fd, buf, len)) perror_exit("xreadall");
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
268 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
269
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
270 // There's no xwriteall(), just xwrite(). When we read, there may or may not
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
271 // be more data waiting. When we write, there is data and it had better go
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
272 // somewhere.
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
273
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
274 void xwrite(int fd, void *buf, size_t len)
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
275 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
276 if (len != writeall(fd, buf, len)) perror_exit("xwrite");
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
277 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
278
340
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
279 // Die if lseek fails, probably due to being called on a pipe.
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
280
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
281 off_t xlseek(int fd, off_t offset, int whence)
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
282 {
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
283 offset = lseek(fd, offset, whence);
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
284 if (offset<0) perror_exit("lseek");
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
285
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
286 return offset;
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
287 }
6e03d6a8df23 Add mkswap.
Rob Landley <rob@landley.net>
parents: 337
diff changeset
288
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
289 char *xgetcwd(void)
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
290 {
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
291 char *buf = getcwd(NULL, 0);
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
292 if (!buf) perror_exit("xgetcwd");
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
293
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
294 return buf;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
295 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
296
95
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
297 void xstat(char *path, struct stat *st)
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
298 {
252
8d751063a563 Add spaces after some commas (from Charlie Shepherd).
Rob Landley <rob@landley.net>
parents: 251
diff changeset
299 if(stat(path, st)) perror_exit("Can't stat %s", path);
95
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
300 }
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
301
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
302 // Cannonicalizes path by removing ".", "..", and "//" elements. This is not
44
e74aa8b8660a Comment tweak.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
303 // the same as realpath(), where "dir/.." could wind up somewhere else by
e74aa8b8660a Comment tweak.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
304 // 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
305 char *xabspath(char *path)
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
306 {
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
307 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
308
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
309 // 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
310 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
311 char *cwd=xgetcwd();
252
8d751063a563 Add spaces after some commas (from Charlie Shepherd).
Rob Landley <rob@landley.net>
parents: 251
diff changeset
312 path = xmsprintf("%s/%s", cwd, path);
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
313 free(cwd);
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
314 } 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
315
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
316 // 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
317 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
318 while (*from) {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
319
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
320 // 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
321 if (*from!='/') {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
322 *(to++) = *(from++);
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
323 continue;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
324 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
325
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
326 // 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
327 while (*from=='/') from++;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
328
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
329 // 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
330 while (*from=='.') {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
331 // Skip .
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
332 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
333 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
334 // 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
335 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
336 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
337 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
338 else break;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
339 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
340 } else break;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
341 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
342 // 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
343 *(to++) = '/';
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
344 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
345 *to = 0;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
346
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
347 return path;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
348 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
349
585
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
350 // Resolve all symlinks, returning malloc() memory.
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
351 char *xrealpath(char *path)
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
352 {
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
353 char *new = realpath(path, NULL);
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
354 if (!new) perror_exit("realpath '%s'", path);
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
355 return new;
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
356 }
1dcd7994abea Add xrealpath() at suggestion of Ashish Briggers.
Rob Landley <rob@landley.net>
parents: 578
diff changeset
357
292
b4077be6c746 Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents: 284
diff changeset
358 void xchdir(char *path)
b4077be6c746 Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents: 284
diff changeset
359 {
344
a0c3767f0a82 Fix thinko.
Rob Landley <rob@landley.net>
parents: 340
diff changeset
360 if (chdir(path)) error_exit("chdir '%s'", path);
292
b4077be6c746 Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents: 284
diff changeset
361 }
b4077be6c746 Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents: 284
diff changeset
362
216
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
363 // Ensure entire path exists.
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
364 // If mode != -1 set permissions on newly created dirs.
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
365 // Requires that path string be writable (for temporary null terminators).
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
366 void xmkpath(char *path, int mode)
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
367 {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
368 char *p, old;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
369 mode_t mask;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
370 int rc;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
371 struct stat st;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
372
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
373 for (p = path; ; p++) {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
374 if (!*p || *p == '/') {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
375 old = *p;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
376 *p = rc = 0;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
377 if (stat(path, &st) || !S_ISDIR(st.st_mode)) {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
378 if (mode != -1) {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
379 mask=umask(0);
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
380 rc = mkdir(path, mode);
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
381 umask(mask);
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
382 } else rc = mkdir(path, 0777);
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
383 }
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
384 *p = old;
252
8d751063a563 Add spaces after some commas (from Charlie Shepherd).
Rob Landley <rob@landley.net>
parents: 251
diff changeset
385 if(rc) perror_exit("mkpath '%s'", path);
216
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
386 }
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
387 if (!*p) break;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
388 }
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
389 }
370
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
390
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
391 // setuid() can fail (for example, too many processes belonging to that user),
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
392 // which opens a security hole if the process continues as the original user.
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
393
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
394 void xsetuid(uid_t uid)
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
395 {
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
396 if (setuid(uid)) perror_exit("xsetuid");
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
397 }
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
398
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 369
diff changeset
399
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
400 // 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
401 // 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
402 // order.
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
403
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
404 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
405 {
295
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 292
diff changeset
406 struct string_list *rlist = NULL, **prlist=&rlist;
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
407 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
408
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
409 for (;;) {
535
d51be130fda2 More stabs at getting #includes right, and moving off of deprecated functions.
Rob Landley <rob@landley.net>
parents: 503
diff changeset
410 char *next = path ? strchr(path, ':') : NULL;
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
411 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
412 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
413 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
414
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
415 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
416 + (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
417 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
418 else {
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
419 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
420 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
421 res += len;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
422 *(res++) = '/';
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
423 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
424 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
425
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
426 // 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
427 if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) {
295
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 292
diff changeset
428 *prlist = rnext;
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 292
diff changeset
429 rnext->next = NULL;
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 292
diff changeset
430 prlist = &(rnext->next);
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
431 } 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
432
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
433 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
434 path += len;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
435 path++;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
436 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
437 free(cwd);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
438
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
439 return rlist;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
440 }
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
441
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
442 // 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
443 // 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
444 // 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
445 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
446 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
447 int i, out = 0;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
448
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
449 if (buflen) {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
450 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
451 int res = n/i;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
452
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
453 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
454 out++;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
455 n -= res*i;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
456 *buf++ = '0' + res;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
457 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
458 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
459 *buf = 0;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
460 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
461 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
462
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
463 // 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
464 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
465 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
466 if (buflen && n<0) {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
467 n = -n;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
468 *buf++ = '-';
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
469 buflen--;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
470 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
471 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
472 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
473
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
474 // 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
475 // 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
476 //
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
477 // 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
478 // 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
479 // 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
480
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
481 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
482
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
483 // 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
484 char *utoa(unsigned n)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
485 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
486 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
487
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
488 return itoa_buf;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
489 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
490
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
491 char *itoa(int n)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
492 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
493 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
494
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
495 return itoa_buf;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
496 }
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
497
102
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
498 // atol() with the kilo/mega/giga/tera/peta/exa extensions.
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
499 // (zetta and yotta don't fit in 64 bits.)
443
41b5ac08208f Make atolx() error_exit() if fed a string that doesn't convert entirely into an integer.
Rob Landley <rob@landley.net>
parents: 419
diff changeset
500 long atolx(char *numstr)
102
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
501 {
615
43489d673976 Add NOP b (byte) suffix to atolx() since od needs it.
Rob Landley <rob@landley.net>
parents: 604
diff changeset
502 char *c, *suffixes="bkmgtpe", *end;
443
41b5ac08208f Make atolx() error_exit() if fed a string that doesn't convert entirely into an integer.
Rob Landley <rob@landley.net>
parents: 419
diff changeset
503 long val = strtol(numstr, &c, 0);
102
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
504
149
58554af48c8b Fix from Charlie Shepherd: at end of string, don't match the null terminator
Rob Landley <rob@landley.net>
parents: 148
diff changeset
505 if (*c) {
58554af48c8b Fix from Charlie Shepherd: at end of string, don't match the null terminator
Rob Landley <rob@landley.net>
parents: 148
diff changeset
506 end = strchr(suffixes, tolower(*c));
615
43489d673976 Add NOP b (byte) suffix to atolx() since od needs it.
Rob Landley <rob@landley.net>
parents: 604
diff changeset
507 if (end) {
43489d673976 Add NOP b (byte) suffix to atolx() since od needs it.
Rob Landley <rob@landley.net>
parents: 604
diff changeset
508 int shift = end-suffixes;
43489d673976 Add NOP b (byte) suffix to atolx() since od needs it.
Rob Landley <rob@landley.net>
parents: 604
diff changeset
509 if (shift--) val *= 1024L<<(shift*10);
43489d673976 Add NOP b (byte) suffix to atolx() since od needs it.
Rob Landley <rob@landley.net>
parents: 604
diff changeset
510 } else {
443
41b5ac08208f Make atolx() error_exit() if fed a string that doesn't convert entirely into an integer.
Rob Landley <rob@landley.net>
parents: 419
diff changeset
511 while (isspace(*c)) c++;
41b5ac08208f Make atolx() error_exit() if fed a string that doesn't convert entirely into an integer.
Rob Landley <rob@landley.net>
parents: 419
diff changeset
512 if (*c) error_exit("not integer: %s", numstr);
41b5ac08208f Make atolx() error_exit() if fed a string that doesn't convert entirely into an integer.
Rob Landley <rob@landley.net>
parents: 419
diff changeset
513 }
149
58554af48c8b Fix from Charlie Shepherd: at end of string, don't match the null terminator
Rob Landley <rob@landley.net>
parents: 148
diff changeset
514 }
397
b7afbc6b753a Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents: 379
diff changeset
515
102
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
516 return val;
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
517 }
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
518
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
519 int numlen(long l)
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
520 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
521 int len = 0;
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
522 while (l) {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
523 l /= 10;
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
524 len++;
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
525 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
526 return len;
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
527 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 554
diff changeset
528
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
529 // Return how long the file at fd is, if there's any way to determine it.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
530 off_t fdlength(int fd)
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
531 {
310
8b8116214b1c Roberto Foglietta pointed out that readall() needs fdlength() to restore
Rob Landley <rob@landley.net>
parents: 309
diff changeset
532 off_t bottom = 0, top = 0, pos, old;
75
89ca591a9236 More random progress on mke2fs. Nothing to see yet.
Rob Landley <rob@landley.net>
parents: 70
diff changeset
533 int size;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
534
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
535 // If the ioctl works for this, return it.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
536
75
89ca591a9236 More random progress on mke2fs. Nothing to see yet.
Rob Landley <rob@landley.net>
parents: 70
diff changeset
537 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
538
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
539 // If not, do a binary search for the last location we can read. (Some
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
540 // block devices don't do BLKGETSIZE right.) This should probably have
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
541 // a CONFIG option...
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
542
310
8b8116214b1c Roberto Foglietta pointed out that readall() needs fdlength() to restore
Rob Landley <rob@landley.net>
parents: 309
diff changeset
543 old = lseek(fd, 0, SEEK_CUR);
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
544 do {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
545 char temp;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
546
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
547 pos = bottom + (top - bottom) / 2;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
548
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
549 // If we can read from the current location, it's bigger.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
550
77
8018c40605d9 The fdlength() ioctl apparently doesn't work on files (and the lseek trick
Rob Landley <rob@landley.net>
parents: 75
diff changeset
551 if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) {
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
552 if (bottom == top) bottom = top = (top+1) * 2;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
553 else bottom = pos;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
554
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
555 // If we can't, it's smaller.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
556
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
557 } else {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
558 if (bottom == top) {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
559 if (!top) return 0;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
560 bottom = top/2;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
561 } else top = pos;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
562 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
563 } while (bottom + 1 != top);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
564
310
8b8116214b1c Roberto Foglietta pointed out that readall() needs fdlength() to restore
Rob Landley <rob@landley.net>
parents: 309
diff changeset
565 lseek(fd, old, SEEK_SET);
8b8116214b1c Roberto Foglietta pointed out that readall() needs fdlength() to restore
Rob Landley <rob@landley.net>
parents: 309
diff changeset
566
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
567 return pos + 1;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
568 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
569
115
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
570 // This can return null (meaning file not found). It just won't return null
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
571 // for memory allocation reasons.
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
572 char *xreadlink(char *name)
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
573 {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
574 int len, size = 0;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
575 char *buf = 0;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
576
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
577 // Grow by 64 byte chunks until it's big enough.
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
578 for(;;) {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
579 size +=64;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
580 buf = xrealloc(buf, size);
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
581 len = readlink(name, buf, size);
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
582
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
583 if (len<0) {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
584 free(buf);
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
585 return 0;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
586 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
587 if (len<size) {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
588 buf[len]=0;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
589 return buf;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
590 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
591 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
592 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
593
77
8018c40605d9 The fdlength() ioctl apparently doesn't work on files (and the lseek trick
Rob Landley <rob@landley.net>
parents: 75
diff changeset
594 /*
8018c40605d9 The fdlength() ioctl apparently doesn't work on files (and the lseek trick
Rob Landley <rob@landley.net>
parents: 75
diff changeset
595 This might be of use or might not. Unknown yet...
8018c40605d9 The fdlength() ioctl apparently doesn't work on files (and the lseek trick
Rob Landley <rob@landley.net>
parents: 75
diff changeset
596
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
597 // Read contents of file as a single freshly allocated nul-terminated string.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
598 char *readfile(char *name)
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
599 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
600 off_t len;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
601 int fd;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
602 char *buf;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
603
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
604 fd = open(name, O_RDONLY);
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
605 if (fd == -1) return 0;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
606 len = fdlength(fd);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
607 buf = xmalloc(len+1);
379
74526fc22c25 xreadall() returns void... how does that even compile?
Rob Landley <rob@landley.net>
parents: 370
diff changeset
608 buf[readall(fd, buf, len)] = 0;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
609
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
610 return buf;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
611 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
612
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
613 char *xreadfile(char *name)
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
614 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
615 char *buf = readfile(name);
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
616 if (!buf) perror_exit("xreadfile %s", name);
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
617 return buf;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
618 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
619
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
620 */
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
621
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
622 // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
623 // exists and is this executable.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
624 void xpidfile(char *name)
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
625 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
626 char pidfile[256], spid[32];
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
627 int i, fd;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
628 pid_t pid;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
629
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
630 sprintf(pidfile, "/var/run/%s.pid", name);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
631 // Try three times to open the sucker.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
632 for (i=0; i<3; i++) {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
633 fd = open(pidfile, O_CREAT|O_EXCL, 0644);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
634 if (fd != -1) break;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
635
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
636 // If it already existed, read it. Loop for race condition.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
637 fd = open(pidfile, O_RDONLY);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
638 if (fd == -1) continue;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
639
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
640 // Is the old program still there?
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
641 spid[xread(fd, spid, sizeof(spid)-1)] = 0;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
642 close(fd);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
643 pid = atoi(spid);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
644 if (fd < 1 || kill(pid, 0) == ESRCH) unlink(pidfile);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
645
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
646 // An else with more sanity checking might be nice here.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
647 }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
648
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
649 if (i == 3) error_exit("xpidfile %s", name);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
650
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
651 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid()));
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
652 close(fd);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
653 }
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
654
308
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
655 // Iterate through an array of files, opening each one and calling a function
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
656 // on that filehandle and name. The special filename "-" means stdin if
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
657 // flags is O_RDONLY, stdout otherwise. An empty argument list calls
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
658 // function() on just stdin/stdout.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
659 //
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
660 // Note: read only filehandles are automatically closed when function()
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
661 // returns, but writeable filehandles must be close by function()
397
b7afbc6b753a Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents: 379
diff changeset
662 void loopfiles_rw(char **argv, int flags, int permissions, int failok,
b7afbc6b753a Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents: 379
diff changeset
663 void (*function)(int fd, char *name))
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
664 {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
665 int fd;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
666
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
667 // If no arguments, read from stdin.
308
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
668 if (!*argv) function(flags ? 1 : 0, "-");
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
669 else do {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
670 // Filename "-" means read from stdin.
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
671 // Inability to open a file prints a warning, but doesn't exit.
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
672
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
673 if (!strcmp(*argv,"-")) fd=0;
397
b7afbc6b753a Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents: 379
diff changeset
674 else if (0>(fd = open(*argv, flags, permissions)) && !failok) {
252
8d751063a563 Add spaces after some commas (from Charlie Shepherd).
Rob Landley <rob@landley.net>
parents: 251
diff changeset
675 perror_msg("%s", *argv);
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
676 toys.exitval = 1;
220
104792581cc9 Fix loopfiles to not call function() on file not found.
Rob Landley <rob@landley.net>
parents: 216
diff changeset
677 continue;
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
678 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
679 function(fd, *argv);
308
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
680 if (!flags) close(fd);
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
681 } while (*++argv);
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
682 }
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
683
397
b7afbc6b753a Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents: 379
diff changeset
684 // Call loopfiles_rw with O_RDONLY and !failok (common case).
308
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
685 void loopfiles(char **argv, void (*function)(int fd, char *name))
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
686 {
397
b7afbc6b753a Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents: 379
diff changeset
687 loopfiles_rw(argv, O_RDONLY, 0, 0, function);
308
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
688 }
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
689
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
690 // Slow, but small.
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
691
284
603275a05524 Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents: 252
diff changeset
692 char *get_rawline(int fd, long *plen, char end)
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
693 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
694 char c, *buf = NULL;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
695 long len = 0;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
696
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
697 for (;;) {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
698 if (1>read(fd, &c, 1)) break;
354
3c9f47f4fd33 Fix bug spotted by Jean-Christphe Dubois: reserve space for null terminator.
Rob Landley <rob@landley.net>
parents: 344
diff changeset
699 if (!(len & 63)) buf=xrealloc(buf, len+65);
284
603275a05524 Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents: 252
diff changeset
700 if ((buf[len++]=c) == end) break;
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
701 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
702 if (buf) buf[len]=0;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
703 if (plen) *plen = len;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
704
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
705 return buf;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
706 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
707
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
708 char *get_line(int fd)
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
709 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
710 long len;
284
603275a05524 Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents: 252
diff changeset
711 char *buf = get_rawline(fd, &len, '\n');
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
712
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
713 if (buf && buf[--len]=='\n') buf[len]=0;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
714
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
715 return buf;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
716 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
717
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
718 // Copy the rest of in to out and close both files.
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
719
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
720 void xsendfile(int in, int out)
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
721 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
722 long len;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
723 char buf[4096];
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
724
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
725 if (in<0) return;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
726 for (;;) {
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
727 len = xread(in, buf, 4096);
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
728 if (len<1) break;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
729 xwrite(out, buf, len);
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
730 }
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
731 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
732
590
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
733 int wfchmodat(int fd, char *name, mode_t mode)
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
734 {
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
735 int rc = fchmodat(fd, name, mode, 0);
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
736
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
737 if (rc) {
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
738 perror_msg("chmod '%s' to %04o", name, mode);
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
739 toys.exitval=1;
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
740 }
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
741 return rc;
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
742 }
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
743
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
744 // Open a temporary file to copy an existing file into.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
745 int copy_tempfile(int fdin, char *name, char **tempname)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
746 {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
747 struct stat statbuf;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
748 int fd;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
749
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
750 *tempname = xstrndup(name, strlen(name)+6);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
751 strcat(*tempname,"XXXXXX");
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
752 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
753
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
754 // Set permissions of output file
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
755
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
756 fstat(fdin, &statbuf);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
757 fchmod(fd, statbuf.st_mode);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
758
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
759 return fd;
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
760 }
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
761
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
762 // Abort the copy and delete the temporary file.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
763 void delete_tempfile(int fdin, int fdout, char **tempname)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
764 {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
765 close(fdin);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
766 close(fdout);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
767 unlink(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
768 free(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
769 *tempname = NULL;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
770 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
771
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
772 // Copy the rest of the data and replace the original with the copy.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
773 void replace_tempfile(int fdin, int fdout, char **tempname)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
774 {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
775 char *temp = xstrdup(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
776
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
777 temp[strlen(temp)-6]=0;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
778 if (fdin != -1) {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
779 xsendfile(fdin, fdout);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
780 xclose(fdin);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
781 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
782 xclose(fdout);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
783 rename(*tempname, temp);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
784 free(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
785 free(temp);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
786 *tempname = NULL;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
787 }
334
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
788
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
789 // Create a 256 entry CRC32 lookup table.
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
790
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 334
diff changeset
791 void crc_init(unsigned int *crc_table, int little_endian)
334
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
792 {
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
793 unsigned int i;
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
794
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
795 // Init the CRC32 table (big endian)
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
796 for (i=0; i<256; i++) {
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 334
diff changeset
797 unsigned int j, c = little_endian ? i : i<<24;
334
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
798 for (j=8; j; j--)
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 334
diff changeset
799 if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1;
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 334
diff changeset
800 else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1);
334
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
801 crc_table[i] = c;
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
802 }
83c461db9df7 Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents: 311
diff changeset
803 }
419
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
804
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
805 // Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
806 // set *x=0 and *y=0 before calling to detect failure to set either, or
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
807 // x=80 y=25 to provide defaults
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
808
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
809 void terminal_size(unsigned *x, unsigned *y)
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
810 {
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
811 struct winsize ws;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
812 int i;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
813
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
814 //memset(&ws, 0, sizeof(ws));
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
815 for (i=0; i<3; i++) {
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
816 if (ioctl(i, TIOCGWINSZ, &ws)) continue;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
817 if (x) *x = ws.ws_col;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
818 if (y) *y = ws.ws_row;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
819 }
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
820 if (x) {
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
821 char *s = getenv("COLUMNS");
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
822
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
823 i = s ? atoi(s) : 0;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
824 if (i>0) *x = i;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
825 }
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
826 if (y) {
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
827 char *s = getenv("ROWS");
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
828
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
829 i = s ? atoi(s) : 0;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
830 if (i>0) *y = i;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
831 }
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
832 }
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
833
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
834 // This should use a raw tty, fixit later.
503
3b9dea897dc0 Upgrade yesno() and make cp -i use it.
Rob Landley <rob@landley.net>
parents: 498
diff changeset
835 int yesno(char *prompt, int def)
419
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
836 {
554
997e016fbdf0 Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Rob Landley <rob@landley.net>
parents: 553
diff changeset
837 FILE *fps[] = {stdin, stdout, stderr};
997e016fbdf0 Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Rob Landley <rob@landley.net>
parents: 553
diff changeset
838 int i;
503
3b9dea897dc0 Upgrade yesno() and make cp -i use it.
Rob Landley <rob@landley.net>
parents: 498
diff changeset
839 char buf;
419
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
840
554
997e016fbdf0 Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Rob Landley <rob@landley.net>
parents: 553
diff changeset
841 for (i=0; i<3; i++) if (isatty(i)) break;
997e016fbdf0 Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Rob Landley <rob@landley.net>
parents: 553
diff changeset
842 if (i == 3) return 1;
419
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
843
554
997e016fbdf0 Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Rob Landley <rob@landley.net>
parents: 553
diff changeset
844 fprintf(fps[i], "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
997e016fbdf0 Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Rob Landley <rob@landley.net>
parents: 553
diff changeset
845 fflush(fps[i]);
997e016fbdf0 Using /dev/tty for yesno() is wrong because yes 'n' | cp -ial needs to work.
Rob Landley <rob@landley.net>
parents: 553
diff changeset
846 while (fread(&buf, 1, 1, fps[i])) {
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 535
diff changeset
847 if (tolower(buf) == 'y') def = 1;
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 535
diff changeset
848 if (tolower(buf) == 'n') def = 0;
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 535
diff changeset
849 else if (!isspace(buf)) continue;
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 535
diff changeset
850
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 535
diff changeset
851 break;
419
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
852 }
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 535
diff changeset
853
419
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
854 return def;
af0cca0aba9d Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents: 399
diff changeset
855 }
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
856
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
857 // Execute a callback for each PID that matches a process name from a list.
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
858 void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid))
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
859 {
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
860 DIR *dp;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
861 struct dirent *entry;
604
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
862 char cmd[sizeof(toybuf)], path[64];
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
863 char **curname;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
864
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
865 if (!(dp = opendir("/proc"))) perror_exit("opendir");
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
866
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
867 while ((entry = readdir(dp))) {
604
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
868 int fd, n;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
869
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
870 if (!isdigit(*entry->d_name)) continue;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
871
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
872 if (sizeof(path) <= snprintf(path, sizeof(path), "/proc/%s/cmdline",
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
873 entry->d_name)) continue;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
874
604
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
875 if (-1 == (fd=open(path, O_RDONLY))) continue;
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
876 n = read(fd, cmd, sizeof(cmd));
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
877 close(fd);
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
878 if (n<1) continue;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
879
604
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
880 for (curname = names; *curname; curname++)
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
881 if (!strcmp(basename(cmd), *curname))
90d6026c36c3 Minor code refactoring.
Rob Landley <rob@landley.net>
parents: 592
diff changeset
882 callback(atol(entry->d_name));
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
883 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
884
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
885 closedir(dp);
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents: 443
diff changeset
886 }
498
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
887
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
888 struct signame {
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
889 int num;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
890 char *name;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
891 };
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
892
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
893 // Signals required by POSIX 2008:
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
894 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
895
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
896 #define SIGNIFY(x) {SIG##x, #x}
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
897
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
898 static struct signame signames[] = {
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
899 SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), SIGNIFY(CHLD), SIGNIFY(CONT),
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
900 SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL),
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
901 SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(STOP), SIGNIFY(TERM),
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
902 SIGNIFY(TSTP), SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(USR1), SIGNIFY(USR2),
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
903 SIGNIFY(SYS), SIGNIFY(TRAP), SIGNIFY(URG), SIGNIFY(VTALRM), SIGNIFY(XCPU),
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
904 SIGNIFY(XFSZ)
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
905 };
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
906
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
907 // not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR)
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
908 // obsolete: SIGNIFY(PROF) SIGNIFY(POLL)
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
909
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
910 // Convert name to signal number. If name == NULL print names.
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
911 int sig_to_num(char *pidstr)
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
912 {
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
913 int i;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
914
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
915 if (pidstr) {
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
916 char *s;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
917 i = strtol(pidstr, &s, 10);
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
918 if (!*s) return i;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
919
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
920 if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
921 }
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
922 for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++)
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
923 if (!pidstr) xputs(signames[i].name);
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
924 else if (!strcasecmp(pidstr, signames[i].name))
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
925 return signames[i].num;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
926
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
927 return -1;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
928 }
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
929
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
930 char *num_to_sig(int sig)
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
931 {
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
932 int i;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
933
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
934 for (i=0; i<sizeof(signames)/sizeof(struct signame); i++)
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
935 if (signames[i].num == sig) return signames[i].name;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
936 return NULL;
c9aaceccd6bd Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents: 480
diff changeset
937 }
551
2548e6e590b2 Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents: 550
diff changeset
938
2548e6e590b2 Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents: 550
diff changeset
939
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
940 mode_t string_to_mode(char *modestr, mode_t mode)
551
2548e6e590b2 Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents: 550
diff changeset
941 {
590
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
942 char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu";
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
943 char *s, *str = modestr;
553
75da5d793fc8 Unwind gratuitous macros.
Rob Landley <rob@landley.net>
parents: 551
diff changeset
944
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
945 // Handle octal mode
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
946 if (isdigit(*str)) {
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
947 mode = strtol(str, &s, 8);
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
948 if (*s || (mode & ~(07777))) goto barf;
553
75da5d793fc8 Unwind gratuitous macros.
Rob Landley <rob@landley.net>
parents: 551
diff changeset
949
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
950 return mode;
551
2548e6e590b2 Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents: 550
diff changeset
951 }
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
952
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
953 // Gaze into the bin of permission...
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
954 for (;;) {
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
955 int i, j, dowho, dohow, dowhat;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
956
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
957 dowho = dohow = dowhat = 0;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
958
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
959 // Find the who, how, and what stanzas, in that order
590
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
960 while (*str && (s = strchr(whos, *str))) {
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
961 dowho |= 1<<(s-whos);
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
962 str++;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
963 }
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
964 if (!dowho) dowho = 8;
590
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
965 if (!*str || !(s = strchr(hows, *str))) goto barf;
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
966 dohow = *(str++);
590
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
967
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
968 if (!dohow) goto barf;
590
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
969 while (*str && (s = strchr(whats, *str))) {
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
970 dowhat |= 1<<(s-whats);
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
971 str++;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
972 }
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
973
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
974 // Convert X to x for directory or if already executable somewhere
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
975 if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
976
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
977 // Copy mode from another category?
590
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
978 if (!dowhat && *str && (s = strchr(whys, *str))) {
7becb497c3c4 Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents: 585
diff changeset
979 dowhat = (mode>>(3*(s-whys)))&7;
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
980 str++;
551
2548e6e590b2 Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents: 550
diff changeset
981 }
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
982
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
983 // Are we ready to do a thing yet?
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
984 if (*str && *(str++) != ',') goto barf;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
985
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
986 // Ok, apply the bits to the mode.
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
987 for (i=0; i<4; i++) {
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
988 for (j=0; j<3; j++) {
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
989 mode_t bit = 0;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
990
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
991 // Figure out new value at this location
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
992 if (i == 3) {
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
993 } else if (dowhat&(1<<j)) bit++;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
994
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
995 // When selection active, modify bit
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
996 if (dowho&(8|(1<<i))) {
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
997 int where = 1<<((3*i)+j);
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
998
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
999 if (dohow == '=' || (bit && dohow == '-'))
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1000 mode &= ~where;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1001 if (bit && dohow != '-') mode |= where;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1002 }
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1003 }
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1004 }
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1005
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1006 if (!*str) break;
551
2548e6e590b2 Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents: 550
diff changeset
1007 }
578
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1008 return mode;
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1009 barf:
5cc8a8fc08b4 First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents: 565
diff changeset
1010 error_exit("bad mode '%s'", modestr);
551
2548e6e590b2 Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents: 550
diff changeset
1011 }