From 906e93d480d8b77aac8ce4b3837ab55b3b496d2d Mon Sep 17 00:00:00 2001 From: Olivier Blin Date: Thu, 5 Jul 2007 14:20:04 +0000 Subject: - move parameters functions in params.c - move string/system utility functions in utils.c --- mdk-stage1/Makefile | 2 +- mdk-stage1/adsl.c | 2 +- mdk-stage1/automatic.c | 1 + mdk-stage1/cdrom.c | 2 + mdk-stage1/dhcp.c | 2 + mdk-stage1/directory.c | 2 + mdk-stage1/disk.c | 2 + mdk-stage1/modules.c | 1 + mdk-stage1/mount.c | 1 + mdk-stage1/network.c | 2 + mdk-stage1/params.c | 174 +++++++++++++++++++++++++++++ mdk-stage1/params.h | 31 ++++++ mdk-stage1/probing.c | 1 + mdk-stage1/rescue-gui.c | 2 +- mdk-stage1/stage1.c | 1 + mdk-stage1/stage1.h | 2 +- mdk-stage1/thirdparty.c | 2 + mdk-stage1/tools.c | 288 +----------------------------------------------- mdk-stage1/tools.h | 26 ----- mdk-stage1/url.c | 1 + mdk-stage1/utils.c | 157 ++++++++++++++++++++++++++ mdk-stage1/utils.h | 34 ++++++ mdk-stage1/wireless.c | 1 + 23 files changed, 421 insertions(+), 316 deletions(-) create mode 100644 mdk-stage1/params.c create mode 100644 mdk-stage1/params.h create mode 100644 mdk-stage1/utils.c create mode 100644 mdk-stage1/utils.h (limited to 'mdk-stage1') diff --git a/mdk-stage1/Makefile b/mdk-stage1/Makefile index 607c7bd40..c7a9a92e9 100644 --- a/mdk-stage1/Makefile +++ b/mdk-stage1/Makefile @@ -104,7 +104,7 @@ STAGE1_NETWORK_LIBS = /usr/$(LIB)/libresolv.a endif #- stage1 itself -STAGE1SRC = stage1.c log.c tools.c modules.c probing.c mount.c automatic.c frontend-common.c lomount.c thirdparty.c zlibsupport.c +STAGE1SRC = stage1.c log.c utils.c params.c tools.c modules.c probing.c mount.c automatic.c frontend-common.c lomount.c thirdparty.c zlibsupport.c CDROMSRC = cdrom.c DISKSRC = disk.c directory.c partition.c NETWORKSRC = network.c nfsmount.c dhcp.c url.c dns.c adsl.c directory.c wireless.c diff --git a/mdk-stage1/adsl.c b/mdk-stage1/adsl.c index 595b3fae0..03eeeb9a9 100644 --- a/mdk-stage1/adsl.c +++ b/mdk-stage1/adsl.c @@ -31,7 +31,7 @@ #include "log.h" #include "network.h" #include "modules.h" -#include "tools.h" +#include "utils.h" #include "frontend.h" #include "automatic.h" diff --git a/mdk-stage1/automatic.c b/mdk-stage1/automatic.c index 3a816bab1..e5c909624 100644 --- a/mdk-stage1/automatic.c +++ b/mdk-stage1/automatic.c @@ -23,6 +23,7 @@ #include #include #include "tools.h" +#include "utils.h" #include "stage1.h" #include "frontend.h" #include "log.h" diff --git a/mdk-stage1/cdrom.c b/mdk-stage1/cdrom.c index 199b1d830..3657fcf4f 100644 --- a/mdk-stage1/cdrom.c +++ b/mdk-stage1/cdrom.c @@ -30,6 +30,8 @@ #include "probing.h" #include "log.h" #include "mount.h" +#include "tools.h" +#include "utils.h" #include "cdrom.h" diff --git a/mdk-stage1/dhcp.c b/mdk-stage1/dhcp.c index f5de6543e..fccb400e1 100644 --- a/mdk-stage1/dhcp.c +++ b/mdk-stage1/dhcp.c @@ -45,6 +45,8 @@ #include "stage1.h" #include "log.h" +#include "tools.h" +#include "utils.h" #include "network.h" #include "frontend.h" #include "automatic.h" diff --git a/mdk-stage1/directory.c b/mdk-stage1/directory.c index 6957efbda..3c644f379 100644 --- a/mdk-stage1/directory.c +++ b/mdk-stage1/directory.c @@ -28,6 +28,8 @@ #include "stage1.h" #include "frontend.h" #include "log.h" +#include "tools.h" +#include "utils.h" #include "lomount.h" char * extract_list_directory(char * direct) diff --git a/mdk-stage1/disk.c b/mdk-stage1/disk.c index 4b85347f5..0fa789d91 100644 --- a/mdk-stage1/disk.c +++ b/mdk-stage1/disk.c @@ -33,6 +33,8 @@ #include "modules.h" #include "probing.h" #include "log.h" +#include "tools.h" +#include "utils.h" #include "mount.h" #include "automatic.h" #include "directory.h" diff --git a/mdk-stage1/modules.c b/mdk-stage1/modules.c index 43120bc4f..48fc20afd 100644 --- a/mdk-stage1/modules.c +++ b/mdk-stage1/modules.c @@ -29,6 +29,7 @@ #include #include "stage1.h" #include "log.h" +#include "utils.h" #include "frontend.h" #include "mount.h" #include "modules_descr.h" diff --git a/mdk-stage1/mount.c b/mdk-stage1/mount.c index 71d6f111f..41fbb99f2 100644 --- a/mdk-stage1/mount.c +++ b/mdk-stage1/mount.c @@ -27,6 +27,7 @@ #include #include #include "log.h" +#include "utils.h" #include "modules.h" #include "mount.h" diff --git a/mdk-stage1/network.c b/mdk-stage1/network.c index 9232684d0..6959fcbc8 100644 --- a/mdk-stage1/network.c +++ b/mdk-stage1/network.c @@ -40,6 +40,8 @@ #include "modules.h" #include "probing.h" #include "log.h" +#include "tools.h" +#include "utils.h" #include "mount.h" #include "automatic.h" #include "dhcp.h" diff --git a/mdk-stage1/params.c b/mdk-stage1/params.c new file mode 100644 index 000000000..311f8b0ea --- /dev/null +++ b/mdk-stage1/params.c @@ -0,0 +1,174 @@ +/* + * Guillaume Cottenceau (gc@mandrakesoft.com) + * + * Copyright 2000 Mandrakesoft + * + * This software may be freely redistributed under the terms of the GNU + * public license. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include +#include +#include +#include +#include +#include "params.h" +#include "utils.h" +#include "automatic.h" +#include "log.h" +#include "bootsplash.h" + +static struct param_elem params[50]; +static int param_number = 0; + +void process_cmdline(void) +{ + char buf[512]; + int size, i; + int fd = -1; + + if (IS_TESTING) { + log_message("TESTING: opening cmdline... "); + + if ((fd = open("cmdline", O_RDONLY)) == -1) + log_message("TESTING: could not open cmdline"); + } + + if (fd == -1) { + log_message("opening /proc/cmdline... "); + + if ((fd = open("/proc/cmdline", O_RDONLY)) == -1) + fatal_error("could not open /proc/cmdline"); + } + + size = read(fd, buf, sizeof(buf)); + buf[size-1] = '\0'; // -1 to eat the \n + close(fd); + + log_message("\t%s", buf); + + i = 0; + while (buf[i] != '\0') { + char *name, *value = NULL; + int j = i; + while (buf[i] != ' ' && buf[i] != '=' && buf[i] != '\0') + i++; + if (i == j) { + i++; + continue; + } + name = memdup(&buf[j], i-j + 1); + name[i-j] = '\0'; + + if (buf[i] == '=') { + int k = i+1; + i++; + while (buf[i] != ' ' && buf[i] != '\0') + i++; + value = memdup(&buf[k], i-k + 1); + value[i-k] = '\0'; + } + + params[param_number].name = name; + params[param_number].value = value; + param_number++; + if (!strcmp(name, "changedisk")) set_param(MODE_CHANGEDISK); + if (!strcmp(name, "updatemodules") || + !strcmp(name, "thirdparty")) set_param(MODE_THIRDPARTY); + if (!strcmp(name, "rescue")) set_param(MODE_RESCUE); + if (!strcmp(name, "keepmounted")) set_param(MODE_KEEP_MOUNTED); + if (!strcmp(name, "noauto")) set_param(MODE_NOAUTO); + if (!strcmp(name, "netauto")) set_param(MODE_NETAUTO); + if (!strcmp(name, "debugstage1")) set_param(MODE_DEBUGSTAGE1); + if (!strcmp(name, "automatic")) { + set_param(MODE_AUTOMATIC); + grab_automatic_params(value); + } + if (buf[i] == '\0') + break; + i++; + } + + if (IS_AUTOMATIC && strcmp(get_auto_value("thirdparty"), "")) { + set_param(MODE_THIRDPARTY); + } + + log_message("\tgot %d args", param_number); +} + + +int stage1_mode = 0; + +int get_param(int i) +{ +#ifdef SPAWN_INTERACTIVE + static int fd = 0; + char buf[5000]; + char * ptr; + int nb; + + if (fd <= 0) { + fd = open(interactive_fifo, O_RDONLY); + if (fd == -1) + return (stage1_mode & i); + fcntl(fd, F_SETFL, O_NONBLOCK); + } + + if (fd > 0) { + if ((nb = read(fd, buf, sizeof(buf))) > 0) { + buf[nb] = '\0'; + ptr = buf; + while ((ptr = strstr(ptr, "+ "))) { + if (!strncmp(ptr+2, "rescue", 6)) set_param(MODE_RESCUE); + ptr++; + } + ptr = buf; + while ((ptr = strstr(ptr, "- "))) { + if (!strncmp(ptr+2, "rescue", 6)) unset_param(MODE_RESCUE); + ptr++; + } + } + } +#endif + + return (stage1_mode & i); +} + +char * get_param_valued(char *param_name) +{ + int i; + for (i = 0; i < param_number ; i++) + if (!strcmp(params[i].name, param_name)) + return params[i].value; + + return NULL; +} + +void set_param_valued(char *param_name, char *param_value) +{ + params[param_number].name = param_name; + params[param_number].value = param_value; + param_number++; +} + +void set_param(int i) +{ + stage1_mode |= i; +} + +void unset_param(int i) +{ + stage1_mode &= ~i; +} + +void unset_automatic(void) +{ + log_message("unsetting automatic"); + unset_param(MODE_AUTOMATIC); + exit_bootsplash(); +} diff --git a/mdk-stage1/params.h b/mdk-stage1/params.h new file mode 100644 index 000000000..2fe624143 --- /dev/null +++ b/mdk-stage1/params.h @@ -0,0 +1,31 @@ +/* + * Guillaume Cottenceau (gc@mandrakesoft.com) + * + * Copyright 2000 Mandrakesoft + * + * This software may be freely redistributed under the terms of the GNU + * public license. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef _PARAMS_H_ +#define _PARAMS_H_ + +void process_cmdline(void); +int get_param(int i); +char * get_param_valued(char *param_name); +void set_param(int i); +void unset_param(int i); +void unset_automatic(void); + +struct param_elem +{ + char * name; + char * value; +}; + +#endif diff --git a/mdk-stage1/probing.c b/mdk-stage1/probing.c index 803c11806..841173e49 100644 --- a/mdk-stage1/probing.c +++ b/mdk-stage1/probing.c @@ -46,6 +46,7 @@ #include "stage1.h" #include "log.h" +#include "utils.h" #include "frontend.h" #include "modules.h" #include "pci-resource/pci-ids.h" diff --git a/mdk-stage1/rescue-gui.c b/mdk-stage1/rescue-gui.c index be63229e8..f0d24d56a 100644 --- a/mdk-stage1/rescue-gui.c +++ b/mdk-stage1/rescue-gui.c @@ -30,7 +30,7 @@ #include "config-stage1.h" #include "frontend.h" -#include "tools.h" +#include "utils.h" #if defined(__i386__) || defined(__x86_64__) #define ENABLE_RESCUE_MS_BOOT 1 diff --git a/mdk-stage1/stage1.c b/mdk-stage1/stage1.c index 41456cc5c..505bf655c 100644 --- a/mdk-stage1/stage1.c +++ b/mdk-stage1/stage1.c @@ -42,6 +42,7 @@ #include "frontend.h" #include "modules.h" #include "tools.h" +#include "utils.h" #include "automatic.h" #include "mount.h" #include "thirdparty.h" diff --git a/mdk-stage1/stage1.h b/mdk-stage1/stage1.h index b6ec98ef7..f21c6ab07 100644 --- a/mdk-stage1/stage1.h +++ b/mdk-stage1/stage1.h @@ -23,7 +23,7 @@ #define _STAGE1_H_ #include "config-stage1.h" -#include "tools.h" +#include "params.h" /* Some global stuff */ diff --git a/mdk-stage1/thirdparty.c b/mdk-stage1/thirdparty.c index 8c73bd88e..dcdb77062 100644 --- a/mdk-stage1/thirdparty.c +++ b/mdk-stage1/thirdparty.c @@ -20,6 +20,8 @@ #include #include "stage1.h" +#include "tools.h" +#include "utils.h" #include "log.h" #include "modules.h" #include "mount.h" diff --git a/mdk-stage1/tools.c b/mdk-stage1/tools.c index 5f30c78ae..e2e523e53 100644 --- a/mdk-stage1/tools.c +++ b/mdk-stage1/tools.c @@ -26,14 +26,12 @@ #include #include #include -#include #include #include #include #include #include #include -#include #include #include #include "stage1.h" @@ -43,194 +41,12 @@ #include "automatic.h" #include "tools.h" +#include "utils.h" +#include "params.h" #include "probing.h" #include "modules.h" #include "lomount.h" -static struct param_elem params[50]; -static int param_number = 0; - -void process_cmdline(void) -{ - char buf[512]; - int size, i; - int fd = -1; - - if (IS_TESTING) { - log_message("TESTING: opening cmdline... "); - - if ((fd = open("cmdline", O_RDONLY)) == -1) - log_message("TESTING: could not open cmdline"); - } - - if (fd == -1) { - log_message("opening /proc/cmdline... "); - - if ((fd = open("/proc/cmdline", O_RDONLY)) == -1) - fatal_error("could not open /proc/cmdline"); - } - - size = read(fd, buf, sizeof(buf)); - buf[size-1] = '\0'; // -1 to eat the \n - close(fd); - - log_message("\t%s", buf); - - i = 0; - while (buf[i] != '\0') { - char *name, *value = NULL; - int j = i; - while (buf[i] != ' ' && buf[i] != '=' && buf[i] != '\0') - i++; - if (i == j) { - i++; - continue; - } - name = memdup(&buf[j], i-j + 1); - name[i-j] = '\0'; - - if (buf[i] == '=') { - int k = i+1; - i++; - while (buf[i] != ' ' && buf[i] != '\0') - i++; - value = memdup(&buf[k], i-k + 1); - value[i-k] = '\0'; - } - - params[param_number].name = name; - params[param_number].value = value; - param_number++; - if (!strcmp(name, "changedisk")) set_param(MODE_CHANGEDISK); - if (!strcmp(name, "updatemodules") || - !strcmp(name, "thirdparty")) set_param(MODE_THIRDPARTY); - if (!strcmp(name, "rescue")) set_param(MODE_RESCUE); - if (!strcmp(name, "keepmounted")) set_param(MODE_KEEP_MOUNTED); - if (!strcmp(name, "noauto")) set_param(MODE_NOAUTO); - if (!strcmp(name, "netauto")) set_param(MODE_NETAUTO); - if (!strcmp(name, "debugstage1")) set_param(MODE_DEBUGSTAGE1); - if (!strcmp(name, "automatic")) { - set_param(MODE_AUTOMATIC); - grab_automatic_params(value); - } - if (buf[i] == '\0') - break; - i++; - } - - if (IS_AUTOMATIC && strcmp(get_auto_value("thirdparty"), "")) { - set_param(MODE_THIRDPARTY); - } - - log_message("\tgot %d args", param_number); -} - - -int stage1_mode = 0; - -int get_param(int i) -{ -#ifdef SPAWN_INTERACTIVE - static int fd = 0; - char buf[5000]; - char * ptr; - int nb; - - if (fd <= 0) { - fd = open(interactive_fifo, O_RDONLY); - if (fd == -1) - return (stage1_mode & i); - fcntl(fd, F_SETFL, O_NONBLOCK); - } - - if (fd > 0) { - if ((nb = read(fd, buf, sizeof(buf))) > 0) { - buf[nb] = '\0'; - ptr = buf; - while ((ptr = strstr(ptr, "+ "))) { - if (!strncmp(ptr+2, "rescue", 6)) set_param(MODE_RESCUE); - ptr++; - } - ptr = buf; - while ((ptr = strstr(ptr, "- "))) { - if (!strncmp(ptr+2, "rescue", 6)) unset_param(MODE_RESCUE); - ptr++; - } - } - } -#endif - - return (stage1_mode & i); -} - -char * get_param_valued(char *param_name) -{ - int i; - for (i = 0; i < param_number ; i++) - if (!strcmp(params[i].name, param_name)) - return params[i].value; - - return NULL; -} - -void set_param_valued(char *param_name, char *param_value) -{ - params[param_number].name = param_name; - params[param_number].value = param_value; - param_number++; -} - -void set_param(int i) -{ - stage1_mode |= i; -} - -void unset_param(int i) -{ - stage1_mode &= ~i; -} - -void unset_automatic(void) -{ - log_message("unsetting automatic"); - unset_param(MODE_AUTOMATIC); - exit_bootsplash(); -} - -// warning, many things rely on the fact that: -// - when failing it returns 0 -// - it stops on first non-digit char -int charstar_to_int(const char * s) -{ - int number = 0; - while (*s && isdigit(*s)) { - number = (number * 10) + (*s - '0'); - s++; - } - return number; -} - -off_t file_size(const char * path) -{ - struct stat statr; - if (stat(path, &statr)) - return -1; - else - return statr.st_size; -} - -int total_memory(void) -{ - int value; - - /* drakx powered: use /proc/kcore and rounds every 4 Mbytes */ - value = 4 * ((int)((float)file_size("/proc/kcore") / 1024 / 1024 / 4 + 0.5)); - log_message("Total Memory: %d Mbytes", value); - - return value; -} - - int image_has_stage2() { return access(COMPRESSED_FILE_REL(IMAGE_LOCATION "/"), R_OK) == 0 || @@ -431,70 +247,6 @@ enum return_type load_compressed_fd(int fd, int size) return preload_mount_compressed_fd(fd, size, COMPRESSED_NAME(""), STAGE2_LOCATION); } -/* pixel's */ -void * memdup(void *src, size_t size) -{ - void * r; - r = malloc(size); - memcpy(r, src, size); - return r; -} - - -void add_to_env(char * name, char * value) -{ - FILE* fakeenv = fopen("/tmp/env", "a"); - if (fakeenv) { - char* e = asprintf_("%s=%s\n", name, value); - fwrite(e, 1, strlen(e), fakeenv); - free(e); - fclose(fakeenv); - } else - log_message("couldn't fopen to fake env"); -} - - -char ** list_directory(char * direct) -{ - char * tmp[50000]; /* in /dev there can be many many files.. */ - int i = 0; - struct dirent *ep; - DIR *dp = opendir(direct); - while (dp && (ep = readdir(dp))) { - if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) { - tmp[i] = strdup(ep->d_name); - i++; - } - } - if (dp) - closedir(dp); - tmp[i] = NULL; - return memdup(tmp, sizeof(char*) * (i+1)); -} - - -int string_array_length(char ** a) -{ - int i = 0; - if (!a) - return -1; - while (a && *a) { - a++; - i++; - } - return i; -} - -int kernel_version(void) -{ - struct utsname val; - if (uname(&val)) { - log_perror("uname failed"); - return -1; - } - return charstar_to_int(val.release + 2); -} - int try_mount(char * dev, char * location) { char device_fullname[50]; @@ -585,39 +337,3 @@ char * floppy_device(void) else return "/dev/fd0"; } - -char * asprintf_(const char *msg, ...) -{ - int n; - char * s; - char dummy; - va_list arg_ptr; - va_start(arg_ptr, msg); - n = vsnprintf(&dummy, sizeof(dummy), msg, arg_ptr); - va_start(arg_ptr, msg); - if ((s = malloc(n + 1))) { - vsnprintf(s, n + 1, msg, arg_ptr); - va_end(arg_ptr); - return s; - } - va_end(arg_ptr); - return strdup(""); -} - -int scall_(int retval, char * msg, char * file, int line) -{ - char tmp[5000]; - sprintf(tmp, "%s(%s:%d) failed", msg, file, line); - if (retval) - log_perror(tmp); - return retval; -} - -void lowercase(char *s) -{ - int i = 0; - while (s[i]) { - s[i] = tolower(s[i]); - i++; - } -} diff --git a/mdk-stage1/tools.h b/mdk-stage1/tools.h index 78ed4b087..f8d9d4ee9 100644 --- a/mdk-stage1/tools.h +++ b/mdk-stage1/tools.h @@ -26,14 +26,6 @@ #include #include "bootsplash.h" -void process_cmdline(void); -int get_param(int i); -void set_param(int i); -void unset_param(int i); -void unset_automatic(void); -int charstar_to_int(const char * s); -off_t file_size(const char * path); -int total_memory(void); int image_has_stage2(); int ramdisk_possible(void); enum return_type copy_file(char * from, char * to, void (*callback_func)(int overall)); @@ -44,11 +36,6 @@ enum return_type mount_compressed_image(char *compressed_image, char *location_ enum return_type mount_compressed_image_may_preload(char *image_name, char *location_mount, int preload); enum return_type load_compressed_fd(int fd, int size); enum return_type may_load_compressed_image(void); -void * memdup(void *src, size_t size); -void add_to_env(char * name, char * value); -char ** list_directory(char * direct); -int string_array_length(char ** a); -int kernel_version(void); int try_mount(char * dev, char * location); #ifndef DISABLE_DISK int get_disks(char *** names, char *** models); @@ -57,18 +44,5 @@ int get_disks(char *** names, char *** models); int get_cdroms(char *** names, char *** models); #endif char * floppy_device(void); -char * asprintf_(const char *msg, ...); -int scall_(int retval, char * msg, char * file, int line); -#define scall(retval, msg) scall_(retval, msg, __FILE__, __LINE__) -void lowercase(char *s); - -struct param_elem -{ - char * name; - char * value; -}; - -#define ptr_begins_static_str(pointer,static_str) (!strncmp(pointer,static_str,sizeof(static_str)-1)) -#define streq(a,b) (!strcmp(a,b)) #endif diff --git a/mdk-stage1/url.c b/mdk-stage1/url.c index 640825cf5..77233847e 100644 --- a/mdk-stage1/url.c +++ b/mdk-stage1/url.c @@ -46,6 +46,7 @@ #include "dns.h" #include "log.h" #include "tools.h" +#include "utils.h" #include "url.h" diff --git a/mdk-stage1/utils.c b/mdk-stage1/utils.c new file mode 100644 index 000000000..5e25c41ff --- /dev/null +++ b/mdk-stage1/utils.c @@ -0,0 +1,157 @@ +/* + * Guillaume Cottenceau (gc@mandrakesoft.com) + * + * Copyright 2000 Mandrakesoft + * + * This software may be freely redistributed under the terms of the GNU + * public license. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" +#include "log.h" + +// warning, many things rely on the fact that: +// - when failing it returns 0 +// - it stops on first non-digit char +int charstar_to_int(const char * s) +{ + int number = 0; + while (*s && isdigit(*s)) { + number = (number * 10) + (*s - '0'); + s++; + } + return number; +} + +off_t file_size(const char * path) +{ + struct stat statr; + if (stat(path, &statr)) + return -1; + else + return statr.st_size; +} + +int total_memory(void) +{ + int value; + + /* drakx powered: use /proc/kcore and rounds every 4 Mbytes */ + value = 4 * ((int)((float)file_size("/proc/kcore") / 1024 / 1024 / 4 + 0.5)); + log_message("Total Memory: %d Mbytes", value); + + return value; +} + +/* pixel's */ +void * memdup(void *src, size_t size) +{ + void * r; + r = malloc(size); + memcpy(r, src, size); + return r; +} + + +void add_to_env(char * name, char * value) +{ + FILE* fakeenv = fopen("/tmp/env", "a"); + if (fakeenv) { + char* e = asprintf_("%s=%s\n", name, value); + fwrite(e, 1, strlen(e), fakeenv); + free(e); + fclose(fakeenv); + } else + log_message("couldn't fopen to fake env"); +} + +char ** list_directory(char * direct) +{ + char * tmp[50000]; /* in /dev there can be many many files.. */ + int i = 0; + struct dirent *ep; + DIR *dp = opendir(direct); + while (dp && (ep = readdir(dp))) { + if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) { + tmp[i] = strdup(ep->d_name); + i++; + } + } + if (dp) + closedir(dp); + tmp[i] = NULL; + return memdup(tmp, sizeof(char*) * (i+1)); +} + + +int string_array_length(char ** a) +{ + int i = 0; + if (!a) + return -1; + while (a && *a) { + a++; + i++; + } + return i; +} + +int kernel_version(void) +{ + struct utsname val; + if (uname(&val)) { + log_perror("uname failed"); + return -1; + } + return charstar_to_int(val.release + 2); +} + +char * asprintf_(const char *msg, ...) +{ + int n; + char * s; + char dummy; + va_list arg_ptr; + va_start(arg_ptr, msg); + n = vsnprintf(&dummy, sizeof(dummy), msg, arg_ptr); + va_start(arg_ptr, msg); + if ((s = malloc(n + 1))) { + vsnprintf(s, n + 1, msg, arg_ptr); + va_end(arg_ptr); + return s; + } + va_end(arg_ptr); + return strdup(""); +} + +int scall_(int retval, char * msg, char * file, int line) +{ + char tmp[5000]; + sprintf(tmp, "%s(%s:%d) failed", msg, file, line); + if (retval) + log_perror(tmp); + return retval; +} + +void lowercase(char *s) +{ + int i = 0; + while (s[i]) { + s[i] = tolower(s[i]); + i++; + } +} diff --git a/mdk-stage1/utils.h b/mdk-stage1/utils.h new file mode 100644 index 000000000..2f76109fc --- /dev/null +++ b/mdk-stage1/utils.h @@ -0,0 +1,34 @@ +/* + * Guillaume Cottenceau (gc@mandrakesoft.com) + * + * Copyright 2000 Mandrakesoft + * + * This software may be freely redistributed under the terms of the GNU + * public license. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef _UTILS_H_ +#define _UTILS_H_ + +int charstar_to_int(const char * s); +off_t file_size(const char * path); +int total_memory(void); +void * memdup(void *src, size_t size); +void add_to_env(char * name, char * value); +char ** list_directory(char * direct); +int string_array_length(char ** a); +int kernel_version(void); +char * asprintf_(const char *msg, ...); +int scall_(int retval, char * msg, char * file, int line); +#define scall(retval, msg) scall_(retval, msg, __FILE__, __LINE__) +void lowercase(char *s); + +#define ptr_begins_static_str(pointer,static_str) (!strncmp(pointer,static_str,sizeof(static_str)-1)) +#define streq(a,b) (!strcmp(a,b)) + +#endif diff --git a/mdk-stage1/wireless.c b/mdk-stage1/wireless.c index cefe6c1f7..a362573bb 100644 --- a/mdk-stage1/wireless.c +++ b/mdk-stage1/wireless.c @@ -24,6 +24,7 @@ #include "automatic.h" #include "stage1.h" #include "log.h" +#include "utils.h" #include "wireless.h" static int wireless_ioctl(int socket, const char *ifname, int request, struct iwreq *wrq); -- cgit v1.2.1