Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove anonymous unions #218

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions toys/example/skeleton.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ GLOBALS(
struct {
long b;
} a;
};
} u;

int more_globals;
)
Expand All @@ -74,17 +74,17 @@ void skeleton_main(void)
// from main.c using the optstring in the NEWTOY macros. Display results.
if (toys.optflags) printf("flags=%llx\n", toys.optflags);
if (FLAG(a)) printf("Saw a\n");
if (FLAG(b)) printf("b=%s\n", TT.s.b);
if (FLAG(c)) printf("c=%ld\n", TT.s.c);
while (TT.s.d) {
printf("d=%s\n", TT.s.d->arg);
TT.s.d = TT.s.d->next;
if (FLAG(b)) printf("b=%s\n", TT.u.s.b);
if (FLAG(c)) printf("c=%ld\n", TT.u.s.c);
while (TT.u.s.d) {
printf("d=%s\n", TT.u.s.d->arg);
TT.u.s.d = TT.u.s.d->next;
}
if (TT.s.e) printf("e was seen %ld times\n", TT.s.e);
if (TT.u.s.e) printf("e was seen %ld times\n", TT.u.s.e);
for (optargs = toys.optargs; *optargs; optargs++)
printf("optarg=%s\n", *optargs);
if (FLAG(walrus)) printf("Saw --walrus\n");
if (TT.s.blubber) printf("--blubber=%s\n", TT.s.blubber);
if (TT.u.s.blubber) printf("--blubber=%s\n", TT.u.s.blubber);

printf("Other globals should start zeroed: %d\n", TT.more_globals);
}
Expand All @@ -101,5 +101,5 @@ void skeleton_alias_main(void)

// Note, this FLAG_b is a different bit position than the other FLAG_b,
// and fills out a different variable of a different type.
if (FLAG(b)) printf("b=%ld", TT.a.b);
if (FLAG(b)) printf("b=%ld", TT.u.a.b);
}
22 changes: 10 additions & 12 deletions toys/pending/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct v_vector {
union {
unsigned hash;
unsigned p;
};
} u;
};

struct diff {
Expand Down Expand Up @@ -93,8 +93,8 @@ enum {

static int comp(const void *a, const void* b)
{
int i = ((struct v_vector *)a)->hash -
((struct v_vector *)b)->hash;
int i = ((struct v_vector *)a)->u.hash -
((struct v_vector *)b)->u.hash;

if (!i) i = ((struct v_vector *)a)->serial -
((struct v_vector *)b)->serial;
Expand Down Expand Up @@ -238,7 +238,7 @@ int bcomp(const void *a, const void *b)
{
struct v_vector *l = (struct v_vector*)a,
*r = (struct v_vector*)b;
int ret = l->hash - r->hash;
int ret = l->u.hash - r->u.hash;

if (!ret) {
if ((r -1)->last) return 0;
Expand Down Expand Up @@ -289,7 +289,7 @@ static int * create_j_vector()
TT.offset[i] = xrealloc(TT.offset[i], size*sizeof(int));
}

v[i][file[i].len].hash = hash & INT_MAX;
v[i][file[i].len].u.hash = hash & INT_MAX;
TT.offset[i][file[i].len] = off;
if ((tok & eof)) {
TT.offset[i][file[i].len] = ++off;
Expand All @@ -308,10 +308,8 @@ static int * create_j_vector()
e = v[1];
e[0].serial = 0;
e[0].last = 1;
for ( i = 1; i <= file[1].len; i++) {
if ((i == file[1].len) || (v[1][i].hash != v[1][i+1].hash)) e[i].last = 1;
else e[i].last = 0;
}
for ( i = 1; i <= file[1].len; i++)
e[i].last = i == file[1].len || v[1][i].u.hash != v[1][i+1].u.hash;

p_vector = xzalloc((file[0].len + 2) * sizeof(int));
for (i = 1; i <= file[0].len; i++) {
Expand All @@ -320,7 +318,7 @@ static int * create_j_vector()
}

for (i = 1; i <= file[0].len; i++)
e[i].p = p_vector[i];
e[i].u.p = p_vector[i];
free(p_vector);

size = 100;
Expand All @@ -332,12 +330,12 @@ static int * create_j_vector()
k = 0; //last successfully filled k candidate.
for (i = 1; i <= file[0].len; i++) {

if (!e[i].p) continue;
if (!e[i].u.p) continue;
if ((size - 2) == k) {
size = size * 11 / 10;
kcand = xrealloc(kcand, (size * sizeof(struct candidate*)));
}
do_merge(kcand, &k, i, e, e[i].p);
do_merge(kcand, &k, i, e, e[i].u.p);
}
free(v[0]); //no need for v_vector now.
free(v[1]);
Expand Down
9 changes: 5 additions & 4 deletions toys/pending/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ GLOBALS(
struct {
char *a;
} exec;
};
} u;

// keep lineno here, we use it to work around a compiler bug
long lineno;
Expand Down Expand Up @@ -2525,7 +2525,7 @@ static void subshell_setup(void)

void sh_main(void)
{
char *new, *cc = TT.sh.c;
char *new, *cc = TT.u.sh.c;
struct sh_function scratch;
int prompt = 0;
struct string_list *sl = 0;
Expand Down Expand Up @@ -2775,8 +2775,9 @@ void exec_main(void)
// exec, handling -acl
TT.isexec = *toys.optargs;
if (FLAG(c)) environ = ee;
if (TT.exec.a || FLAG(l))
*toys.optargs = xmprintf("%s%s", FLAG(l) ? "-" : "", TT.exec.a?:TT.isexec);
if (TT.u.exec.a || FLAG(l))
*toys.optargs = xmprintf("%s%s", FLAG(l) ? "-" : "",
TT.u.exec.a?:TT.isexec);
sh_exec(toys.optargs);

// report error (usually ENOENT) and return
Expand Down
14 changes: 7 additions & 7 deletions toys/posix/cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ GLOBALS(
struct {
char *preserve;
} c;
};
} u;

char *destname;
struct stat top;
Expand Down Expand Up @@ -374,7 +374,7 @@ void cp_main(void)

// Not using comma_args() (yet?) because interpeting as letters.
if (FLAG(preserve)) {
char *pre = xstrdup(TT.c.preserve ? TT.c.preserve : "mot"), *s;
char *pre = xstrdup(TT.u.c.preserve ? TT.u.c.preserve : "mot"), *s;

if (comma_remove(pre, "all")) TT.pflags = ~0;
for (i=0; i<ARRAY_LEN(cp_preserve); i++)
Expand Down Expand Up @@ -474,9 +474,9 @@ static inline int cp_flag_v(void) { return FLAG_v; };

static int install_node(struct dirtree *try)
{
try->st.st_mode = TT.i.m ? string_to_mode(TT.i.m, try->st.st_mode) : 0755;
if (TT.i.g) try->st.st_gid = TT.gid;
if (TT.i.o) try->st.st_uid = TT.uid;
try->st.st_mode = TT.u.i.m ? string_to_mode(TT.u.i.m, try->st.st_mode) : 0755;
if (TT.u.i.g) try->st.st_gid = TT.gid;
if (TT.u.i.o) try->st.st_uid = TT.uid;

// Always returns 0 because no -r
cp_node(try);
Expand All @@ -492,8 +492,8 @@ void install_main(void)
{
char **ss;

TT.uid = TT.i.o ? xgetuid(TT.i.o) : -1;
TT.gid = TT.i.g ? xgetgid(TT.i.g) : -1;
TT.uid = TT.u.i.o ? xgetuid(TT.u.i.o) : -1;
TT.gid = TT.u.i.g ? xgetgid(TT.u.i.g) : -1;

if (FLAG(d)) {
for (ss = toys.optargs; *ss; ss++) {
Expand Down
Loading