Mercurial > hg > toybox
annotate lib/lib.c @ 1613:96aa7ec74936 draft
Fix yet another sed bug.
The s/// command would copy the \ of substitutions before deciding what to
do with them (generally overwriting the \ with the new data). When the
substitution was A) at the very end of the new string, B) resolved to nothing,
it could leave a trailing \ that didn't belong there and didn't get overwritten
because the "copy trailing data" part that copies the original string's null
terminator already happened before the \ overwrote it.
The ghostwheel() function restarts regexes after embedded NUL bytes, but
if the string it's passed is _longer_ than the length it's told then it
gets confused (and it means we're off the end of our allocation so segfaults
are likely).
Fix: test for \ first and move the "copy byte" logic into an else case.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 15 Dec 2014 03:34:55 -0600 |
parents | c4c9267467f8 |
children | 45fb262230c9 |
rev | line source |
---|---|
951
62d59b8aea34
Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
949
diff
changeset
|
1 /* lib.c - various reusable stuff. |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
2 * |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
3 * 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
|
4 */ |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
5 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
6 #include "toys.h" |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
7 |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
8 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
|
9 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
10 char *s = ": %s"; |
245
67a0839bda77
Teach perror_exit() to take a NULL argument when we just want "command: error".
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
11 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
12 fprintf(stderr, "%s: ", toys.which->name); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
13 if (msg) vfprintf(stderr, msg, va); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
14 else s+=2; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
15 if (err) fprintf(stderr, s, strerror(err)); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
16 putc('\n', stderr); |
780
6cc69be43c42
Have error_msg() and friends set TT.exitval to 1 if it's still 0, clean out other places that were setting it that no longer need to.
Rob Landley <rob@landley.net>
parents:
762
diff
changeset
|
17 if (!toys.exitval) toys.exitval++; |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
18 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
19 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
20 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
|
21 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
22 va_list va; |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
23 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
24 va_start(va, msg); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
25 verror_msg(msg, 0, va); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
26 va_end(va); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
27 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
28 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
29 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
|
30 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
31 va_list va; |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
32 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
33 va_start(va, msg); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
34 verror_msg(msg, errno, va); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
35 va_end(va); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
36 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
37 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
38 // Die with an error message. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
39 void error_exit(char *msg, ...) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
40 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
41 va_list va; |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
42 |
858
34ac05521d94
Move guts of help command into show_help() in lib/help.c, with config TOYBOX_HELP controlling infrastructure.
Rob Landley <rob@landley.net>
parents:
798
diff
changeset
|
43 if (CFG_TOYBOX_HELP && toys.exithelp) show_help(); |
144
1fbc50374a30
Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents:
143
diff
changeset
|
44 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
45 va_start(va, msg); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
46 verror_msg(msg, 0, va); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
47 va_end(va); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
48 |
925
07693eb46e26
Add xexit() and make error_exit() use it.
Rob Landley <rob@landley.net>
parents:
916
diff
changeset
|
49 xexit(); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
50 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
51 |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
52 // Die with an error message and strerror(errno) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
53 void perror_exit(char *msg, ...) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
54 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
55 va_list va; |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
56 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
57 va_start(va, msg); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
58 verror_msg(msg, errno, va); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
59 va_end(va); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
60 |
925
07693eb46e26
Add xexit() and make error_exit() use it.
Rob Landley <rob@landley.net>
parents:
916
diff
changeset
|
61 xexit(); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
62 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
63 |
63
69efffcacd70
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents:
53
diff
changeset
|
64 // 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
|
65 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
|
66 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
67 size_t count = 0; |
309
79a61cd58596
Bug spotted by Roberto Foglietta: at EOF readall() should return count, not len.
Rob Landley <rob@landley.net>
parents:
308
diff
changeset
|
68 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
69 while (count<len) { |
1141
37cbbbe547a3
Doing math on void pointers isn't portable, reported by Nathan McSween.
Rob Landley <rob@landley.net>
parents:
1131
diff
changeset
|
70 int i = read(fd, (char *)buf+count, len-count); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
71 if (!i) break; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
72 if (i<0) return i; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
73 count += i; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
74 } |
8
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
75 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
76 return count; |
8
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
77 } |
04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
landley@driftwood
parents:
7
diff
changeset
|
78 |
50
63c168b65fa6
Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
79 // 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
|
80 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
|
81 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
82 size_t count = 0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
83 while (count<len) { |
1202
4f080cdb2f6e
Various cleanups found by Tom Sparrow's static analysis.
Rob Landley <rob@landley.net>
parents:
1145
diff
changeset
|
84 int i = write(fd, count+(char *)buf, len-count); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
85 if (i<1) return i; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
86 count += i; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
87 } |
50
63c168b65fa6
Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
88 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
89 return count; |
50
63c168b65fa6
Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
90 } |
63c168b65fa6
Add rewrite(), writeall(),and xwrite() to match the read versions.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
91 |
1070
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
92 // skip this many bytes of input. Return 0 for success, >0 means this much |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
93 // left after input skipped. |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
94 off_t lskip(int fd, off_t offset) |
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
95 { |
1070
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
96 off_t cur = lseek(fd, 0, SEEK_CUR); |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
97 |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
98 if (cur != -1) { |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
99 off_t end = lseek(fd, 0, SEEK_END) - cur; |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
100 |
1070
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
101 if (end > 0 && end < offset) return offset - end; |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
102 end = offset+cur; |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
103 if (end == lseek(fd, end, SEEK_SET)) return 0; |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
104 perror_exit("lseek"); |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
105 } |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
106 |
1070
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
107 while (offset>0) { |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
108 int try = offset>sizeof(libbuf) ? sizeof(libbuf) : offset, or; |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
109 |
1070
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
110 or = readall(fd, libbuf, try); |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
111 if (or < 0) perror_exit("lskip to %lld", (long long)offset); |
1212
86745958cea9
Fix another bug reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
1202
diff
changeset
|
112 else offset -= or; |
1070
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
113 if (or < try) break; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
114 } |
1070
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
115 |
16167a7c1b5a
Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents:
1043
diff
changeset
|
116 return offset; |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
117 } |
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
118 |
1219
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
119 // flags: 1=make last dir (with mode lastmode, otherwise skips last component) |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
120 // 2=make path (already exists is ok) |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
121 // 4=verbose |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
122 // returns 0 = path ok, 1 = error |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
123 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags) |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
124 { |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
125 struct stat buf; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
126 char *s; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
127 |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
128 // mkdir -p one/two/three is not an error if the path already exists, |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
129 // but is if "three" is a file. The others we dereference and catch |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
130 // not-a-directory along the way, but the last one we must explicitly |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
131 // test for. Might as well do it up front. |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
132 |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
133 if (!fstatat(atfd, dir, &buf, 0) && !S_ISDIR(buf.st_mode)) { |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
134 errno = EEXIST; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
135 return 1; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
136 } |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
137 |
1226
e1b09affb6db
Fix mkdir -p with absolute paths.
Rob Landley <rob@landley.net>
parents:
1219
diff
changeset
|
138 for (s = dir; ;s++) { |
1219
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
139 char save = 0; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
140 mode_t mode = (0777&~toys.old_umask)|0300; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
141 |
1226
e1b09affb6db
Fix mkdir -p with absolute paths.
Rob Landley <rob@landley.net>
parents:
1219
diff
changeset
|
142 // find next '/', but don't try to mkdir "" at start of absolute path |
e1b09affb6db
Fix mkdir -p with absolute paths.
Rob Landley <rob@landley.net>
parents:
1219
diff
changeset
|
143 if (*s == '/' && (flags&2) && s != dir) { |
1219
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
144 save = *s; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
145 *s = 0; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
146 } else if (*s) continue; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
147 |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
148 // Use the mode from the -m option only for the last directory. |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
149 if (!save) { |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
150 if (flags&1) mode = lastmode; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
151 else break; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
152 } |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
153 |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
154 if (mkdirat(atfd, dir, mode)) { |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
155 if (!(flags&2) || errno != EEXIST) return 1; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
156 } else if (flags&4) |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
157 fprintf(stderr, "%s: created directory '%s'\n", toys.which->name, dir); |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
158 |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
159 if (!(*s = save)) break; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
160 } |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
161 |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
162 return 0; |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
163 } |
468444e5c7c5
Move mkpathat to lib, remove redundant function used by patch.
Rob Landley <rob@landley.net>
parents:
1218
diff
changeset
|
164 |
708
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
165 // Split a path into linked list of components, tracking head and tail of list. |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
166 // Filters out // entries with no contents. |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
167 struct string_list **splitpath(char *path, struct string_list **list) |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
168 { |
708
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
169 char *new = path; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
170 |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
171 *list = 0; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
172 do { |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
173 int len; |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
174 |
708
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
175 if (*path && *path != '/') continue; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
176 len = path-new; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
177 if (len > 0) { |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
178 *list = xmalloc(sizeof(struct string_list) + len + 1); |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
179 (*list)->next = 0; |
1604
f057223498e4
Remove more strncpy() calls.
Rob Landley <rob@landley.net>
parents:
1535
diff
changeset
|
180 memcpy((*list)->str, new, len); |
708
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
181 (*list)->str[len] = 0; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
182 list = &(*list)->next; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
183 } |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
184 new = path+1; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
185 } while (*path++); |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
186 |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
187 return list; |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
188 } |
50d759f8b371
Remove readlink -m for being poorly defined ("readlink -m /dev/null/and/more" answers what question, exactly?), rewrite xabspath() to work right and not depend on realpath, fix subtle longstanding bug in llist_traverse().
Rob Landley <rob@landley.net>
parents:
707
diff
changeset
|
189 |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
190 // 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
|
191 // 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
|
192 // order. |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
193 |
20
3981c96f9285
Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
16
diff
changeset
|
194 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
|
195 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
196 struct string_list *rlist = NULL, **prlist=&rlist; |
1360
46f068aaa480
find_in_path() is supposed to work with a NULL path, but didn't. Fix it.
Rob Landley <rob@landley.net>
parents:
1345
diff
changeset
|
197 char *cwd; |
46f068aaa480
find_in_path() is supposed to work with a NULL path, but didn't. Fix it.
Rob Landley <rob@landley.net>
parents:
1345
diff
changeset
|
198 |
46f068aaa480
find_in_path() is supposed to work with a NULL path, but didn't. Fix it.
Rob Landley <rob@landley.net>
parents:
1345
diff
changeset
|
199 if (!path) return 0; |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
200 |
1360
46f068aaa480
find_in_path() is supposed to work with a NULL path, but didn't. Fix it.
Rob Landley <rob@landley.net>
parents:
1345
diff
changeset
|
201 cwd = xgetcwd(); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
202 for (;;) { |
1360
46f068aaa480
find_in_path() is supposed to work with a NULL path, but didn't. Fix it.
Rob Landley <rob@landley.net>
parents:
1345
diff
changeset
|
203 char *next = strchr(path, ':'); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
204 int len = next ? next-path : strlen(path); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
205 struct string_list *rnext; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
206 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
|
207 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
208 rnext = xmalloc(sizeof(void *) + strlen(filename) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
209 + (len ? len : strlen(cwd)) + 2); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
210 if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
211 else { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
212 char *res = rnext->str; |
1604
f057223498e4
Remove more strncpy() calls.
Rob Landley <rob@landley.net>
parents:
1535
diff
changeset
|
213 |
f057223498e4
Remove more strncpy() calls.
Rob Landley <rob@landley.net>
parents:
1535
diff
changeset
|
214 memcpy(res, path, len); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
215 res += len; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
216 *(res++) = '/'; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
217 strcpy(res, filename); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
218 } |
16
dd10785b6532
Add xabspath(), is_file_type(), which_in_path(), and find_in_path().
Rob Landley <rob@landley.net>
parents:
8
diff
changeset
|
219 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
220 // Confirm it's not a directory. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
221 if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
222 *prlist = rnext; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
223 rnext->next = NULL; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
224 prlist = &(rnext->next); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
225 } 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
|
226 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
227 if (!next) break; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
228 path += len; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
229 path++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
230 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
231 free(cwd); |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
232 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
233 return rlist; |
4
732b055e17f7
Add xmsprintf(), xgetcwd(), xgetcwd(), find_in_path().
landley@driftwood
parents:
3
diff
changeset
|
234 } |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
4
diff
changeset
|
235 |
102
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
97
diff
changeset
|
236 // atol() with the kilo/mega/giga/tera/peta/exa extensions. |
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
97
diff
changeset
|
237 // (zetta and yotta don't fit in 64 bits.) |
443
41b5ac08208f
Make atolx() error_exit() if fed a string that doesn't convert entirely into an integer.
Rob Landley <rob@landley.net>
parents:
419
diff
changeset
|
238 long atolx(char *numstr) |
102
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
97
diff
changeset
|
239 { |
1394
9dbfd5390b92
find needs "c" suffix to -size.
Rob Landley <rob@landley.net>
parents:
1360
diff
changeset
|
240 char *c, *suffixes="cbkmgtpe", *end; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
241 long val = strtol(numstr, &c, 0); |
102
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
97
diff
changeset
|
242 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
243 if (*c) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
244 if (c != numstr && (end = strchr(suffixes, tolower(*c)))) { |
1394
9dbfd5390b92
find needs "c" suffix to -size.
Rob Landley <rob@landley.net>
parents:
1360
diff
changeset
|
245 int shift = end-suffixes-2; |
9dbfd5390b92
find needs "c" suffix to -size.
Rob Landley <rob@landley.net>
parents:
1360
diff
changeset
|
246 if (shift >= 0) val *= 1024L<<(shift*10); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
247 } else { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
248 while (isspace(*c)) c++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
249 if (*c) error_exit("not integer: %s", numstr); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
250 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
251 } |
397
b7afbc6b753a
Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents:
379
diff
changeset
|
252 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
253 return val; |
102
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
97
diff
changeset
|
254 } |
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
97
diff
changeset
|
255 |
1131
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
256 long atolx_range(char *numstr, long low, long high) |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
257 { |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
258 long val = atolx(numstr); |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
259 |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
260 if (val < low) error_exit("%ld < %ld", val, low); |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
261 if (val > high) error_exit("%ld > %ld", val, high); |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
262 |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
263 return val; |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
264 } |
f9678ea553c8
Oops, cleaned up ifconfig uses atolx_range() instead of get_int_list(). Check that in.
Rob Landley <rob@landley.net>
parents:
1108
diff
changeset
|
265 |
565
44abf4d901f3
Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents:
554
diff
changeset
|
266 int numlen(long l) |
44abf4d901f3
Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents:
554
diff
changeset
|
267 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
268 int len = 0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
269 while (l) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
270 l /= 10; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
271 len++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
272 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
273 return len; |
565
44abf4d901f3
Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents:
554
diff
changeset
|
274 } |
44abf4d901f3
Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents:
554
diff
changeset
|
275 |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
276 int stridx(char *haystack, char needle) |
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
277 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
278 char *off; |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
279 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
280 if (!needle) return -1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
281 off = strchr(haystack, needle); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
282 if (!off) return -1; |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
283 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
284 return off-haystack; |
623
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
285 } |
f51beec92738
New infrastructure for od (oops).
Rob Landley <rob@landley.net>
parents:
615
diff
changeset
|
286 |
1525
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
287 int unescape(char c) |
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
288 { |
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
289 char *from = "\\abefnrtv", *to = "\\\a\b\033\f\n\r\t\v"; |
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
290 int idx = stridx(from, c); |
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
291 |
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
292 return (idx == -1) ? 0 : to[idx]; |
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
293 } |
95cb37adb024
Factor out printf-style escape parsing logic from echo.c.
Rob Landley <rob@landley.net>
parents:
1523
diff
changeset
|
294 |
1345
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
295 // If *a starts with b, advance *a past it and return 1, else return 0; |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
296 int strstart(char **a, char *b) |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
297 { |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
298 int len = strlen(b), i = !strncmp(*a, b, len); |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
299 |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
300 if (i) *a += len; |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
301 |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
302 return i; |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
303 } |
b7598d21ca10
Forgot to check in strstart().
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
304 |
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
|
305 // 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
|
306 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
|
307 { |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
308 struct stat st; |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
309 off_t base = 0, range = 1, expand = 1, old; |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
310 |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
311 if (!fstat(fd, &st) && S_ISREG(st.st_mode)) return st.st_size; |
53
41d55b5d49fd
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
52
diff
changeset
|
312 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
313 // If the ioctl works for this, return it. |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
314 // TODO: is blocksize still always 512, or do we stat for it? |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
315 // unsigned int size; |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
316 // if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
53
41d55b5d49fd
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
52
diff
changeset
|
317 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
318 // If not, do a binary search for the last location we can read. (Some |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
319 // block devices don't do BLKGETSIZE right.) This should probably have |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
320 // a CONFIG option... |
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
|
321 |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
322 // If not, do a binary search for the last location we can read. |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
323 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
324 old = lseek(fd, 0, SEEK_CUR); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
325 do { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
326 char temp; |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
327 off_t pos = base + range / 2; |
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
|
328 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
329 if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) { |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
330 off_t delta = (pos + 1) - base; |
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
|
331 |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
332 base += delta; |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
333 if (expand) range = (expand <<= 1) - base; |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
334 else range -= delta; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
335 } else { |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
336 expand = 0; |
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
337 range = pos - base; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
338 } |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
339 } while (range > 0); |
53
41d55b5d49fd
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
52
diff
changeset
|
340 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
341 lseek(fd, old, SEEK_SET); |
310
8b8116214b1c
Roberto Foglietta pointed out that readall() needs fdlength() to restore
Rob Landley <rob@landley.net>
parents:
309
diff
changeset
|
342 |
992
910f35ff76be
Achille Fouilleul pointed out that fdlength wasn't returning the right length in the binary search case.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
343 return base; |
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
|
344 } |
41d55b5d49fd
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
52
diff
changeset
|
345 |
1043
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
346 // Read contents of file as a single nul-terminated string. |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
347 // malloc new one if buf=len=0 |
1273
79e847fec774
In function readfile(), the buffer buf is free'd when readall() fails. This free can cause a crash, if the buffer passed by user of function is not malloc'ed one.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1226
diff
changeset
|
348 char *readfile(char *name, char *ibuf, off_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
|
349 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
350 int fd; |
1273
79e847fec774
In function readfile(), the buffer buf is free'd when readall() fails. This free can cause a crash, if the buffer passed by user of function is not malloc'ed one.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1226
diff
changeset
|
351 char *buf; |
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
|
352 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
353 fd = open(name, O_RDONLY); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
354 if (fd == -1) return 0; |
1043
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
355 |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
356 if (len<1) { |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
357 len = fdlength(fd); |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
358 // proc files don't report a length, so try 1 page minimum. |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
359 if (len<4096) len = 4096; |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
360 } |
1273
79e847fec774
In function readfile(), the buffer buf is free'd when readall() fails. This free can cause a crash, if the buffer passed by user of function is not malloc'ed one.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1226
diff
changeset
|
361 if (!ibuf) buf = xmalloc(len+1); |
79e847fec774
In function readfile(), the buffer buf is free'd when readall() fails. This free can cause a crash, if the buffer passed by user of function is not malloc'ed one.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1226
diff
changeset
|
362 else buf = ibuf; |
1043
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
363 |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
364 len = readall(fd, buf, len-1); |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
365 close(fd); |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
366 if (len<0) { |
1273
79e847fec774
In function readfile(), the buffer buf is free'd when readall() fails. This free can cause a crash, if the buffer passed by user of function is not malloc'ed one.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1226
diff
changeset
|
367 if (ibuf != buf) free(buf); |
1043
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
368 buf = 0; |
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1042
diff
changeset
|
369 } else buf[len] = 0; |
53
41d55b5d49fd
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
52
diff
changeset
|
370 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
371 return buf; |
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
|
372 } |
41d55b5d49fd
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
52
diff
changeset
|
373 |
883
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
374 // Sleep for this many thousandths of a second |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
375 void msleep(long miliseconds) |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
376 { |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
377 struct timespec ts; |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
378 |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
379 ts.tv_sec = miliseconds/1000; |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
380 ts.tv_nsec = (miliseconds%1000)*1000000; |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
381 nanosleep(&ts, &ts); |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
382 } |
aca8323e2690
Add posix headers to toynet.h, move xioctl() to lib.c, introduce lib/net.c and move xsocket() to it.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
383 |
1399
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
384 // Inefficient, but deals with unaligned access |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
385 int64_t peek_le(void *ptr, unsigned size) |
913
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
386 { |
1399
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
387 int64_t ret = 0; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
388 char *c = ptr; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
389 int i; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
390 |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
391 for (i=0; i<size; i++) ret |= ((int64_t)c[i])<<i; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
392 |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
393 return ret; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
394 } |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
395 |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
396 int64_t peek_be(void *ptr, unsigned size) |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
397 { |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
398 int64_t ret = 0; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
399 char *c = ptr; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
400 |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
401 while (size--) ret = (ret<<8)|c[size]; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
402 |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
403 return ret; |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
404 } |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
405 |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
406 int64_t peek(void *ptr, unsigned size) |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
407 { |
a0c328bc2c14
Little endian and big endian versions of peek (for host.c).
Rob Landley <rob@landley.net>
parents:
1394
diff
changeset
|
408 return IS_BIG_ENDIAN ? peek_be(ptr, size) : peek_le(ptr, size); |
913
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
409 } |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
410 |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
411 void poke(void *ptr, uint64_t val, int size) |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
412 { |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
413 if (size & 8) { |
1218
fba5d8d3905c
Add "volatile" annotation to peek/poke to stop potential optimizer overreach.
Rob Landley <rob@landley.net>
parents:
1212
diff
changeset
|
414 volatile uint64_t *p = (uint64_t *)ptr; |
913
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
415 *p = val; |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
416 } else if (size & 4) { |
1218
fba5d8d3905c
Add "volatile" annotation to peek/poke to stop potential optimizer overreach.
Rob Landley <rob@landley.net>
parents:
1212
diff
changeset
|
417 volatile int *p = (int *)ptr; |
913
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
418 *p = val; |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
419 } else if (size & 2) { |
1218
fba5d8d3905c
Add "volatile" annotation to peek/poke to stop potential optimizer overreach.
Rob Landley <rob@landley.net>
parents:
1212
diff
changeset
|
420 volatile short *p = (short *)ptr; |
913
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
421 *p = val; |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
422 } else { |
1218
fba5d8d3905c
Add "volatile" annotation to peek/poke to stop potential optimizer overreach.
Rob Landley <rob@landley.net>
parents:
1212
diff
changeset
|
423 volatile char *p = (char *)ptr; |
913
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
424 *p = val; |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
425 } |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
426 } |
7fed25030389
Enable readfile() and add peek() and poke() functions.
Rob Landley <rob@landley.net>
parents:
904
diff
changeset
|
427 |
308 | 428 // Iterate through an array of files, opening each one and calling a function |
429 // on that filehandle and name. The special filename "-" means stdin if | |
430 // flags is O_RDONLY, stdout otherwise. An empty argument list calls | |
431 // function() on just stdin/stdout. | |
432 // | |
1523
e5b52720f539
Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
Rob Landley <rob@landley.net>
parents:
1436
diff
changeset
|
433 // Note: pass O_CLOEXEC to automatically close filehandles when function() |
e5b52720f539
Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
Rob Landley <rob@landley.net>
parents:
1436
diff
changeset
|
434 // returns, otherwise filehandles must be closed by function() |
397
b7afbc6b753a
Forgot to check in loopfiles_rw changes needed by truncate.
Rob Landley <rob@landley.net>
parents:
379
diff
changeset
|
435 void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
436 void (*function)(int fd, char *name)) |
185
29e2051296fd
Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents:
167
diff
changeset
|
437 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
438 int fd; |
185
29e2051296fd
Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents:
167
diff
changeset
|
439 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
440 // If no arguments, read from stdin. |
1535
b50de98c9708
O_CLOEXEC was confusing the O_RDONLY test in loopfiles(), resulting in attempts to read from stdout instead of stdin for "-" or no arguments.
Rob Landley <rob@landley.net>
parents:
1525
diff
changeset
|
441 if (!*argv) function((flags & O_ACCMODE) != O_RDONLY ? 1 : 0, "-"); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
442 else do { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
443 // Filename "-" means read from stdin. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
444 // Inability to open a file prints a warning, but doesn't exit. |
185
29e2051296fd
Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents:
167
diff
changeset
|
445 |
1535
b50de98c9708
O_CLOEXEC was confusing the O_RDONLY test in loopfiles(), resulting in attempts to read from stdout instead of stdin for "-" or no arguments.
Rob Landley <rob@landley.net>
parents:
1525
diff
changeset
|
446 if (!strcmp(*argv, "-")) fd=0; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
447 else if (0>(fd = open(*argv, flags, permissions)) && !failok) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
448 perror_msg("%s", *argv); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
449 toys.exitval = 1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
450 continue; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
451 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
452 function(fd, *argv); |
1523
e5b52720f539
Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
Rob Landley <rob@landley.net>
parents:
1436
diff
changeset
|
453 if (flags & O_CLOEXEC) close(fd); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
454 } while (*++argv); |
185
29e2051296fd
Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents:
167
diff
changeset
|
455 } |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
456 |
1523
e5b52720f539
Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
Rob Landley <rob@landley.net>
parents:
1436
diff
changeset
|
457 // Call loopfiles_rw with O_RDONLY|O_CLOEXEC and !failok (common case). |
308 | 458 void loopfiles(char **argv, void (*function)(int fd, char *name)) |
459 { | |
1523
e5b52720f539
Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
Rob Landley <rob@landley.net>
parents:
1436
diff
changeset
|
460 loopfiles_rw(argv, O_RDONLY|O_CLOEXEC, 0, 0, function); |
308 | 461 } |
462 | |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
463 // Slow, but small. |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
464 |
284
603275a05524
Teach get_rawline() to continue until a configurable char, and xstrndup()
Rob Landley <rob@landley.net>
parents:
252
diff
changeset
|
465 char *get_rawline(int fd, long *plen, char end) |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
466 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
467 char c, *buf = NULL; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
468 long len = 0; |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
469 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
470 for (;;) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
471 if (1>read(fd, &c, 1)) break; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
472 if (!(len & 63)) buf=xrealloc(buf, len+65); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
473 if ((buf[len++]=c) == end) break; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
474 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
475 if (buf) buf[len]=0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
476 if (plen) *plen = len; |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
477 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
478 return buf; |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
479 } |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
480 |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
481 char *get_line(int fd) |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
482 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
483 long len; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
484 char *buf = get_rawline(fd, &len, '\n'); |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
485 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
486 if (buf && buf[--len]=='\n') buf[len]=0; |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
487 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
488 return buf; |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
489 } |
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
490 |
590
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
585
diff
changeset
|
491 int wfchmodat(int fd, char *name, mode_t mode) |
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
585
diff
changeset
|
492 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
493 int rc = fchmodat(fd, name, mode, 0); |
590
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
585
diff
changeset
|
494 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
495 if (rc) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
496 perror_msg("chmod '%s' to %04o", name, mode); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
497 toys.exitval=1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
498 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
499 return rc; |
590
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
585
diff
changeset
|
500 } |
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
585
diff
changeset
|
501 |
643
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
502 static char *tempfile2zap; |
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
503 static void tempfile_handler(int i) |
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
504 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
505 if (1 < (long)tempfile2zap) unlink(tempfile2zap); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
506 _exit(1); |
643
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
507 } |
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
508 |
209 | 509 // Open a temporary file to copy an existing file into. |
510 int copy_tempfile(int fdin, char *name, char **tempname) | |
511 { | |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
512 struct stat statbuf; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
513 int fd; |
209 | 514 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
515 *tempname = xstrndup(name, strlen(name)+6); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
516 strcat(*tempname,"XXXXXX"); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
517 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file"); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
518 if (!tempfile2zap) sigatexit(tempfile_handler); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
519 tempfile2zap = *tempname; |
209 | 520 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
521 // Set permissions of output file |
209 | 522 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
523 fstat(fdin, &statbuf); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
524 fchmod(fd, statbuf.st_mode); |
209 | 525 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
526 return fd; |
201
5d523752715a
Start of "patch" support. Writes to stdout at the moment.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
527 } |
209 | 528 |
529 // Abort the copy and delete the temporary file. | |
530 void delete_tempfile(int fdin, int fdout, char **tempname) | |
531 { | |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
532 close(fdin); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
533 close(fdout); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
534 unlink(*tempname); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
535 tempfile2zap = (char *)1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
536 free(*tempname); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
537 *tempname = NULL; |
209 | 538 } |
539 | |
540 // Copy the rest of the data and replace the original with the copy. | |
541 void replace_tempfile(int fdin, int fdout, char **tempname) | |
542 { | |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
543 char *temp = xstrdup(*tempname); |
209 | 544 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
545 temp[strlen(temp)-6]=0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
546 if (fdin != -1) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
547 xsendfile(fdin, fdout); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
548 xclose(fdin); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
549 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
550 xclose(fdout); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
551 rename(*tempname, temp); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
552 tempfile2zap = (char *)1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
553 free(*tempname); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
554 free(temp); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
555 *tempname = NULL; |
209 | 556 } |
334
83c461db9df7
Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents:
311
diff
changeset
|
557 |
83c461db9df7
Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents:
311
diff
changeset
|
558 // Create a 256 entry CRC32 lookup table. |
83c461db9df7
Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents:
311
diff
changeset
|
559 |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
334
diff
changeset
|
560 void crc_init(unsigned int *crc_table, int little_endian) |
334
83c461db9df7
Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents:
311
diff
changeset
|
561 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
562 unsigned int i; |
334
83c461db9df7
Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents:
311
diff
changeset
|
563 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
564 // Init the CRC32 table (big endian) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
565 for (i=0; i<256; i++) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
566 unsigned int j, c = little_endian ? i : i<<24; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
567 for (j=8; j; j--) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
568 if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
569 else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
570 crc_table[i] = c; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
571 } |
334
83c461db9df7
Check in crc_init needed by cksum. (Oops.)
Rob Landley <rob@landley.net>
parents:
311
diff
changeset
|
572 } |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
573 |
1605 | 574 // Init base64 table |
575 | |
576 void base64_init(char *p) | |
577 { | |
578 int i; | |
579 | |
580 for (i = 'A'; i != ':'; i++) { | |
581 if (i == 'Z'+1) i = 'a'; | |
582 if (i == 'z'+1) i = '0'; | |
583 *(p++) = i; | |
584 } | |
585 *(p++) = '+'; | |
586 *(p++) = '/'; | |
587 } | |
588 | |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
589 // Quick and dirty query size of terminal, doesn't do ANSI probe fallback. |
1108
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
590 // set x=80 y=25 before calling to provide defaults. Returns 0 if couldn't |
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
591 // determine size. |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
592 |
1108
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
593 int terminal_size(unsigned *xx, unsigned *yy) |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
594 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
595 struct winsize ws; |
1108
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
596 unsigned i, x = 0, y = 0; |
1097 | 597 char *s; |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
598 |
1108
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
599 // stdin, stdout, stderr |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
600 for (i=0; i<3; i++) { |
1097 | 601 memset(&ws, 0, sizeof(ws)); |
602 if (!ioctl(i, TIOCGWINSZ, &ws)) { | |
603 if (ws.ws_col) x = ws.ws_col; | |
604 if (ws.ws_row) y = ws.ws_row; | |
605 | |
606 break; | |
607 } | |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
608 } |
1097 | 609 s = getenv("COLUMNS"); |
610 if (s) sscanf(s, "%u", &x); | |
611 s = getenv("ROWS"); | |
612 if (s) sscanf(s, "%u", &y); | |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
613 |
1108
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
614 // Never return 0 for either value, leave it at default instead. |
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
615 if (xx && x) *xx = x; |
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
616 if (yy && y) *yy = y; |
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
617 |
fb8467436e6a
Tweak terminal_size to never set either to 0, and return true/false whether it could determine at least one coordinate.
Rob Landley <rob@landley.net>
parents:
1097
diff
changeset
|
618 return x || y; |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
619 } |
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
620 |
503
3b9dea897dc0
Upgrade yesno() and make cp -i use it.
Rob Landley <rob@landley.net>
parents:
498
diff
changeset
|
621 int yesno(char *prompt, int def) |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
622 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
623 char buf; |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
624 |
738
075eaff297f8
Make yesno() always read from stdin and write to stderr. (If we need to find our tty, open /dev/tty, but existing users don't.)
Rob Landley <rob@landley.net>
parents:
715
diff
changeset
|
625 fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); |
075eaff297f8
Make yesno() always read from stdin and write to stderr. (If we need to find our tty, open /dev/tty, but existing users don't.)
Rob Landley <rob@landley.net>
parents:
715
diff
changeset
|
626 fflush(stderr); |
075eaff297f8
Make yesno() always read from stdin and write to stderr. (If we need to find our tty, open /dev/tty, but existing users don't.)
Rob Landley <rob@landley.net>
parents:
715
diff
changeset
|
627 while (fread(&buf, 1, 1, stdin)) { |
075eaff297f8
Make yesno() always read from stdin and write to stderr. (If we need to find our tty, open /dev/tty, but existing users don't.)
Rob Landley <rob@landley.net>
parents:
715
diff
changeset
|
628 int new; |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
629 |
798
16bcabb8cf97
Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents:
791
diff
changeset
|
630 // The letter changes the value, the newline (or space) returns it. |
738
075eaff297f8
Make yesno() always read from stdin and write to stderr. (If we need to find our tty, open /dev/tty, but existing users don't.)
Rob Landley <rob@landley.net>
parents:
715
diff
changeset
|
631 if (isspace(buf)) break; |
075eaff297f8
Make yesno() always read from stdin and write to stderr. (If we need to find our tty, open /dev/tty, but existing users don't.)
Rob Landley <rob@landley.net>
parents:
715
diff
changeset
|
632 if (-1 != (new = stridx("ny", tolower(buf)))) def = new; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
633 } |
550
b2194045c40e
Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
634 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
635 return def; |
419
af0cca0aba9d
Quick and dirty terminal_size() and yesno() functions, both of which need to be improved.
Rob Landley <rob@landley.net>
parents:
399
diff
changeset
|
636 } |
475
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
443
diff
changeset
|
637 |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
638 struct signame { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
639 int num; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
640 char *name; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
641 }; |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
642 |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
643 // Signals required by POSIX 2008: |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
644 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
645 |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
646 #define SIGNIFY(x) {SIG##x, #x} |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
647 |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
648 static struct signame signames[] = { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
649 SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
650 SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL), |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
651 SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM), |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
652 SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP), |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
653 SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ), |
643
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
654 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
655 // Start of non-terminal signals |
643
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
656 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
657 SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP), |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
658 SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG) |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
659 }; |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
660 |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
661 // not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR) |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
662 // obsolete: SIGNIFY(PROF) SIGNIFY(POLL) |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
663 |
1299
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
664 // Handler that sets toys.signal, and writes to toys.signalfd if set |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
665 void generic_signal(int sig) |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
666 { |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
667 if (toys.signalfd) { |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
668 char c = sig; |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
669 |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
670 writeall(toys.signalfd, &c, 1); |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
671 } |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
672 toys.signal = sig; |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
673 } |
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
674 |
643
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
675 // Install the same handler on every signal that defaults to killing the process |
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
676 void sigatexit(void *handler) |
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
677 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
678 int i; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
679 for (i=0; signames[i].num != SIGCHLD; i++) signal(signames[i].num, handler); |
643
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
680 } |
1299
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1276
diff
changeset
|
681 |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
682 // Convert name to signal number. If name == NULL print names. |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
683 int sig_to_num(char *pidstr) |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
684 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
685 int i; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
686 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
687 if (pidstr) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
688 char *s; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
689 i = strtol(pidstr, &s, 10); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
690 if (!*s) return i; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
691 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
692 if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
693 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
694 for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
695 if (!pidstr) xputs(signames[i].name); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
696 else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
697 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
698 return -1; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
699 } |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
700 |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
701 char *num_to_sig(int sig) |
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
702 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
703 int i; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
704 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
705 for (i=0; i<sizeof(signames)/sizeof(struct signame); i++) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
706 if (signames[i].num == sig) return signames[i].name; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
707 return NULL; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
480
diff
changeset
|
708 } |
551
2548e6e590b2
Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents:
550
diff
changeset
|
709 |
643
7bdebd2af1d6
Add signal handler to clean up tempfile.
Rob Landley <rob@landley.net>
parents:
640
diff
changeset
|
710 // premute mode bits based on posix mode strings. |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
711 mode_t string_to_mode(char *modestr, mode_t mode) |
551
2548e6e590b2
Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents:
550
diff
changeset
|
712 { |
1436
d7be3d62a5cb
If string_to_mode() is called on a base mode with S_ISDIR() and such set, pass those extra bits through.
Rob Landley <rob@landley.net>
parents:
1399
diff
changeset
|
713 char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu", |
d7be3d62a5cb
If string_to_mode() is called on a base mode with S_ISDIR() and such set, pass those extra bits through.
Rob Landley <rob@landley.net>
parents:
1399
diff
changeset
|
714 *s, *str = modestr; |
d7be3d62a5cb
If string_to_mode() is called on a base mode with S_ISDIR() and such set, pass those extra bits through.
Rob Landley <rob@landley.net>
parents:
1399
diff
changeset
|
715 mode_t extrabits = mode & ~(07777); |
553 | 716 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
717 // Handle octal mode |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
718 if (isdigit(*str)) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
719 mode = strtol(str, &s, 8); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
720 if (*s || (mode & ~(07777))) goto barf; |
553 | 721 |
1436
d7be3d62a5cb
If string_to_mode() is called on a base mode with S_ISDIR() and such set, pass those extra bits through.
Rob Landley <rob@landley.net>
parents:
1399
diff
changeset
|
722 return mode | extrabits; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
723 } |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
724 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
725 // Gaze into the bin of permission... |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
726 for (;;) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
727 int i, j, dowho, dohow, dowhat, amask; |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
728 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
729 dowho = dohow = dowhat = amask = 0; |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
730 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
731 // Find the who, how, and what stanzas, in that order |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
732 while (*str && (s = strchr(whos, *str))) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
733 dowho |= 1<<(s-whos); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
734 str++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
735 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
736 // If who isn't specified, like "a" but honoring umask. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
737 if (!dowho) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
738 dowho = 8; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
739 umask(amask=umask(0)); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
740 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
741 if (!*str || !(s = strchr(hows, *str))) goto barf; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
742 dohow = *(str++); |
590
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
585
diff
changeset
|
743 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
744 if (!dohow) goto barf; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
745 while (*str && (s = strchr(whats, *str))) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
746 dowhat |= 1<<(s-whats); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
747 str++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
748 } |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
749 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
750 // Convert X to x for directory or if already executable somewhere |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
751 if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1; |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
752 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
753 // Copy mode from another category? |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
754 if (!dowhat && *str && (s = strchr(whys, *str))) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
755 dowhat = (mode>>(3*(s-whys)))&7; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
756 str++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
757 } |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
758 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
759 // Are we ready to do a thing yet? |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
760 if (*str && *(str++) != ',') goto barf; |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
761 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
762 // Ok, apply the bits to the mode. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
763 for (i=0; i<4; i++) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
764 for (j=0; j<3; j++) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
765 mode_t bit = 0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
766 int where = 1<<((3*i)+j); |
638
92200901cfe1
Make chmod +w respect umask, implement +s and +t, fix ls to show suid/sgid/stid without x bit.
Rob Landley <rob@landley.net>
parents:
623
diff
changeset
|
767 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
768 if (amask & where) continue; |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
769 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
770 // Figure out new value at this location |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
771 if (i == 3) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
772 // suid/sticky bit. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
773 if (j) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
774 if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
775 } else if (dowhat & 16) bit++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
776 } else { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
777 if (!(dowho&(8|(1<<i)))) continue; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
778 if (dowhat&(1<<j)) bit++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
779 } |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
780 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
781 // When selection active, modify bit |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
782 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
783 if (dohow == '=' || (bit && dohow == '-')) mode &= ~where; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
784 if (bit && dohow != '-') mode |= where; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
785 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
786 } |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
787 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
788 if (!*str) break; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
789 } |
1436
d7be3d62a5cb
If string_to_mode() is called on a base mode with S_ISDIR() and such set, pass those extra bits through.
Rob Landley <rob@landley.net>
parents:
1399
diff
changeset
|
790 |
d7be3d62a5cb
If string_to_mode() is called on a base mode with S_ISDIR() and such set, pass those extra bits through.
Rob Landley <rob@landley.net>
parents:
1399
diff
changeset
|
791 return mode|extrabits; |
578
5cc8a8fc08b4
First pass at a complete rewrite of string_to_mode(). (It compiled!)
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
792 barf: |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
671
diff
changeset
|
793 error_exit("bad mode '%s'", modestr); |
551
2548e6e590b2
Add string to mode_t parser
Daniel Walter <d.walter@0x90.at>
parents:
550
diff
changeset
|
794 } |
658 | 795 |
916 | 796 // Format access mode into a drwxrwxrwx string |
797 void mode_to_string(mode_t mode, char *buf) | |
885
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
798 { |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
799 char c, d; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
800 int i, bit; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
801 |
916 | 802 buf[10]=0; |
885
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
803 for (i=0; i<9; i++) { |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
804 bit = mode & (1<<i); |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
805 c = i%3; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
806 if (!c && (mode & (1<<((d=i/3)+9)))) { |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
807 c = "tss"[d]; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
808 if (!bit) c &= ~0x20; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
809 } else c = bit ? "xwr"[c] : '-'; |
916 | 810 buf[9-i] = c; |
885
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
811 } |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
812 |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
813 if (S_ISDIR(mode)) c = 'd'; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
814 else if (S_ISBLK(mode)) c = 'b'; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
815 else if (S_ISCHR(mode)) c = 'c'; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
816 else if (S_ISLNK(mode)) c = 'l'; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
817 else if (S_ISFIFO(mode)) c = 'p'; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
818 else if (S_ISSOCK(mode)) c = 's'; |
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
819 else c = '-'; |
916 | 820 *buf = c; |
885
beb32d780164
Add library function for the file permission formatting in ls and stat
Felix Janda <felix.janda@posteo.de>
parents:
883
diff
changeset
|
821 } |
1145
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
822 |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
823 // Execute a callback for each PID that matches a process name from a list. |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
824 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)) |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
825 { |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
826 DIR *dp; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
827 struct dirent *entry; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
828 |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
829 if (!(dp = opendir("/proc"))) perror_exit("opendir"); |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
830 |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
831 while ((entry = readdir(dp))) { |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
832 unsigned u; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
833 char *cmd, **curname; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
834 |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
835 if (!(u = atoi(entry->d_name))) continue; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
836 sprintf(libbuf, "/proc/%u/cmdline", u); |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
837 if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
838 |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
839 for (curname = names; *curname; curname++) |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
840 if (**curname == '/' ? !strcmp(cmd, *curname) |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
841 : !strcmp(basename(cmd), basename(*curname))) |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
842 if (callback(u, *curname)) break; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
843 if (*curname) break; |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
844 } |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
845 closedir(dp); |
80c9df5145fe
Move names_to_pid from pending to lib.
Rob Landley <rob@landley.net>
parents:
1141
diff
changeset
|
846 } |
1276
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
847 |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
848 // display first few digits of number with power of two units, except we're |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
849 // actually just counting decimal digits and showing mil/bil/trillions. |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
850 int human_readable(char *buf, unsigned long long num) |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
851 { |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
852 int end, len; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
853 |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
854 len = sprintf(buf, "%lld", num); |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
855 end = ((len-1)%3)+1; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
856 len /= 3; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
857 |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
858 if (len && end == 1) { |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
859 buf[2] = buf[1]; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
860 buf[1] = '.'; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
861 end = 3; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
862 } |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
863 buf[end++] = ' '; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
864 if (len) buf[end++] = " KMGTPE"[len]; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
865 buf[end++] = 'B'; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
866 buf[end++] = 0; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
867 |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
868 return end; |
d48bdc1cb017
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents:
1273
diff
changeset
|
869 } |