2
0

Make source more standard C11 w/POSIX:2008 CLI.

This commit is contained in:
Maarten Billemont 2017-09-25 10:33:31 -04:00
parent 7ece02c73d
commit 3af8aba40c
7 changed files with 26 additions and 28 deletions

View File

@ -176,7 +176,7 @@ static const char *mpw_sitePasswordFromCrypt_v0(
// Decrypt // Decrypt
const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, &bufSize ); const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, &bufSize );
mpw_free( &cipherBuf, cipherBufSize ); mpw_free( &cipherBuf, cipherBufSize );
const char *plainText = strndup( (char *)plainBytes, bufSize ); const char *plainText = mpw_strndup( (char *)plainBytes, bufSize );
mpw_free( &plainBytes, bufSize ); mpw_free( &plainBytes, bufSize );
if (!plainText) if (!plainText)
err( "AES decryption error: %s\n", strerror( errno ) ); err( "AES decryption error: %s\n", strerror( errno ) );

View File

@ -28,7 +28,7 @@ char *mpw_get_token(const char **in, const char *eol, char *delim) {
// Find characters up to the first delim. // Find characters up to the first delim.
size_t len = strcspn( *in, delim ); size_t len = strcspn( *in, delim );
char *token = len && len <= (size_t)(eol - *in)? strndup( *in, len ): NULL; char *token = len && len <= (size_t)(eol - *in)? mpw_strndup( *in, len ): NULL;
// Advance past the delimitor. // Advance past the delimitor.
*in = min( eol, *in + len + 1 ); *in = min( eol, *in + len + 1 );
@ -38,30 +38,12 @@ char *mpw_get_token(const char **in, const char *eol, char *delim) {
time_t mpw_mktime( time_t mpw_mktime(
const char *time) { const char *time) {
struct tm tm = { .tm_isdst = -1, .tm_gmtoff = 0 }; struct tm tm = { .tm_isdst = -1 };
if (time && sscanf( time, "%4d-%2d-%2dT%2d:%2d:%2dZ", if (time && sscanf( time, "%4d-%2d-%2dT%2d:%2d:%2dZ",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec ) == 6) { &tm.tm_hour, &tm.tm_min, &tm.tm_sec ) == 6) {
tm.tm_year -= 1900; // tm_year 0 = rfc3339 year 1900 tm.tm_year -= 1900; // tm_year 0 = rfc3339 year 1900
tm.tm_mon -= 1; // tm_mon 0 = rfc3339 month 1 tm.tm_mon -= 1; // tm_mon 0 = rfc3339 month 1
/*
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)
frame #0: 0x00007fff9fe4d219 libsystem_notify.dylib`_nc_table_find_64 + 22
libsystem_notify.dylib`_nc_table_find_64:
-> 0x7fff9fe4d219 <+22>: divl 0x4(%rdi)
0x7fff9fe4d21c <+25>: movq 0x8(%rdi), %rax
0x7fff9fe4d220 <+29>: movq (%rax,%rdx,8), %rcx
0x7fff9fe4d224 <+33>: xorl %eax, %eax
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)
* frame #0: 0x00007fff9fe4d219 libsystem_notify.dylib`_nc_table_find_64 + 22
frame #1: 0x00007fff9fe4a21e libsystem_notify.dylib`registration_node_find + 53
frame #2: 0x00007fff9fe4b78d libsystem_notify.dylib`notify_check + 105
frame #3: 0x00007fff9fccc164 libsystem_c.dylib`notify_check_tz + 24
frame #4: 0x00007fff9fccbd97 libsystem_c.dylib`tzsetwall_basic + 45
frame #5: 0x00007fff9fccdcd0 libsystem_c.dylib`mktime + 46
frame #6: 0x0000000100009496 mpw`mpw_mktime(time="2017-04-16T03:16:35Z") at mpw-marshal-util.c:47
*/
return mktime( &tm ); return mktime( &tm );
} }

View File

@ -46,7 +46,7 @@ MPMarshalledUser *mpw_marshal_user(
.sites = NULL, .sites = NULL,
}; };
return user; return user;
}; }
MPMarshalledSite *mpw_marshal_site( MPMarshalledSite *mpw_marshal_site(
MPMarshalledUser *user, const char *siteName, const MPResultType resultType, MPMarshalledUser *user, const char *siteName, const MPResultType resultType,
@ -74,7 +74,7 @@ MPMarshalledSite *mpw_marshal_site(
.questions = NULL, .questions = NULL,
}; };
return site; return site;
}; }
MPMarshalledQuestion *mpw_marshal_question( MPMarshalledQuestion *mpw_marshal_question(
MPMarshalledSite *site, const char *keyword) { MPMarshalledSite *site, const char *keyword) {
@ -456,7 +456,7 @@ static MPMarshalledUser *mpw_marshal_read_flat(
char *headerValue = mpw_get_token( &positionInLine, endOfLine, "\n" ); char *headerValue = mpw_get_token( &positionInLine, endOfLine, "\n" );
if (!headerName || !headerValue) { if (!headerName || !headerValue) {
error->type = MPMarshalErrorStructure; error->type = MPMarshalErrorStructure;
error->description = mpw_str( "Invalid header: %s", strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) ); error->description = mpw_str( "Invalid header: %s", mpw_strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) );
return NULL; return NULL;
} }

View File

@ -482,7 +482,7 @@ static char *mpw_tputs(const char *str, int affcnt) {
str_tputs = calloc( str_tputs_max, sizeof( char ) ); str_tputs = calloc( str_tputs_max, sizeof( char ) );
str_tputs_cursor = -1; str_tputs_cursor = -1;
char *result = tputs( str, affcnt, mpw_tputc ) == ERR? NULL: strndup( str_tputs, str_tputs_max ); char *result = tputs( str, affcnt, mpw_tputc ) == ERR? NULL: mpw_strndup( str_tputs, str_tputs_max );
if (str_tputs) if (str_tputs)
mpw_free( &str_tputs, str_tputs_max ); mpw_free( &str_tputs, str_tputs_max );
@ -566,3 +566,14 @@ const size_t mpw_utf8_strlen(const char *utf8String) {
return charlen; return charlen;
} }
char *mpw_strndup(const char *src, size_t max) {
size_t len = 0;
for (; len < max && src[len] != '\0'; ++len);
char *dst = malloc( len + 1 );
memcpy( dst, src, len );
dst[len] = '\0';
return dst;
}

View File

@ -211,5 +211,7 @@ const char *mpw_identicon(const char *fullName, const char *masterPassword);
/** @return The amount of display characters in the given UTF-8 string. */ /** @return The amount of display characters in the given UTF-8 string. */
const size_t mpw_utf8_strlen(const char *utf8String); const size_t mpw_utf8_strlen(const char *utf8String);
/** Drop-in for non-standard strndup(3). */
char *mpw_strndup(const char *src, size_t max);
#endif // _MPW_UTIL_H #endif // _MPW_UTIL_H

View File

@ -16,7 +16,8 @@
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>. // LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
//============================================================================== //==============================================================================
#define _WITH_GETLINE #define _POSIX_C_SOURCE 200809L
#include "mpw-cli-util.h" #include "mpw-cli-util.h"
#include <unistd.h> #include <unistd.h>
@ -188,7 +189,7 @@ bool mpw_mkdirs(const char *filePath) {
// The path to mkdir is the filePath without the last path component. // The path to mkdir is the filePath without the last path component.
char *pathEnd = strrchr( filePath, '/' ); char *pathEnd = strrchr( filePath, '/' );
char *path = pathEnd? strndup( filePath, (size_t)(pathEnd - filePath) ): NULL; char *path = pathEnd? mpw_strndup( filePath, (size_t)(pathEnd - filePath) ): NULL;
if (!path) if (!path)
return false; return false;

View File

@ -16,6 +16,8 @@
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>. // LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
//============================================================================== //==============================================================================
#define _POSIX_C_SOURCE 200809L
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@ -273,7 +275,7 @@ void cli_free(Arguments *args, Operation *operation) {
void cli_args(Arguments *args, Operation *operation, const int argc, char *const argv[]) { void cli_args(Arguments *args, Operation *operation, const int argc, char *const argv[]) {
for (int opt; (opt = getopt( argc, argv, "u:U:m:M:t:P:c:a:p:C:f:F:R:vqh" )) != EOF; for (int opt; (opt = getopt( argc, argv, "u:U:m:M:t:P:c:a:p:C:f:F:R:vqh" )) != EOF;
optarg? mpw_zero( optarg, strlen( optarg ) ): NULL) optarg? mpw_zero( optarg, strlen( optarg ) ): (void)0)
switch (opt) { switch (opt) {
case 'u': case 'u':
args->fullName = optarg && strlen( optarg )? strdup( optarg ): NULL; args->fullName = optarg && strlen( optarg )? strdup( optarg ): NULL;