Mercurial > hg > toybox
annotate lib/functions.c @ 2:67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
author | landley@driftwood |
---|---|
date | Thu, 05 Oct 2006 16:18:03 -0400 |
parents | |
children | 266a462ed18c |
rev | line source |
---|---|
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
1 /* vi: set ts=4 :*/ |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
2 /* functions.c - reusable stuff. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
3 * |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
4 * Functions with the x prefix never return failure, they either succeed or |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
5 * kill the program with an error message. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
6 */ |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
7 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
8 #include "toys.h" |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
9 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
10 // Die with an error message. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
11 void error_exit(char *msg, ...) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
12 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
13 va_list args; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
14 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
15 va_start(args, msg); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
16 fprintf(stderr, "%s: ", toys.which->name); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
17 vfprintf(stderr, msg, args); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
18 va_end(args); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
19 exit(toys.exitval); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
20 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
21 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
22 void strlcpy(char *dest, char *src, size_t size) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
23 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
24 strncpy(dest,src,size); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
25 dest[size-1] = 0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
26 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
27 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
28 // Die unless we can allocate memory. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
29 void *xmalloc(size_t size) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
30 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
31 void *ret = malloc(size); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
32 if (!ret) error_exit("xmalloc"); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
33 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
34 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
35 // Die unless we can copy this string. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
36 void *xstrndup(char *s, size_t n) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
37 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
38 void *ret = xmalloc(++n); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
39 strlcpy(ret, s, n); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
40 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
41 return ret; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
42 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
43 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
44 // Die unless we can exec argv[] |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
45 void *xexec(char **argv) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
46 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
47 execvp(argv[0], argv); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
48 error_exit("No %s", argv[0]); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
49 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
50 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
51 // Die unless we can open/create a file, returning file descriptor. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
52 int xopen(char *path, int flags, int mode) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
53 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
54 int fd = open(path, flags, mode); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
55 if (fd == -1) error_exit("No file %s\n", path); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
56 return fd; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
57 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
58 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
59 // Die unless we can open/create a file, returning FILE *. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
60 FILE *xfopen(char *path, char *mode) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
61 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
62 FILE *f = fopen(path, mode); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
63 if (!f) error_exit("No file %s\n", path); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
64 return f; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
65 } |