annotate lib/lib.c @ 250:101a71a76c24

Fix filename in header
author Charlie Shepherd <masterdriverz@gentoo.org>
date Fri, 25 Jan 2008 12:36:24 +0000
parents 67a0839bda77
children d9ddba2c7acf
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);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
94 bzero(ret,size);
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.
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
109 void *xstrndup(char *s, size_t n)
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
110 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
111 void *ret = xmalloc(++n);
167
f16c8e5e9435 Replace strlcpy() with xstrcpy(), which exits if the string won't fit.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
112 xstrcpy(ret, s, n);
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
113
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
114 return ret;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
115 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
116
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
117 // Die unless we can allocate a copy of this string.
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
118 void *xstrdup(char *s)
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
119 {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
120 return xstrndup(s,strlen(s));
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
121 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
122
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
123 // Die unless we can allocate enough space to sprintf() into.
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
124 char *xmsprintf(char *format, ...)
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
125 {
126
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
126 va_list va, va2;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
127 int len;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
128 char *ret;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
129
126
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
130 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
131 va_copy(va2, va);
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
132
126
7b22987a7b47 Vladimir Oleynik pointed out that va_start() twice in the same function
Rob Landley <rob@landley.net>
parents: 124
diff changeset
133 // How long is it?
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
134 len = vsnprintf(0, 0, format, va);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
135 len++;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
136 va_end(va);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
137
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
138 // Allocate and do the sprintf()
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
139 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
140 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
141 va_end(va2);
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
142
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
143 return ret;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
144 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
145
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
146 void xprintf(char *format, ...)
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
147 {
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
148 va_list va;
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
149 va_start(va, format);
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
150
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
151 vprintf(format, va);
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
152 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
153 }
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
154
128
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
155 void xputs(char *s)
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
156 {
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
157 if (EOF == puts(s)) perror_exit("write");
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
158 }
0a90a5fbc1bf Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents: 126
diff changeset
159
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
160 void xputc(char c)
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
161 {
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
162 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
163 }
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 void xflush(void)
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
166 {
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
167 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
168 }
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
169
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
170 // 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
171 // 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
172 void xexec(char **argv)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
173 {
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
174 toy_exec(argv);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
175 execvp(argv[0], argv);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
176 error_exit("No %s", argv[0]);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
177 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
178
51
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
179 void xaccess(char *path, int flags)
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
180 {
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
181 if (access(path, flags)) perror_exit("Can't access '%s'\n", path);
51
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
182 }
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
183
214
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
184 // 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
185 void xunlink(char *path)
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
186 {
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
187 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
188 }
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
189
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
190 // 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
191 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
192 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
193 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
194 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
195 return fd;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
196 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
197
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
198 // 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
199 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
200 {
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 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
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
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
204 void xclose(int fd)
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
205 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
206 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
207 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
208
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
209 // 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
210 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
211 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
212 FILE *f = fopen(path, mode);
70
a1b464bbef08 Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents: 63
diff changeset
213 if (!f) perror_exit("No file %s\n", path);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
214 return f;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
215 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
216
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
217 // 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
218 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
219 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
220 size_t count = 0;
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
221 while (count<len) {
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
222 int i = read(fd, buf+count, len-count);
8
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
223 if (!i) return len;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
224 if (i<0) return i;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
225 count += i;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
226 }
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
227
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
228 return count;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
229 }
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
230
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
231 // 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
232 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
233 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
234 size_t count = 0;
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
235 while (count<len) {
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
236 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
237 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
238 count += i;
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
239 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
240
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
241 return count;
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
242 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
243
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
244 // 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
245 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
246 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
247 len = read(fd, buf, len);
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
248 if (len < 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
249
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
250 return 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
251 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
252
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
253 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
254 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
255 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
256 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
257
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
258 // 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
259 // 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
260 // somewhere.
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
261
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
262 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
263 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
264 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
265 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
266
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
267 char *xgetcwd(void)
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
268 {
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
269 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
270 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
271
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
272 return buf;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
273 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
274
95
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
275 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
276 {
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
277 if(stat(path, st)) perror_exit("Can't stat %s\n",path);
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
278 }
a636e4d20f13 Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents: 77
diff changeset
279
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
280 // Cannonicalizes path by removing ".", "..", and "//" elements. This is not
44
e74aa8b8660a Comment tweak.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
281 // the same as realpath(), where "dir/.." could wind up somewhere else by
e74aa8b8660a Comment tweak.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
282 // 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
283 char *xabspath(char *path)
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
284 {
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
285 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
286
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
287 // 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
288 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
289 char *cwd=xgetcwd();
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
290 path = xmsprintf("%s/%s",cwd,path);
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
291 free(cwd);
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
292 } 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
293
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
294 // 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
295 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
296 while (*from) {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
297
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
298 // 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
299 if (*from!='/') {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
300 *(to++) = *(from++);
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
301 continue;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
302 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
303
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
304 // 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
305 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
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 // 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
308 while (*from=='.') {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
309 // Skip .
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
310 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
311 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
312 // 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
313 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
314 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
315 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
316 else break;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
317 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
318 } else break;
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 // 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
321 *(to++) = '/';
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
322 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
323 *to = 0;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
324
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
325 return path;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
326 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
327
216
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
328 // 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
329 // 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
330 // 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
331 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
332 {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
333 char *p, old;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
334 mode_t mask;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
335 int rc;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
336 struct stat st;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
337
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
338 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
339 if (!*p || *p == '/') {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
340 old = *p;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
341 *p = rc = 0;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
342 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
343 if (mode != -1) {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
344 mask=umask(0);
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
345 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
346 umask(mask);
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
347 } 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
348 }
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
349 *p = old;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
350 if(rc) perror_exit("mkpath '%s'",path);
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
351 }
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
352 if (!*p) break;
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
353 }
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
354 }
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
355 // 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
356 // 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
357 // order.
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
358
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
359 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
360 {
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
361 struct string_list *rlist = NULL;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
362 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
363
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
364 for (;;) {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
365 char *next = path ? index(path, ':') : NULL;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
366 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
367 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
368 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
369
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
370 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
371 + (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
372 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
373 else {
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
374 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
375 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
376 res += len;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
377 *(res++) = '/';
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
378 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
379 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
380
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
381 // 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
382 if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) {
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
383 rnext->next = rlist;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
384 rlist = rnext;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
385 } 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
386
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
387 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
388 path += len;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
389 path++;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
390 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
391 free(cwd);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
392
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
393 return rlist;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
394 }
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
395
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
396 // 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
397 // 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
398 // 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
399 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
400 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
401 int i, out = 0;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
402
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
403 if (buflen) {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
404 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
405 int res = n/i;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
406
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
407 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
408 out++;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
409 n -= res*i;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
410 *buf++ = '0' + res;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
411 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
412 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
413 *buf = 0;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
414 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
415 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
416
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
417 // 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
418 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
419 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
420 if (buflen && n<0) {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
421 n = -n;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
422 *buf++ = '-';
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
423 buflen--;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
424 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
425 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
426 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
427
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
428 // 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
429 // 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
430 //
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
431 // 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
432 // 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
433 // 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
434
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
435 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
436
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
437 // 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
438 char *utoa(unsigned n)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
439 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
440 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
441
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
442 return itoa_buf;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
443 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
444
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
445 char *itoa(int n)
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 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
448
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
449 return itoa_buf;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
450 }
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
451
102
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
452 // 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
453 // (zetta and yotta don't fit in 64 bits.)
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
454 long atolx(char *c)
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
455 {
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
456 char *suffixes="kmgtpe", *end;
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
457 long val = strtol(c, &c, 0);
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
458
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
459 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
460 end = strchr(suffixes, tolower(*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
461 if (end) val *= 1024L<<((end-suffixes)*10);
58554af48c8b Fix from Charlie Shepherd: at end of string, don't match the null terminator
Rob Landley <rob@landley.net>
parents: 148
diff changeset
462 }
102
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
463 return val;
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
464 }
aa4fa2543a65 Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents: 97
diff changeset
465
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
466 // 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
467 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
468 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
469 off_t bottom = 0, top = 0, pos;
75
89ca591a9236 More random progress on mke2fs. Nothing to see yet.
Rob Landley <rob@landley.net>
parents: 70
diff changeset
470 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
471
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
472 // 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
473
75
89ca591a9236 More random progress on mke2fs. Nothing to see yet.
Rob Landley <rob@landley.net>
parents: 70
diff changeset
474 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
475
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
476 // 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
477 // 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
478 // 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
479
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
480 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
481 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
482
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
483 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
484
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
485 // 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
486
77
8018c40605d9 The fdlength() ioctl apparently doesn't work on files (and the lseek trick
Rob Landley <rob@landley.net>
parents: 75
diff changeset
487 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
488 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
489 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
490
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
491 // 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
492
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
493 } 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
494 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
495 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
496 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
497 } 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
498 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
499 } 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
500
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
501 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
502 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
503
115
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
504 // 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
505 // 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
506 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
507 {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
508 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
509 char *buf = 0;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
510
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
511 // 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
512 for(;;) {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
513 size +=64;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
514 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
515 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
516
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
517 if (len<0) {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
518 free(buf);
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
519 return 0;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
520 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
521 if (len<size) {
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
522 buf[len]=0;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
523 return buf;
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
524 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
525 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
526 }
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 102
diff changeset
527
77
8018c40605d9 The fdlength() ioctl apparently doesn't work on files (and the lseek trick
Rob Landley <rob@landley.net>
parents: 75
diff changeset
528 /*
8018c40605d9 The fdlength() ioctl apparently doesn't work on files (and the lseek trick
Rob Landley <rob@landley.net>
parents: 75
diff changeset
529 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
530
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
531 // 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
532 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
533 {
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 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
535 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
536 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
537
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
538 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
539 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
540 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
541 buf = xmalloc(len+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
542 buf[xread(fd, buf, len)] = 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
543
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 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
545 }
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 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
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 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
550 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
551 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
552 }
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
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
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 // 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
557 // 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
558 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
559 {
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 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
561 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
562 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
563
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 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
565 // 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
566 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
567 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
568 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
569
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
570 // 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
571 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
572 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
573
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
574 // 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
575 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
576 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
577 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
578 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
579
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
580 // 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
581 }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 153
diff changeset
582
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
583 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
584
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
585 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
586 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
587 }
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
588
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
589 // Iterate through an array of files, opening each one (read only) and
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
590 // calling a function on that filehandle and name. The special filename
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
591 // "-" means stdin. An empty argument list calls function() on stdin.
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
592 void loopfiles(char **argv, void (*function)(int fd, char *name))
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
593 {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
594 int fd;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
595
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
596 // If no arguments, read from stdin.
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 185
diff changeset
597 if (!*argv) function(0, "-");
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
598 else do {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
599 // Filename "-" means read from stdin.
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
600 // 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
601
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
602 if (!strcmp(*argv,"-")) fd=0;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
603 else if (0>(fd = open(*argv, O_RDONLY))) {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
604 perror_msg("%s",*argv);
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
605 toys.exitval = 1;
220
104792581cc9 Fix loopfiles to not call function() on file not found.
Rob Landley <rob@landley.net>
parents: 216
diff changeset
606 continue;
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
607 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
608 function(fd, *argv);
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
609 close(fd);
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
610 } while (*++argv);
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 167
diff changeset
611 }
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
612
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
613 // Slow, but small.
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
614
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
615 char *get_rawline(int fd, long *plen)
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
616 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
617 char c, *buf = NULL;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
618 long len = 0;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
619
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
620 for (;;) {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
621 if (1>read(fd, &c, 1)) break;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
622 if (!(len & 63)) buf=xrealloc(buf, len+64);
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
623 if ((buf[len++]=c) == '\n') break;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
624 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
625 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
626 if (plen) *plen = len;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
627
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
628 return buf;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
629 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
630
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
631 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
632 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
633 long len;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
634 char *buf = get_rawline(fd, &len);
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
635
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
636 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
637
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
638 return buf;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
639 }
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
640
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
641 // 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
642
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
643 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
644 {
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
645 long len;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
646 char buf[4096];
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
647
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
648 if (in<0) return;
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
649 for (;;) {
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
650 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
651 if (len<1) break;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
652 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
653 }
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
654 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
655
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
656 // Open a temporary file to copy an existing file into.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
657 int copy_tempfile(int fdin, char *name, char **tempname)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
658 {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
659 struct stat statbuf;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
660 int fd;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
661
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
662 *tempname = xstrndup(name, strlen(name)+6);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
663 strcat(*tempname,"XXXXXX");
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
664 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
665
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
666 // Set permissions of output file
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
667
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
668 fstat(fdin, &statbuf);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
669 fchmod(fd, statbuf.st_mode);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
670
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
671 return fd;
201
5d523752715a Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
672 }
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
673
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
674 // Abort the copy and delete the temporary file.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
675 void delete_tempfile(int fdin, int fdout, char **tempname)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
676 {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
677 close(fdin);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
678 close(fdout);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
679 unlink(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
680 free(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
681 *tempname = NULL;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
682 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
683
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
684 // 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
685 void replace_tempfile(int fdin, int fdout, char **tempname)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
686 {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
687 char *temp = xstrdup(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
688
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
689 temp[strlen(temp)-6]=0;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
690 if (fdin != -1) {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
691 xsendfile(fdin, fdout);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
692 xclose(fdin);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
693 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
694 xclose(fdout);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
695 rename(*tempname, temp);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
696 free(*tempname);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
697 free(temp);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
698 *tempname = NULL;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents: 201
diff changeset
699 }