diff options
| author | aldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2005-10-06 12:10:48 +0000 |
|---|---|---|
| committer | aldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2005-10-06 12:10:48 +0000 |
| commit | 31bc4cb99e9ae441cacd74534fb169b3198da88e (patch) | |
| tree | d963d3fc50c71d20964db43601029d2aec0c47ac /libbb | |
| parent | f5fe5b8632284670c4b16eccb34be770ab43402d (diff) | |
| download | busybox-w32-31bc4cb99e9ae441cacd74534fb169b3198da88e.tar.gz busybox-w32-31bc4cb99e9ae441cacd74534fb169b3198da88e.tar.bz2 busybox-w32-31bc4cb99e9ae441cacd74534fb169b3198da88e.zip | |
- add llist_free_one() and llist_free() to libbb; Add a bit of documentation.
- change llist_add_to_end as proposed by vodz in http://busybox.net/lists/busybox/2005-September/016411.html
- remove unneeded includes, add short boilerplate and copyright to llist.c
- move COMM_LEN from find_pid_by_name to libbb.h and use it in procps_status_t
- add reverse_pidlist() to find_pid_by_name. Will be needed for pidof.
git-svn-id: svn://busybox.net/trunk/busybox@11785 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/find_pid_by_name.c | 37 | ||||
| -rw-r--r-- | libbb/llist.c | 54 |
2 files changed, 60 insertions, 31 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index 570e7bd93..966595ddb 100644 --- a/libbb/find_pid_by_name.c +++ b/libbb/find_pid_by_name.c | |||
| @@ -4,19 +4,7 @@ | |||
| 4 | * | 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify | 7 | * Licensed under the GPL v2, see the file LICENSE in this tarball. |
| 8 | * it under the terms of the GNU General Public License as published by | ||
| 9 | * the Free Software Foundation; either version 2 of the License, or | ||
| 10 | * (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License | ||
| 18 | * along with this program; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 20 | */ | 8 | */ |
| 21 | 9 | ||
| 22 | #include <stdio.h> | 10 | #include <stdio.h> |
| @@ -25,10 +13,6 @@ | |||
| 25 | #include <stdlib.h> | 13 | #include <stdlib.h> |
| 26 | #include "libbb.h" | 14 | #include "libbb.h" |
| 27 | 15 | ||
| 28 | #define COMM_LEN 16 /* synchronize with size of comm in struct task_struct | ||
| 29 | in /usr/include/linux/sched.h */ | ||
| 30 | |||
| 31 | |||
| 32 | /* find_pid_by_name() | 16 | /* find_pid_by_name() |
| 33 | * | 17 | * |
| 34 | * Modified by Vladimir Oleynik for use with libbb/procps.c | 18 | * Modified by Vladimir Oleynik for use with libbb/procps.c |
| @@ -37,6 +21,7 @@ | |||
| 37 | * the proc filesystem. | 21 | * the proc filesystem. |
| 38 | * | 22 | * |
| 39 | * Returns a list of all matching PIDs | 23 | * Returns a list of all matching PIDs |
| 24 | * It is the caller's duty to free the returned pidlist. | ||
| 40 | */ | 25 | */ |
| 41 | extern long* find_pid_by_name( const char* pidName) | 26 | extern long* find_pid_by_name( const char* pidName) |
| 42 | { | 27 | { |
| @@ -45,7 +30,7 @@ extern long* find_pid_by_name( const char* pidName) | |||
| 45 | procps_status_t * p; | 30 | procps_status_t * p; |
| 46 | 31 | ||
| 47 | pidList = xmalloc(sizeof(long)); | 32 | pidList = xmalloc(sizeof(long)); |
| 48 | while ((p = procps_scan(0)) != 0) | 33 | while ((p = procps_scan(0)) != 0) |
| 49 | { | 34 | { |
| 50 | if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) { | 35 | if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) { |
| 51 | pidList=xrealloc( pidList, sizeof(long) * (i+2)); | 36 | pidList=xrealloc( pidList, sizeof(long) * (i+2)); |
| @@ -57,6 +42,22 @@ extern long* find_pid_by_name( const char* pidName) | |||
| 57 | return pidList; | 42 | return pidList; |
| 58 | } | 43 | } |
| 59 | 44 | ||
| 45 | extern long *pidlist_reverse(long *pidList) | ||
| 46 | { | ||
| 47 | int i=0; | ||
| 48 | while (pidList[i] > 0 && i++); | ||
| 49 | if ( i-- > 0) { | ||
| 50 | long k; | ||
| 51 | int j; | ||
| 52 | for (j = 0; i > j; i--, j++) { | ||
| 53 | k = pidList[i]; | ||
| 54 | pidList[i] = pidList[j]; | ||
| 55 | pidList[j] = k; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | return pidList; | ||
| 59 | } | ||
| 60 | |||
| 60 | /* END CODE */ | 61 | /* END CODE */ |
| 61 | /* | 62 | /* |
| 62 | Local Variables: | 63 | Local Variables: |
diff --git a/libbb/llist.c b/libbb/llist.c index cb87176c5..ce7daddee 100644 --- a/libbb/llist.c +++ b/libbb/llist.c | |||
| @@ -1,9 +1,18 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * linked list helper functions. | ||
| 4 | * | ||
| 5 | * Copyright (C) 2003 Glenn McGrath | ||
| 6 | * Copyright (C) 2005 Vladimir Oleynik | ||
| 7 | * Copyright (C) 2005 Bernhard Fischer | ||
| 8 | * | ||
| 9 | * Licensed under the GPL v2, see the file LICENSE in this tarball. | ||
| 10 | */ | ||
| 1 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| 2 | #include <string.h> | ||
| 3 | #include "unarchive.h" | ||
| 4 | #include "libbb.h" | 12 | #include "libbb.h" |
| 5 | 13 | ||
| 6 | #ifdef L_llist_add_to | 14 | #ifdef L_llist_add_to |
| 15 | /* Add data to the start of the linked list. */ | ||
| 7 | extern llist_t *llist_add_to(llist_t *old_head, char *new_item) | 16 | extern llist_t *llist_add_to(llist_t *old_head, char *new_item) |
| 8 | { | 17 | { |
| 9 | llist_t *new_head; | 18 | llist_t *new_head; |
| @@ -17,27 +26,46 @@ extern llist_t *llist_add_to(llist_t *old_head, char *new_item) | |||
| 17 | #endif | 26 | #endif |
| 18 | 27 | ||
| 19 | #ifdef L_llist_add_to_end | 28 | #ifdef L_llist_add_to_end |
| 29 | /* Add data to the end of the linked list. */ | ||
| 20 | extern llist_t *llist_add_to_end(llist_t *list_head, char *data) | 30 | extern llist_t *llist_add_to_end(llist_t *list_head, char *data) |
| 21 | { | 31 | { |
| 22 | llist_t *new_item, *tmp, *prev; | 32 | llist_t *new_item; |
| 23 | 33 | ||
| 24 | new_item = xmalloc(sizeof(llist_t)); | 34 | new_item = xmalloc(sizeof(llist_t)); |
| 25 | new_item->data = data; | 35 | new_item->data = data; |
| 26 | new_item->link = NULL; | 36 | new_item->link = NULL; |
| 27 | 37 | ||
| 28 | prev = NULL; | 38 | if (list_head == NULL) { |
| 29 | tmp = list_head; | ||
| 30 | while (tmp) { | ||
| 31 | prev = tmp; | ||
| 32 | tmp = tmp->link; | ||
| 33 | } | ||
| 34 | if (prev) { | ||
| 35 | prev->link = new_item; | ||
| 36 | } else { | ||
| 37 | list_head = new_item; | 39 | list_head = new_item; |
| 40 | } else { | ||
| 41 | llist_t *tail = list_head; | ||
| 42 | while (tail->link) | ||
| 43 | tail = tail->link; | ||
| 44 | tail->link = new_item; | ||
| 38 | } | 45 | } |
| 46 | return list_head; | ||
| 47 | } | ||
| 48 | #endif | ||
| 39 | 49 | ||
| 40 | return (list_head); | 50 | #ifdef L_llist_free_one |
| 51 | /* Free the current list element and advance to the next entry in the list. | ||
| 52 | * Returns a pointer to the next element. */ | ||
| 53 | extern llist_t *llist_free_one(llist_t *elm) | ||
| 54 | { | ||
| 55 | llist_t *next = elm ? elm->link : NULL; | ||
| 56 | #if ENABLE_DMALLOC /* avoid warnings from dmalloc's error-free-null option */ | ||
| 57 | if (elm) | ||
| 58 | #endif | ||
| 59 | free(elm); | ||
| 60 | elm = next; | ||
| 61 | return elm; | ||
| 41 | } | 62 | } |
| 42 | #endif | 63 | #endif |
| 43 | 64 | ||
| 65 | #ifdef L_llist_free | ||
| 66 | /* Recursively free all elements in the linked list. */ | ||
| 67 | extern void llist_free(llist_t *elm) | ||
| 68 | { | ||
| 69 | while ((elm = llist_free_one(elm))); | ||
| 70 | } | ||
| 71 | #endif | ||
