005-bsd-compat.patch 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345
  1. diff -Nur busybox-1.17.1.orig/include/libbb.h busybox-1.17.1/include/libbb.h
  2. --- busybox-1.17.1.orig/include/libbb.h 2010-07-25 00:12:43.000000000 +0200
  3. +++ busybox-1.17.1/include/libbb.h 2010-07-28 20:13:21.000000000 +0200
  4. @@ -40,6 +40,7 @@
  5. /* Try to pull in PATH_MAX */
  6. #include <limits.h>
  7. #include <sys/param.h>
  8. +#if !(defined __APPLE__ || defined BSD )
  9. #ifdef HAVE_MNTENT_H
  10. #include <mntent.h>
  11. #endif
  12. @@ -52,6 +53,7 @@
  13. #include <selinux/flask.h>
  14. #include <selinux/av_permissions.h>
  15. #endif
  16. +#endif
  17. #if ENABLE_LOCALE_SUPPORT
  18. # include <locale.h>
  19. #else
  20. diff -Nur busybox-1.17.1.orig/include/libbb.h.orig busybox-1.17.1/include/libbb.h.orig
  21. --- busybox-1.17.1.orig/include/libbb.h.orig 1970-01-01 01:00:00.000000000 +0100
  22. +++ busybox-1.17.1/include/libbb.h.orig 2010-07-25 00:12:43.000000000 +0200
  23. @@ -0,0 +1,1756 @@
  24. +/* vi: set sw=4 ts=4: */
  25. +/*
  26. + * Busybox main internal header file
  27. + *
  28. + * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
  29. + * Permission has been granted to redistribute this code under the GPL.
  30. + *
  31. + * Licensed under the GPL version 2, see the file LICENSE in this tarball.
  32. + */
  33. +#ifndef LIBBB_H
  34. +#define LIBBB_H 1
  35. +
  36. +#include "platform.h"
  37. +
  38. +#include <ctype.h>
  39. +#include <dirent.h>
  40. +#include <errno.h>
  41. +#include <fcntl.h>
  42. +#include <inttypes.h>
  43. +#include <netdb.h>
  44. +#include <setjmp.h>
  45. +#include <signal.h>
  46. +#include <stdint.h>
  47. +#include <stdio.h>
  48. +#include <stdlib.h>
  49. +#include <stdarg.h>
  50. +#include <stddef.h>
  51. +#include <string.h>
  52. +#include <sys/poll.h>
  53. +#include <sys/ioctl.h>
  54. +#include <sys/mman.h>
  55. +#include <sys/socket.h>
  56. +#include <sys/stat.h>
  57. +#include <sys/time.h>
  58. +#include <sys/types.h>
  59. +#include <sys/wait.h>
  60. +#include <termios.h>
  61. +#include <time.h>
  62. +#include <unistd.h>
  63. +/* Try to pull in PATH_MAX */
  64. +#include <limits.h>
  65. +#include <sys/param.h>
  66. +#ifdef HAVE_MNTENT_H
  67. +#include <mntent.h>
  68. +#endif
  69. +#ifdef HAVE_SYS_STATFS_H
  70. +#include <sys/statfs.h>
  71. +#endif
  72. +#if ENABLE_SELINUX
  73. +#include <selinux/selinux.h>
  74. +#include <selinux/context.h>
  75. +#include <selinux/flask.h>
  76. +#include <selinux/av_permissions.h>
  77. +#endif
  78. +#if ENABLE_LOCALE_SUPPORT
  79. +# include <locale.h>
  80. +#else
  81. +# define setlocale(x,y) ((void)0)
  82. +#endif
  83. +#ifdef DMALLOC
  84. +# include <dmalloc.h>
  85. +#endif
  86. +#include <pwd.h>
  87. +#include <grp.h>
  88. +#if ENABLE_FEATURE_SHADOWPASSWDS
  89. +# if !ENABLE_USE_BB_SHADOW
  90. +/* If using busybox's shadow implementation, do not include the shadow.h
  91. + * header as the toolchain may not provide it at all.
  92. + */
  93. +# include <shadow.h>
  94. +# endif
  95. +#endif
  96. +#if defined __FreeBSD__
  97. +# include <netinet/in.h>
  98. +# include <arpa/inet.h>
  99. +#elif defined __APPLE__
  100. +# include <netinet/in.h>
  101. +#else
  102. +# include <arpa/inet.h>
  103. +# if !defined(__socklen_t_defined) && !defined(_SOCKLEN_T_DECLARED)
  104. +/* We #define socklen_t *after* includes, otherwise we get
  105. + * typedef redefinition errors from system headers
  106. + * (in case "is it defined already" detection above failed)
  107. + */
  108. +# define socklen_t bb_socklen_t
  109. + typedef unsigned socklen_t;
  110. +# endif
  111. +#endif
  112. +
  113. +
  114. +/* Some libc's forget to declare these, do it ourself */
  115. +
  116. +extern char **environ;
  117. +#if defined(__GLIBC__) && __GLIBC__ < 2
  118. +int vdprintf(int d, const char *format, va_list ap);
  119. +#endif
  120. +/* klogctl is in libc's klog.h, but we cheat and not #include that */
  121. +int klogctl(int type, char *b, int len);
  122. +/* This is declared here rather than #including <libgen.h> in order to avoid
  123. + * confusing the two versions of basename. See the dirname/basename man page
  124. + * for details. */
  125. +#if !defined __FreeBSD__
  126. +char *dirname(char *path);
  127. +#endif
  128. +/* Include our own copy of struct sysinfo to avoid binary compatibility
  129. + * problems with Linux 2.4, which changed things. Grumble, grumble. */
  130. +struct sysinfo {
  131. + long uptime; /* Seconds since boot */
  132. + unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  133. + unsigned long totalram; /* Total usable main memory size */
  134. + unsigned long freeram; /* Available memory size */
  135. + unsigned long sharedram; /* Amount of shared memory */
  136. + unsigned long bufferram; /* Memory used by buffers */
  137. + unsigned long totalswap; /* Total swap space size */
  138. + unsigned long freeswap; /* swap space still available */
  139. + unsigned short procs; /* Number of current processes */
  140. + unsigned short pad; /* Padding needed for m68k */
  141. + unsigned long totalhigh; /* Total high memory size */
  142. + unsigned long freehigh; /* Available high memory size */
  143. + unsigned int mem_unit; /* Memory unit size in bytes */
  144. + char _f[20 - 2 * sizeof(long) - sizeof(int)]; /* Padding: libc5 uses this.. */
  145. +};
  146. +int sysinfo(struct sysinfo* info);
  147. +#ifndef PATH_MAX
  148. +# define PATH_MAX 256
  149. +#endif
  150. +#ifndef BUFSIZ
  151. +# define BUFSIZ 4096
  152. +#endif
  153. +
  154. +
  155. +/* Make all declarations hidden (-fvisibility flag only affects definitions) */
  156. +/* (don't include system headers after this until corresponding pop!) */
  157. +PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
  158. +
  159. +
  160. +#if ENABLE_USE_BB_PWD_GRP
  161. +# include "pwd_.h"
  162. +# include "grp_.h"
  163. +#endif
  164. +#if ENABLE_FEATURE_SHADOWPASSWDS
  165. +# if ENABLE_USE_BB_SHADOW
  166. +# include "shadow_.h"
  167. +# endif
  168. +#endif
  169. +
  170. +/* Tested to work correctly with all int types (IIRC :]) */
  171. +#define MAXINT(T) (T)( \
  172. + ((T)-1) > 0 \
  173. + ? (T)-1 \
  174. + : (T)~((T)1 << (sizeof(T)*8-1)) \
  175. + )
  176. +
  177. +#define MININT(T) (T)( \
  178. + ((T)-1) > 0 \
  179. + ? (T)0 \
  180. + : ((T)1 << (sizeof(T)*8-1)) \
  181. + )
  182. +
  183. +/* Large file support */
  184. +/* Note that CONFIG_LFS=y forces bbox to be built with all common ops
  185. + * (stat, lseek etc) mapped to "largefile" variants by libc.
  186. + * Practically it means that open() automatically has O_LARGEFILE added
  187. + * and all filesize/file_offset parameters and struct members are "large"
  188. + * (in today's world - signed 64bit). For full support of large files,
  189. + * we need a few helper #defines (below) and careful use of off_t
  190. + * instead of int/ssize_t. No lseek64(), O_LARGEFILE etc necessary */
  191. +#if ENABLE_LFS
  192. +/* CONFIG_LFS is on */
  193. +# if ULONG_MAX > 0xffffffff
  194. +/* "long" is long enough on this system */
  195. +typedef unsigned long uoff_t;
  196. +# define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
  197. +/* usage: sz = BB_STRTOOFF(s, NULL, 10); if (errno || sz < 0) die(); */
  198. +# define BB_STRTOOFF bb_strtoul
  199. +# define STRTOOFF strtoul
  200. +/* usage: printf("size: %"OFF_FMT"d (%"OFF_FMT"x)\n", sz, sz); */
  201. +# define OFF_FMT "l"
  202. +# else
  203. +/* "long" is too short, need "long long" */
  204. +typedef unsigned long long uoff_t;
  205. +# define XATOOFF(a) xatoull_range(a, 0, LLONG_MAX)
  206. +# define BB_STRTOOFF bb_strtoull
  207. +# define STRTOOFF strtoull
  208. +# define OFF_FMT "ll"
  209. +# endif
  210. +#else
  211. +/* CONFIG_LFS is off */
  212. +# if UINT_MAX == 0xffffffff
  213. +/* While sizeof(off_t) == sizeof(int), off_t is typedef'ed to long anyway.
  214. + * gcc will throw warnings on printf("%d", off_t). Crap... */
  215. +typedef unsigned long uoff_t;
  216. +# define XATOOFF(a) xatoi_u(a)
  217. +# define BB_STRTOOFF bb_strtou
  218. +# define STRTOOFF strtol
  219. +# define OFF_FMT "l"
  220. +# else
  221. +typedef unsigned long uoff_t;
  222. +# define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
  223. +# define BB_STRTOOFF bb_strtoul
  224. +# define STRTOOFF strtol
  225. +# define OFF_FMT "l"
  226. +# endif
  227. +#endif
  228. +/* scary. better ideas? (but do *test* them first!) */
  229. +#define OFF_T_MAX ((off_t)~((off_t)1 << (sizeof(off_t)*8-1)))
  230. +
  231. +/* Some useful definitions */
  232. +#undef FALSE
  233. +#define FALSE ((int) 0)
  234. +#undef TRUE
  235. +#define TRUE ((int) 1)
  236. +#undef SKIP
  237. +#define SKIP ((int) 2)
  238. +
  239. +/* for mtab.c */
  240. +#define MTAB_GETMOUNTPT '1'
  241. +#define MTAB_GETDEVICE '2'
  242. +
  243. +#define BUF_SIZE 8192
  244. +#define EXPAND_ALLOC 1024
  245. +
  246. +/* Macros for min/max. */
  247. +#ifndef MIN
  248. +#define MIN(a,b) (((a)<(b))?(a):(b))
  249. +#endif
  250. +
  251. +#ifndef MAX
  252. +#define MAX(a,b) (((a)>(b))?(a):(b))
  253. +#endif
  254. +
  255. +/* buffer allocation schemes */
  256. +#if ENABLE_FEATURE_BUFFERS_GO_ON_STACK
  257. +#define RESERVE_CONFIG_BUFFER(buffer,len) char buffer[len]
  258. +#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
  259. +#define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
  260. +#else
  261. +#if ENABLE_FEATURE_BUFFERS_GO_IN_BSS
  262. +#define RESERVE_CONFIG_BUFFER(buffer,len) static char buffer[len]
  263. +#define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
  264. +#define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
  265. +#else
  266. +#define RESERVE_CONFIG_BUFFER(buffer,len) char *buffer = xmalloc(len)
  267. +#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer = xmalloc(len)
  268. +#define RELEASE_CONFIG_BUFFER(buffer) free(buffer)
  269. +#endif
  270. +#endif
  271. +
  272. +#if defined(__GLIBC__)
  273. +/* glibc uses __errno_location() to get a ptr to errno */
  274. +/* We can just memorize it once - no multithreading in busybox :) */
  275. +extern int *const bb_errno;
  276. +#undef errno
  277. +#define errno (*bb_errno)
  278. +#endif
  279. +
  280. +unsigned long long monotonic_ns(void) FAST_FUNC;
  281. +unsigned long long monotonic_us(void) FAST_FUNC;
  282. +unsigned long long monotonic_ms(void) FAST_FUNC;
  283. +unsigned monotonic_sec(void) FAST_FUNC;
  284. +
  285. +extern void chomp(char *s) FAST_FUNC;
  286. +extern void trim(char *s) FAST_FUNC;
  287. +extern char *skip_whitespace(const char *) FAST_FUNC;
  288. +extern char *skip_non_whitespace(const char *) FAST_FUNC;
  289. +extern char *skip_dev_pfx(const char *tty_name) FAST_FUNC;
  290. +
  291. +extern char *strrstr(const char *haystack, const char *needle) FAST_FUNC;
  292. +
  293. +//TODO: supply a pointer to char[11] buffer (avoid statics)?
  294. +extern const char *bb_mode_string(mode_t mode) FAST_FUNC;
  295. +extern int is_directory(const char *name, int followLinks, struct stat *statBuf) FAST_FUNC;
  296. +enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */
  297. + FILEUTILS_PRESERVE_STATUS = 1 << 0, /* -p */
  298. + FILEUTILS_DEREFERENCE = 1 << 1, /* !-d */
  299. + FILEUTILS_RECUR = 1 << 2, /* -R */
  300. + FILEUTILS_FORCE = 1 << 3, /* -f */
  301. + FILEUTILS_INTERACTIVE = 1 << 4, /* -i */
  302. + FILEUTILS_MAKE_HARDLINK = 1 << 5, /* -l */
  303. + FILEUTILS_MAKE_SOFTLINK = 1 << 6, /* -s */
  304. + FILEUTILS_DEREF_SOFTLINK = 1 << 7, /* -L */
  305. + FILEUTILS_DEREFERENCE_L0 = 1 << 8, /* -H */
  306. +#if ENABLE_SELINUX
  307. + FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 9, /* -c */
  308. + FILEUTILS_SET_SECURITY_CONTEXT = 1 << 10,
  309. +#endif
  310. +};
  311. +#define FILEUTILS_CP_OPTSTR "pdRfilsLH" IF_SELINUX("c")
  312. +extern int remove_file(const char *path, int flags) FAST_FUNC;
  313. +/* NB: without FILEUTILS_RECUR in flags, it will basically "cat"
  314. + * the source, not copy (unless "source" is a directory).
  315. + * This makes "cp /dev/null file" and "install /dev/null file" (!!!)
  316. + * work coreutils-compatibly. */
  317. +extern int copy_file(const char *source, const char *dest, int flags) FAST_FUNC;
  318. +
  319. +enum {
  320. + ACTION_RECURSE = (1 << 0),
  321. + ACTION_FOLLOWLINKS = (1 << 1),
  322. + ACTION_FOLLOWLINKS_L0 = (1 << 2),
  323. + ACTION_DEPTHFIRST = (1 << 3),
  324. + /*ACTION_REVERSE = (1 << 4), - unused */
  325. + ACTION_QUIET = (1 << 5),
  326. + ACTION_DANGLING_OK = (1 << 6),
  327. +};
  328. +typedef uint8_t recurse_flags_t;
  329. +extern int recursive_action(const char *fileName, unsigned flags,
  330. + int FAST_FUNC (*fileAction)(const char *fileName, struct stat* statbuf, void* userData, int depth),
  331. + int FAST_FUNC (*dirAction)(const char *fileName, struct stat* statbuf, void* userData, int depth),
  332. + void* userData, unsigned depth) FAST_FUNC;
  333. +extern int device_open(const char *device, int mode) FAST_FUNC;
  334. +enum { GETPTY_BUFSIZE = 16 }; /* more than enough for "/dev/ttyXXX" */
  335. +extern int xgetpty(char *line) FAST_FUNC;
  336. +extern int get_console_fd_or_die(void) FAST_FUNC;
  337. +extern void console_make_active(int fd, const int vt_num) FAST_FUNC;
  338. +extern char *find_block_device(const char *path) FAST_FUNC;
  339. +/* bb_copyfd_XX print read/write errors and return -1 if they occur */
  340. +extern off_t bb_copyfd_eof(int fd1, int fd2) FAST_FUNC;
  341. +extern off_t bb_copyfd_size(int fd1, int fd2, off_t size) FAST_FUNC;
  342. +extern void bb_copyfd_exact_size(int fd1, int fd2, off_t size) FAST_FUNC;
  343. +/* "short" copy can be detected by return value < size */
  344. +/* this helper yells "short read!" if param is not -1 */
  345. +extern void complain_copyfd_and_die(off_t sz) NORETURN FAST_FUNC;
  346. +extern char bb_process_escape_sequence(const char **ptr) FAST_FUNC;
  347. +/* xxxx_strip version can modify its parameter:
  348. + * "/" -> "/"
  349. + * "abc" -> "abc"
  350. + * "abc/def" -> "def"
  351. + * "abc/def/" -> "def" !!
  352. + */
  353. +extern char *bb_get_last_path_component_strip(char *path) FAST_FUNC;
  354. +/* "abc/def/" -> "" and it never modifies 'path' */
  355. +extern char *bb_get_last_path_component_nostrip(const char *path) FAST_FUNC;
  356. +
  357. +int ndelay_on(int fd) FAST_FUNC;
  358. +int ndelay_off(int fd) FAST_FUNC;
  359. +int close_on_exec_on(int fd) FAST_FUNC;
  360. +void xdup2(int, int) FAST_FUNC;
  361. +void xmove_fd(int, int) FAST_FUNC;
  362. +
  363. +
  364. +DIR *xopendir(const char *path) FAST_FUNC;
  365. +DIR *warn_opendir(const char *path) FAST_FUNC;
  366. +
  367. +char *xmalloc_realpath(const char *path) FAST_FUNC RETURNS_MALLOC;
  368. +char *xmalloc_readlink(const char *path) FAST_FUNC RETURNS_MALLOC;
  369. +char *xmalloc_readlink_or_warn(const char *path) FAST_FUNC RETURNS_MALLOC;
  370. +/* !RETURNS_MALLOC: it's a realloc-like function */
  371. +char *xrealloc_getcwd_or_warn(char *cwd) FAST_FUNC;
  372. +
  373. +char *xmalloc_follow_symlinks(const char *path) FAST_FUNC RETURNS_MALLOC;
  374. +
  375. +
  376. +enum {
  377. + /* bb_signals(BB_FATAL_SIGS, handler) catches all signals which
  378. + * otherwise would kill us, except for those resulting from bugs:
  379. + * SIGSEGV, SIGILL, SIGFPE.
  380. + * Other fatal signals not included (TODO?):
  381. + * SIGBUS Bus error (bad memory access)
  382. + * SIGPOLL Pollable event. Synonym of SIGIO
  383. + * SIGPROF Profiling timer expired
  384. + * SIGSYS Bad argument to routine
  385. + * SIGTRAP Trace/breakpoint trap
  386. + *
  387. + * The only known arch with some of these sigs not fitting
  388. + * into 32 bits is parisc (SIGXCPU=33, SIGXFSZ=34, SIGSTKFLT=36).
  389. + * Dance around with long long to guard against that...
  390. + */
  391. + BB_FATAL_SIGS = (int)(0
  392. + + (1LL << SIGHUP)
  393. + + (1LL << SIGINT)
  394. + + (1LL << SIGTERM)
  395. + + (1LL << SIGPIPE) // Write to pipe with no readers
  396. + + (1LL << SIGQUIT) // Quit from keyboard
  397. + + (1LL << SIGABRT) // Abort signal from abort(3)
  398. + + (1LL << SIGALRM) // Timer signal from alarm(2)
  399. + + (1LL << SIGVTALRM) // Virtual alarm clock
  400. + + (1LL << SIGXCPU) // CPU time limit exceeded
  401. + + (1LL << SIGXFSZ) // File size limit exceeded
  402. + + (1LL << SIGUSR1) // Yes kids, these are also fatal!
  403. + + (1LL << SIGUSR2)
  404. + + 0),
  405. +};
  406. +void bb_signals(int sigs, void (*f)(int)) FAST_FUNC;
  407. +/* Unlike signal() and bb_signals, sets handler with sigaction()
  408. + * and in a way that while signal handler is run, no other signals
  409. + * will be blocked; syscalls will not be restarted: */
  410. +void bb_signals_recursive_norestart(int sigs, void (*f)(int)) FAST_FUNC;
  411. +/* syscalls like read() will be interrupted with EINTR: */
  412. +void signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int)) FAST_FUNC;
  413. +/* syscalls like read() won't be interrupted (though select/poll will be): */
  414. +void signal_SA_RESTART_empty_mask(int sig, void (*handler)(int)) FAST_FUNC;
  415. +void wait_for_any_sig(void) FAST_FUNC;
  416. +void kill_myself_with_sig(int sig) NORETURN FAST_FUNC;
  417. +void sig_block(int sig) FAST_FUNC;
  418. +void sig_unblock(int sig) FAST_FUNC;
  419. +/* Will do sigaction(signum, act, NULL): */
  420. +int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC;
  421. +/* SIG_BLOCK/SIG_UNBLOCK all signals: */
  422. +int sigprocmask_allsigs(int how) FAST_FUNC;
  423. +/* Standard handler which just records signo */
  424. +extern smallint bb_got_signal;
  425. +void record_signo(int signo); /* not FAST_FUNC! */
  426. +
  427. +
  428. +void xsetgid(gid_t gid) FAST_FUNC;
  429. +void xsetuid(uid_t uid) FAST_FUNC;
  430. +void xchdir(const char *path) FAST_FUNC;
  431. +void xchroot(const char *path) FAST_FUNC;
  432. +void xsetenv(const char *key, const char *value) FAST_FUNC;
  433. +void bb_unsetenv(const char *key) FAST_FUNC;
  434. +void bb_unsetenv_and_free(char *key) FAST_FUNC;
  435. +void xunlink(const char *pathname) FAST_FUNC;
  436. +void xstat(const char *pathname, struct stat *buf) FAST_FUNC;
  437. +int xopen(const char *pathname, int flags) FAST_FUNC;
  438. +int xopen_nonblocking(const char *pathname) FAST_FUNC;
  439. +int xopen3(const char *pathname, int flags, int mode) FAST_FUNC;
  440. +int open_or_warn(const char *pathname, int flags) FAST_FUNC;
  441. +int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
  442. +int open_or_warn_stdin(const char *pathname) FAST_FUNC;
  443. +void xrename(const char *oldpath, const char *newpath) FAST_FUNC;
  444. +int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC;
  445. +off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC;
  446. +off_t fdlength(int fd) FAST_FUNC;
  447. +
  448. +uoff_t FAST_FUNC get_volume_size_in_bytes(int fd,
  449. + const char *override,
  450. + unsigned override_units,
  451. + int extend);
  452. +
  453. +void xpipe(int filedes[2]) FAST_FUNC;
  454. +/* In this form code with pipes is much more readable */
  455. +struct fd_pair { int rd; int wr; };
  456. +#define piped_pair(pair) pipe(&((pair).rd))
  457. +#define xpiped_pair(pair) xpipe(&((pair).rd))
  458. +
  459. +/* Useful for having small structure members/global variables */
  460. +typedef int8_t socktype_t;
  461. +typedef int8_t family_t;
  462. +struct BUG_too_small {
  463. + char BUG_socktype_t_too_small[(0
  464. + | SOCK_STREAM
  465. + | SOCK_DGRAM
  466. + | SOCK_RDM
  467. + | SOCK_SEQPACKET
  468. + | SOCK_RAW
  469. + ) <= 127 ? 1 : -1];
  470. + char BUG_family_t_too_small[(0
  471. + | AF_UNSPEC
  472. + | AF_INET
  473. + | AF_INET6
  474. + | AF_UNIX
  475. +#ifdef AF_PACKET
  476. + | AF_PACKET
  477. +#endif
  478. +#ifdef AF_NETLINK
  479. + | AF_NETLINK
  480. +#endif
  481. + /* | AF_DECnet */
  482. + /* | AF_IPX */
  483. + ) <= 127 ? 1 : -1];
  484. +};
  485. +
  486. +
  487. +void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC;
  488. +time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC;
  489. +
  490. +
  491. +int xsocket(int domain, int type, int protocol) FAST_FUNC;
  492. +void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;
  493. +void xlisten(int s, int backlog) FAST_FUNC;
  494. +void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) FAST_FUNC;
  495. +ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  496. + socklen_t tolen) FAST_FUNC;
  497. +/* SO_REUSEADDR allows a server to rebind to an address that is already
  498. + * "in use" by old connections to e.g. previous server instance which is
  499. + * killed or crashed. Without it bind will fail until all such connections
  500. + * time out. Linux does not allow multiple live binds on same ip:port
  501. + * regardless of SO_REUSEADDR (unlike some other flavors of Unix).
  502. + * Turn it on before you call bind(). */
  503. +void setsockopt_reuseaddr(int fd) FAST_FUNC; /* On Linux this never fails. */
  504. +int setsockopt_broadcast(int fd) FAST_FUNC;
  505. +int setsockopt_bindtodevice(int fd, const char *iface) FAST_FUNC;
  506. +/* NB: returns port in host byte order */
  507. +unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port) FAST_FUNC;
  508. +typedef struct len_and_sockaddr {
  509. + socklen_t len;
  510. + union {
  511. + struct sockaddr sa;
  512. + struct sockaddr_in sin;
  513. +#if ENABLE_FEATURE_IPV6
  514. + struct sockaddr_in6 sin6;
  515. +#endif
  516. + } u;
  517. +} len_and_sockaddr;
  518. +enum {
  519. + LSA_LEN_SIZE = offsetof(len_and_sockaddr, u),
  520. + LSA_SIZEOF_SA = sizeof(
  521. + union {
  522. + struct sockaddr sa;
  523. + struct sockaddr_in sin;
  524. +#if ENABLE_FEATURE_IPV6
  525. + struct sockaddr_in6 sin6;
  526. +#endif
  527. + }
  528. + )
  529. +};
  530. +/* Create stream socket, and allocate suitable lsa.
  531. + * (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6))
  532. + * af == AF_UNSPEC will result in trying to create IPv6 socket,
  533. + * and if kernel doesn't support it, fall back to IPv4.
  534. + * This is useful if you plan to bind to resulting local lsa.
  535. + */
  536. +#if ENABLE_FEATURE_IPV6
  537. +int xsocket_type(len_and_sockaddr **lsap, int af, int sock_type) FAST_FUNC;
  538. +#else
  539. +int xsocket_type(len_and_sockaddr **lsap, int sock_type) FAST_FUNC;
  540. +#define xsocket_type(lsap, af, sock_type) xsocket_type((lsap), (sock_type))
  541. +#endif
  542. +int xsocket_stream(len_and_sockaddr **lsap) FAST_FUNC;
  543. +/* Create server socket bound to bindaddr:port. bindaddr can be NULL,
  544. + * numeric IP ("N.N.N.N") or numeric IPv6 address,
  545. + * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
  546. + * Only if there is no suffix, port argument is used */
  547. +/* NB: these set SO_REUSEADDR before bind */
  548. +int create_and_bind_stream_or_die(const char *bindaddr, int port) FAST_FUNC;
  549. +int create_and_bind_dgram_or_die(const char *bindaddr, int port) FAST_FUNC;
  550. +/* Create client TCP socket connected to peer:port. Peer cannot be NULL.
  551. + * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname,
  552. + * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
  553. + * If there is no suffix, port argument is used */
  554. +int create_and_connect_stream_or_die(const char *peer, int port) FAST_FUNC;
  555. +/* Connect to peer identified by lsa */
  556. +int xconnect_stream(const len_and_sockaddr *lsa) FAST_FUNC;
  557. +/* Get local address of bound or accepted socket */
  558. +len_and_sockaddr *get_sock_lsa(int fd) FAST_FUNC RETURNS_MALLOC;
  559. +/* Get remote address of connected or accepted socket */
  560. +len_and_sockaddr *get_peer_lsa(int fd) FAST_FUNC RETURNS_MALLOC;
  561. +/* Return malloc'ed len_and_sockaddr with socket address of host:port
  562. + * Currently will return IPv4 or IPv6 sockaddrs only
  563. + * (depending on host), but in theory nothing prevents e.g.
  564. + * UNIX socket address being returned, IPX sockaddr etc...
  565. + * On error does bb_error_msg and returns NULL */
  566. +len_and_sockaddr* host2sockaddr(const char *host, int port) FAST_FUNC RETURNS_MALLOC;
  567. +/* Version which dies on error */
  568. +len_and_sockaddr* xhost2sockaddr(const char *host, int port) FAST_FUNC RETURNS_MALLOC;
  569. +len_and_sockaddr* xdotted2sockaddr(const char *host, int port) FAST_FUNC RETURNS_MALLOC;
  570. +/* Same, useful if you want to force family (e.g. IPv6) */
  571. +#if !ENABLE_FEATURE_IPV6
  572. +#define host_and_af2sockaddr(host, port, af) host2sockaddr((host), (port))
  573. +#define xhost_and_af2sockaddr(host, port, af) xhost2sockaddr((host), (port))
  574. +#else
  575. +len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af) FAST_FUNC RETURNS_MALLOC;
  576. +len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af) FAST_FUNC RETURNS_MALLOC;
  577. +#endif
  578. +/* Assign sin[6]_port member if the socket is an AF_INET[6] one,
  579. + * otherwise no-op. Useful for ftp.
  580. + * NB: does NOT do htons() internally, just direct assignment. */
  581. +void set_nport(len_and_sockaddr *lsa, unsigned port) FAST_FUNC;
  582. +/* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */
  583. +int get_nport(const struct sockaddr *sa) FAST_FUNC;
  584. +/* Reverse DNS. Returns NULL on failure. */
  585. +char* xmalloc_sockaddr2host(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
  586. +/* This one doesn't append :PORTNUM */
  587. +char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
  588. +/* This one also doesn't fall back to dotted IP (returns NULL) */
  589. +char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
  590. +/* inet_[ap]ton on steroids */
  591. +char* xmalloc_sockaddr2dotted(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
  592. +char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
  593. +// "old" (ipv4 only) API
  594. +// users: traceroute.c hostname.c - use _list_ of all IPs
  595. +struct hostent *xgethostbyname(const char *name) FAST_FUNC;
  596. +// Also mount.c and inetd.c are using gethostbyname(),
  597. +// + inet_common.c has additional IPv4-only stuff
  598. +
  599. +
  600. +void socket_want_pktinfo(int fd) FAST_FUNC;
  601. +ssize_t send_to_from(int fd, void *buf, size_t len, int flags,
  602. + const struct sockaddr *to,
  603. + const struct sockaddr *from,
  604. + socklen_t tolen) FAST_FUNC;
  605. +ssize_t recv_from_to(int fd, void *buf, size_t len, int flags,
  606. + struct sockaddr *from,
  607. + struct sockaddr *to,
  608. + socklen_t sa_size) FAST_FUNC;
  609. +
  610. +
  611. +char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC;
  612. +char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC;
  613. +void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
  614. +char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
  615. +char *strncpy_IFNAMSIZ(char *dst, const char *src) FAST_FUNC;
  616. +/* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc.
  617. + * But potentially slow, don't use in one-billion-times loops */
  618. +int bb_putchar(int ch) FAST_FUNC;
  619. +/* Note: does not use stdio, writes to fd 2 directly */
  620. +int bb_putchar_stderr(char ch) FAST_FUNC;
  621. +char *xasprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2))) FAST_FUNC RETURNS_MALLOC;
  622. +// gcc-4.1.1 still isn't good enough at optimizing it
  623. +// (+200 bytes compared to macro)
  624. +//static ALWAYS_INLINE
  625. +//int LONE_DASH(const char *s) { return s[0] == '-' && !s[1]; }
  626. +//static ALWAYS_INLINE
  627. +//int NOT_LONE_DASH(const char *s) { return s[0] != '-' || s[1]; }
  628. +#define LONE_DASH(s) ((s)[0] == '-' && !(s)[1])
  629. +#define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
  630. +#define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
  631. +#define NOT_LONE_CHAR(s,c) ((s)[0] != (c) || (s)[1])
  632. +#define DOT_OR_DOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2])))
  633. +
  634. +typedef struct uni_stat_t {
  635. + unsigned byte_count;
  636. + unsigned unicode_count;
  637. + unsigned unicode_width;
  638. +} uni_stat_t;
  639. +/* Returns a string with unprintable chars replaced by '?' or
  640. + * SUBST_WCHAR. This function is unicode-aware. */
  641. +const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str);
  642. +/* Prints unprintable char ch as ^C or M-c to file
  643. + * (M-c is used only if ch is ORed with PRINTABLE_META),
  644. + * else it is printed as-is (except for ch = 0x9b) */
  645. +enum { PRINTABLE_META = 0x100 };
  646. +void fputc_printable(int ch, FILE *file) FAST_FUNC;
  647. +
  648. +/* dmalloc will redefine these to it's own implementation. It is safe
  649. + * to have the prototypes here unconditionally. */
  650. +void *malloc_or_warn(size_t size) FAST_FUNC RETURNS_MALLOC;
  651. +void *xmalloc(size_t size) FAST_FUNC RETURNS_MALLOC;
  652. +void *xzalloc(size_t size) FAST_FUNC RETURNS_MALLOC;
  653. +void *xrealloc(void *old, size_t size) FAST_FUNC;
  654. +/* After xrealloc_vector(v, 4, idx) it's ok to use
  655. + * at least v[idx] and v[idx+1], for all idx values.
  656. + * shift specifies how many new elements are added (1: 2, 2: 4... 8: 256...)
  657. + * when all elements are used up. New elements are zeroed out. */
  658. +#define xrealloc_vector(vector, shift, idx) \
  659. + xrealloc_vector_helper((vector), (sizeof((vector)[0]) << 8) + (shift), (idx))
  660. +void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) FAST_FUNC;
  661. +
  662. +
  663. +extern ssize_t safe_read(int fd, void *buf, size_t count) FAST_FUNC;
  664. +extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC;
  665. +// NB: will return short read on error, not -1,
  666. +// if some data was read before error occurred
  667. +extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC;
  668. +extern void xread(int fd, void *buf, size_t count) FAST_FUNC;
  669. +extern unsigned char xread_char(int fd) FAST_FUNC;
  670. +extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
  671. +extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
  672. +// Reads one line a-la fgets (but doesn't save terminating '\n').
  673. +// Reads byte-by-byte. Useful when it is important to not read ahead.
  674. +// Bytes are appended to pfx (which must be malloced, or NULL).
  675. +extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC;
  676. +/* Reads block up to *maxsz_p (default: INT_MAX - 4095) */
  677. +extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
  678. +/* Returns NULL if file can't be opened (default max size: INT_MAX - 4095) */
  679. +extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
  680. +/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
  681. +#if ENABLE_FEATURE_SEAMLESS_LZMA \
  682. + || ENABLE_FEATURE_SEAMLESS_BZ2 \
  683. + || ENABLE_FEATURE_SEAMLESS_GZ \
  684. + /* || ENABLE_FEATURE_SEAMLESS_Z */
  685. +extern void setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) FAST_FUNC;
  686. +#else
  687. +# define setup_unzip_on_fd(...) ((void)0)
  688. +#endif
  689. +/* Autodetects .gz etc */
  690. +extern int open_zipped(const char *fname) FAST_FUNC;
  691. +extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
  692. +/* Never returns NULL */
  693. +extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
  694. +
  695. +extern ssize_t safe_write(int fd, const void *buf, size_t count) FAST_FUNC;
  696. +// NB: will return short write on error, not -1,
  697. +// if some data was written before error occurred
  698. +extern ssize_t full_write(int fd, const void *buf, size_t count) FAST_FUNC;
  699. +extern void xwrite(int fd, const void *buf, size_t count) FAST_FUNC;
  700. +extern void xwrite_str(int fd, const char *str) FAST_FUNC;
  701. +extern ssize_t full_write1_str(const char *str) FAST_FUNC;
  702. +extern ssize_t full_write2_str(const char *str) FAST_FUNC;
  703. +extern void xopen_xwrite_close(const char* file, const char *str) FAST_FUNC;
  704. +
  705. +/* Close fd, but check for failures (some types of write errors) */
  706. +extern void xclose(int fd) FAST_FUNC;
  707. +
  708. +/* Reads and prints to stdout till eof, then closes FILE. Exits on error: */
  709. +extern void xprint_and_close_file(FILE *file) FAST_FUNC;
  710. +
  711. +extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC;
  712. +extern char *bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) FAST_FUNC;
  713. +/* Reads up to (and including) TERMINATING_STRING: */
  714. +extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC RETURNS_MALLOC;
  715. +/* Same, with limited max size, and returns the length (excluding NUL): */
  716. +extern char *xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
  717. +/* Chops off TERMINATING_STRING from the end: */
  718. +extern char *xmalloc_fgetline_str(FILE *file, const char *terminating_string) FAST_FUNC RETURNS_MALLOC;
  719. +/* Reads up to (and including) "\n" or NUL byte: */
  720. +extern char *xmalloc_fgets(FILE *file) FAST_FUNC RETURNS_MALLOC;
  721. +/* Chops off '\n' from the end, unlike fgets: */
  722. +extern char *xmalloc_fgetline(FILE *file) FAST_FUNC RETURNS_MALLOC;
  723. +/* Same, but doesn't try to conserve space (may have some slack after the end) */
  724. +/* extern char *xmalloc_fgetline_fast(FILE *file) FAST_FUNC RETURNS_MALLOC; */
  725. +
  726. +void die_if_ferror(FILE *file, const char *msg) FAST_FUNC;
  727. +void die_if_ferror_stdout(void) FAST_FUNC;
  728. +int fflush_all(void) FAST_FUNC;
  729. +void fflush_stdout_and_exit(int retval) NORETURN FAST_FUNC;
  730. +int fclose_if_not_stdin(FILE *file) FAST_FUNC;
  731. +FILE* xfopen(const char *filename, const char *mode) FAST_FUNC;
  732. +/* Prints warning to stderr and returns NULL on failure: */
  733. +FILE* fopen_or_warn(const char *filename, const char *mode) FAST_FUNC;
  734. +/* "Opens" stdin if filename is special, else just opens file: */
  735. +FILE* xfopen_stdin(const char *filename) FAST_FUNC;
  736. +FILE* fopen_or_warn_stdin(const char *filename) FAST_FUNC;
  737. +FILE* fopen_for_read(const char *path) FAST_FUNC;
  738. +FILE* xfopen_for_read(const char *path) FAST_FUNC;
  739. +FILE* fopen_for_write(const char *path) FAST_FUNC;
  740. +FILE* xfopen_for_write(const char *path) FAST_FUNC;
  741. +FILE* xfdopen_for_read(int fd) FAST_FUNC;
  742. +FILE* xfdopen_for_write(int fd) FAST_FUNC;
  743. +
  744. +int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */;
  745. +void qsort_string_vector(char **sv, unsigned count) FAST_FUNC;
  746. +
  747. +/* Wrapper which restarts poll on EINTR or ENOMEM.
  748. + * On other errors complains [perror("poll")] and returns.
  749. + * Warning! May take (much) longer than timeout_ms to return!
  750. + * If this is a problem, use bare poll and open-code EINTR/ENOMEM handling */
  751. +int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms) FAST_FUNC;
  752. +
  753. +char *safe_gethostname(void) FAST_FUNC;
  754. +char *safe_getdomainname(void) FAST_FUNC;
  755. +
  756. +/* Convert each alpha char in str to lower-case */
  757. +char* str_tolower(char *str) FAST_FUNC;
  758. +
  759. +char *utoa(unsigned n) FAST_FUNC;
  760. +char *itoa(int n) FAST_FUNC;
  761. +/* Returns a pointer past the formatted number, does NOT null-terminate */
  762. +char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) FAST_FUNC;
  763. +char *itoa_to_buf(int n, char *buf, unsigned buflen) FAST_FUNC;
  764. +/* Intelligent formatters of bignums */
  765. +void smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
  766. +void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
  767. +/* If block_size == 0, display size without fractional part,
  768. + * else display (size * block_size) with one decimal digit.
  769. + * If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...),
  770. + * else divide by display_unit and do not use suffix. */
  771. +#define HUMAN_READABLE_MAX_WIDTH 7 /* "1024.0G" */
  772. +#define HUMAN_READABLE_MAX_WIDTH_STR "7"
  773. +//TODO: provide pointer to buf (avoid statics)?
  774. +const char *make_human_readable_str(unsigned long long size,
  775. + unsigned long block_size, unsigned long display_unit) FAST_FUNC;
  776. +/* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */
  777. +char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC;
  778. +/* Reverse */
  779. +char* hex2bin(char *dst, const char *str, int count) FAST_FUNC;
  780. +
  781. +/* Generate a UUID */
  782. +void generate_uuid(uint8_t *buf) FAST_FUNC;
  783. +
  784. +/* Last element is marked by mult == 0 */
  785. +struct suffix_mult {
  786. + char suffix[4];
  787. + unsigned mult;
  788. +};
  789. +#include "xatonum.h"
  790. +/* Specialized: */
  791. +/* Using xatoi() instead of naive atoi() is not always convenient -
  792. + * in many places people want *non-negative* values, but store them
  793. + * in signed int. Therefore we need this one:
  794. + * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */
  795. +int xatoi_u(const char *numstr) FAST_FUNC;
  796. +/* Useful for reading port numbers */
  797. +uint16_t xatou16(const char *numstr) FAST_FUNC;
  798. +
  799. +
  800. +/* These parse entries in /etc/passwd and /etc/group. This is desirable
  801. + * for BusyBox since we want to avoid using the glibc NSS stuff, which
  802. + * increases target size and is often not needed on embedded systems. */
  803. +long xuname2uid(const char *name) FAST_FUNC;
  804. +long xgroup2gid(const char *name) FAST_FUNC;
  805. +/* wrapper: allows string to contain numeric uid or gid */
  806. +unsigned long get_ug_id(const char *s, long FAST_FUNC (*xname2id)(const char *)) FAST_FUNC;
  807. +/* from chpst. Does not die, returns 0 on failure */
  808. +struct bb_uidgid_t {
  809. + uid_t uid;
  810. + gid_t gid;
  811. +};
  812. +/* always sets uid and gid */
  813. +int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok) FAST_FUNC;
  814. +/* always sets uid and gid, allows numeric; exits on failure */
  815. +void xget_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC;
  816. +/* chown-like handling of "user[:[group]" */
  817. +void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) FAST_FUNC;
  818. +struct passwd* xgetpwnam(const char *name) FAST_FUNC;
  819. +struct group* xgetgrnam(const char *name) FAST_FUNC;
  820. +struct passwd* xgetpwuid(uid_t uid) FAST_FUNC;
  821. +struct group* xgetgrgid(gid_t gid) FAST_FUNC;
  822. +char* xuid2uname(uid_t uid) FAST_FUNC;
  823. +char* xgid2group(gid_t gid) FAST_FUNC;
  824. +char* uid2uname(uid_t uid) FAST_FUNC;
  825. +char* gid2group(gid_t gid) FAST_FUNC;
  826. +char* uid2uname_utoa(long uid) FAST_FUNC;
  827. +char* gid2group_utoa(long gid) FAST_FUNC;
  828. +/* versions which cache results (useful for ps, ls etc) */
  829. +const char* get_cached_username(uid_t uid) FAST_FUNC;
  830. +const char* get_cached_groupname(gid_t gid) FAST_FUNC;
  831. +void clear_username_cache(void) FAST_FUNC;
  832. +/* internally usernames are saved in fixed-sized char[] buffers */
  833. +enum { USERNAME_MAX_SIZE = 16 - sizeof(int) };
  834. +#if ENABLE_FEATURE_CHECK_NAMES
  835. +void die_if_bad_username(const char* name) FAST_FUNC;
  836. +#else
  837. +#define die_if_bad_username(name) ((void)(name))
  838. +#endif
  839. +
  840. +#if ENABLE_FEATURE_UTMP
  841. +void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
  842. +void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
  843. +#else
  844. +# define write_new_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
  845. +# define update_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
  846. +#endif
  847. +
  848. +int execable_file(const char *name) FAST_FUNC;
  849. +char *find_execable(const char *filename, char **PATHp) FAST_FUNC;
  850. +int exists_execable(const char *filename) FAST_FUNC;
  851. +
  852. +/* BB_EXECxx always execs (it's not doing NOFORK/NOEXEC stuff),
  853. + * but it may exec busybox and call applet instead of searching PATH.
  854. + */
  855. +#if ENABLE_FEATURE_PREFER_APPLETS
  856. +int bb_execvp(const char *file, char *const argv[]) FAST_FUNC;
  857. +#define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd)
  858. +#define BB_EXECLP(prog,cmd,...) \
  859. + execlp((find_applet_by_name(prog) >= 0) ? CONFIG_BUSYBOX_EXEC_PATH : prog, \
  860. + cmd, __VA_ARGS__)
  861. +#else
  862. +#define BB_EXECVP(prog,cmd) execvp(prog,cmd)
  863. +#define BB_EXECLP(prog,cmd,...) execlp(prog,cmd, __VA_ARGS__)
  864. +#endif
  865. +int BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
  866. +
  867. +/* xvfork() can't be a _function_, return after vfork mangles stack
  868. + * in the parent. It must be a macro. */
  869. +#define xvfork() \
  870. +({ \
  871. + pid_t bb__xvfork_pid = vfork(); \
  872. + if (bb__xvfork_pid < 0) \
  873. + bb_perror_msg_and_die("vfork"); \
  874. + bb__xvfork_pid; \
  875. +})
  876. +#if BB_MMU
  877. +pid_t xfork(void) FAST_FUNC;
  878. +#endif
  879. +
  880. +/* NOMMU friendy fork+exec: */
  881. +pid_t spawn(char **argv) FAST_FUNC;
  882. +pid_t xspawn(char **argv) FAST_FUNC;
  883. +
  884. +pid_t safe_waitpid(pid_t pid, int *wstat, int options) FAST_FUNC;
  885. +pid_t wait_any_nohang(int *wstat) FAST_FUNC;
  886. +/* wait4pid: unlike waitpid, waits ONLY for one process.
  887. + * Returns sig + 0x180 if child is killed by signal.
  888. + * It's safe to pass negative 'pids' from failed [v]fork -
  889. + * wait4pid will return -1 (and will not clobber [v]fork's errno).
  890. + * IOW: rc = wait4pid(spawn(argv));
  891. + * if (rc < 0) bb_perror_msg("%s", argv[0]);
  892. + * if (rc > 0) bb_error_msg("exit code: %d", rc & 0xff);
  893. + */
  894. +int wait4pid(pid_t pid) FAST_FUNC;
  895. +/* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */
  896. +int spawn_and_wait(char **argv) FAST_FUNC;
  897. +struct nofork_save_area {
  898. + jmp_buf die_jmp;
  899. + const char *applet_name;
  900. + int xfunc_error_retval;
  901. + uint32_t option_mask32;
  902. + int die_sleep;
  903. + smallint saved;
  904. +};
  905. +void save_nofork_data(struct nofork_save_area *save) FAST_FUNC;
  906. +void restore_nofork_data(struct nofork_save_area *save) FAST_FUNC;
  907. +/* Does NOT check that applet is NOFORK, just blindly runs it */
  908. +int run_nofork_applet(int applet_no, char **argv) FAST_FUNC;
  909. +int run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char **argv) FAST_FUNC;
  910. +
  911. +/* Helpers for daemonization.
  912. + *
  913. + * bb_daemonize(flags) = daemonize, does not compile on NOMMU
  914. + *
  915. + * bb_daemonize_or_rexec(flags, argv) = daemonizes on MMU (and ignores argv),
  916. + * rexec's itself on NOMMU with argv passed as command line.
  917. + * Thus bb_daemonize_or_rexec may cause your <applet>_main() to be re-executed
  918. + * from the start. (It will detect it and not reexec again second time).
  919. + * You have to audit carefully that you don't do something twice as a result
  920. + * (opening files/sockets, parsing config files etc...)!
  921. + *
  922. + * Both of the above will redirect fd 0,1,2 to /dev/null and drop ctty
  923. + * (will do setsid()).
  924. + *
  925. + * fork_or_rexec(argv) = bare-bones fork on MMU,
  926. + * "vfork + re-exec ourself" on NOMMU. No fd redirection, no setsid().
  927. + * On MMU ignores argv.
  928. + *
  929. + * Helper for network daemons in foreground mode:
  930. + *
  931. + * bb_sanitize_stdio() = make sure that fd 0,1,2 are opened by opening them
  932. + * to /dev/null if they are not.
  933. + */
  934. +enum {
  935. + DAEMON_CHDIR_ROOT = 1,
  936. + DAEMON_DEVNULL_STDIO = 2,
  937. + DAEMON_CLOSE_EXTRA_FDS = 4,
  938. + DAEMON_ONLY_SANITIZE = 8, /* internal use */
  939. +};
  940. +#if BB_MMU
  941. + enum { re_execed = 0 };
  942. +# define fork_or_rexec(argv) xfork()
  943. +# define bb_daemonize_or_rexec(flags, argv) bb_daemonize_or_rexec(flags)
  944. +# define bb_daemonize(flags) bb_daemonize_or_rexec(flags, bogus)
  945. +#else
  946. + extern bool re_execed;
  947. + void re_exec(char **argv) NORETURN FAST_FUNC;
  948. + pid_t fork_or_rexec(char **argv) FAST_FUNC;
  949. + int BUG_fork_is_unavailable_on_nommu(void) FAST_FUNC;
  950. + int BUG_daemon_is_unavailable_on_nommu(void) FAST_FUNC;
  951. + void BUG_bb_daemonize_is_unavailable_on_nommu(void) FAST_FUNC;
  952. +# define fork() BUG_fork_is_unavailable_on_nommu()
  953. +# define xfork() BUG_fork_is_unavailable_on_nommu()
  954. +# define daemon(a,b) BUG_daemon_is_unavailable_on_nommu()
  955. +# define bb_daemonize(a) BUG_bb_daemonize_is_unavailable_on_nommu()
  956. +#endif
  957. +void bb_daemonize_or_rexec(int flags, char **argv) FAST_FUNC;
  958. +void bb_sanitize_stdio(void) FAST_FUNC;
  959. +/* Clear dangerous stuff, set PATH. Return 1 if was run by different user. */
  960. +int sanitize_env_if_suid(void) FAST_FUNC;
  961. +
  962. +
  963. +char* single_argv(char **argv) FAST_FUNC;
  964. +extern const char *const bb_argv_dash[]; /* "-", NULL */
  965. +extern const char *opt_complementary;
  966. +#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
  967. +#define No_argument "\0"
  968. +#define Required_argument "\001"
  969. +#define Optional_argument "\002"
  970. +extern const char *applet_long_options;
  971. +#endif
  972. +extern uint32_t option_mask32;
  973. +extern uint32_t getopt32(char **argv, const char *applet_opts, ...) FAST_FUNC;
  974. +
  975. +
  976. +typedef struct llist_t {
  977. + char *data;
  978. + struct llist_t *link;
  979. +} llist_t;
  980. +void llist_add_to(llist_t **old_head, void *data) FAST_FUNC;
  981. +void llist_add_to_end(llist_t **list_head, void *data) FAST_FUNC;
  982. +void *llist_pop(llist_t **elm) FAST_FUNC;
  983. +void llist_unlink(llist_t **head, llist_t *elm) FAST_FUNC;
  984. +void llist_free(llist_t *elm, void (*freeit)(void *data)) FAST_FUNC;
  985. +llist_t *llist_rev(llist_t *list) FAST_FUNC;
  986. +llist_t *llist_find_str(llist_t *first, const char *str) FAST_FUNC;
  987. +/* BTW, surprisingly, changing API to
  988. + * llist_t *llist_add_to(llist_t *old_head, void *data)
  989. + * etc does not result in smaller code... */
  990. +
  991. +/* start_stop_daemon and udhcpc are special - they want
  992. + * to create pidfiles regardless of FEATURE_PIDFILE */
  993. +#if ENABLE_FEATURE_PIDFILE || defined(WANT_PIDFILE)
  994. +/* True only if we created pidfile which is *file*, not /dev/null etc */
  995. +extern smallint wrote_pidfile;
  996. +void write_pidfile(const char *path) FAST_FUNC;
  997. +#define remove_pidfile(path) do { if (wrote_pidfile) unlink(path); } while (0)
  998. +#else
  999. +enum { wrote_pidfile = 0 };
  1000. +#define write_pidfile(path) ((void)0)
  1001. +#define remove_pidfile(path) ((void)0)
  1002. +#endif
  1003. +
  1004. +enum {
  1005. + LOGMODE_NONE = 0,
  1006. + LOGMODE_STDIO = (1 << 0),
  1007. + LOGMODE_SYSLOG = (1 << 1) * ENABLE_FEATURE_SYSLOG,
  1008. + LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
  1009. +};
  1010. +extern const char *msg_eol;
  1011. +extern smallint logmode;
  1012. +extern int die_sleep;
  1013. +extern int xfunc_error_retval;
  1014. +extern jmp_buf die_jmp;
  1015. +extern void xfunc_die(void) NORETURN FAST_FUNC;
  1016. +extern void bb_show_usage(void) NORETURN FAST_FUNC;
  1017. +extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  1018. +extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
  1019. +extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  1020. +extern void bb_simple_perror_msg(const char *s) FAST_FUNC;
  1021. +extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
  1022. +extern void bb_simple_perror_msg_and_die(const char *s) NORETURN FAST_FUNC;
  1023. +extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  1024. +extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
  1025. +extern void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC;
  1026. +extern void bb_perror_nomsg(void) FAST_FUNC;
  1027. +extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  1028. +extern void bb_verror_msg(const char *s, va_list p, const char *strerr) FAST_FUNC;
  1029. +
  1030. +/* We need to export XXX_main from libbusybox
  1031. + * only if we build "individual" binaries
  1032. + */
  1033. +#if ENABLE_FEATURE_INDIVIDUAL
  1034. +#define MAIN_EXTERNALLY_VISIBLE EXTERNALLY_VISIBLE
  1035. +#else
  1036. +#define MAIN_EXTERNALLY_VISIBLE
  1037. +#endif
  1038. +
  1039. +
  1040. +/* Applets which are useful from another applets */
  1041. +int bb_cat(char** argv);
  1042. +/* If shell needs them, they exist even if not enabled as applets */
  1043. +int echo_main(int argc, char** argv) IF_ECHO(MAIN_EXTERNALLY_VISIBLE);
  1044. +int printf_main(int argc, char **argv) IF_PRINTF(MAIN_EXTERNALLY_VISIBLE);
  1045. +int test_main(int argc, char **argv) IF_TEST(MAIN_EXTERNALLY_VISIBLE);
  1046. +int kill_main(int argc, char **argv) IF_KILL(MAIN_EXTERNALLY_VISIBLE);
  1047. +/* Similar, but used by chgrp, not shell */
  1048. +int chown_main(int argc, char **argv) IF_CHOWN(MAIN_EXTERNALLY_VISIBLE);
  1049. +/* Used by ftpd */
  1050. +int ls_main(int argc, char **argv) IF_LS(MAIN_EXTERNALLY_VISIBLE);
  1051. +/* Don't need IF_xxx() guard for these */
  1052. +int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1053. +int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1054. +
  1055. +#if ENABLE_ROUTE
  1056. +void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
  1057. +#endif
  1058. +
  1059. +
  1060. +/* Networking */
  1061. +int create_icmp_socket(void) FAST_FUNC;
  1062. +int create_icmp6_socket(void) FAST_FUNC;
  1063. +/* interface.c */
  1064. +/* This structure defines protocol families and their handlers. */
  1065. +struct aftype {
  1066. + const char *name;
  1067. + const char *title;
  1068. + int af;
  1069. + int alen;
  1070. + char* FAST_FUNC (*print)(unsigned char *);
  1071. + const char* FAST_FUNC (*sprint)(struct sockaddr *, int numeric);
  1072. + int FAST_FUNC (*input)(/*int type,*/ const char *bufp, struct sockaddr *);
  1073. + void FAST_FUNC (*herror)(char *text);
  1074. + int FAST_FUNC (*rprint)(int options);
  1075. + int FAST_FUNC (*rinput)(int typ, int ext, char **argv);
  1076. + /* may modify src */
  1077. + int FAST_FUNC (*getmask)(char *src, struct sockaddr *mask, char *name);
  1078. +};
  1079. +/* This structure defines hardware protocols and their handlers. */
  1080. +struct hwtype {
  1081. + const char *name;
  1082. + const char *title;
  1083. + int type;
  1084. + int alen;
  1085. + char* FAST_FUNC (*print)(unsigned char *);
  1086. + int FAST_FUNC (*input)(const char *, struct sockaddr *);
  1087. + int FAST_FUNC (*activate)(int fd);
  1088. + int suppress_null_addr;
  1089. +};
  1090. +extern smallint interface_opt_a;
  1091. +int display_interfaces(char *ifname) FAST_FUNC;
  1092. +#if ENABLE_FEATURE_HWIB
  1093. +int in_ib(const char *bufp, struct sockaddr *sap) FAST_FUNC;
  1094. +#else
  1095. +#define in_ib(a, b) 1 /* fail */
  1096. +#endif
  1097. +const struct aftype *get_aftype(const char *name) FAST_FUNC;
  1098. +const struct hwtype *get_hwtype(const char *name) FAST_FUNC;
  1099. +const struct hwtype *get_hwntype(int type) FAST_FUNC;
  1100. +
  1101. +
  1102. +#ifndef BUILD_INDIVIDUAL
  1103. +extern int find_applet_by_name(const char *name) FAST_FUNC;
  1104. +/* Returns only if applet is not found. */
  1105. +extern void run_applet_and_exit(const char *name, char **argv) FAST_FUNC;
  1106. +extern void run_applet_no_and_exit(int a, char **argv) NORETURN FAST_FUNC;
  1107. +#endif
  1108. +
  1109. +#ifdef HAVE_MNTENT_H
  1110. +extern int match_fstype(const struct mntent *mt, const char *fstypes) FAST_FUNC;
  1111. +extern struct mntent *find_mount_point(const char *name, int subdir_too) FAST_FUNC;
  1112. +#endif
  1113. +extern void erase_mtab(const char * name) FAST_FUNC;
  1114. +extern unsigned int tty_baud_to_value(speed_t speed) FAST_FUNC;
  1115. +extern speed_t tty_value_to_baud(unsigned int value) FAST_FUNC;
  1116. +#if ENABLE_DESKTOP
  1117. +extern void bb_warn_ignoring_args(char *arg) FAST_FUNC;
  1118. +#else
  1119. +# define bb_warn_ignoring_args(arg) ((void)0)
  1120. +#endif
  1121. +
  1122. +extern int get_linux_version_code(void) FAST_FUNC;
  1123. +
  1124. +extern char *query_loop(const char *device) FAST_FUNC;
  1125. +extern int del_loop(const char *device) FAST_FUNC;
  1126. +/* If *devname is not NULL, use that name, otherwise try to find free one,
  1127. + * malloc and return it in *devname.
  1128. + * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */
  1129. +extern int set_loop(char **devname, const char *file, unsigned long long offset) FAST_FUNC;
  1130. +
  1131. +/* Like bb_ask below, but asks on stdin with no timeout. */
  1132. +char *bb_ask_stdin(const char * prompt) FAST_FUNC;
  1133. +//TODO: pass buf pointer or return allocated buf (avoid statics)?
  1134. +char *bb_ask(const int fd, int timeout, const char * prompt) FAST_FUNC;
  1135. +int bb_ask_confirmation(void) FAST_FUNC;
  1136. +
  1137. +int bb_parse_mode(const char* s, mode_t* theMode) FAST_FUNC;
  1138. +
  1139. +/*
  1140. + * Config file parser
  1141. + */
  1142. +enum {
  1143. + PARSE_COLLAPSE = 0x00010000, // treat consecutive delimiters as one
  1144. + PARSE_TRIM = 0x00020000, // trim leading and trailing delimiters
  1145. +// TODO: COLLAPSE and TRIM seem to always go in pair
  1146. + PARSE_GREEDY = 0x00040000, // last token takes entire remainder of the line
  1147. + PARSE_MIN_DIE = 0x00100000, // die if < min tokens found
  1148. + // keep a copy of current line
  1149. + PARSE_KEEP_COPY = 0x00200000 * ENABLE_FEATURE_CROND_D,
  1150. +// PARSE_ESCAPE = 0x00400000, // process escape sequences in tokens
  1151. + // NORMAL is:
  1152. + // * remove leading and trailing delimiters and collapse
  1153. + // multiple delimiters into one
  1154. + // * warn and continue if less than mintokens delimiters found
  1155. + // * grab everything into last token
  1156. + PARSE_NORMAL = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY,
  1157. +};
  1158. +typedef struct parser_t {
  1159. + FILE *fp;
  1160. + char *line;
  1161. + char *data;
  1162. + int lineno;
  1163. +} parser_t;
  1164. +parser_t* config_open(const char *filename) FAST_FUNC;
  1165. +parser_t* config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) FAST_FUNC;
  1166. +/* delims[0] is a comment char (use '\0' to disable), the rest are token delimiters */
  1167. +int config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims) FAST_FUNC;
  1168. +#define config_read(parser, tokens, max, min, str, flags) \
  1169. + config_read(parser, tokens, ((flags) | (((min) & 0xFF) << 8) | ((max) & 0xFF)), str)
  1170. +void config_close(parser_t *parser) FAST_FUNC;
  1171. +
  1172. +/* Concatenate path and filename to new allocated buffer.
  1173. + * Add "/" only as needed (no duplicate "//" are produced).
  1174. + * If path is NULL, it is assumed to be "/".
  1175. + * filename should not be NULL. */
  1176. +char *concat_path_file(const char *path, const char *filename) FAST_FUNC;
  1177. +char *concat_subpath_file(const char *path, const char *filename) FAST_FUNC;
  1178. +const char *bb_basename(const char *name) FAST_FUNC;
  1179. +/* NB: can violate const-ness (similarly to strchr) */
  1180. +char *last_char_is(const char *s, int c) FAST_FUNC;
  1181. +
  1182. +
  1183. +int bb_make_directory(char *path, long mode, int flags) FAST_FUNC;
  1184. +
  1185. +int get_signum(const char *name) FAST_FUNC;
  1186. +const char *get_signame(int number) FAST_FUNC;
  1187. +void print_signames(void) FAST_FUNC;
  1188. +
  1189. +char *bb_simplify_path(const char *path) FAST_FUNC;
  1190. +/* Returns ptr to NUL */
  1191. +char *bb_simplify_abs_path_inplace(char *path) FAST_FUNC;
  1192. +
  1193. +#define FAIL_DELAY 3
  1194. +extern void bb_do_delay(int seconds) FAST_FUNC;
  1195. +extern void change_identity(const struct passwd *pw) FAST_FUNC;
  1196. +extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) NORETURN FAST_FUNC;
  1197. +#if ENABLE_SELINUX
  1198. +extern void renew_current_security_context(void) FAST_FUNC;
  1199. +extern void set_current_security_context(security_context_t sid) FAST_FUNC;
  1200. +extern context_t set_security_context_component(security_context_t cur_context,
  1201. + char *user, char *role, char *type, char *range) FAST_FUNC;
  1202. +extern void setfscreatecon_or_die(security_context_t scontext) FAST_FUNC;
  1203. +extern void selinux_preserve_fcontext(int fdesc) FAST_FUNC;
  1204. +#else
  1205. +#define selinux_preserve_fcontext(fdesc) ((void)0)
  1206. +#endif
  1207. +extern void selinux_or_die(void) FAST_FUNC;
  1208. +
  1209. +/* setup_environment:
  1210. + * if chdir pw->pw_dir: ok: else if to_tmp == 1: goto /tmp else: goto / or die
  1211. + * if clear_env = 1: cd(pw->pw_dir), clear environment, then set
  1212. + * TERM=(old value)
  1213. + * USER=pw->pw_name, LOGNAME=pw->pw_name
  1214. + * PATH=bb_default_[root_]path
  1215. + * HOME=pw->pw_dir
  1216. + * SHELL=shell
  1217. + * else if change_env = 1:
  1218. + * if not root (if pw->pw_uid != 0):
  1219. + * USER=pw->pw_name, LOGNAME=pw->pw_name
  1220. + * HOME=pw->pw_dir
  1221. + * SHELL=shell
  1222. + * else does nothing
  1223. + */
  1224. +#define SETUP_ENV_CHANGEENV (1 << 0)
  1225. +#define SETUP_ENV_CLEARENV (1 << 1)
  1226. +#define SETUP_ENV_TO_TMP (1 << 2)
  1227. +extern void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC;
  1228. +extern int correct_password(const struct passwd *pw) FAST_FUNC;
  1229. +/* Returns a malloced string */
  1230. +#if !ENABLE_USE_BB_CRYPT
  1231. +#define pw_encrypt(clear, salt, cleanup) pw_encrypt(clear, salt)
  1232. +#endif
  1233. +extern char *pw_encrypt(const char *clear, const char *salt, int cleanup) FAST_FUNC;
  1234. +extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC;
  1235. +/* rnd is additional random input. New one is returned.
  1236. + * Useful if you call crypt_make_salt many times in a row:
  1237. + * rnd = crypt_make_salt(buf1, 4, 0);
  1238. + * rnd = crypt_make_salt(buf2, 4, rnd);
  1239. + * rnd = crypt_make_salt(buf3, 4, rnd);
  1240. + * (otherwise we risk having same salt generated)
  1241. + */
  1242. +extern int crypt_make_salt(char *p, int cnt, int rnd) FAST_FUNC;
  1243. +
  1244. +/* Returns number of lines changed, or -1 on error */
  1245. +#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
  1246. +#define update_passwd(filename, username, data, member) \
  1247. + update_passwd(filename, username, data)
  1248. +#endif
  1249. +extern int update_passwd(const char *filename,
  1250. + const char *username,
  1251. + const char *data,
  1252. + const char *member) FAST_FUNC;
  1253. +
  1254. +int index_in_str_array(const char *const string_array[], const char *key) FAST_FUNC;
  1255. +int index_in_strings(const char *strings, const char *key) FAST_FUNC;
  1256. +int index_in_substr_array(const char *const string_array[], const char *key) FAST_FUNC;
  1257. +int index_in_substrings(const char *strings, const char *key) FAST_FUNC;
  1258. +const char *nth_string(const char *strings, int n) FAST_FUNC;
  1259. +
  1260. +extern void print_login_issue(const char *issue_file, const char *tty) FAST_FUNC;
  1261. +extern void print_login_prompt(void) FAST_FUNC;
  1262. +
  1263. +char *xmalloc_ttyname(int fd) FAST_FUNC RETURNS_MALLOC;
  1264. +/* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */
  1265. +int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC;
  1266. +
  1267. +int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC;
  1268. +
  1269. +/* NB: "unsigned request" is crucial! "int request" will break some arches! */
  1270. +int ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))) FAST_FUNC;
  1271. +int ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))) FAST_FUNC;
  1272. +#if ENABLE_IOCTL_HEX2STR_ERROR
  1273. +int bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC;
  1274. +int bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC;
  1275. +#define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp,#request)
  1276. +#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp,#request)
  1277. +#else
  1278. +int bb_ioctl_or_warn(int fd, unsigned request, void *argp) FAST_FUNC;
  1279. +int bb_xioctl(int fd, unsigned request, void *argp) FAST_FUNC;
  1280. +#define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp)
  1281. +#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp)
  1282. +#endif
  1283. +
  1284. +char *is_in_ino_dev_hashtable(const struct stat *statbuf) FAST_FUNC;
  1285. +void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) FAST_FUNC;
  1286. +void reset_ino_dev_hashtable(void) FAST_FUNC;
  1287. +#ifdef __GLIBC__
  1288. +/* At least glibc has horrendously large inline for this, so wrap it */
  1289. +unsigned long long bb_makedev(unsigned int major, unsigned int minor) FAST_FUNC;
  1290. +#undef makedev
  1291. +#define makedev(a,b) bb_makedev(a,b)
  1292. +#endif
  1293. +
  1294. +
  1295. +/* "Keycodes" that report an escape sequence.
  1296. + * We use something which fits into signed char,
  1297. + * yet doesn't represent any valid Unicode character.
  1298. + * Also, -1 is reserved for error indication and we don't use it. */
  1299. +enum {
  1300. + KEYCODE_UP = -2,
  1301. + KEYCODE_DOWN = -3,
  1302. + KEYCODE_RIGHT = -4,
  1303. + KEYCODE_LEFT = -5,
  1304. + KEYCODE_HOME = -6,
  1305. + KEYCODE_END = -7,
  1306. + KEYCODE_INSERT = -8,
  1307. + KEYCODE_DELETE = -9,
  1308. + KEYCODE_PAGEUP = -10,
  1309. + KEYCODE_PAGEDOWN = -11,
  1310. +
  1311. + KEYCODE_CTRL_UP = KEYCODE_UP & ~0x40,
  1312. + KEYCODE_CTRL_DOWN = KEYCODE_DOWN & ~0x40,
  1313. + KEYCODE_CTRL_RIGHT = KEYCODE_RIGHT & ~0x40,
  1314. + KEYCODE_CTRL_LEFT = KEYCODE_LEFT & ~0x40,
  1315. +#if 0
  1316. + KEYCODE_FUN1 = -12,
  1317. + KEYCODE_FUN2 = -13,
  1318. + KEYCODE_FUN3 = -14,
  1319. + KEYCODE_FUN4 = -15,
  1320. + KEYCODE_FUN5 = -16,
  1321. + KEYCODE_FUN6 = -17,
  1322. + KEYCODE_FUN7 = -18,
  1323. + KEYCODE_FUN8 = -19,
  1324. + KEYCODE_FUN9 = -20,
  1325. + KEYCODE_FUN10 = -21,
  1326. + KEYCODE_FUN11 = -22,
  1327. + KEYCODE_FUN12 = -23,
  1328. +#endif
  1329. + KEYCODE_CURSOR_POS = -0x100, /* 0xfff..fff00 */
  1330. + /* How long is the longest ESC sequence we know?
  1331. + * We want it big enough to be able to contain
  1332. + * cursor position sequence "ESC [ 9999 ; 9999 R"
  1333. + */
  1334. + KEYCODE_BUFFER_SIZE = 16
  1335. +};
  1336. +/* Note: fd may be in blocking or non-blocking mode, both make sense.
  1337. + * For one, less uses non-blocking mode.
  1338. + * Only the first read syscall inside read_key may block indefinitely
  1339. + * (unless fd is in non-blocking mode),
  1340. + * subsequent reads will time out after a few milliseconds.
  1341. + * Return of -1 means EOF or error (errno == 0 on EOF).
  1342. + * buffer[0] is used as a counter of buffered chars and must be 0
  1343. + * on first call.
  1344. + * timeout:
  1345. + * -2: do not poll for input;
  1346. + * -1: poll(-1) (i.e. block);
  1347. + * >=0: poll for TIMEOUT milliseconds, return -1/EAGAIN on timeout
  1348. + */
  1349. +int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC;
  1350. +void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC;
  1351. +
  1352. +
  1353. +#if ENABLE_FEATURE_EDITING
  1354. +/* It's NOT just ENABLEd or disabled. It's a number: */
  1355. +# ifdef CONFIG_FEATURE_EDITING_HISTORY
  1356. +# define MAX_HISTORY (CONFIG_FEATURE_EDITING_HISTORY + 0)
  1357. +# else
  1358. +# define MAX_HISTORY 0
  1359. +# endif
  1360. +typedef struct line_input_t {
  1361. + int flags;
  1362. + const char *path_lookup;
  1363. +# if MAX_HISTORY
  1364. + int cnt_history;
  1365. + int cur_history;
  1366. +# if ENABLE_FEATURE_EDITING_SAVEHISTORY
  1367. + unsigned cnt_history_in_file;
  1368. + const char *hist_file;
  1369. +# endif
  1370. + char *history[MAX_HISTORY + 1];
  1371. +# endif
  1372. +} line_input_t;
  1373. +enum {
  1374. + DO_HISTORY = 1 * (MAX_HISTORY > 0),
  1375. + SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY,
  1376. + TAB_COMPLETION = 4 * ENABLE_FEATURE_TAB_COMPLETION,
  1377. + USERNAME_COMPLETION = 8 * ENABLE_FEATURE_USERNAME_COMPLETION,
  1378. + VI_MODE = 0x10 * ENABLE_FEATURE_EDITING_VI,
  1379. + WITH_PATH_LOOKUP = 0x20,
  1380. + FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
  1381. +};
  1382. +line_input_t *new_line_input_t(int flags) FAST_FUNC;
  1383. +/* So far static: void free_line_input_t(line_input_t *n) FAST_FUNC; */
  1384. +/* maxsize must be >= 2.
  1385. + * Returns:
  1386. + * -1 on read errors or EOF, or on bare Ctrl-D,
  1387. + * 0 on ctrl-C (the line entered is still returned in 'command'),
  1388. + * >0 length of input string, including terminating '\n'
  1389. + */
  1390. +int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state) FAST_FUNC;
  1391. +#else
  1392. +#define MAX_HISTORY 0
  1393. +int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
  1394. +#define read_line_input(prompt, command, maxsize, state) \
  1395. + read_line_input(prompt, command, maxsize)
  1396. +#endif
  1397. +
  1398. +
  1399. +#ifndef COMM_LEN
  1400. +# ifdef TASK_COMM_LEN
  1401. +enum { COMM_LEN = TASK_COMM_LEN };
  1402. +# else
  1403. +/* synchronize with sizeof(task_struct.comm) in /usr/include/linux/sched.h */
  1404. +enum { COMM_LEN = 16 };
  1405. +# endif
  1406. +#endif
  1407. +typedef struct procps_status_t {
  1408. + DIR *dir;
  1409. + IF_FEATURE_SHOW_THREADS(DIR *task_dir;)
  1410. + uint8_t shift_pages_to_bytes;
  1411. + uint8_t shift_pages_to_kb;
  1412. +/* Fields are set to 0/NULL if failed to determine (or not requested) */
  1413. + uint16_t argv_len;
  1414. + char *argv0;
  1415. + char *exe;
  1416. + IF_SELINUX(char *context;)
  1417. + /* Everything below must contain no ptrs to malloc'ed data:
  1418. + * it is memset(0) for each process in procps_scan() */
  1419. + unsigned long vsz, rss; /* we round it to kbytes */
  1420. + unsigned long stime, utime;
  1421. + unsigned long start_time;
  1422. + unsigned pid;
  1423. + unsigned ppid;
  1424. + unsigned pgid;
  1425. + unsigned sid;
  1426. + unsigned uid;
  1427. + unsigned gid;
  1428. +#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
  1429. + unsigned ruid;
  1430. + unsigned rgid;
  1431. + int niceness;
  1432. +#endif
  1433. + unsigned tty_major,tty_minor;
  1434. +#if ENABLE_FEATURE_TOPMEM
  1435. + unsigned long mapped_rw;
  1436. + unsigned long mapped_ro;
  1437. + unsigned long shared_clean;
  1438. + unsigned long shared_dirty;
  1439. + unsigned long private_clean;
  1440. + unsigned long private_dirty;
  1441. + unsigned long stack;
  1442. +#endif
  1443. + char state[4];
  1444. + /* basename of executable in exec(2), read from /proc/N/stat
  1445. + * (if executable is symlink or script, it is NOT replaced
  1446. + * by link target or interpreter name) */
  1447. + char comm[COMM_LEN];
  1448. + /* user/group? - use passwd/group parsing functions */
  1449. +#if ENABLE_FEATURE_TOP_SMP_PROCESS
  1450. + int last_seen_on_cpu;
  1451. +#endif
  1452. +} procps_status_t;
  1453. +/* flag bits for procps_scan(xx, flags) calls */
  1454. +enum {
  1455. + PSSCAN_PID = 1 << 0,
  1456. + PSSCAN_PPID = 1 << 1,
  1457. + PSSCAN_PGID = 1 << 2,
  1458. + PSSCAN_SID = 1 << 3,
  1459. + PSSCAN_UIDGID = 1 << 4,
  1460. + PSSCAN_COMM = 1 << 5,
  1461. + /* PSSCAN_CMD = 1 << 6, - use read_cmdline instead */
  1462. + PSSCAN_ARGV0 = 1 << 7,
  1463. + PSSCAN_EXE = 1 << 8,
  1464. + PSSCAN_STATE = 1 << 9,
  1465. + PSSCAN_VSZ = 1 << 10,
  1466. + PSSCAN_RSS = 1 << 11,
  1467. + PSSCAN_STIME = 1 << 12,
  1468. + PSSCAN_UTIME = 1 << 13,
  1469. + PSSCAN_TTY = 1 << 14,
  1470. + PSSCAN_SMAPS = (1 << 15) * ENABLE_FEATURE_TOPMEM,
  1471. + /* NB: used by find_pid_by_name(). Any applet using it
  1472. + * needs to be mentioned here. */
  1473. + PSSCAN_ARGVN = (1 << 16) * (ENABLE_KILLALL
  1474. + || ENABLE_PGREP || ENABLE_PKILL
  1475. + || ENABLE_PIDOF
  1476. + || ENABLE_SESTATUS
  1477. + ),
  1478. + PSSCAN_CONTEXT = (1 << 17) * ENABLE_SELINUX,
  1479. + PSSCAN_START_TIME = 1 << 18,
  1480. + PSSCAN_CPU = (1 << 19) * ENABLE_FEATURE_TOP_SMP_PROCESS,
  1481. + PSSCAN_NICE = (1 << 20) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS,
  1482. + PSSCAN_RUIDGID = (1 << 21) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS,
  1483. + PSSCAN_TASKS = (1 << 22) * ENABLE_FEATURE_SHOW_THREADS,
  1484. + /* These are all retrieved from proc/NN/stat in one go: */
  1485. + PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
  1486. + /**/ | PSSCAN_COMM | PSSCAN_STATE
  1487. + /**/ | PSSCAN_VSZ | PSSCAN_RSS
  1488. + /**/ | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
  1489. + /**/ | PSSCAN_TTY | PSSCAN_NICE
  1490. + /**/ | PSSCAN_CPU
  1491. +};
  1492. +//procps_status_t* alloc_procps_scan(void) FAST_FUNC;
  1493. +void free_procps_scan(procps_status_t* sp) FAST_FUNC;
  1494. +procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
  1495. +/* Format cmdline (up to col chars) into char buf[size] */
  1496. +/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
  1497. +void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
  1498. +pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
  1499. +pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
  1500. +
  1501. +
  1502. +extern const char bb_uuenc_tbl_base64[];
  1503. +extern const char bb_uuenc_tbl_std[];
  1504. +void bb_uuencode(char *store, const void *s, int length, const char *tbl) FAST_FUNC;
  1505. +
  1506. +typedef struct sha1_ctx_t {
  1507. + uint32_t hash[8]; /* 5, +3 elements for sha256 */
  1508. + uint64_t total64;
  1509. + uint8_t wbuffer[64]; /* NB: always correctly aligned for uint64_t */
  1510. + void (*process_block)(struct sha1_ctx_t*) FAST_FUNC;
  1511. +} sha1_ctx_t;
  1512. +void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC;
  1513. +void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx) FAST_FUNC;
  1514. +void sha1_end(void *resbuf, sha1_ctx_t *ctx) FAST_FUNC;
  1515. +typedef struct sha1_ctx_t sha256_ctx_t;
  1516. +void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
  1517. +#define sha256_hash sha1_hash
  1518. +#define sha256_end sha1_end
  1519. +typedef struct sha512_ctx_t {
  1520. + uint64_t hash[8];
  1521. + uint64_t total64[2];
  1522. + uint8_t wbuffer[128]; /* NB: always correctly aligned for uint64_t */
  1523. +} sha512_ctx_t;
  1524. +void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
  1525. +void sha512_hash(const void *buffer, size_t len, sha512_ctx_t *ctx) FAST_FUNC;
  1526. +void sha512_end(void *resbuf, sha512_ctx_t *ctx) FAST_FUNC;
  1527. +#if 1
  1528. +typedef struct md5_ctx_t {
  1529. + uint32_t A;
  1530. + uint32_t B;
  1531. + uint32_t C;
  1532. + uint32_t D;
  1533. + uint64_t total;
  1534. + uint32_t buflen;
  1535. + char buffer[128];
  1536. +} md5_ctx_t;
  1537. +#else
  1538. +/* libbb/md5prime.c uses a bit different one: */
  1539. +typedef struct md5_ctx_t {
  1540. + uint32_t state[4]; /* state (ABCD) */
  1541. + uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
  1542. + unsigned char buffer[64]; /* input buffer */
  1543. +} md5_ctx_t;
  1544. +#endif
  1545. +void md5_begin(md5_ctx_t *ctx) FAST_FUNC;
  1546. +void md5_hash(const void *data, size_t length, md5_ctx_t *ctx) FAST_FUNC;
  1547. +void md5_end(void *resbuf, md5_ctx_t *ctx) FAST_FUNC;
  1548. +
  1549. +
  1550. +uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
  1551. +
  1552. +typedef struct masks_labels_t {
  1553. + const char *labels;
  1554. + const int masks[];
  1555. +} masks_labels_t;
  1556. +int print_flags_separated(const int *masks, const char *labels,
  1557. + int flags, const char *separator) FAST_FUNC;
  1558. +int print_flags(const masks_labels_t *ml, int flags) FAST_FUNC;
  1559. +
  1560. +typedef struct bb_progress_t {
  1561. + off_t lastsize;
  1562. + unsigned lastupdate_sec;
  1563. + unsigned start_sec;
  1564. + smallint inited;
  1565. +} bb_progress_t;
  1566. +
  1567. +void bb_progress_init(bb_progress_t *p) FAST_FUNC;
  1568. +void bb_progress_update(bb_progress_t *p, const char *curfile,
  1569. + off_t beg_range, off_t transferred,
  1570. + off_t totalsize) FAST_FUNC;
  1571. +
  1572. +extern const char *applet_name;
  1573. +/* "BusyBox vN.N.N (timestamp or extra_version)" */
  1574. +extern const char bb_banner[];
  1575. +extern const char bb_msg_memory_exhausted[];
  1576. +extern const char bb_msg_invalid_date[];
  1577. +extern const char bb_msg_read_error[];
  1578. +extern const char bb_msg_write_error[];
  1579. +extern const char bb_msg_unknown[];
  1580. +extern const char bb_msg_can_not_create_raw_socket[];
  1581. +extern const char bb_msg_perm_denied_are_you_root[];
  1582. +extern const char bb_msg_you_must_be_root[];
  1583. +extern const char bb_msg_requires_arg[];
  1584. +extern const char bb_msg_invalid_arg[];
  1585. +extern const char bb_msg_standard_input[];
  1586. +extern const char bb_msg_standard_output[];
  1587. +
  1588. +extern const char bb_str_default[];
  1589. +/* NB: (bb_hexdigits_upcase[i] | 0x20) -> lowercase hex digit */
  1590. +extern const char bb_hexdigits_upcase[];
  1591. +
  1592. +extern const char bb_path_mtab_file[];
  1593. +extern const char bb_path_passwd_file[];
  1594. +extern const char bb_path_shadow_file[];
  1595. +extern const char bb_path_gshadow_file[];
  1596. +extern const char bb_path_group_file[];
  1597. +extern const char bb_path_motd_file[];
  1598. +extern const char bb_path_wtmp_file[];
  1599. +extern const char bb_dev_null[];
  1600. +extern const char bb_busybox_exec_path[];
  1601. +/* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin,
  1602. + * but I want to save a few bytes here */
  1603. +extern const char bb_PATH_root_path[]; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */
  1604. +#define bb_default_root_path (bb_PATH_root_path + sizeof("PATH"))
  1605. +#define bb_default_path (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin"))
  1606. +
  1607. +extern const int const_int_0;
  1608. +extern const int const_int_1;
  1609. +
  1610. +
  1611. +/* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
  1612. +enum { COMMON_BUFSIZE = (BUFSIZ >= 256*sizeof(void*) ? BUFSIZ+1 : 256*sizeof(void*)) };
  1613. +extern char bb_common_bufsiz1[COMMON_BUFSIZE];
  1614. +/* This struct is deliberately not defined. */
  1615. +/* See docs/keep_data_small.txt */
  1616. +struct globals;
  1617. +/* '*const' ptr makes gcc optimize code much better.
  1618. + * Magic prevents ptr_to_globals from going into rodata.
  1619. + * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
  1620. +extern struct globals *const ptr_to_globals;
  1621. +/* At least gcc 3.4.6 on mipsel system needs optimization barrier */
  1622. +#define barrier() __asm__ __volatile__("":::"memory")
  1623. +#define SET_PTR_TO_GLOBALS(x) do { \
  1624. + (*(struct globals**)&ptr_to_globals) = (void*)(x); \
  1625. + barrier(); \
  1626. +} while (0)
  1627. +
  1628. +/* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
  1629. + * use bb_default_login_shell and following defines.
  1630. + * If you change LIBBB_DEFAULT_LOGIN_SHELL,
  1631. + * don't forget to change increment constant. */
  1632. +#define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh"
  1633. +extern const char bb_default_login_shell[];
  1634. +/* "/bin/sh" */
  1635. +#define DEFAULT_SHELL (bb_default_login_shell+1)
  1636. +/* "sh" */
  1637. +#define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
  1638. +
  1639. +#if ENABLE_FEATURE_DEVFS
  1640. +# define CURRENT_VC "/dev/vc/0"
  1641. +# define VC_1 "/dev/vc/1"
  1642. +# define VC_2 "/dev/vc/2"
  1643. +# define VC_3 "/dev/vc/3"
  1644. +# define VC_4 "/dev/vc/4"
  1645. +# define VC_5 "/dev/vc/5"
  1646. +# if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  1647. +/* Yes, this sucks, but both SH (including sh64) and H8 have a SCI(F) for their
  1648. + respective serial ports .. as such, we can't use the common device paths for
  1649. + these. -- PFM */
  1650. +# define SC_0 "/dev/ttsc/0"
  1651. +# define SC_1 "/dev/ttsc/1"
  1652. +# define SC_FORMAT "/dev/ttsc/%d"
  1653. +# else
  1654. +# define SC_0 "/dev/tts/0"
  1655. +# define SC_1 "/dev/tts/1"
  1656. +# define SC_FORMAT "/dev/tts/%d"
  1657. +# endif
  1658. +# define VC_FORMAT "/dev/vc/%d"
  1659. +# define LOOP_FORMAT "/dev/loop/%d"
  1660. +# define LOOP_NAMESIZE (sizeof("/dev/loop/") + sizeof(int)*3 + 1)
  1661. +# define LOOP_NAME "/dev/loop/"
  1662. +# define FB_0 "/dev/fb/0"
  1663. +#else
  1664. +# define CURRENT_VC "/dev/tty0"
  1665. +# define VC_1 "/dev/tty1"
  1666. +# define VC_2 "/dev/tty2"
  1667. +# define VC_3 "/dev/tty3"
  1668. +# define VC_4 "/dev/tty4"
  1669. +# define VC_5 "/dev/tty5"
  1670. +# if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  1671. +# define SC_0 "/dev/ttySC0"
  1672. +# define SC_1 "/dev/ttySC1"
  1673. +# define SC_FORMAT "/dev/ttySC%d"
  1674. +# else
  1675. +# define SC_0 "/dev/ttyS0"
  1676. +# define SC_1 "/dev/ttyS1"
  1677. +# define SC_FORMAT "/dev/ttyS%d"
  1678. +# endif
  1679. +# define VC_FORMAT "/dev/tty%d"
  1680. +# define LOOP_FORMAT "/dev/loop%d"
  1681. +# define LOOP_NAMESIZE (sizeof("/dev/loop") + sizeof(int)*3 + 1)
  1682. +# define LOOP_NAME "/dev/loop"
  1683. +# define FB_0 "/dev/fb0"
  1684. +#endif
  1685. +
  1686. +/* The following devices are the same on devfs and non-devfs systems. */
  1687. +#define CURRENT_TTY "/dev/tty"
  1688. +#define DEV_CONSOLE "/dev/console"
  1689. +
  1690. +
  1691. +#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
  1692. +
  1693. +
  1694. +/* We redefine ctype macros. Unicode-correct handling of char types
  1695. + * can't be done with such byte-oriented operations anyway,
  1696. + * we don't lose anything.
  1697. + */
  1698. +#undef isalnum
  1699. +#undef isalpha
  1700. +#undef isascii
  1701. +#undef isblank
  1702. +#undef iscntrl
  1703. +#undef isdigit
  1704. +#undef isgraph
  1705. +#undef islower
  1706. +#undef isprint
  1707. +#undef ispunct
  1708. +#undef isspace
  1709. +#undef isupper
  1710. +#undef isxdigit
  1711. +#undef toupper
  1712. +#undef tolower
  1713. +
  1714. +/* We save ~500 bytes on isdigit alone.
  1715. + * BTW, x86 likes (unsigned char) cast more than (unsigned). */
  1716. +
  1717. +/* These work the same for ASCII and Unicode,
  1718. + * assuming no one asks "is this a *Unicode* letter?" using isalpha(letter) */
  1719. +#define isascii(a) ((unsigned char)(a) <= 0x7f)
  1720. +#define isdigit(a) ((unsigned char)((a) - '0') <= 9)
  1721. +#define isupper(a) ((unsigned char)((a) - 'A') <= ('Z' - 'A'))
  1722. +#define islower(a) ((unsigned char)((a) - 'a') <= ('z' - 'a'))
  1723. +#define isalpha(a) ((unsigned char)(((a)|0x20) - 'a') <= ('z' - 'a'))
  1724. +#define isblank(a) ({ unsigned char bb__isblank = (a); bb__isblank == ' ' || bb__isblank == '\t'; })
  1725. +#define iscntrl(a) ({ unsigned char bb__iscntrl = (a); bb__iscntrl < ' ' || bb__iscntrl == 0x7f; })
  1726. +/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
  1727. + * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
  1728. + */
  1729. +#define isspace(a) ({ unsigned char bb__isspace = (a) - 9; bb__isspace == (' ' - 9) || bb__isspace <= (13 - 9); })
  1730. +// Unsafe wrt NUL: #define ispunct(a) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a)) != NULL)
  1731. +#define ispunct(a) (strchrnul("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a))[0])
  1732. +// Bigger code: #define isalnum(a) ({ unsigned char bb__isalnum = (a) - '0'; bb__isalnum <= 9 || ((bb__isalnum - ('A' - '0')) & 0xdf) <= 25; })
  1733. +#define isalnum(a) bb_ascii_isalnum(a)
  1734. +static ALWAYS_INLINE int bb_ascii_isalnum(unsigned char a)
  1735. +{
  1736. + unsigned char b = a - '0';
  1737. + if (b <= 9)
  1738. + return (b <= 9);
  1739. + b = (a|0x20) - 'a';
  1740. + return b <= 'z' - 'a';
  1741. +}
  1742. +#define isxdigit(a) bb_ascii_isxdigit(a)
  1743. +static ALWAYS_INLINE int bb_ascii_isxdigit(unsigned char a)
  1744. +{
  1745. + unsigned char b = a - '0';
  1746. + if (b <= 9)
  1747. + return (b <= 9);
  1748. + b = (a|0x20) - 'a';
  1749. + return b <= 'f' - 'a';
  1750. +}
  1751. +#define toupper(a) bb_ascii_toupper(a)
  1752. +static ALWAYS_INLINE unsigned char bb_ascii_toupper(unsigned char a)
  1753. +{
  1754. + unsigned char b = a - 'a';
  1755. + if (b <= ('z' - 'a'))
  1756. + a -= 'a' - 'A';
  1757. + return a;
  1758. +}
  1759. +#define tolower(a) bb_ascii_tolower(a)
  1760. +static ALWAYS_INLINE unsigned char bb_ascii_tolower(unsigned char a)
  1761. +{
  1762. + unsigned char b = a - 'A';
  1763. + if (b <= ('Z' - 'A'))
  1764. + a += 'a' - 'A';
  1765. + return a;
  1766. +}
  1767. +
  1768. +/* In ASCII and Unicode, these are likely to be very different.
  1769. + * Let's prevent ambiguous usage from the start */
  1770. +#define isgraph(a) isgraph_is_ambiguous_dont_use(a)
  1771. +#define isprint(a) isprint_is_ambiguous_dont_use(a)
  1772. +/* NB: must not treat EOF as isgraph or isprint */
  1773. +#define isgraph_asciionly(a) ((unsigned)((a) - 0x21) <= 0x7e - 0x21)
  1774. +#define isprint_asciionly(a) ((unsigned)((a) - 0x20) <= 0x7e - 0x20)
  1775. +
  1776. +
  1777. +POP_SAVED_FUNCTION_VISIBILITY
  1778. +
  1779. +#endif
  1780. diff -Nur busybox-1.17.1.orig/include/platform.h busybox-1.17.1/include/platform.h
  1781. --- busybox-1.17.1.orig/include/platform.h 2010-07-06 04:25:54.000000000 +0200
  1782. +++ busybox-1.17.1/include/platform.h 2010-07-28 20:14:27.000000000 +0200
  1783. @@ -150,18 +150,19 @@
  1784. /* ---- Endian Detection ------------------------------------ */
  1785. +# include <sys/param.h>
  1786. #if defined(__digital__) && defined(__unix__)
  1787. # include <sex.h>
  1788. # define __BIG_ENDIAN__ (BYTE_ORDER == BIG_ENDIAN)
  1789. # define __BYTE_ORDER BYTE_ORDER
  1790. -#elif defined __FreeBSD__
  1791. +#elif defined __FreeBSD__ || defined __APPLE__
  1792. # include <sys/resource.h> /* rlimit */
  1793. # include <machine/endian.h>
  1794. # define bswap_64 __bswap64
  1795. # define bswap_32 __bswap32
  1796. # define bswap_16 __bswap16
  1797. # define __BIG_ENDIAN__ (_BYTE_ORDER == _BIG_ENDIAN)
  1798. -#elif !defined __APPLE__
  1799. +#elif !defined BSD
  1800. # include <byteswap.h>
  1801. # include <endian.h>
  1802. #endif
  1803. @@ -172,9 +173,15 @@
  1804. #elif defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
  1805. # define BB_BIG_ENDIAN 1
  1806. # define BB_LITTLE_ENDIAN 0
  1807. +#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
  1808. +# define BB_BIG_ENDIAN 1
  1809. +# define BB_LITTLE_ENDIAN 0
  1810. #elif (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || defined(__386__)
  1811. # define BB_BIG_ENDIAN 0
  1812. # define BB_LITTLE_ENDIAN 1
  1813. +#elif (defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN) || defined(__386__)
  1814. +# define BB_BIG_ENDIAN 0
  1815. +# define BB_LITTLE_ENDIAN 1
  1816. #else
  1817. # error "Can't determine endianness"
  1818. #endif
  1819. diff -Nur busybox-1.17.1.orig/include/platform.h.orig busybox-1.17.1/include/platform.h.orig
  1820. --- busybox-1.17.1.orig/include/platform.h.orig 1970-01-01 01:00:00.000000000 +0100
  1821. +++ busybox-1.17.1/include/platform.h.orig 2010-07-06 04:25:54.000000000 +0200
  1822. @@ -0,0 +1,409 @@
  1823. +/* vi: set sw=4 ts=4: */
  1824. +/*
  1825. + Copyright 2006, Bernhard Reutner-Fischer
  1826. +
  1827. + Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  1828. +*/
  1829. +#ifndef BB_PLATFORM_H
  1830. +#define BB_PLATFORM_H 1
  1831. +
  1832. +/* Assume all these functions exist by default. Platforms where it is not
  1833. + * true will #undef them below.
  1834. + */
  1835. +#define HAVE_FDPRINTF 1
  1836. +#define HAVE_MEMRCHR 1
  1837. +#define HAVE_MKDTEMP 1
  1838. +#define HAVE_SETBIT 1
  1839. +#define HAVE_STRCASESTR 1
  1840. +#define HAVE_STRCHRNUL 1
  1841. +#define HAVE_STRSEP 1
  1842. +#define HAVE_STRSIGNAL 1
  1843. +#define HAVE_VASPRINTF 1
  1844. +
  1845. +/* Convenience macros to test the version of gcc. */
  1846. +#undef __GNUC_PREREQ
  1847. +#if defined __GNUC__ && defined __GNUC_MINOR__
  1848. +# define __GNUC_PREREQ(maj, min) \
  1849. + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  1850. +#else
  1851. +# define __GNUC_PREREQ(maj, min) 0
  1852. +#endif
  1853. +
  1854. +/* __restrict is known in EGCS 1.2 and above. */
  1855. +#if !__GNUC_PREREQ(2,92)
  1856. +# ifndef __restrict
  1857. +# define __restrict
  1858. +# endif
  1859. +#endif
  1860. +
  1861. +/* Define macros for some gcc attributes. This permits us to use the
  1862. + macros freely, and know that they will come into play for the
  1863. + version of gcc in which they are supported. */
  1864. +
  1865. +#if !__GNUC_PREREQ(2,7)
  1866. +# ifndef __attribute__
  1867. +# define __attribute__(x)
  1868. +# endif
  1869. +#endif
  1870. +
  1871. +#undef inline
  1872. +#if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
  1873. +/* it's a keyword */
  1874. +#elif __GNUC_PREREQ(2,7)
  1875. +# define inline __inline__
  1876. +#else
  1877. +# define inline
  1878. +#endif
  1879. +
  1880. +#ifndef __const
  1881. +# define __const const
  1882. +#endif
  1883. +
  1884. +#define UNUSED_PARAM __attribute__ ((__unused__))
  1885. +#define NORETURN __attribute__ ((__noreturn__))
  1886. +/* "The malloc attribute is used to tell the compiler that a function
  1887. + * may be treated as if any non-NULL pointer it returns cannot alias
  1888. + * any other pointer valid when the function returns. This will often
  1889. + * improve optimization. Standard functions with this property include
  1890. + * malloc and calloc. realloc-like functions have this property as long
  1891. + * as the old pointer is never referred to (including comparing it
  1892. + * to the new pointer) after the function returns a non-NULL value."
  1893. + */
  1894. +#define RETURNS_MALLOC __attribute__ ((malloc))
  1895. +#define PACKED __attribute__ ((__packed__))
  1896. +#define ALIGNED(m) __attribute__ ((__aligned__(m)))
  1897. +
  1898. +/* __NO_INLINE__: some gcc's do not honor inlining! :( */
  1899. +#if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
  1900. +# define ALWAYS_INLINE __attribute__ ((always_inline)) inline
  1901. +/* I've seen a toolchain where I needed __noinline__ instead of noinline */
  1902. +# define NOINLINE __attribute__((__noinline__))
  1903. +# if !ENABLE_WERROR
  1904. +# define DEPRECATED __attribute__ ((__deprecated__))
  1905. +# define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
  1906. +# else
  1907. +# define DEPRECATED
  1908. +# define UNUSED_PARAM_RESULT
  1909. +# endif
  1910. +#else
  1911. +# define ALWAYS_INLINE inline
  1912. +# define NOINLINE
  1913. +# define DEPRECATED
  1914. +# define UNUSED_PARAM_RESULT
  1915. +#endif
  1916. +
  1917. +/* -fwhole-program makes all symbols local. The attribute externally_visible
  1918. + forces a symbol global. */
  1919. +#if __GNUC_PREREQ(4,1)
  1920. +# define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
  1921. +//__attribute__ ((__externally_visible__))
  1922. +#else
  1923. +# define EXTERNALLY_VISIBLE
  1924. +#endif
  1925. +
  1926. +/* At 4.4 gcc become much more anal about this, need to use "aliased" types */
  1927. +#if __GNUC_PREREQ(4,4)
  1928. +# define FIX_ALIASING __attribute__((__may_alias__))
  1929. +#else
  1930. +# define FIX_ALIASING
  1931. +#endif
  1932. +
  1933. +/* We use __extension__ in some places to suppress -pedantic warnings
  1934. + about GCC extensions. This feature didn't work properly before
  1935. + gcc 2.8. */
  1936. +#if !__GNUC_PREREQ(2,8)
  1937. +# ifndef __extension__
  1938. +# define __extension__
  1939. +# endif
  1940. +#endif
  1941. +
  1942. +/* gcc-2.95 had no va_copy but only __va_copy. */
  1943. +#if !__GNUC_PREREQ(3,0)
  1944. +# include <stdarg.h>
  1945. +# if !defined va_copy && defined __va_copy
  1946. +# define va_copy(d,s) __va_copy((d),(s))
  1947. +# endif
  1948. +#endif
  1949. +
  1950. +/* FAST_FUNC is a qualifier which (possibly) makes function call faster
  1951. + * and/or smaller by using modified ABI. It is usually only needed
  1952. + * on non-static, busybox internal functions. Recent versions of gcc
  1953. + * optimize statics automatically. FAST_FUNC on static is required
  1954. + * only if you need to match a function pointer's type */
  1955. +#if __GNUC_PREREQ(3,0) && defined(i386) /* || defined(__x86_64__)? */
  1956. +/* stdcall makes callee to pop arguments from stack, not caller */
  1957. +# define FAST_FUNC __attribute__((regparm(3),stdcall))
  1958. +/* #elif ... - add your favorite arch today! */
  1959. +#else
  1960. +# define FAST_FUNC
  1961. +#endif
  1962. +
  1963. +/* Make all declarations hidden (-fvisibility flag only affects definitions) */
  1964. +/* (don't include system headers after this until corresponding pop!) */
  1965. +#if __GNUC_PREREQ(4,1)
  1966. +# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)")
  1967. +# define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop")
  1968. +#else
  1969. +# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
  1970. +# define POP_SAVED_FUNCTION_VISIBILITY
  1971. +#endif
  1972. +
  1973. +/* ---- Endian Detection ------------------------------------ */
  1974. +
  1975. +#if defined(__digital__) && defined(__unix__)
  1976. +# include <sex.h>
  1977. +# define __BIG_ENDIAN__ (BYTE_ORDER == BIG_ENDIAN)
  1978. +# define __BYTE_ORDER BYTE_ORDER
  1979. +#elif defined __FreeBSD__
  1980. +# include <sys/resource.h> /* rlimit */
  1981. +# include <machine/endian.h>
  1982. +# define bswap_64 __bswap64
  1983. +# define bswap_32 __bswap32
  1984. +# define bswap_16 __bswap16
  1985. +# define __BIG_ENDIAN__ (_BYTE_ORDER == _BIG_ENDIAN)
  1986. +#elif !defined __APPLE__
  1987. +# include <byteswap.h>
  1988. +# include <endian.h>
  1989. +#endif
  1990. +
  1991. +#if defined(__BIG_ENDIAN__) && __BIG_ENDIAN__
  1992. +# define BB_BIG_ENDIAN 1
  1993. +# define BB_LITTLE_ENDIAN 0
  1994. +#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
  1995. +# define BB_BIG_ENDIAN 1
  1996. +# define BB_LITTLE_ENDIAN 0
  1997. +#elif (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || defined(__386__)
  1998. +# define BB_BIG_ENDIAN 0
  1999. +# define BB_LITTLE_ENDIAN 1
  2000. +#else
  2001. +# error "Can't determine endianness"
  2002. +#endif
  2003. +
  2004. +/* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
  2005. +#if BB_BIG_ENDIAN
  2006. +# define SWAP_BE16(x) (x)
  2007. +# define SWAP_BE32(x) (x)
  2008. +# define SWAP_BE64(x) (x)
  2009. +# define SWAP_LE16(x) bswap_16(x)
  2010. +# define SWAP_LE32(x) bswap_32(x)
  2011. +# define SWAP_LE64(x) bswap_64(x)
  2012. +#else
  2013. +# define SWAP_BE16(x) bswap_16(x)
  2014. +# define SWAP_BE32(x) bswap_32(x)
  2015. +# define SWAP_BE64(x) bswap_64(x)
  2016. +# define SWAP_LE16(x) (x)
  2017. +# define SWAP_LE32(x) (x)
  2018. +# define SWAP_LE64(x) (x)
  2019. +#endif
  2020. +
  2021. +/* ---- Unaligned access ------------------------------------ */
  2022. +
  2023. +/* NB: unaligned parameter should be a pointer, aligned one -
  2024. + * a lvalue. This makes it more likely to not swap them by mistake
  2025. + */
  2026. +#if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
  2027. +# include <stdint.h>
  2028. +typedef int bb__aliased_int FIX_ALIASING;
  2029. +typedef uint16_t bb__aliased_uint16_t FIX_ALIASING;
  2030. +typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
  2031. +# define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp))
  2032. +# define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p))
  2033. +# define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p))
  2034. +# define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v))
  2035. +# define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v))
  2036. +/* #elif ... - add your favorite arch today! */
  2037. +#else
  2038. +/* performs reasonably well (gcc usually inlines memcpy here) */
  2039. +# define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
  2040. +# define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
  2041. +# define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
  2042. +# define move_to_unaligned16(u16p, v) do { \
  2043. + uint16_t __t = (v); \
  2044. + memcpy((u16p), &__t, 4); \
  2045. +} while (0)
  2046. +# define move_to_unaligned32(u32p, v) do { \
  2047. + uint32_t __t = (v); \
  2048. + memcpy((u32p), &__t, 4); \
  2049. +} while (0)
  2050. +#endif
  2051. +
  2052. +/* ---- Compiler dependent settings ------------------------- */
  2053. +
  2054. +#if (defined __digital__ && defined __unix__) \
  2055. + || defined __APPLE__ || defined __FreeBSD__
  2056. +# undef HAVE_MNTENT_H
  2057. +# undef HAVE_SYS_STATFS_H
  2058. +#else
  2059. +# define HAVE_MNTENT_H 1
  2060. +# define HAVE_SYS_STATFS_H 1
  2061. +#endif
  2062. +
  2063. +/*----- Kernel versioning ------------------------------------*/
  2064. +
  2065. +#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
  2066. +
  2067. +/* ---- Miscellaneous --------------------------------------- */
  2068. +
  2069. +#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ < 5 && \
  2070. + !defined(__dietlibc__) && \
  2071. + !defined(_NEWLIB_VERSION) && \
  2072. + !(defined __digital__ && defined __unix__)
  2073. +# error "Sorry, this libc version is not supported :("
  2074. +#endif
  2075. +
  2076. +/* Don't perpetuate e2fsck crap into the headers. Clean up e2fsck instead. */
  2077. +
  2078. +#if defined __GLIBC__ || defined __UCLIBC__ \
  2079. + || defined __dietlibc__ || defined _NEWLIB_VERSION
  2080. +# include <features.h>
  2081. +#endif
  2082. +
  2083. +/* Size-saving "small" ints (arch-dependent) */
  2084. +#if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
  2085. +/* add other arches which benefit from this... */
  2086. +typedef signed char smallint;
  2087. +typedef unsigned char smalluint;
  2088. +#else
  2089. +/* for arches where byte accesses generate larger code: */
  2090. +typedef int smallint;
  2091. +typedef unsigned smalluint;
  2092. +#endif
  2093. +
  2094. +/* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
  2095. +#if (defined __digital__ && defined __unix__)
  2096. +/* old system without (proper) C99 support */
  2097. +# define bool smalluint
  2098. +#else
  2099. +/* modern system, so use it */
  2100. +# include <stdbool.h>
  2101. +#endif
  2102. +
  2103. +/* Try to defeat gcc's alignment of "char message[]"-like data */
  2104. +#if 1 /* if needed: !defined(arch1) && !defined(arch2) */
  2105. +# define ALIGN1 __attribute__((aligned(1)))
  2106. +# define ALIGN2 __attribute__((aligned(2)))
  2107. +# define ALIGN4 __attribute__((aligned(4)))
  2108. +#else
  2109. +/* Arches which MUST have 2 or 4 byte alignment for everything are here */
  2110. +# define ALIGN1
  2111. +# define ALIGN2
  2112. +# define ALIGN4
  2113. +#endif
  2114. +
  2115. +
  2116. +/* uclibc does not implement daemon() for no-mmu systems.
  2117. + * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
  2118. + * For earlier versions there is no reliable way to check if we are building
  2119. + * for a mmu-less system.
  2120. + */
  2121. +#if ENABLE_NOMMU || \
  2122. + (defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \
  2123. + __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__)
  2124. +# define BB_MMU 0
  2125. +# define USE_FOR_NOMMU(...) __VA_ARGS__
  2126. +# define USE_FOR_MMU(...)
  2127. +#else
  2128. +# define BB_MMU 1
  2129. +# define USE_FOR_NOMMU(...)
  2130. +# define USE_FOR_MMU(...) __VA_ARGS__
  2131. +#endif
  2132. +
  2133. +/* Don't use lchown with glibc older than 2.1.x */
  2134. +#if defined(__GLIBC__) && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 1
  2135. +# define lchown chown
  2136. +#endif
  2137. +
  2138. +#if defined(__digital__) && defined(__unix__)
  2139. +
  2140. +# include <standards.h>
  2141. +# include <inttypes.h>
  2142. +# define PRIu32 "u"
  2143. +/* use legacy setpgrp(pid_t,pid_t) for now. move to platform.c */
  2144. +# define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
  2145. +# if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
  2146. +# define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
  2147. +# endif
  2148. +# if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
  2149. +# define ADJ_FREQUENCY MOD_FREQUENCY
  2150. +# endif
  2151. +# if !defined ADJ_TIMECONST && defined MOD_TIMECONST
  2152. +# define ADJ_TIMECONST MOD_TIMECONST
  2153. +# endif
  2154. +# if !defined ADJ_TICK && defined MOD_CLKB
  2155. +# define ADJ_TICK MOD_CLKB
  2156. +# endif
  2157. +
  2158. +#else
  2159. +
  2160. +# define bb_setpgrp() setpgrp()
  2161. +
  2162. +#endif
  2163. +
  2164. +#if defined(__GLIBC__)
  2165. +# define fdprintf dprintf
  2166. +#endif
  2167. +
  2168. +#if defined(__dietlibc__)
  2169. +# undef HAVE_STRCHRNUL
  2170. +#endif
  2171. +
  2172. +#if defined(__WATCOMC__)
  2173. +# undef HAVE_FDPRINTF
  2174. +# undef HAVE_MEMRCHR
  2175. +# undef HAVE_MKDTEMP
  2176. +# undef HAVE_SETBIT
  2177. +# undef HAVE_STRCASESTR
  2178. +# undef HAVE_STRCHRNUL
  2179. +# undef HAVE_STRSEP
  2180. +# undef HAVE_STRSIGNAL
  2181. +# undef HAVE_VASPRINTF
  2182. +#endif
  2183. +
  2184. +#if defined(__FreeBSD__)
  2185. +# undef HAVE_STRCHRNUL
  2186. +#endif
  2187. +
  2188. +/*
  2189. + * Now, define prototypes for all the functions defined in platform.c
  2190. + * These must come after all the HAVE_* macros are defined (or not)
  2191. + */
  2192. +
  2193. +#ifndef HAVE_FDPRINTF
  2194. +extern int fdprintf(int fd, const char *format, ...);
  2195. +#endif
  2196. +
  2197. +#ifndef HAVE_MEMRCHR
  2198. +extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
  2199. +#endif
  2200. +
  2201. +#ifndef HAVE_MKDTEMP
  2202. +extern char *mkdtemp(char *template) FAST_FUNC;
  2203. +#endif
  2204. +
  2205. +#ifndef HAVE_SETBIT
  2206. +# define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
  2207. +# define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
  2208. +#endif
  2209. +
  2210. +#ifndef HAVE_STRCASESTR
  2211. +extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
  2212. +#endif
  2213. +
  2214. +#ifndef HAVE_STRCHRNUL
  2215. +extern char *strchrnul(const char *s, int c) FAST_FUNC;
  2216. +#endif
  2217. +
  2218. +#ifndef HAVE_STRSEP
  2219. +extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
  2220. +#endif
  2221. +
  2222. +#ifndef HAVE_STRSIGNAL
  2223. +/* Not exactly the same: instead of "Stopped" it shows "STOP" etc */
  2224. +# define strsignal(sig) get_signame(sig)
  2225. +#endif
  2226. +
  2227. +#ifndef HAVE_VASPRINTF
  2228. +extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
  2229. +#endif
  2230. +
  2231. +#endif
  2232. diff -Nur busybox-1.17.1.orig/scripts/gen_build_files.sh busybox-1.17.1/scripts/gen_build_files.sh
  2233. --- busybox-1.17.1.orig/scripts/gen_build_files.sh 2010-07-25 00:12:56.000000000 +0200
  2234. +++ busybox-1.17.1/scripts/gen_build_files.sh 2010-07-28 20:13:21.000000000 +0200
  2235. @@ -48,7 +48,7 @@
  2236. fi
  2237. # (Re)generate */Kbuild and */Config.in
  2238. -{ cd -- "$srctree" && find -type d; } | while read -r d; do
  2239. +{ cd -- "$srctree" && find . -type d; } | while read -r d; do
  2240. d="${d#./}"
  2241. src="$srctree/$d/Kbuild.src"
  2242. diff -Nur busybox-1.17.1.orig/scripts/gen_build_files.sh.orig busybox-1.17.1/scripts/gen_build_files.sh.orig
  2243. --- busybox-1.17.1.orig/scripts/gen_build_files.sh.orig 1970-01-01 01:00:00.000000000 +0100
  2244. +++ busybox-1.17.1/scripts/gen_build_files.sh.orig 2010-07-25 00:12:56.000000000 +0200
  2245. @@ -0,0 +1,96 @@
  2246. +#!/bin/sh
  2247. +
  2248. +test $# -ge 2 || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
  2249. +
  2250. +# cd to objtree
  2251. +cd -- "$2" || { echo "Syntax: $0 SRCTREE OBJTREE"; exit 1; }
  2252. +# In separate objtree build, include/ might not exist yet
  2253. +mkdir include 2>/dev/null
  2254. +
  2255. +srctree="$1"
  2256. +
  2257. +# (Re)generate include/applets.h
  2258. +src="$srctree/include/applets.src.h"
  2259. +dst="include/applets.h"
  2260. +s=`sed -n 's@^//applet:@@p' -- "$srctree"/*/*.c "$srctree"/*/*/*.c`
  2261. +old=`cat "$dst" 2>/dev/null`
  2262. +# Why "IFS='' read -r REPLY"??
  2263. +# This atrocity is needed to read lines without mangling.
  2264. +# IFS='' prevents whitespace trimming,
  2265. +# -r suppresses backslash handling.
  2266. +new=`echo "/* DO NOT EDIT. This file is generated from applets.src.h */"
  2267. +while IFS='' read -r REPLY; do
  2268. + test x"$REPLY" = x"INSERT" && REPLY="$s"
  2269. + printf "%s\n" "$REPLY"
  2270. +done <"$src"`
  2271. +if test x"$new" != x"$old"; then
  2272. + echo " GEN $dst"
  2273. + printf "%s\n" "$new" >"$dst"
  2274. +fi
  2275. +
  2276. +# (Re)generate include/usage.h
  2277. +src="$srctree/include/usage.src.h"
  2278. +dst="include/usage.h"
  2279. +# We add line continuation backslash after each line,
  2280. +# and insert empty line before each line which doesn't start
  2281. +# with space or tab
  2282. +# (note: we need to use \\\\ because of ``)
  2283. +s=`sed -n -e 's@^//usage:\([ \t].*\)$@\1 \\\\@p' -e 's@^//usage:\([^ \t].*\)$@\n\1 \\\\@p' -- "$srctree"/*/*.c "$srctree"/*/*/*.c`
  2284. +old=`cat "$dst" 2>/dev/null`
  2285. +new=`echo "/* DO NOT EDIT. This file is generated from usage.src.h */"
  2286. +while IFS='' read -r REPLY; do
  2287. + test x"$REPLY" = x"INSERT" && REPLY="$s"
  2288. + printf "%s\n" "$REPLY"
  2289. +done <"$src"`
  2290. +if test x"$new" != x"$old"; then
  2291. + echo " GEN $dst"
  2292. + printf "%s\n" "$new" >"$dst"
  2293. +fi
  2294. +
  2295. +# (Re)generate */Kbuild and */Config.in
  2296. +{ cd -- "$srctree" && find -type d; } | while read -r d; do
  2297. + d="${d#./}"
  2298. +
  2299. + src="$srctree/$d/Kbuild.src"
  2300. + dst="$d/Kbuild"
  2301. + if test -f "$src"; then
  2302. + mkdir -p -- "$d" 2>/dev/null
  2303. + #echo " CHK $dst"
  2304. +
  2305. + s=`sed -n 's@^//kbuild:@@p' -- "$srctree/$d"/*.c`
  2306. +
  2307. + old=`cat "$dst" 2>/dev/null`
  2308. + new=`echo "# DO NOT EDIT. This file is generated from Kbuild.src"
  2309. + while IFS='' read -r REPLY; do
  2310. + test x"$REPLY" = x"INSERT" && REPLY="$s"
  2311. + printf "%s\n" "$REPLY"
  2312. + done <"$src"`
  2313. + if test x"$new" != x"$old"; then
  2314. + echo " GEN $dst"
  2315. + printf "%s\n" "$new" >"$dst"
  2316. + fi
  2317. + fi
  2318. +
  2319. + src="$srctree/$d/Config.src"
  2320. + dst="$d/Config.in"
  2321. + if test -f "$src"; then
  2322. + mkdir -p -- "$d" 2>/dev/null
  2323. + #echo " CHK $dst"
  2324. +
  2325. + s=`sed -n 's@^//config:@@p' -- "$srctree/$d"/*.c`
  2326. +
  2327. + old=`cat "$dst" 2>/dev/null`
  2328. + new=`echo "# DO NOT EDIT. This file is generated from Config.src"
  2329. + while IFS='' read -r REPLY; do
  2330. + test x"$REPLY" = x"INSERT" && REPLY="$s"
  2331. + printf "%s\n" "$REPLY"
  2332. + done <"$src"`
  2333. + if test x"$new" != x"$old"; then
  2334. + echo " GEN $dst"
  2335. + printf "%s\n" "$new" >"$dst"
  2336. + fi
  2337. + fi
  2338. +done
  2339. +
  2340. +# Last read failed. This is normal. Don't exit with its error code:
  2341. +exit 0