aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-04-09 22:48:12 +0000
committerEric Andersen <andersen@codepoet.org>2001-04-09 22:48:12 +0000
commite5dfced23a904d08afa5dcee190c3c3d845d9f50 (patch)
treeef367ee8a9096884fb40debdc9e10af8583f9d5f /libbb
parenta75e2867435faa68ea03735fe09ad298fa3e4e72 (diff)
downloadbusybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.gz
busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.tar.bz2
busybox-w32-e5dfced23a904d08afa5dcee190c3c3d845d9f50.zip
Apply Vladimir's latest cleanup patch.
-Erik
Diffstat (limited to 'libbb')
-rw-r--r--libbb/concat_path_file.c24
-rw-r--r--libbb/libbb.h7
-rw-r--r--libbb/print_file.c29
-rw-r--r--libbb/process_escape_sequence.c73
-rw-r--r--libbb/xgetcwd.c52
5 files changed, 133 insertions, 52 deletions
diff --git a/libbb/concat_path_file.c b/libbb/concat_path_file.c
new file mode 100644
index 000000000..d53dc0e2e
--- /dev/null
+++ b/libbb/concat_path_file.c
@@ -0,0 +1,24 @@
1/*
2 * busybox library eXtendet funcion
3 *
4 * concatenate path and file name to new allocation buffer,
5 * not addition '/' if path name already have '/'
6 *
7*/
8
9#include "libbb.h"
10
11extern char *concat_path_file(const char *path, const char *filename)
12{
13 char *outbuf;
14 int l;
15 int flg_slash = 1;
16
17 l = strlen(path);
18 if(l>0 && path[l-1] == '/')
19 flg_slash--;
20 l += strlen(filename);
21 outbuf = xmalloc(l+1+flg_slash);
22 sprintf(outbuf, (flg_slash ? "%s/%s" : "%s%s"), path, filename);
23 return outbuf;
24}
diff --git a/libbb/libbb.h b/libbb/libbb.h
index 05f61f25b..0001cac6f 100644
--- a/libbb/libbb.h
+++ b/libbb/libbb.h
@@ -131,7 +131,7 @@ extern int find_real_root_device_name(char* name);
131extern char *get_line_from_file(FILE *file); 131extern char *get_line_from_file(FILE *file);
132extern void print_file(FILE *file); 132extern void print_file(FILE *file);
133extern int print_file_by_name(char *filename); 133extern int print_file_by_name(char *filename);
134extern char process_escape_sequence(char **ptr); 134extern char process_escape_sequence(const char **ptr);
135extern char *get_last_path_component(char *path); 135extern char *get_last_path_component(char *path);
136extern FILE *wfopen(const char *path, const char *mode); 136extern FILE *wfopen(const char *path, const char *mode);
137extern FILE *xfopen(const char *path, const char *mode); 137extern FILE *xfopen(const char *path, const char *mode);
@@ -150,7 +150,7 @@ extern char *xstrndup (const char *s, int n);
150extern char * safe_strncpy(char *dst, const char *src, size_t size); 150extern char * safe_strncpy(char *dst, const char *src, size_t size);
151 151
152struct suffix_mult { 152struct suffix_mult {
153 char *suffix; 153 const char *suffix;
154 int mult; 154 int mult;
155}; 155};
156 156
@@ -213,4 +213,7 @@ enum {
213int ask_confirmation(void); 213int ask_confirmation(void);
214int klogctl(int type, char * b, int len); 214int klogctl(int type, char * b, int len);
215 215
216char *xgetcwd(char *cwd);
217char *concat_path_file(const char *path, const char *filename);
218
216#endif /* __LIBBB_H__ */ 219#endif /* __LIBBB_H__ */
diff --git a/libbb/print_file.c b/libbb/print_file.c
index 52a39774f..b47723454 100644
--- a/libbb/print_file.c
+++ b/libbb/print_file.c
@@ -2,9 +2,7 @@
2/* 2/*
3 * Utility routines. 3 * Utility routines.
4 * 4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what 5 * Copyright (C) 1999-2001 Erik Andersen <andersee@debian.org>
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 * 6 *
9 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -19,13 +17,10 @@
19 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */ 20 */
27 21
28#include <stdio.h> 22#include <stdio.h>
23#include <sys/stat.h>
29#include "libbb.h" 24#include "libbb.h"
30 25
31 26
@@ -41,11 +36,21 @@ extern void print_file(FILE *file)
41 36
42extern int print_file_by_name(char *filename) 37extern int print_file_by_name(char *filename)
43{ 38{
44 FILE *file; 39 struct stat statBuf;
45 if ((file = wfopen(filename, "r")) == NULL) 40 int status = TRUE;
46 return FALSE; 41
47 print_file(file); 42 if(is_directory(filename, TRUE, &statBuf)==TRUE) {
48 return TRUE; 43 error_msg("%s: Is directory", filename);
44 status = FALSE;
45 } else {
46 FILE *f = wfopen(filename, "r");
47 if(f!=NULL)
48 print_file(f);
49 else
50 status = FALSE;
51 }
52
53 return status;
49} 54}
50 55
51 56
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c
index ad2be94ee..67b0490ce 100644
--- a/libbb/process_escape_sequence.c
+++ b/libbb/process_escape_sequence.c
@@ -2,9 +2,8 @@
2/* 2/*
3 * Utility routines. 3 * Utility routines.
4 * 4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what 5 * Copyright (C) Manuel Nova III <mnovoa3@bellsouth.net>
6 * isn't something I'm going to worry about... If you wrote something 6 * and Vladimir Oleynik <vodz@usa.net>
7 * here, please feel free to acknowledge your work.
8 * 7 *
9 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
@@ -20,9 +19,7 @@
20 * along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * 21 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell 22 *
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */ 23 */
27 24
28#include <stdio.h> 25#include <stdio.h>
@@ -31,45 +28,45 @@
31 28
32 29
33 30
34char process_escape_sequence(char **ptr) 31char process_escape_sequence(const char **ptr)
35{ 32{
36 static const char charmap[] = { 33 static const char charmap[] = {
37 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, 34 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
38 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; 35 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
39
40 const char *p;
41 char *q;
42 int num_digits;
43 unsigned int n;
44 36
45 n = 0; 37 const char *p;
46 q = *ptr; 38 const char *q;
39 int num_digits;
40 unsigned int n;
41
42 n = 0;
43 q = *ptr;
47 44
48 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) { 45 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
49 if ((*q < '0') || (*q > '7')) { /* not a digit? */ 46 if ((*q < '0') || (*q > '7')) { /* not a digit? */
50 break; 47 break;
51 } 48 }
52 n = n * 8 + (*q++ - '0'); 49 n = n * 8 + (*q++ - '0');
53 } 50 }
54 51
55 if (num_digits == 0) { /* mnemonic escape sequence? */ 52 if (num_digits == 0) { /* mnemonic escape sequence? */
56 for (p=charmap ; *p ; p++) { 53 for (p=charmap ; *p ; p++) {
57 if (*p == *q) { 54 if (*p == *q) {
58 q++; 55 q++;
59 break; 56 break;
60 } 57 }
61 } 58 }
62 n = *(p+(sizeof(charmap)/2)); 59 n = *(p+(sizeof(charmap)/2));
63 } 60 }
64 61
65 /* doesn't hurt to fall through to here from mnemonic case */ 62 /* doesn't hurt to fall through to here from mnemonic case */
66 if (n > UCHAR_MAX) { /* is octal code too big for a char? */ 63 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
67 n /= 8; /* adjust value and */ 64 n /= 8; /* adjust value and */
68 --q; /* back up one char */ 65 --q; /* back up one char */
69 } 66 }
70 67
71 *ptr = q; 68 *ptr = q;
72 return (char) n; 69 return (char) n;
73} 70}
74 71
75 72
diff --git a/libbb/xgetcwd.c b/libbb/xgetcwd.c
new file mode 100644
index 000000000..274668166
--- /dev/null
+++ b/libbb/xgetcwd.c
@@ -0,0 +1,52 @@
1/*
2 * xgetcwd.c -- return current directory with unlimited length
3 * Copyright (C) 1992, 1996 Free Software Foundation, Inc.
4 * Written by David MacKenzie <djm@gnu.ai.mit.edu>.
5 *
6 * Special function for busybox written by Vladimir Oleynik <vodz@usa.net>
7*/
8
9#include <stdlib.h>
10#include <errno.h>
11#include <unistd.h>
12#include <limits.h>
13#include "libbb.h"
14
15/* Amount to increase buffer size by in each try. */
16#define PATH_INCR 32
17
18/* Return the current directory, newly allocated, arbitrarily long.
19 Return NULL and set errno on error.
20 If argument is not NULL (previous usage allocate memory), call free()
21*/
22
23char *
24xgetcwd (char *cwd)
25{
26 char *ret;
27 unsigned path_max;
28
29 errno = 0;
30 path_max = (unsigned) PATH_MAX;
31 path_max += 2; /* The getcwd docs say to do this. */
32
33 if(cwd==0)
34 cwd = xmalloc (path_max);
35
36 errno = 0;
37 while ((ret = getcwd (cwd, path_max)) == NULL && errno == ERANGE) {
38 path_max += PATH_INCR;
39 cwd = xrealloc (cwd, path_max);
40 errno = 0;
41 }
42
43 if (ret == NULL) {
44 int save_errno = errno;
45 free (cwd);
46 errno = save_errno;
47 perror_msg("getcwd()");
48 return NULL;
49 }
50
51 return cwd;
52}