2
0

Improved error checking, NULL handling and API documentation.

This commit is contained in:
Maarten Billemont
2020-01-23 15:55:03 -05:00
parent b42bc732ac
commit ad4081be61
12 changed files with 136 additions and 79 deletions

View File

@@ -128,7 +128,7 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
// Read response.
color_set( 2, NULL );
attron( A_STANDOUT );
int result = ERR;
int result;
char str[MPW_MAX_INPUT + 1];
if (silent) {
mvprintw( rows / 2 + 1, (cols - 5) / 2, "[ * ]" );
@@ -180,6 +180,17 @@ const char *mpw_getpass(const char *prompt) {
const char *mpw_path(const char *prefix, const char *extension) {
if (!prefix || !extension)
return NULL;
// Compose filename.
char *path = mpw_strdup( mpw_str( "%s.%s", prefix, extension ) );
if (!path)
return NULL;
// This is a filename, remove all potential directory separators.
for (char *slash; (slash = strstr( path, "/" )); *slash = '_');
// Resolve user's home directory.
char *homeDir = NULL;
if (!homeDir)
@@ -201,12 +212,6 @@ const char *mpw_path(const char *prefix, const char *extension) {
if (!homeDir)
homeDir = getcwd( NULL, 0 );
// Compose filename.
char *path = mpw_strdup( mpw_str( "%s.%s", prefix, extension ) );
// This is a filename, remove all potential directory separators.
for (char *slash; (slash = strstr( path, "/" )); *slash = '_');
// Compose pathname.
if (homeDir) {
const char *homePath = mpw_str( "%s/.mpw.d/%s", homeDir, path );
@@ -277,6 +282,8 @@ const char *mpw_read_file(FILE *file) {
while ((mpw_realloc( &buf, &bufSize, blockSize )) &&
(bufOffset += (readSize = fread( buf + bufOffset, 1, blockSize, file ))) &&
(readSize == blockSize));
if (ferror( file ))
mpw_free( &buf, bufSize );
return buf;
}
@@ -345,7 +352,9 @@ const char *mpw_identicon_str(MPIdenticon identicon) {
}
const char *str = mpw_str( "%s%s%s%s%s%s",
colorString, identicon.leftArm, identicon.body, identicon.rightArm, identicon.accessory, resetString );
colorString? colorString: "",
identicon.leftArm, identicon.body, identicon.rightArm, identicon.accessory,
resetString? resetString: "" );
mpw_free_strings( &colorString, &resetString, NULL );
return mpw_strdup( str );

View File

@@ -35,7 +35,7 @@
const char *mpw_getenv(const char *variableName);
/** Use the askpass program to prompt the user.
* @return A newly allocated string or NULL if askpass is not supported or an error occurred. */
* @return A newly allocated string or NULL if askpass is not enabled or could not be executed. */
const char *mpw_askpass(const char *prompt);
/** Ask the user a question.
@@ -49,7 +49,7 @@ const char *mpw_getpass(const char *prompt);
/** Get the absolute path to the mpw configuration file with the given prefix name and file extension.
* Resolves the file <prefix.extension> as located in the <.mpw.d> directory inside the user's home directory
* or current directory if it couldn't be resolved.
* @return A newly allocated string. */
* @return A newly allocated string or NULL if the prefix or extension is missing or the path could not be allocated. */
const char *mpw_path(const char *prefix, const char *extension);
/** mkdir all the directories up to the directory of the given file path.
@@ -57,13 +57,13 @@ const char *mpw_path(const char *prefix, const char *extension);
bool mpw_mkdirs(const char *filePath);
/** Read until EOF from the given file descriptor.
* @return A newly allocated string or NULL the read buffer couldn't be allocated. */
* @return A newly allocated string or NULL if the an IO error occurred or the read buffer couldn't be allocated. */
const char *mpw_read_fd(int fd);
/** Read the file contents of a given file.
* @return A newly allocated string or NULL the read buffer couldn't be allocated. */
* @return A newly allocated string or NULL if the file is missing, an IO error occurred or the read buffer couldn't be allocated. */
const char *mpw_read_file(FILE *file);
/** Encode a visual fingerprint for a user.
* @return A newly allocated string. */
* @return A newly allocated string or NULL if the identicon couldn't be allocated. */
const char *mpw_identicon_str(MPIdenticon identicon);