annotate lib/functions.c @ 63:69efffcacd70

Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which shouldn't be a problem if we register signal handlers with sigaction(SA_RESTART) Straighten out count and len (I generally consistently use "count" for the current progress and "len" for the total, but this time I got them backwards for some reason and don't want to confuse myself in future.)
author Rob Landley <rob@landley.net>
date Thu, 18 Jan 2007 21:54:08 -0500
parents 41d55b5d49fd
children a1b464bbef08
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 :*/
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
2 /* functions.c - reusable stuff.
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
3 *
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
4 * Functions with the x prefix are wrappers for library functions. They either
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
5 * succeed or kill the program with an error message, but never return failure.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
6 * They usually have the same arguments and return value as the function they
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
7 * wrap.
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
8 *
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
9 * Copyright 2006 Rob Landley <rob@landley.net>
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
10 */
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
11
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
12 #include "toys.h"
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
13
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
14 void verror_msg(char *msg, int err, va_list va)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
15 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
16 fprintf(stderr, "%s: ", toys.which->name);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
17 vfprintf(stderr, msg, va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
18 if (err) fprintf(stderr, ": %s", strerror(err));
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
19 putc('\n', stderr);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
20 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
21
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
22 void error_msg(char *msg, ...)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
23 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
24 va_list va;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
25
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
26 va_start(va, msg);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
27 verror_msg(msg, 0, va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
28 va_end(va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
29 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
30
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
31 void perror_msg(char *msg, ...)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
32 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
33 va_list va;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
34
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
35 va_start(va, msg);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
36 verror_msg(msg, errno, va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
37 va_end(va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
38 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
39
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
40 // Die with an error message.
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
41 void error_exit(char *msg, ...)
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
42 {
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
43 va_list va;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
44
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
45 va_start(va, msg);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
46 verror_msg(msg, 0, va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
47 va_end(va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
48
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
49 exit(toys.exitval);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
50 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
51
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
52
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
53 // 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
54 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
55 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
56 va_list va;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
57
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
58 va_start(va, msg);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
59 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
60 va_end(va);
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
61
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
62 exit(toys.exitval);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
63 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
64
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
65 // Stub until the online help system goes in.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
66 void usage_exit(void)
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
67 {
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 exit(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
69 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
70
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
71 // Like strncpy but always null terminated.
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
72 void strlcpy(char *dest, char *src, size_t size)
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
73 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
74 strncpy(dest,src,size);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
75 dest[size-1] = 0;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
76 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
77
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
78 // 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
79 void *xmalloc(size_t size)
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 void *ret = malloc(size);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
82 if (!ret) error_exit("xmalloc");
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
83
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
84 return ret;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
85 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
86
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
87 // Die unless we can allocate prezeroed memory.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
88 void *xzalloc(size_t size)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
89 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
90 void *ret = xmalloc(size);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
91 bzero(ret,size);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
92 return ret;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
93 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
94
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
95 // 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
96 // moving it. (Notice different arguments from libc function.)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
97 void xrealloc(void **ptr, size_t size)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
98 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
99 *ptr = realloc(*ptr, size);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
100 if (!*ptr) error_exit("xrealloc");
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
101 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
102
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
103 // 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
104 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
105 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
106 void *ret = xmalloc(++n);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
107 strlcpy(ret, s, n);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
108
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
109 return ret;
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
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
112 // 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
113 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
114 {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
115 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
116 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
117
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
118 // Die unless we can allocate enough space to sprintf() into.
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
119 char *xmsprintf(char *format, ...)
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
120 {
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
121 va_list va;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
122 int len;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
123 char *ret;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
124
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
125 // How long is it?
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
126
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
127 va_start(va, format);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
128 len = vsnprintf(0, 0, format, va);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
129 len++;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
130 va_end(va);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
131
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
132 // Allocate and do the sprintf()
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
133 ret = xmalloc(len);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
134 va_start(va, format);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
135 vsnprintf(ret, len, format, va);
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 return ret;
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
139 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
140
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
141 // 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
142 // 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
143 void xexec(char **argv)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
144 {
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
145 toy_exec(argv);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
146 execvp(argv[0], argv);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
147 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
148 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
149
51
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
150 void xaccess(char *path, int flags)
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
151 {
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
152 if (access(path, flags)) error_exit("Can't access '%s'\n", path);
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
153 }
63edd4de94dd Add xaccess()
Rob Landley <rob@landley.net>
parents: 50
diff changeset
154
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
155 // 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
156 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
157 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
158 int fd = open(path, flags, mode);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
159 if (fd == -1) error_exit("No file %s\n", path);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
160 return fd;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
161 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
162
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
163 // 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
164 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
165 {
bb4c38853a7d xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents: 44
diff changeset
166 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
167 }
bb4c38853a7d xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents: 44
diff changeset
168
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
169 // 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
170 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
171 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
172 FILE *f = fopen(path, mode);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
173 if (!f) error_exit("No file %s\n", path);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
174 return f;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
175 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
176
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
177 // 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
178 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
179 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
180 size_t count = 0;
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
181 while (count<len) {
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
182 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
183 if (!i) return len;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
184 if (i<0) return i;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
185 count += i;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
186 }
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
187
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
188 return count;
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
189 }
04f66da2bdbf Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents: 7
diff changeset
190
50
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
191 // 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
192 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
193 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
194 size_t count = 0;
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
195 while (count<len) {
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
196 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
197 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
198 count += i;
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
199 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
200
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
201 return count;
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
202 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
203
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
204 // 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
205 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
206 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
207 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
208 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
209
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
210 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
211 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
212
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
213 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
214 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
215 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
216 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
217
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
218 // 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
219 // 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
220 // somewhere.
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
221
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
222 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
223 {
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 53
diff changeset
224 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
225 }
63c168b65fa6 Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
226
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
227 char *xgetcwd(void)
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
228 {
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
229 char *buf = getcwd(NULL, 0);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
230 if (!buf) error_exit("xgetcwd");
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
231
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
232 return buf;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
233 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
234
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
235 // Cannonicalizes path by removing ".", "..", and "//" elements. This is not
44
e74aa8b8660a Comment tweak.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
236 // the same as realpath(), where "dir/.." could wind up somewhere else by
e74aa8b8660a Comment tweak.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
237 // 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
238 char *xabspath(char *path)
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
239 {
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
240 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
241
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
242 // 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
243 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
244 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
245 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
246 free(cwd);
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
247 } 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
248
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
249 // 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
250 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
251 while (*from) {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
252
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
253 // 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
254 if (*from!='/') {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
255 *(to++) = *(from++);
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
256 continue;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
257 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
258
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
259 // 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
260 while (*from=='/') from++;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
261
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
262 // 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
263 while (*from=='.') {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
264 // Skip .
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
265 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
266 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
267 // 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
268 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
269 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
270 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
271 else break;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
272 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
273 } else break;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
274 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
275 // 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
276 *(to++) = '/';
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
277 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
278 *to = 0;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
279
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
280 return path;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
281 }
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
282
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
283 // 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
284 // 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
285 // order.
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
286
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
287 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
288 {
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
289 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
290 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
291
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
292 for (;;) {
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
293 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
294 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
295 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
296 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
297
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
298 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
299 + (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
300 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
301 else {
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
302 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
303 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
304 res += len;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
305 *(res++) = '/';
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
306 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
307 }
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
308
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
309 // 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
310 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
311 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
312 rlist = rnext;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
313 } 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
314
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
315 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
316 path += len;
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
317 path++;
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
318 }
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
319 free(cwd);
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
320
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 16
diff changeset
321 return rlist;
16
dd10785b6532 Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents: 8
diff changeset
322
4
732b055e17f7 Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents: 3
diff changeset
323 }
7
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
324
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
325 // 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
326 // 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
327 // 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
328 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
329 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
330 int i, out = 0;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
331
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
332 if (buflen) {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
333 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
334 int res = n/i;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
335
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
336 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
337 out++;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
338 n -= res*i;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
339 *buf++ = '0' + res;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
340 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
341 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
342 *buf = 0;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
343 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
344 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
345
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
346 // 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
347 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
348 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
349 if (buflen && n<0) {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
350 n = -n;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
351 *buf++ = '-';
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
352 buflen--;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
353 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
354 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
355 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
356
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
357 // 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
358 // 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
359 //
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
360 // 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
361 // 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
362 // 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
363
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
364 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
365
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
366 // 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
367 char *utoa(unsigned n)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
368 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
369 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
370
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
371 return itoa_buf;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
372 }
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
373
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
374 char *itoa(int n)
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
375 {
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
376 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
377
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
378 return itoa_buf;
fc9c0503d5e2 Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents: 4
diff changeset
379 }
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
380
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
381 /*
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
382 This might be of use or might not. Unknown yet...
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
383
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
384
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
385 // 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
386 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
387 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
388 off_t bottom = 0, top = 0, 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
389 long size;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
390
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
391 // 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
392
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
393 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
394
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
395 // 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
396 // 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
397 // 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
398
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
399 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
400 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
401
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
402 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
403
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
404 // 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
405
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
406 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==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
407 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
408 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
409
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
410 // 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
411
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
412 } 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
413 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
414 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
415 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
416 } 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
417 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
418 } 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
419
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
420 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
421 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
422
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
423 // 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
424 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
425 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
426 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
427 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
428 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
429
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
430 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
431 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
432 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
433 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
434 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
435
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
436 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
437 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
438
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
439 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
440 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
441 char *buf = readfile(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
442 if (!buf) error_exit("xreadfile %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
443 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
444 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
445
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
446 */
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
447
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
448 // 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
449 // 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
450 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
451 {
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
452 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
453 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
454 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
455
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
456 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
457 // 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
458 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
459 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
460 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
461
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
462 // 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
463 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
464 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
465
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 // 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
467 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
468 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
469 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
470 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
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 // 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
473 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 52
diff changeset
474
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 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
476
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 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
478 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
479 }