Mercurial > hg > toybox
comparison toys/ls.c @ 650:dd82e0b28eda
Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 18 Aug 2012 21:12:02 -0500 |
parents | 92200901cfe1 |
children |
comparison
equal
deleted
inserted
replaced
649:2364ace48ab1 | 650:dd82e0b28eda |
---|---|
86 DEFINE_GLOBALS( | 86 DEFINE_GLOBALS( |
87 struct dirtree *files; | 87 struct dirtree *files; |
88 | 88 |
89 unsigned screen_width; | 89 unsigned screen_width; |
90 int nl_title; | 90 int nl_title; |
91 | |
92 // group and user can make overlapping use of the utoa() buf, so move it | |
93 char uid_buf[12]; | |
91 ) | 94 ) |
92 | 95 |
93 #define TT this.ls | 96 #define TT this.ls |
94 | 97 |
95 void dlist_to_dirtree(struct dirtree *parent) | 98 void dlist_to_dirtree(struct dirtree *parent) |
119 } | 122 } |
120 | 123 |
121 static char *getusername(uid_t uid) | 124 static char *getusername(uid_t uid) |
122 { | 125 { |
123 struct passwd *pw = getpwuid(uid); | 126 struct passwd *pw = getpwuid(uid); |
124 return pw ? pw->pw_name : utoa(uid); | 127 utoa_to_buf(uid, TT.uid_buf, 12); |
128 return pw ? pw->pw_name : TT.uid_buf; | |
125 } | 129 } |
126 | 130 |
127 static char *getgroupname(gid_t gid) | 131 static char *getgroupname(gid_t gid) |
128 { | 132 { |
129 struct group *gr = getgrgid(gid); | 133 struct group *gr = getgrgid(gid); |
363 if (flags & FLAG_s) | 367 if (flags & FLAG_s) |
364 xprintf("% *lu ", len[6], (unsigned long)st->st_blocks); | 368 xprintf("% *lu ", len[6], (unsigned long)st->st_blocks); |
365 | 369 |
366 if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) { | 370 if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) { |
367 struct tm *tm; | 371 struct tm *tm; |
368 char perm[11], thyme[64], c, d, *usr, *upad, buf[12], *grp, *grpad; | 372 char perm[11], thyme[64], c, d, *usr, *upad, *grp, *grpad; |
369 int i, bit; | 373 int i, bit; |
370 | 374 |
371 perm[10]=0; | 375 perm[10]=0; |
372 for (i=0; i<9; i++) { | 376 for (i=0; i<9; i++) { |
373 bit = mode & (1<<i); | 377 bit = mode & (1<<i); |
400 | 404 |
401 if (flags&FLAG_g) usr = upad = toybuf+256; | 405 if (flags&FLAG_g) usr = upad = toybuf+256; |
402 else { | 406 else { |
403 upad = toybuf+255-(totals[3]-len[3]); | 407 upad = toybuf+255-(totals[3]-len[3]); |
404 if (flags&FLAG_n) { | 408 if (flags&FLAG_n) { |
405 usr = buf; | 409 usr = TT.uid_buf; |
406 utoa_to_buf(st->st_uid, buf, 12); | 410 utoa_to_buf(st->st_uid, TT.uid_buf, 12); |
407 } else usr = getusername(st->st_uid); | 411 } else usr = getusername(st->st_uid); |
408 } | 412 } |
409 | 413 |
410 // Coerce the st types into something we know we can print. | 414 // Coerce the st types into something we know we can print. |
411 xprintf("%s% *ld %s%s%s%s% *"PRId64" %s ", perm, totals[2]+1, | 415 xprintf("%s% *ld %s%s%s%s% *"PRId64" %s ", perm, totals[2]+1, |