+
+
+
+
+
+
+#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
+
+/* Use this to avoid needing the glibc NSS stuff
+ * This uses storage buf to hold things.
+ * */
+uid_t
+my_getid(const char *filename, char *name, uid_t id)
+{
+ FILE *file;
+ char *rname, *start, *end, buf[128];
+ uid_t rid;
+
+ file=fopen(filename,"r");
+ if (file == NULL) {
+ perror(filename);
+ return (-1);
+ }
+
+ while (fgets (buf, 128, file) != NULL) {
+ if (buf[0] == '#')
+ continue;
+
+ start = buf;
+ end = strchr (start, ':');
+ if (end == NULL)
+ continue;
+ *end = '\0';
+ rname = start;
+
+ start = end + 1;
+ end = strchr (start, ':');
+ if (end == NULL)
+ continue;
+
+ start = end + 1;
+ rid = (uid_t) strtol (start, &end, 10);
+ if (end == start)
+ continue;
+
+ if (name) {
+ if (0 == strcmp(rname, name))
+ return( rid);
+ }
+ if ( id != -1 && id == rid ) {
+ strncpy(name, rname, 8);
+ return( TRUE);
+ }
+ }
+ fclose(file);
+ return (-1);
+}
+
+uid_t
+my_getpwnam(char *name)
+{
+ return my_getid("/etc/passwd", name, -1);
+}
+
+gid_t
+my_getgrnam(char *name)
+{
+ return my_getid("/etc/group", name, -1);
+}
+
+void
+my_getpwuid(char* name, uid_t uid)
+{
+ my_getid("/etc/passwd", name, uid);
+}
+
+void
+my_getgrgid(char* group, gid_t gid)
+{
+ my_getid("/etc/group", group, gid);
+}
+
+
+#endif
+
+
+
+
+#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
+
+
+#include <linux/kd.h>
+#include <sys/ioctl.h>
+
+int is_a_console(int fd)
+{
+ char arg;
+
+ arg = 0;
+ return (ioctl(fd, KDGKBTYPE, &arg) == 0
+ && ((arg == KB_101) || (arg == KB_84)));
+}
+
+static int open_a_console(char *fnam)
+{
+ int fd;
+
+ /* try read-only */
+ fd = open(fnam, O_RDWR);
+
+ /* if failed, try read-only */
+ if (fd < 0 && errno == EACCES)
+ fd = open(fnam, O_RDONLY);
+
+ /* if failed, try write-only */
+ if (fd < 0 && errno == EACCES)
+ fd = open(fnam, O_WRONLY);
+
+ /* if failed, fail */
+ if (fd < 0)
+ return -1;
+
+ /* if not a console, fail */
+ if (! is_a_console(fd))
+ {
+ close(fd);
+ return -1;
+ }
+
+ /* success */
+ return fd;
+}
+
+/*
+ * Get an fd for use with kbd/console ioctls.
+ * We try several things because opening /dev/console will fail
+ * if someone else used X (which does a chown on /dev/console).
+ *
+ * if tty_name is non-NULL, try this one instead.
+ */
+
+int get_console_fd(char* tty_name)
+{
+ int fd;
+
+ if (tty_name)
+ {
+ if (-1 == (fd = open_a_console(tty_name)))
+ return -1;
+ else
+ return fd;
+ }
+
+ fd = open_a_console("/dev/tty");
+ if (fd >= 0)
+ return fd;
+
+ fd = open_a_console("/dev/tty0");
+ if (fd >= 0)
+ return fd;
+
+ fd = open_a_console("/dev/console");
+ if (fd >= 0)
+ return fd;
+
+ for (fd = 0; fd < 3; fd++)
+ if (is_a_console(fd))
+ return fd;
+
+ fprintf(stderr,
+ "Couldnt get a file descriptor referring to the console\n");
+ return -1; /* total failure */
+}
+
+
+#endif
+
+
+#if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED)
+
+/* Do a case insensitive strstr() */
+char* stristr(char *haystack, const char *needle)
+{
+ int len = strlen( needle );
+ while( *haystack ) {
+ if( !strncasecmp( haystack, needle, len ) )
+ break;
+ haystack++;
+ }
+
+ if( !(*haystack) )
+ haystack = NULL;
+
+ return haystack;
+}
+
+/* This tries to find a needle in a haystack, but does so by
+ * only trying to match literal strings (look 'ma, no regexps!)
+ * This is short, sweet, and carries _very_ little baggage,
+ * unlike its beefier cousin in regexp.c
+ * -Erik Andersen
+ */
+extern int find_match(char *haystack, char *needle, int ignoreCase)
+{
+
+ if (ignoreCase == FALSE)
+ haystack = strstr (haystack, needle);
+ else
+ haystack = stristr (haystack, needle);
+ if (haystack == NULL)
+ return FALSE;
+ return TRUE;
+}
+
+
+/* This performs substitutions after a string match has been found. */
+extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
+{
+ int foundOne=0;
+ char *where, *slider, *slider1, *oldhayStack;
+
+ if (ignoreCase == FALSE)
+ where = strstr (haystack, needle);
+ else
+ where = stristr (haystack, needle);
+
+ if (strcmp(needle, newNeedle)==0)
+ return FALSE;
+
+ oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
+ while(where!=NULL) {
+ foundOne++;
+ strcpy(oldhayStack, haystack);
+#if 0
+ if ( strlen(newNeedle) > strlen(needle)) {
+ haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
+ strlen(needle) + strlen(newNeedle)));
+ }
+#endif
+ for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
+ *slider=0;
+ haystack=strcat(haystack, newNeedle);
+ slider1+=strlen(needle);
+ haystack = strcat(haystack, slider1);
+ where = strstr (slider, needle);
+ }
+ free( oldhayStack);
+
+ if (foundOne > 0)
+ return TRUE;
+ else
+ return FALSE;
+}
+
+
+#endif