Mercurial > hg > toybox
annotate lib/lib.h @ 475:1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 18 Feb 2012 15:12:41 -0600 |
parents | af0cca0aba9d |
children | 7f292460d268 |
rev | line source |
---|---|
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
1 /* vi: set ts=4 :*/ |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
2 /* lib.h - header file for lib directory |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
3 * |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
4 * Copyright 2006 Rob Landley <rob@landley.net> |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
5 */ |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
6 |
417
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
7 // Unfortunately, sizeof() doesn't work in a preprocessor test. TODO. |
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
8 |
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
9 //#if sizeof(double) <= sizeof(long) |
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
10 //typedef double FLOAT; |
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
11 //#else |
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
12 typedef float FLOAT; |
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
13 //#endif |
57e1335e59f6
Commit 415 needs the other two files. (Oops.)
Rob Landley <rob@landley.net>
parents:
397
diff
changeset
|
14 |
35 | 15 // libc generally has this, but the headers are screwed up |
16 ssize_t getline(char **lineptr, size_t *n, FILE *stream); | |
17 | |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
18 // llist.c |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
19 |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
20 struct string_list { |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
21 struct string_list *next; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
22 char str[0]; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
23 }; |
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
24 |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
25 struct arg_list { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
26 struct arg_list *next; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
27 char *arg; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
28 }; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
29 |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
30 struct double_list { |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
31 struct double_list *next; |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
32 struct double_list *prev; |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
33 char *data; |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
34 }; |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
35 |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
36 void llist_free(void *list, void (*freeit)(void *data)); |
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
37 void *llist_pop(void *list); // actually void **list, but the compiler's dumb |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
311
diff
changeset
|
38 struct double_list *dlist_add(struct double_list **list, char *data); |
238
630b2e12db16
Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
39 |
143
9cbb323f297f
Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
137
diff
changeset
|
40 // args.c |
9cbb323f297f
Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
137
diff
changeset
|
41 void get_optflags(void); |
9cbb323f297f
Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
137
diff
changeset
|
42 |
9cbb323f297f
Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
137
diff
changeset
|
43 // dirtree.c |
95
a636e4d20f13
Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents:
75
diff
changeset
|
44 struct dirtree { |
97
4c81e6375719
Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents:
95
diff
changeset
|
45 struct dirtree *next, *child, *parent; |
95
a636e4d20f13
Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents:
75
diff
changeset
|
46 struct stat st; |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
284
diff
changeset
|
47 int depth; |
95
a636e4d20f13
Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents:
75
diff
changeset
|
48 char name[]; |
a636e4d20f13
Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents:
75
diff
changeset
|
49 }; |
a636e4d20f13
Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents:
75
diff
changeset
|
50 |
143
9cbb323f297f
Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
137
diff
changeset
|
51 struct dirtree *dirtree_add_node(char *path); |
9cbb323f297f
Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
137
diff
changeset
|
52 struct dirtree *dirtree_read(char *path, struct dirtree *parent, |
263
7c53152a483b
Make cp pass most of its test suite. Still need to add symlink support.
Rob Landley <rob@landley.net>
parents:
262
diff
changeset
|
53 int (*callback)(char *path, struct dirtree *node)); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
54 |
143
9cbb323f297f
Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
137
diff
changeset
|
55 // lib.c |
167
f16c8e5e9435
Replace strlcpy() with xstrcpy(), which exits if the string won't fit.
Rob Landley <rob@landley.net>
parents:
166
diff
changeset
|
56 void xstrcpy(char *dest, char *src, size_t size); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
57 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
|
58 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
|
59 void perror_msg(char *msg, ...); |
166
5f1d3ad8a888
Add noreturn mark to [p]error_exit(), suggested by Charlie Shepherd.
Rob Landley <rob@landley.net>
parents:
144
diff
changeset
|
60 void error_exit(char *msg, ...) noreturn; |
5f1d3ad8a888
Add noreturn mark to [p]error_exit(), suggested by Charlie Shepherd.
Rob Landley <rob@landley.net>
parents:
144
diff
changeset
|
61 void perror_exit(char *msg, ...) noreturn; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
62 void *xmalloc(size_t size); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
63 void *xzalloc(size_t size); |
115
19b5567f0a1b
Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents:
102
diff
changeset
|
64 void *xrealloc(void *ptr, size_t size); |
369
5715eed39575
Correct return types of xstrdup() and xstrndup()
Rob Landley <rob@landley.net>
parents:
340
diff
changeset
|
65 char *xstrndup(char *s, size_t n); |
5715eed39575
Correct return types of xstrdup() and xstrndup()
Rob Landley <rob@landley.net>
parents:
340
diff
changeset
|
66 char *xstrdup(char *s); |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
67 char *xmsprintf(char *format, ...); |
70
a1b464bbef08
Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
68 void xprintf(char *format, ...); |
128
0a90a5fbc1bf
Add xputs() to detect EOF on writes.
Rob Landley <rob@landley.net>
parents:
124
diff
changeset
|
69 void xputs(char *s); |
70
a1b464bbef08
Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
70 void xputc(char c); |
a1b464bbef08
Add "echo". Has -n and -e (but not \0123 yet).
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
71 void xflush(void); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
72 void xexec(char **argv); |
51 | 73 void xaccess(char *path, int flags); |
214
98820d1eaa79
Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents:
209
diff
changeset
|
74 void xunlink(char *path); |
49
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
35
diff
changeset
|
75 int xcreate(char *path, int flags, int mode); |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
35
diff
changeset
|
76 int xopen(char *path, int flags); |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
77 void xclose(int fd); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
78 FILE *xfopen(char *path, char *mode); |
63
69efffcacd70
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents:
53
diff
changeset
|
79 ssize_t readall(int fd, void *buf, size_t len); |
69efffcacd70
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents:
53
diff
changeset
|
80 ssize_t writeall(int fd, void *buf, size_t len); |
69efffcacd70
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents:
53
diff
changeset
|
81 size_t xread(int fd, void *buf, size_t len); |
69efffcacd70
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents:
53
diff
changeset
|
82 void xreadall(int fd, void *buf, size_t len); |
69efffcacd70
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents:
53
diff
changeset
|
83 void xwrite(int fd, void *buf, size_t len); |
340 | 84 off_t xlseek(int fd, off_t offset, int whence); |
311
962bbf7341f8
Tweak from Roberto Foglietta.
Rob Landley <rob@landley.net>
parents:
308
diff
changeset
|
85 char *readfile(char *name); |
962bbf7341f8
Tweak from Roberto Foglietta.
Rob Landley <rob@landley.net>
parents:
308
diff
changeset
|
86 char *xreadfile(char *name); |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
87 char *xgetcwd(void); |
95
a636e4d20f13
Add xstat(), read_dirtree(), and read_dirtree_node().
Rob Landley <rob@landley.net>
parents:
75
diff
changeset
|
88 void xstat(char *path, 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
|
89 char *xabspath(char *path); |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
284
diff
changeset
|
90 void xchdir(char *path); |
216
5697a3f7c8cf
Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents:
214
diff
changeset
|
91 void xmkpath(char *path, int mode); |
370 | 92 void xsetuid(uid_t uid); |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
93 struct string_list *find_in_path(char *path, char *filename); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
94 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
|
95 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
|
96 char *utoa(unsigned n); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
97 char *itoa(int n); |
102
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
97
diff
changeset
|
98 long atolx(char *c); |
75
89ca591a9236
More random progress on mke2fs. Nothing to see yet.
Rob Landley <rob@landley.net>
parents:
70
diff
changeset
|
99 off_t fdlength(int fd); |
115
19b5567f0a1b
Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents:
102
diff
changeset
|
100 char *xreadlink(char *name); |
397
b7afbc6b753a
Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
101 void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
b7afbc6b753a
Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
102 void (*function)(int fd, char *name)); |
185
29e2051296fd
Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents:
167
diff
changeset
|
103 void loopfiles(char **argv, void (*function)(int fd, char *name)); |
284
603275a05524
Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents:
263
diff
changeset
|
104 char *get_rawline(int fd, long *plen, char end); |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
105 char *get_line(int fd); |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
185
diff
changeset
|
106 void xsendfile(int in, int out); |
209 | 107 int copy_tempfile(int fdin, char *name, char **tempname); |
108 void delete_tempfile(int fdin, int fdout, char **tempname); | |
109 void replace_tempfile(int fdin, int fdout, char **tempname); | |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
334
diff
changeset
|
110 void crc_init(unsigned int *crc_table, int little_endian); |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
417
diff
changeset
|
111 void terminal_size(unsigned *x, unsigned *y); |
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
417
diff
changeset
|
112 int yesno(int def); |
475
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
419
diff
changeset
|
113 int for_each_pid_with_name_in(char **names, |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
419
diff
changeset
|
114 void (*callback) (const char *pid)); |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
417
diff
changeset
|
115 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
116 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
117 // getmountlist.c |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
118 struct mtab_list { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
119 struct mtab_list *next; |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
120 struct stat stat; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
121 struct statvfs statvfs; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
122 char *dir; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
123 char *device; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
124 char type[0]; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
125 }; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
126 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
127 struct mtab_list *getmountlist(int die); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
128 |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
63
diff
changeset
|
129 void bunzipStream(int src_fd, int dst_fd); |