summaryrefslogtreecommitdiffstats
path: root/toolbox
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-04-08 11:35:40 -0700
committerElliott Hughes <enh@google.com>2015-04-08 11:38:16 -0700
commitcf5f9b99cb6ba982d00db0d20dcb0cbb3b4a2453 (patch)
tree63c31610bab5d117a36a4056d2bf2251e84883e1 /toolbox
parent74a1875766a0f41dd67cb8825ae671fd5b343f12 (diff)
downloadsystem_core-cf5f9b99cb6ba982d00db0d20dcb0cbb3b4a2453.zip
system_core-cf5f9b99cb6ba982d00db0d20dcb0cbb3b4a2453.tar.gz
system_core-cf5f9b99cb6ba982d00db0d20dcb0cbb3b4a2453.tar.bz2
Move ls implementation detail into ls.
Also remove the unused generality, and remove a dependency on a linux/ header file when there's a sys/ one that's even better. Change-Id: If07608cc9ae2b7c2928ee2a9e33d20d63a9a8f45
Diffstat (limited to 'toolbox')
-rw-r--r--toolbox/Android.mk1
-rw-r--r--toolbox/dynarray.c104
-rw-r--r--toolbox/dynarray.h80
-rw-r--r--toolbox/ls.c118
4 files changed, 107 insertions, 196 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 6f5567d..ad99a39 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -63,7 +63,6 @@ OUR_TOOLS := \
ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS)
LOCAL_SRC_FILES := \
- dynarray.c \
toolbox.c \
$(patsubst %,%.c,$(OUR_TOOLS)) \
diff --git a/toolbox/dynarray.c b/toolbox/dynarray.c
deleted file mode 100644
index 47594e0..0000000
--- a/toolbox/dynarray.c
+++ /dev/null
@@ -1,104 +0,0 @@
-#include "dynarray.h"
-#include <limits.h>
-#include <stdlib.h>
-#include <string.h>
-
-void
-dynarray_init( dynarray_t *a )
-{
- a->count = a->capacity = 0;
- a->items = NULL;
-}
-
-
-static void
-dynarray_reserve_more( dynarray_t *a, int count )
-{
- int old_cap = a->capacity;
- int new_cap = old_cap;
- const int max_cap = INT_MAX/sizeof(void*);
- void** new_items;
- int new_count = a->count + count;
-
- if (count <= 0)
- return;
-
- if (count > max_cap - a->count)
- abort();
-
- new_count = a->count + count;
-
- while (new_cap < new_count) {
- old_cap = new_cap;
- new_cap += (new_cap >> 2) + 4;
- if (new_cap < old_cap || new_cap > max_cap) {
- new_cap = max_cap;
- }
- }
- new_items = realloc(a->items, new_cap*sizeof(void*));
- if (new_items == NULL)
- abort();
-
- a->items = new_items;
- a->capacity = new_cap;
-}
-
-void
-dynarray_append( dynarray_t *a, void* item )
-{
- if (a->count >= a->capacity)
- dynarray_reserve_more(a, 1);
-
- a->items[a->count++] = item;
-}
-
-void
-dynarray_done( dynarray_t *a )
-{
- free(a->items);
- a->items = NULL;
- a->count = a->capacity = 0;
-}
-
-// string arrays
-
-void strlist_init( strlist_t *list )
-{
- dynarray_init(list);
-}
-
-void strlist_append_b( strlist_t *list, const void* str, size_t slen )
-{
- char *copy = malloc(slen+1);
- memcpy(copy, str, slen);
- copy[slen] = '\0';
- dynarray_append(list, copy);
-}
-
-void strlist_append_dup( strlist_t *list, const char *str)
-{
- strlist_append_b(list, str, strlen(str));
-}
-
-void strlist_done( strlist_t *list )
-{
- STRLIST_FOREACH(list, string, free(string));
- dynarray_done(list);
-}
-
-static int strlist_compare_strings(const void* a, const void* b)
-{
- const char *sa = *(const char **)a;
- const char *sb = *(const char **)b;
- return strcmp(sa, sb);
-}
-
-void strlist_sort( strlist_t *list )
-{
- if (list->count > 0) {
- qsort(list->items,
- (size_t)list->count,
- sizeof(void*),
- strlist_compare_strings);
- }
-}
diff --git a/toolbox/dynarray.h b/toolbox/dynarray.h
deleted file mode 100644
index f73fb3b..0000000
--- a/toolbox/dynarray.h
+++ /dev/null
@@ -1,80 +0,0 @@
-#ifndef DYNARRAY_H
-#define DYNARRAY_H
-
-#include <stddef.h>
-
-/* simple dynamic array of pointers */
-typedef struct {
- int count;
- int capacity;
- void** items;
-} dynarray_t;
-
-#define DYNARRAY_INITIALIZER { 0, 0, NULL }
-
-void dynarray_init( dynarray_t *a );
-void dynarray_done( dynarray_t *a );
-
-void dynarray_append( dynarray_t *a, void* item );
-
-/* Used to iterate over a dynarray_t
- * _array :: pointer to the array
- * _item_type :: type of objects pointed to by the array
- * _item :: name of a local variable defined within the loop
- * with type '_item_type'
- * _stmnt :: C statement that will be executed in each iteration.
- *
- * You case use 'break' and 'continue' within _stmnt
- *
- * This macro is only intended for simple uses. I.e. do not add or
- * remove items from the array during iteration.
- */
-#define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \
- do { \
- int _nn_##__LINE__ = 0; \
- for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \
- _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \
- _stmnt; \
- } \
- } while (0)
-
-#define DYNARRAY_FOREACH(_array,_item,_stmnt) \
- DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt)
-
-/* Simple dynamic string arrays
- *
- * NOTE: A strlist_t owns the strings it references.
- */
-typedef dynarray_t strlist_t;
-
-#define STRLIST_INITIALIZER DYNARRAY_INITIALIZER
-
-/* Used to iterate over a strlist_t
- * _list :: pointer to strlist_t object
- * _string :: name of local variable name defined within the loop with
- * type 'char*'
- * _stmnt :: C statement executed in each iteration
- *
- * This macro is only intended for simple uses. Do not add or remove items
- * to/from the list during iteration.
- */
-#define STRLIST_FOREACH(_list,_string,_stmnt) \
- DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt)
-
-void strlist_init( strlist_t *list );
-
-/* note: strlist_done will free all the strings owned by the list */
-void strlist_done( strlist_t *list );
-
-/* append a new string made of the first 'slen' characters from 'str'
- * followed by a trailing zero.
- */
-void strlist_append_b( strlist_t *list, const void* str, size_t slen );
-
-/* append the copy of a given input string to a strlist_t */
-void strlist_append_dup( strlist_t *list, const char *str);
-
-/* sort the strings in a given list (using strcmp) */
-void strlist_sort( strlist_t *list );
-
-#endif /* DYNARRAY_H */ \ No newline at end of file
diff --git a/toolbox/ls.c b/toolbox/ls.c
index 963fcb5..9a89dd4 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -1,23 +1,119 @@
+#include <dirent.h>
+#include <errno.h>
+#include <grp.h>
+#include <limits.h>
+#include <pwd.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
#include <sys/types.h>
-#include <dirent.h>
-#include <errno.h>
+#include <time.h>
+#include <unistd.h>
#include <selinux/selinux.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <time.h>
+// simple dynamic array of strings.
+typedef struct {
+ int count;
+ int capacity;
+ void** items;
+} strlist_t;
+
+#define STRLIST_INITIALIZER { 0, 0, NULL }
+
+/* Used to iterate over a strlist_t
+ * _list :: pointer to strlist_t object
+ * _item :: name of local variable name defined within the loop with
+ * type 'char*'
+ * _stmnt :: C statement executed in each iteration
+ *
+ * This macro is only intended for simple uses. Do not add or remove items
+ * to/from the list during iteration.
+ */
+#define STRLIST_FOREACH(_list,_item,_stmnt) \
+ do { \
+ int _nn_##__LINE__ = 0; \
+ for (;_nn_##__LINE__ < (_list)->count; ++ _nn_##__LINE__) { \
+ char* _item = (char*)(_list)->items[_nn_##__LINE__]; \
+ _stmnt; \
+ } \
+ } while (0)
+
+static void dynarray_reserve_more( strlist_t *a, int count ) {
+ int old_cap = a->capacity;
+ int new_cap = old_cap;
+ const int max_cap = INT_MAX/sizeof(void*);
+ void** new_items;
+ int new_count = a->count + count;
+
+ if (count <= 0)
+ return;
+
+ if (count > max_cap - a->count)
+ abort();
+
+ new_count = a->count + count;
+
+ while (new_cap < new_count) {
+ old_cap = new_cap;
+ new_cap += (new_cap >> 2) + 4;
+ if (new_cap < old_cap || new_cap > max_cap) {
+ new_cap = max_cap;
+ }
+ }
+ new_items = realloc(a->items, new_cap*sizeof(void*));
+ if (new_items == NULL)
+ abort();
-#include <pwd.h>
-#include <grp.h>
+ a->items = new_items;
+ a->capacity = new_cap;
+}
-#include <linux/kdev_t.h>
-#include <limits.h>
+void strlist_init( strlist_t *list ) {
+ list->count = list->capacity = 0;
+ list->items = NULL;
+}
+
+// append a new string made of the first 'slen' characters from 'str'
+// followed by a trailing zero.
+void strlist_append_b( strlist_t *list, const void* str, size_t slen ) {
+ char *copy = malloc(slen+1);
+ memcpy(copy, str, slen);
+ copy[slen] = '\0';
+ if (list->count >= list->capacity)
+ dynarray_reserve_more(list, 1);
+ list->items[list->count++] = copy;
+}
+
+// append the copy of a given input string to a strlist_t.
+void strlist_append_dup( strlist_t *list, const char *str) {
+ strlist_append_b(list, str, strlen(str));
+}
+
+// note: strlist_done will free all the strings owned by the list.
+void strlist_done( strlist_t *list ) {
+ STRLIST_FOREACH(list, string, free(string));
+ free(list->items);
+ list->items = NULL;
+ list->count = list->capacity = 0;
+}
+
+static int strlist_compare_strings(const void* a, const void* b) {
+ const char *sa = *(const char **)a;
+ const char *sb = *(const char **)b;
+ return strcmp(sa, sb);
+}
+
+/* sort the strings in a given list (using strcmp) */
+void strlist_sort( strlist_t *list ) {
+ if (list->count > 0) {
+ qsort(list->items, (size_t)list->count, sizeof(void*), strlist_compare_strings);
+ }
+}
-#include "dynarray.h"
// bits for flags argument
#define LIST_LONG (1 << 0)
@@ -200,7 +296,7 @@ static int listfile_long(const char *path, struct stat *s, int flags)
case S_IFCHR:
printf("%s %-8s %-8s %3d, %3d %s %s\n",
mode, user, group,
- (int) MAJOR(s->st_rdev), (int) MINOR(s->st_rdev),
+ major(s->st_rdev), minor(s->st_rdev),
date, name);
break;
case S_IFREG: