Add Westermo chomp() function, slightly improved from Finit original.

Signed-off-by: Tobias Waldekranz <tobias@waldekranz.com>
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Tobias Waldekranz
2015-06-17 22:07:10 +02:00
committed by Joachim Nilsson
parent 745256909e
commit 121e8ee888
4 changed files with 68 additions and 11 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ MAKEFLAGS = --no-print-directory --silent
DISTFILES = README LICENSE
HEADERS = lite.h
OBJS := copyfile.o dir.o fexist.o fisdir.o fmode.o rsync.o
OBJS := chomp.o copyfile.o dir.o fexist.o fisdir.o fmode.o rsync.o
OBJS += strlcpy.o strlcat.o strtonum.o
DEPS := $(OBJS:.o=.d)
JUNK = *~ *.bak *.map .*.d *.d DEADJOE semantic.cache *.gdb *.elf core core.*
+13 -10
View File
@@ -8,21 +8,20 @@ missing C library functions.
Generic Functions
-----------------
- `chomp(str)`
Perl like chomp function, chop off last char if newline.
- `copyfile(src, dst, len, symlink)`
Like the shell `cp(1)` and `dd(1),` can also create symlinks.
- `dir(dir, ext, filter, list, strip)`
Wrapper for `scandir()` with optional filter. Returns a list of
names: files and directories that must be freed after use. See
the unit test at the bottom for an example.
- `rsync(src, dst, delete, *filter())`
Very simple `rsync()` to copy files files and directories
recursively.
- `copyfile(src, dst, len, symlink)`
Like the shell `cp(1)` and `dd(1),` can also create symlinks.
- `fexist(file)`
Check for the existance of a file, returns True(1) or False(0).
@@ -35,6 +34,11 @@ Generic Functions
Returns the `mode_t` bits of a file or directory.
- `rsync(src, dst, delete, *filter())`
Very simple `rsync()` to copy files files and directories
recursively.
OpenBSD Functions
-----------------
@@ -50,7 +54,6 @@ TODO
----
- Improve documentation, possibly use kdoc or gdoc2 to generate docs from API.
- Add `chomp()` from Finit
<!--
+52
View File
@@ -0,0 +1,52 @@
/* Perl inspired chomp() implementation.
*
* Copyright (c) 2014 Joachim Nilsson <troglobit@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <string.h>
/**
* chomp - Perl like chomp function, chop off last char if newline.
* @str: String to chomp
*
* Returns:
* If @str is a valid pointer this function returns @str, otherwise
* @errno is set to %EINVAL and this function returns %NULL.
*/
char *chomp (char *str)
{
char *p;
if (!str)
{
errno = EINVAL;
return NULL;
}
p = strrchr (str, '\n');
if (p)
*p = 0;
return str;
}
/**
* Local Variables:
* version-control: t
* indent-tabs-mode: nil
* c-file-style: "ellemtel"
* End:
*/
+2
View File
@@ -33,6 +33,8 @@
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
#include <unistd.h>
char *chomp (char *str);
int fexist (char *file);
int fisdir (char *file);
mode_t fmode (char *file);