summaryrefslogtreecommitdiff
path: root/lib/cvtoct.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-03-25 17:36:52 -0400
committerFreya Murphy <freya@freyacat.org>2025-03-25 17:38:22 -0400
commit6af21e6a4f2251e71353562d5df7f376fdffc270 (patch)
treede20c7afc9878422c81e34f30c6b010075e9e69a /lib/cvtoct.c
downloadcomus-6af21e6a4f2251e71353562d5df7f376fdffc270.tar.gz
comus-6af21e6a4f2251e71353562d5df7f376fdffc270.tar.bz2
comus-6af21e6a4f2251e71353562d5df7f376fdffc270.zip
initial checkout from wrc
Diffstat (limited to '')
-rw-r--r--lib/cvtoct.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/cvtoct.c b/lib/cvtoct.c
new file mode 100644
index 0000000..dafd8ff
--- /dev/null
+++ b/lib/cvtoct.c
@@ -0,0 +1,54 @@
+/**
+** @file cvtoct.c
+**
+** @author Numerous CSCI-452 classes
+**
+** @brief C implementations of common library functions
+*/
+
+#ifndef CVTOCT_SRC_INC
+#define CVTOCT_SRC_INC
+
+#include <common.h>
+
+#include <lib.h>
+
+/**
+** cvtoct(buf,value)
+**
+** convert a 32-bit unsigned value into a mininal-length (up to
+** 11-character) NUL-terminated character string
+**
+** @param buf Destination buffer
+** @param value Value to convert
+**
+** @return The number of characters placed into the buffer
+** (not including the NUL)
+**
+** NOTE: assumes buf is large enough to hold the resulting string
+*/
+int cvtoct( char *buf, uint32_t value ) {
+ int i;
+ int chars_stored = 0;
+ char *bp = buf;
+ uint32_t val;
+
+ val = ( value & 0xc0000000 );
+ val >>= 30;
+ for( i = 0; i < 11; i += 1 ){
+
+ if( i == 10 || val != 0 || chars_stored ) {
+ chars_stored = 1;
+ val &= 0x7;
+ *bp++ = val + '0';
+ }
+ value <<= 3;
+ val = ( value & 0xe0000000 );
+ val >>= 29;
+ }
+ *bp = '\0';
+
+ return bp - buf;
+}
+
+#endif