cmp_n.diff 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. Index: coreutils/Config.in
  2. ===================================================================
  3. RCS file: /var/cvs/busybox/coreutils/Config.in,v
  4. retrieving revision 1.24
  5. diff -u -r1.24 Config.in
  6. --- a/coreutils/Config.in 15 Mar 2004 08:28:19 -0000 1.24
  7. +++ b/coreutils/Config.in 31 Mar 2004 11:51:17 -0000
  8. @@ -59,6 +59,21 @@
  9. cmp is used to compare two files and returns the result
  10. to standard output.
  11. +config CONFIG_FEATURE_CMP_SKIP
  12. + bool " Enable optional arguments SKIP1 and SKIP2"
  13. + default n
  14. + depends on CONFIG_CMP
  15. + help
  16. + SKIP1 and SKIP2 specify how many bytes to ignore
  17. + at the start of each file.
  18. +
  19. +config CONFIG_FEATURE_CMP_LIMIT
  20. + bool " Enable limit of inputs"
  21. + default n
  22. + depends on CONFIG_CMP
  23. + help
  24. + Enable cmp option (-n).
  25. +
  26. config CONFIG_CP
  27. bool "cp"
  28. default n
  29. Index: coreutils/cmp.c
  30. ===================================================================
  31. RCS file: /var/cvs/busybox/coreutils/cmp.c,v
  32. retrieving revision 1.9
  33. diff -u -r1.9 cmp.c
  34. --- a/coreutils/cmp.c 19 Mar 2003 09:11:32 -0000 1.9
  35. +++ b/coreutils/cmp.c 31 Mar 2004 11:51:17 -0000
  36. @@ -39,6 +39,12 @@
  37. #include <unistd.h>
  38. #include "busybox.h"
  39. +#ifdef CONFIG_FEATURE_CMP_SKIP
  40. +#define MAX_OPTIONAL_ARGS 3
  41. +#else
  42. +#define MAX_OPTIONAL_ARGS 1
  43. +#endif
  44. +
  45. static FILE *cmp_xfopen_input(const char *filename)
  46. {
  47. FILE *fp;
  48. @@ -58,12 +64,57 @@
  49. static const char fmt_l_opt[] = "%.0s%.0s%d %3o %3o\n"; /* nicer gnu format */
  50. #endif
  51. -static const char opt_chars[] = "sl";
  52. +#ifdef CONFIG_FEATURE_CMP_LIMIT
  53. +#define OPTCHR_LIMIT "n:"
  54. +#define OPTARG_LIMIT ,&limit_str
  55. +#else
  56. +#define OPTCHR_LIMIT
  57. +#define OPTARG_LIMIT
  58. +#endif
  59. +
  60. +static const char opt_chars[] = "sl" OPTCHR_LIMIT;
  61. enum {
  62. OPT_s = 1,
  63. - OPT_l = 2
  64. + OPT_l = 2,
  65. + OPT_n = 4
  66. +};
  67. +
  68. +#ifdef CONFIG_LFS
  69. +#define SUFFIX_STRUCT suffix_mult64
  70. +#define PARSE_FUNC bb_xgetllarg10_sfx
  71. +#else
  72. +#define SUFFIX_STRUCT suffix_mult
  73. +#define PARSE_FUNC bb_xgetlarg10_sfx
  74. +#endif
  75. +
  76. +#if defined(CONFIG_FEATURE_CMP_SKIP) || defined(CONFIG_FEATURE_CMP_LIMIT)
  77. +static const struct SUFFIX_STRUCT suffixes[] = {
  78. + { "k", 1UL << 10 },
  79. + { "M", 1UL << 20 },
  80. + { "G", 1UL << 30 },
  81. +#ifdef CONFIG_LFS
  82. + { "T", 1ULL << 40 },
  83. + { "P", 1ULL << 50 },
  84. + { "E", 1ULL << 60 },
  85. +#endif
  86. + { NULL, 0 }
  87. };
  88. +#endif
  89. +
  90. +#ifdef CONFIG_FEATURE_CMP_SKIP
  91. +static void skip_input(FILE *fp, off_t skip, const char *filename)
  92. +{
  93. + if (skip > 0 && fseeko(fp, skip, SEEK_CUR) != 0) {
  94. + while (skip-- > 0) {
  95. + if (getc(fp) == EOF) {
  96. + bb_xferror(fp, filename);
  97. + break;
  98. + }
  99. + }
  100. + }
  101. +}
  102. +#endif
  103. int cmp_main(int argc, char **argv)
  104. {
  105. @@ -73,12 +124,26 @@
  106. int c1, c2, char_pos, line_pos;
  107. int opt_flags;
  108. int exit_val = 0;
  109. +#ifdef CONFIG_FEATURE_CMP_SKIP
  110. + off_t skip1 = 0, skip2 = 0;
  111. +#endif
  112. +#ifdef CONFIG_FEATURE_CMP_LIMIT
  113. + off_t limit = -1;
  114. + char *limit_str;
  115. +#endif
  116. bb_default_error_retval = 2; /* 1 is returned if files are different. */
  117. - opt_flags = bb_getopt_ulflags(argc, argv, opt_chars);
  118. + opt_flags = bb_getopt_ulflags(argc, argv, opt_chars OPTARG_LIMIT);
  119. - if ((opt_flags == 3) || (((unsigned int)(--argc - optind)) > 1)) {
  120. +#ifdef CONFIG_FEATURE_CMP_LIMIT
  121. + if (opt_flags & OPT_n) {
  122. + limit = PARSE_FUNC(limit_str, suffixes);
  123. + opt_flags &= 3;
  124. + }
  125. +#endif
  126. +
  127. + if ((opt_flags == 3) || (((unsigned int)(--argc - optind)) > MAX_OPTIONAL_ARGS)) {
  128. bb_show_usage();
  129. }
  130. @@ -87,6 +152,13 @@
  131. filename2 = "-";
  132. if (*++argv) {
  133. filename2 = *argv;
  134. +#ifdef CONFIG_FEATURE_CMP_SKIP
  135. + if (*++argv) {
  136. + skip1 = PARSE_FUNC(*argv, suffixes);
  137. + if (*++argv)
  138. + skip2 = PARSE_FUNC(*argv, suffixes);
  139. + }
  140. +#endif
  141. }
  142. fp2 = cmp_xfopen_input(filename2);
  143. @@ -98,6 +170,11 @@
  144. return 0;
  145. }
  146. +#ifdef CONFIG_FEATURE_CMP_SKIP
  147. + skip_input(fp1, skip1, filename1);
  148. + skip_input(fp2, skip2, filename2);
  149. +#endif
  150. +
  151. fmt = fmt_differ;
  152. if (opt_flags == OPT_l) {
  153. fmt = fmt_l_opt;
  154. @@ -106,6 +183,10 @@
  155. char_pos = 0;
  156. line_pos = 1;
  157. do {
  158. +#ifdef CONFIG_FEATURE_CMP_LIMIT
  159. + if (limit-- == 0)
  160. + break;
  161. +#endif
  162. c1 = getc(fp1);
  163. c2 = getc(fp2);
  164. ++char_pos;
  165. Index: include/usage.h
  166. ===================================================================
  167. RCS file: /var/cvs/busybox/include/usage.h,v
  168. retrieving revision 1.198
  169. diff -u -r1.198 usage.h
  170. --- a/include/usage.h 29 Mar 2004 08:20:08 -0000 1.198
  171. +++ b/include/usage.h 31 Mar 2004 11:51:17 -0000
  172. @@ -186,14 +186,29 @@
  173. #define clear_full_usage \
  174. "Clear screen."
  175. +#ifdef CONFIG_FEATURE_CMP_SKIP
  176. +#define USAGE_CMP_SKIP(a) a
  177. +#else
  178. +#define USAGE_CMP_SKIP(a)
  179. +#endif
  180. +
  181. +#ifdef CONFIG_FEATURE_CMP_LIMIT
  182. +#define USAGE_CMP_LIMIT(a) a
  183. +#else
  184. +#define USAGE_CMP_LIMIT(a)
  185. +#endif
  186. +
  187. #define cmp_trivial_usage \
  188. - "[OPTION]... FILE1 [FILE2]"
  189. + "[OPTION]... FILE1 [FILE2" USAGE_CMP_SKIP(" [SKIP1 [SKIP2]]") "]"
  190. #define cmp_full_usage \
  191. - "Compare files.\n\n" \
  192. + "Compare files.\n" \
  193. + USAGE_CMP_SKIP("SKIP1 and SKIP2 are the number of bytes to skip in each file.\n") \
  194. + "\n" \
  195. "Options:\n" \
  196. - "\t-l\tWrite the byte numbers (decimal) and values (octal)\n" \
  197. - "\t\t for all differing bytes.\n" \
  198. - "\t-s\tquiet mode - do not print"
  199. + "\t-l\t\tWrite the byte numbers (decimal) and values (octal)\n" \
  200. + "\t\t\t for all differing bytes.\n" \
  201. + USAGE_CMP_LIMIT("\t-n LIMIT\tCompare at most LIMIT bytes.\n") \
  202. + "\t-s\t\tquiet mode - do not print"
  203. #define cp_trivial_usage \
  204. "[OPTION]... SOURCE DEST"
  205. Index: include/libbb.h
  206. ===================================================================
  207. RCS file: /var/cvs/busybox/include/libbb.h,v
  208. retrieving revision 1.129
  209. diff -u -r1.129 libbb.h
  210. --- a/include/libbb.h 15 Mar 2004 08:28:38 -0000 1.129
  211. +++ b/include/libbb.h 31 Mar 2004 11:51:17 -0000
  212. @@ -217,6 +217,21 @@
  213. const struct suffix_mult *suffixes);
  214. extern long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes);
  215. +struct suffix_mult64 {
  216. + const char *suffix;
  217. + unsigned long long mult;
  218. +};
  219. +
  220. +extern unsigned long long bb_xgetullarg_bnd_sfx(const char *arg, int base,
  221. + unsigned long long lower,
  222. + unsigned long long upper,
  223. + const struct suffix_mult64 *suffixes);
  224. +
  225. +extern long long bb_xgetllarg_bnd_sfx(const char *arg, int base,
  226. + long long lower,
  227. + long long upper,
  228. + const struct suffix_mult64 *suffixes);
  229. +extern long long bb_xgetllarg10_sfx(const char *arg, const struct suffix_mult64 *suffixes);
  230. //#warning pitchable now?
  231. extern unsigned long bb_xparse_number(const char *numstr,
  232. Index: libbb/Makefile.in
  233. ===================================================================
  234. RCS file: /var/cvs/busybox/libbb/Makefile.in,v
  235. retrieving revision 1.34
  236. diff -u -r1.34 Makefile.in
  237. --- a/libbb/Makefile.in 6 Mar 2004 22:11:45 -0000 1.34
  238. +++ b/libbb/Makefile.in 31 Mar 2004 11:51:17 -0000
  239. @@ -70,7 +70,8 @@
  240. LIBBB_MSRC3:=$(LIBBB_DIR)xgetularg.c
  241. LIBBB_MOBJ3:=xgetularg_bnd_sfx.o xgetlarg_bnd_sfx.o getlarg10_sfx.o \
  242. - xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o
  243. + xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o \
  244. + xgetullarg_bnd_sfx.o xgetllarg_bnd_sfx.o xgetllarg10_sfx.o
  245. LIBBB_MSRC4:=$(LIBBB_DIR)/safe_strtol.c
  246. LIBBB_MOBJ4:=safe_strtoi.o safe_strtod.o safe_strtol.o safe_strtoul.o
  247. Index: libbb/xgetularg.c
  248. ===================================================================
  249. RCS file: /var/cvs/busybox/libbb/xgetularg.c,v
  250. retrieving revision 1.2
  251. diff -u -r1.2 xgetularg.c
  252. --- a/libbb/xgetularg.c 15 Mar 2004 08:28:44 -0000 1.2
  253. +++ b/libbb/xgetularg.c 31 Mar 2004 11:51:17 -0000
  254. @@ -158,3 +158,106 @@
  255. return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
  256. }
  257. #endif
  258. +
  259. +#ifdef L_xgetullarg_bnd_sfx
  260. +extern
  261. +unsigned long long bb_xgetullarg_bnd_sfx(const char *arg, int base,
  262. + unsigned long long lower,
  263. + unsigned long long upper,
  264. + const struct suffix_mult64 *suffixes)
  265. +{
  266. + unsigned long long r;
  267. + int old_errno;
  268. + char *e;
  269. +
  270. + assert(arg);
  271. +
  272. + /* Disallow '-' and any leading whitespace. Speed isn't critical here
  273. + * since we're parsing commandline args. So make sure we get the
  274. + * actual isspace function rather than a larger macro implementaion. */
  275. + if ((*arg == '-') || (isspace)(*arg)) {
  276. + bb_show_usage();
  277. + }
  278. +
  279. + /* Since this is a lib function, we're not allowed to reset errno to 0.
  280. + * Doing so could break an app that is deferring checking of errno.
  281. + * So, save the old value so that we can restore it if successful. */
  282. + old_errno = errno;
  283. + errno = 0;
  284. + r = strtoull(arg, &e, base);
  285. + /* Do the initial validity check. Note: The standards do not
  286. + * guarantee that errno is set if no digits were found. So we
  287. + * must test for this explicitly. */
  288. + if (errno || (arg == e)) { /* error or no digits */
  289. + bb_show_usage();
  290. + }
  291. + errno = old_errno; /* Ok. So restore errno. */
  292. +
  293. + /* Do optional suffix parsing. Allow 'empty' suffix tables.
  294. + * Note that we also all nul suffixes with associated multipliers,
  295. + * to allow for scaling of the arg by some default multiplier. */
  296. +
  297. + if (suffixes) {
  298. + while (suffixes->suffix) {
  299. + if (strcmp(suffixes->suffix, e) == 0) {
  300. + if (ULONG_LONG_MAX / suffixes->mult < r) { /* Overflow! */
  301. + bb_show_usage();
  302. + }
  303. + ++e;
  304. + r *= suffixes->mult;
  305. + break;
  306. + }
  307. + ++suffixes;
  308. + }
  309. + }
  310. +
  311. + /* Finally, check for illegal trailing chars and range limits. */
  312. + /* Note: although we allow leading space (via stroul), trailing space
  313. + * is an error. It would be easy enough to allow though if desired. */
  314. + if (*e || (r < lower) || (r > upper)) {
  315. + bb_show_usage();
  316. + }
  317. +
  318. + return r;
  319. +}
  320. +#endif
  321. +
  322. +#ifdef L_xgetllarg_bnd_sfx
  323. +extern
  324. +long long bb_xgetllarg_bnd_sfx(const char *arg, int base,
  325. + long long lower,
  326. + long long upper,
  327. + const struct suffix_mult64 *suffixes)
  328. +{
  329. + unsigned long long u = LONG_LONG_MAX;
  330. + long long r;
  331. + const char *p = arg;
  332. +
  333. + if ((*p == '-') && (p[1] != '+')) {
  334. + ++p;
  335. +#if LONG_LONG_MAX == (-(LONG_LONG_MIN + 1))
  336. + ++u; /* two's complement */
  337. +#endif
  338. + }
  339. +
  340. + r = bb_xgetullarg_bnd_sfx(p, base, 0, u, suffixes);
  341. +
  342. + if (*arg == '-') {
  343. + r = -r;
  344. + }
  345. +
  346. + if ((r < lower) || (r > upper)) {
  347. + bb_show_usage();
  348. + }
  349. +
  350. + return r;
  351. +}
  352. +#endif
  353. +
  354. +#ifdef L_xgetllarg10_sfx
  355. +extern
  356. +long long bb_xgetllarg10_sfx(const char *arg, const struct suffix_mult64 *suffixes)
  357. +{
  358. + return bb_xgetllarg_bnd_sfx(arg, 10, LONG_LONG_MIN, LONG_LONG_MAX, suffixes);
  359. +}
  360. +#endif