X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=utility.c;h=8c3c5e5fad4c10a6a29274b1685a30ab876e4ffa;hb=7c22b771625cee60ca37be7c4cd27f2c360d0aba;hp=50f497ff638b0700460f25102ad24f24ed7b8430;hpb=ae6eae02dd85a196a4545376a50166aede8ce7c1;p=oweals%2Fbusybox.git diff --git a/utility.c b/utility.c index 50f497ff6..8c3c5e5fa 100644 --- a/utility.c +++ b/utility.c @@ -50,7 +50,6 @@ #include #include #include -#include #include #include /* for uname(2) */ @@ -68,11 +67,11 @@ #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF -# if defined BB_FEATURE_USE_PROCFS -const char mtab_file[] = "/proc/mounts"; -# else -# if defined BB_MTAB +# if defined BB_MTAB const char mtab_file[] = "/etc/mtab"; +# else +# if defined BB_FEATURE_USE_PROCFS +const char mtab_file[] = "/proc/mounts"; # else # if defined BB_FEATURE_USE_DEVPS_PATCH const char mtab_file[] = "/dev/mtab"; @@ -154,6 +153,13 @@ extern _syscall5(int, mount, const char *, special_file, const char *, dir, const char *, fstype, unsigned long int, rwflag, const void *, data); #endif +#if defined BB_INSMOD || defined BB_LSMOD +#ifndef __NR_query_module +#define __NR_query_module 167 +#endif +_syscall5(int, query_module, const char *, name, int, which, + void *, buf, size_t, bufsize, size_t*, ret); +#endif #if defined (BB_CP_MV) || defined (BB_DU) @@ -258,6 +264,29 @@ int isDirectory(const char *fileName, const int followLinks, struct stat *statBu } #endif +#if defined (BB_AR) || defined BB_CP_MV +/* + * Copy readSize bytes between two file descriptors + */ +int copySubFile(int srcFd, int dstFd, size_t remaining) +{ + size_t size; + char buffer[BUFSIZ]; + + while (remaining > 0) { + if (remaining > BUFSIZ) + size = BUFSIZ; + else + size = remaining; + if (fullWrite(dstFd, buffer, fullRead(srcFd, buffer, size)) < size) + return(FALSE); + remaining -= size; + } + return (TRUE); +} +#endif + + #if defined (BB_CP_MV) /* * Copy one file to another, while possibly preserving its modes, times, and @@ -271,9 +300,7 @@ copyFile(const char *srcName, const char *destName, { int rfd; int wfd; - int rcc; int status; - char buf[BUF_SIZE]; struct stat srcStatBuf; struct stat dstStatBuf; struct utimbuf times; @@ -358,8 +385,7 @@ copyFile(const char *srcName, const char *destName, return FALSE; } - wfd = - open(destName, O_WRONLY | O_CREAT | O_TRUNC, + wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC, srcStatBuf.st_mode); if (wfd < 0) { perror(destName); @@ -367,14 +393,9 @@ copyFile(const char *srcName, const char *destName, return FALSE; } - while ((rcc = read(rfd, buf, sizeof(buf))) > 0) { - if (fullWrite(wfd, buf, rcc) < 0) - goto error_exit; - } - if (rcc < 0) { - goto error_exit; - } - + if (copySubFile(rfd, wfd, srcStatBuf.st_size)==FALSE) + goto error_exit; + close(rfd); if (close(wfd) < 0) { return FALSE; @@ -412,7 +433,7 @@ copyFile(const char *srcName, const char *destName, -#if defined BB_TAR || defined BB_LS +#if defined BB_TAR || defined BB_LS ||defined BB_AR #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) @@ -514,7 +535,7 @@ int fullWrite(int fd, const char *buf, int len) #endif /* BB_TAR || BB_CP_MV || BB_AR */ -#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH +#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV /* * Read all of the supplied buffer from a file. * This does multiple reads as necessary. @@ -641,7 +662,7 @@ int recursiveAction(const char *fileName, status = recursiveAction(nextFile, TRUE, followLinks, depthFirst, fileAction, dirAction, userData); - if (status < 0) { + if (status == FALSE) { closedir(dir); return FALSE; } @@ -706,7 +727,7 @@ extern int createPath(const char *name, int mode) #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \ - || defined (BB_MKFIFO) || defined (BB_MKNOD) + || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR) /* [ugoa]{+|-|=}[rwxst] */ @@ -722,6 +743,9 @@ extern int parse_mode(const char *s, mode_t * theMode) char type; char c; + if (s==NULL) + return (FALSE); + do { for (;;) { switch (c = *s++) { @@ -1012,82 +1036,6 @@ int get_console_fd(char *tty_name) #endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */ -#if !defined BB_REGEXP && (defined BB_GREP || 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 *) xmalloc((unsigned) (strlen(haystack))); - while (where != NULL) { - foundOne++; - strcpy(oldhayStack, haystack); - 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 /* ! BB_REGEXP && (BB_GREP || BB_SED) */ - - #if defined BB_FIND || defined BB_INSMOD /* * Routine to see if a text string is matched by a wildcard pattern. @@ -1286,7 +1234,7 @@ extern long getNum(const char *cp) #endif /* BB_DD || BB_TAIL */ -#if defined BB_INIT || defined BB_SYSLOGD || defined BB_AR +#if defined BB_INIT || defined BB_SYSLOGD /* try to open up the specified device */ extern int device_open(char *device, int mode) { @@ -1342,7 +1290,7 @@ extern pid_t* findPidByName( char* pidName) * some new processes start up while we wait. The kernel will * just ignore any extras if we give it too many, and will trunc. * the list if we give it too few. */ - pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); + pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); pid_array[0] = num_pids+10; /* Now grab the pid list */ @@ -1368,9 +1316,7 @@ extern pid_t* findPidByName( char* pidName) if ((strstr(info.command_line, pidName) != NULL) && (strlen(pidName) == strlen(info.command_line))) { - pidList=realloc( pidList, sizeof(pid_t) * (j+2)); - if (pidList==NULL) - fatalError(memory_exhausted); + pidList=xrealloc( pidList, sizeof(pid_t) * (j+2)); pidList[j++]=info.pid; } } @@ -1414,14 +1360,12 @@ extern pid_t* findPidByName( char* pidName) FILE *status; char filename[256]; char buffer[256]; - char* p; /* If it isn't a number, we don't want it */ if (!isdigit(*next->d_name)) continue; - /* Now open the status file */ - sprintf(filename, "/proc/%s/status", next->d_name); + sprintf(filename, "/proc/%s/cmdline", next->d_name); status = fopen(filename, "r"); if (!status) { continue; @@ -1429,24 +1373,12 @@ extern pid_t* findPidByName( char* pidName) fgets(buffer, 256, status); fclose(status); - /* Make sure we only match on the process name */ - p=buffer+5; /* Skip the name */ - while ((p)++) { - if (*p==0 || *p=='\n') { - *p='\0'; - break; - } - } - p=buffer+6; /* Skip the "Name:\t" */ - - if ((strstr(p, pidName) != NULL) - && (strlen(pidName) == strlen(p))) { - pidList=realloc( pidList, sizeof(pid_t) * (i+2)); - if (pidList==NULL) - fatalError(memory_exhausted); + if (strstr(get_last_path_component(buffer), pidName) != NULL) { + pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); pidList[i++]=strtol(next->d_name, NULL, 0); } } + if (pidList) pidList[i]=0; return pidList; @@ -1465,16 +1397,24 @@ extern void *xmalloc(size_t size) return ptr; } -void *xrealloc(void *old, size_t size) +extern void *xrealloc(void *old, size_t size) { void *ptr = realloc(old, size); if (!ptr) fatalError(memory_exhausted); return ptr; } + +extern void *xcalloc(size_t nmemb, size_t size) +{ + void *ptr = calloc(nmemb, size); + if (!ptr) + fatalError(memory_exhausted); + return ptr; +} #endif -#if defined BB_FEATURE_NFSMOUNT +#if defined BB_FEATURE_NFSMOUNT || defined BB_SH || defined BB_LS # ifndef DMALLOC extern char * xstrdup (const char *s) { char *t; @@ -1490,7 +1430,9 @@ extern char * xstrdup (const char *s) { return t; } # endif +#endif +#if defined BB_FEATURE_NFSMOUNT extern char * xstrndup (const char *s, int n) { char *t; @@ -1629,7 +1571,7 @@ extern int find_real_root_device_name(char* name) if (strcmp(entry->d_name, "..") == 0) continue; - sprintf( fileName, "/dev/%s", entry->d_name); + snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name); if (stat(fileName, &statBuf) != 0) continue; @@ -1666,7 +1608,7 @@ extern char *get_line_from_file(FILE *file) break; /* grow the line buffer as necessary */ if (idx > linebufsz-2) - linebuf = realloc(linebuf, linebufsz += GROWBY); + linebuf = xrealloc(linebuf, linebufsz += GROWBY); linebuf[idx++] = (char)ch; if ((char)ch == '\n') break; @@ -1747,7 +1689,7 @@ char process_escape_sequence(char **ptr) } #endif -#if defined BB_BASENAME || defined BB_LN +#if defined BB_BASENAME || defined BB_LN || defined BB_SH char *get_last_path_component(char *path) { char *s=path+strlen(path)-1;