1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#define FOR_factor
24#include "toys.h"
25
26static void factor(char *s)
27{
28 unsigned long long l, ll, lll;
29 char *pat1 = FLAG(x) ? " %llx" : " %llu", *pat2 = FLAG(x) ? "^%x" : "^%u";
30
31 for (;;) {
32 char *err = s;
33 int dash = 0, ii;
34
35 while(isspace(*s)) s++;
36 if (*s=='-') dash = *s++;
37 if (!*s) return;
38
39 errno = 0;
40 l = strtoull(s, &s, 0);
41 if (errno || (*s && !isspace(*s))) {
42 error_msg("%s: not integer", err);
43 while (*s && !isspace(*s)) s++;
44 continue;
45 }
46
47 if (dash) xputc('-');
48 printf(pat1+1, l);
49 xputc(':');
50
51
52 if (dash) printf(" -1");
53
54
55 for (ll = 2;; ll += 1+(ll!=2)) {
56 lll = ll*ll;
57 if (lll>l || lll<ll) {
58 if (l>1 || ll==2) printf(pat1, l);
59 break;
60 }
61 for (ii = 0; !(l%ll); ii++) {
62 if (!ii || !FLAG(h)) printf(pat1, ll);
63 l /= ll;
64 }
65 if (ii>1 && FLAG(h)) printf(pat2, ii);
66 }
67 xputc('\n');
68 }
69}
70
71void factor_main(void)
72{
73 char *s = 0, **ss;
74 size_t len = 0;
75
76 if (toys.optc) for (ss = toys.optargs; *ss; ss++) factor(*ss);
77 else for (;;) {
78 if (-1 == getline(&s, &len, stdin)) break;
79 factor(s);
80 }
81}
82