From f822e622bb4a112e38dc74bf11f1b3a1e6e066f2 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 7 Apr 2014 18:33:12 +0200 Subject: add test + fix example in info manual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 19 +++++++++---- info/libpassphrase.texinfo | 4 ++- src/passphrase.h | 4 +-- src/test.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ src/test.h | 30 ++++++++++++++++++++ 5 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 src/test.c create mode 100644 src/test.h diff --git a/Makefile b/Makefile index ee19cf7..c9afc10 100644 --- a/Makefile +++ b/Makefile @@ -57,24 +57,24 @@ STD = gnu99 # C preprocessor flags CPPFLAGS_ = $(foreach D, $(OPTIONS), -D'$(D)=1') $(CPPFLAGS) # C compiling flags -CFLAGS_ = -std=$(STD) $(WARN) -fPIC $(CFLAGS) +_fPIC = -fPIC +CFLAGS_ = -std=$(STD) $(WARN) $(_fPIC) $(CFLAGS) # Linking flags -LDFLAGS_ = -shared $(LDFLAGS) +_shared = -shared +LDFLAGS_ = $(_shared) $(LDFLAGS) # Flags to use when compiling and assembling CC_FLAGS = $(CPPFLAGS_) $(CFLAGS_) $(OPTIMISE) # Flags to use when linking LD_FLAGS = $(LDFLAGS_) $(CFLAGS_) $(OPTIMISE) -SRC = passphrase -OBJ = $(foreach S, $(SRC), obj/$(S).o) .PHONY: default default: libpassphrase info .PHONY: all -all: libpassphrase doc +all: libpassphrase test doc .PHONY: doc doc: info pdf ps dvi @@ -82,7 +82,14 @@ doc: info pdf ps dvi .PHONY: libpassphrase libpassphrase: bin/libpassphrase.so -bin/libpassphrase.so: $(OBJ) +.PHONY: test +test: bin/test + +bin/libpassphrase.so: obj/passphrase.o + @mkdir -p bin + $(CC) $(LD_FLAGS) -o "$@" $^ + +bin/test: obj/passphrase.o obj/test.o @mkdir -p bin $(CC) $(LD_FLAGS) -o "$@" $^ diff --git a/info/libpassphrase.texinfo b/info/libpassphrase.texinfo index 5136d20..7eaf9a5 100644 --- a/info/libpassphrase.texinfo +++ b/info/libpassphrase.texinfo @@ -149,11 +149,13 @@ been disabled. @example #include /* For libpassphrase */ #include /* For output */ +#include /* For free */ int main(int argc, char** argv) @{ /* Variables for the passphrase */ - char* passphrase, passphrase_; + char* passphrase; + char* passphrase_; /* Hide the passphrase */ passphrase_disable_echo(); diff --git a/src/passphrase.h b/src/passphrase.h index d6ecdce..932b6e5 100644 --- a/src/passphrase.h +++ b/src/passphrase.h @@ -16,8 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#ifndef __PASSPHRASE_H__ -#define __PASSPHRASE_H__ +#ifndef PASSPHRASE_H +#define PASSPHRASE_H /** diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..1416be3 --- /dev/null +++ b/src/test.c @@ -0,0 +1,69 @@ +/** + * libpassphrase – Personalisable library for TTY passphrase reading + * + * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "test.h" + + +/** + * Main test function + * + * @param argc Number of elements in `argv` + * @param argv Command line arguments + * @return Zero on success + */ +int main(int argc, char** argv) +{ + /* Variables for the passphrase */ + char* passphrase; + char* passphrase_; + + /* Hide the passphrase */ + passphrase_disable_echo(); + + /* Do things needed before reading the passphrase */ + printf("Passphrase: "); + fflush(stdout); + + /* Read the passphrase */ + passphrase = passphrase_read(); + if (passphrase == NULL) + { + /* Something went wrong, print what and exit */ + perror(*argv); + return 1; + } + + /* Use the passphrase */ + printf("You entered: %s\n", passphrase); + + /* Wipe and free the passphrase */ + passphrase_ = passphrase; + while (*passphrase) + *passphrase++ = 0; + free(passphrase_); + + /* Stop hiding user input */ + passphrase_reenable_echo(); + + /* End of program */ + return 0; + + /* `argc` was never used */ + (void) argc; +} + diff --git a/src/test.h b/src/test.h new file mode 100644 index 0000000..12ddb4a --- /dev/null +++ b/src/test.h @@ -0,0 +1,30 @@ +/** + * libpassphrase – Personalisable library for TTY passphrase reading + * + * Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef TEST_H +#define TEST_H + + +#include +#include + +#include "passphrase.h" + + +#endif + -- cgit v1.2.3-70-g09d2