diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2009-03-24 13:30:02 +0100 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2009-03-24 13:30:02 +0100 |
commit | bc7a5d8980f1d0a507f5593347ee425d864de79c (patch) | |
tree | 766baa18eff49167a606543b4555e15d375b362b /src/port | |
parent | b305975aaf8052277406ad11291bec3cdce93dff (diff) | |
download | wolfbones-bc7a5d8980f1d0a507f5593347ee425d864de79c.tar.gz wolfbones-bc7a5d8980f1d0a507f5593347ee425d864de79c.tar.bz2 |
added a simple itoa
Diffstat (limited to 'src/port')
-rw-r--r-- | src/port/stdlib.c | 88 |
1 files changed, 41 insertions, 47 deletions
diff --git a/src/port/stdlib.c b/src/port/stdlib.c index 5cfaa8a..3a21fe6 100644 --- a/src/port/stdlib.c +++ b/src/port/stdlib.c @@ -24,53 +24,47 @@ #include <errno.h> /* for errno */ -char *wolf_port_itoa( int value, char *string, int radix ); - -int wolf_port_lockf( int fd, int cmd, off_t len ) { - struct flock fl; - - memset( (char *)&fl, 0, sizeof( fl ) ); - /* lockf is always relative to the current file position. */ - fl.l_whence = SEEK_CUR; - fl.l_start = 0; - fl.l_len = len; - - errno = 0; - - switch( cmd ) { - case F_TEST: - fl.l_type = F_RDLCK; - if( fcntl( fd, F_GETLK, &fl ) < 0 ) { - return -1; - } - if( fl.l_type == F_UNLCK || - fl.l_pid == getpid( ) ) { - return 0; - } - errno = EACCES; - return -1; - - case F_ULOCK: - fl.l_type = F_UNLCK; - cmd = F_SETLK; - break; - - case F_LOCK: - fl.l_type = F_WRLCK; - cmd = F_SETLKW; - break; - - case F_TLOCK: - fl.l_type = F_WRLCK; - cmd = F_SETLK; - break; - - default: - errno = EINVAL; - return -1; +static void reverse( char *s ) { + int i, j; + char c; + + for( i = 0, j = (int)strlen( s )-1; i<j; i++, j-- ) { + c = s[i]; + s[i] = s[j]; + s[j] = c; + } +} + +char *wolf_port_itoa( int value, char *string, int radix ) { + int sign; + int i; + int digit; + + /* remember sign and make value positive */ + sign = value; + if( sign < 0 ) { + value = -value; } - - return fcntl( fd, cmd, &fl ); + + i = 0; + do { + digit = value % radix; + value = value / radix; + if( digit < 10 ) { + string[i++] = (char)( digit + '0' ); + } else { + string[i++] = (char)( digit - 10 + 'A' ); + } + } while( value > 0 ); + + if( sign < 0 ) { + string[i++] = '-'; + } + string[i] = '\0'; + + reverse( string ); + + return string; } -#endif /* !defined HAVE_LOCKF || defined TEST_HAVE_LOCKF */ +#endif /* !defined HAVE_ITOA || defined TEST_HAVE_ITOA */ |