diff options
author | Rob Landley <rob@landley.net> | 2007-01-14 20:20:06 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2007-01-14 20:20:06 -0500 |
commit | 055cfcbe5b0534c700b30216f54336b7581f7be4 (patch) | |
tree | 8c62c700db81dc09b8d9f7c79c504651dff3acd2 /lib | |
parent | 3388f4c4de628702ba77e2fec941c809877ad576 (diff) | |
download | toybox-055cfcbe5b0534c700b30216f54336b7581f7be4.tar.gz |
Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/functions.c | 123 | ||||
-rw-r--r-- | lib/lib.h | 4 | ||||
-rw-r--r-- | lib/portability.h | 21 |
3 files changed, 143 insertions, 5 deletions
diff --git a/lib/functions.c b/lib/functions.c index ef787617..98a344a2 100644 --- a/lib/functions.c +++ b/lib/functions.c @@ -49,6 +49,7 @@ void error_exit(char *msg, ...) exit(toys.exitval); } + // Die with an error message and strerror(errno) void perror_exit(char *msg, ...) { @@ -61,6 +62,12 @@ void perror_exit(char *msg, ...) exit(toys.exitval); } +// Stub until the online help system goes in. +void usage_exit(void) +{ + exit(1); +} + // Like strncpy but always null terminated. void strlcpy(char *dest, char *src, size_t size) { @@ -213,11 +220,19 @@ ssize_t writeall(int fd, void *buf, size_t count) return count; } -// Die if we can't fill a buffer -void xread(int fd, void *buf, size_t count) +// Die if there's an error other than EOF. +size_t xread(int fd, void *buf, size_t count) { - if (count != readall(fd, buf, count)) perror_exit("xread"); -} + count = reread(fd, buf, count); + if (count < 0) perror_exit("xread"); + + return count; +} + +void xreadall(int fd, void *buf, size_t count) +{ + if (count != readall(fd, buf, count)) perror_exit("xreadall"); +} void xwrite(int fd, void *buf, size_t count) { @@ -377,3 +392,103 @@ char *itoa(int n) return itoa_buf; } + +/* + This might be of use or might not. Unknown yet... + + +// Return how long the file at fd is, if there's any way to determine it. +off_t fdlength(int fd) +{ + off_t bottom = 0, top = 0, pos; + long size; + + // If the ioctl works for this, return it. + + if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; + + // If not, do a binary search for the last location we can read. (Some + // block devices don't do BLKGETSIZE right.) This should probably have + // a CONFIG option... + + do { + char temp; + + pos = bottom + (top - bottom) / 2; + + // If we can read from the current location, it's bigger. + + if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { + if (bottom == top) bottom = top = (top+1) * 2; + else bottom = pos; + + // If we can't, it's smaller. + + } else { + if (bottom == top) { + if (!top) return 0; + bottom = top/2; + } else top = pos; + } + } while (bottom + 1 != top); + + return pos + 1; +} + +// Read contents of file as a single freshly allocated nul-terminated string. +char *readfile(char *name) +{ + off_t len; + int fd; + char *buf; + + fd = open(pidfile, O_RDONLY); + if (fd == -1) return 0; + len = fdlength(fd); + buf = xmalloc(len+1); + buf[xread(fd, buf, len)] = 0; + + return buf; +} + +char *xreadfile(char *name) +{ + char *buf = readfile(name); + if (!buf) error_exit("xreadfile %s", name); + return buf; +} + +*/ + +// Open a /var/run/NAME.pid file, dying if we can't write it or if it currently +// exists and is this executable. +void xpidfile(char *name) +{ + char pidfile[256], spid[32]; + int i, fd; + pid_t pid; + + sprintf(pidfile, "/var/run/%s.pid", name); + // Try three times to open the sucker. + for (i=0; i<3; i++) { + fd = open(pidfile, O_CREAT|O_EXCL, 0644); + if (fd != -1) break; + + // If it already existed, read it. Loop for race condition. + fd = open(pidfile, O_RDONLY); + if (fd == -1) continue; + + // Is the old program still there? + spid[xread(fd, spid, sizeof(spid)-1)] = 0; + close(fd); + pid = atoi(spid); + if (fd < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); + + // An else with more sanity checking might be nice here. + } + + if (i == 3) error_exit("xpidfile %s", name); + + xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); + close(fd); +} @@ -30,6 +30,7 @@ void error_msg(char *msg, ...); void perror_msg(char *msg, ...); void error_exit(char *msg, ...); void perror_exit(char *msg, ...); +void usage_exit(void); void strlcpy(char *dest, char *src, size_t size); void *xmalloc(size_t size); void *xzalloc(size_t size); @@ -46,7 +47,8 @@ ssize_t reread(int fd, void *buf, size_t count); ssize_t rewrite(int fd, void *buf, size_t count); ssize_t readall(int fd, void *buf, size_t count); ssize_t writeall(int fd, void *buf, size_t count); -void xread(int fd, void *buf, size_t count); +size_t xread(int fd, void *buf, size_t count); +void xreadall(int fd, void *buf, size_t count); void xwrite(int fd, void *buf, size_t count); char *xgetcwd(void); char *xabspath(char *path); diff --git a/lib/portability.h b/lib/portability.h new file mode 100644 index 00000000..dca0ddea --- /dev/null +++ b/lib/portability.h @@ -0,0 +1,21 @@ +#include <endian.h> + +#if __BYTE_ORDER == __BIG_ENDIAN +#define IS_BIG_ENDIAN 1 +#define IS_LITTLE_ENDIAN 0 +#define SWAP_BE16(x) (x) +#define SWAP_BE32(x) (x) +#define SWAP_BE64(x) (x) +#define SWAP_LE16(x) bswap_16(x) +#define SWAP_LE32(x) bswap_32(x) +#define SWAP_LE64(x) bswap_64(x) +#else +#define IS_LITTLE_ENDIAN 1 +#define IS_BIG_ENDIAN 0 +#define SWAP_BE16(x) bswap_16(x) +#define SWAP_BE32(x) bswap_32(x) +#define SWAP_BE64(x) bswap_64(x) +#define SWAP_LE16(x) (x) +#define SWAP_LE32(x) (x) +#define SWAP_LE64(x) (x) +#endif |