* Make a group line out of a struct group. */
| 426 | * Make a group line out of a struct group. |
| 427 | */ |
| 428 | char * |
| 429 | gr_make(const struct group *gr) |
| 430 | { |
| 431 | const char *group_line_format = "%s:%s:%ju:"; |
| 432 | const char *sep; |
| 433 | char *line; |
| 434 | char *p; |
| 435 | size_t line_size; |
| 436 | int ndx; |
| 437 | |
| 438 | /* Calculate the length of the group line. */ |
| 439 | line_size = snprintf(NULL, 0, group_line_format, gr->gr_name, |
| 440 | gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1; |
| 441 | if (gr->gr_mem != NULL) { |
| 442 | for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) |
| 443 | line_size += strlen(gr->gr_mem[ndx]) + 1; |
| 444 | if (ndx > 0) |
| 445 | line_size--; |
| 446 | } |
| 447 | |
| 448 | /* Create the group line and fill it. */ |
| 449 | if ((line = p = malloc(line_size)) == NULL) |
| 450 | return (NULL); |
| 451 | p += sprintf(p, group_line_format, gr->gr_name, gr->gr_passwd, |
| 452 | (uintmax_t)gr->gr_gid); |
| 453 | if (gr->gr_mem != NULL) { |
| 454 | sep = ""; |
| 455 | for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { |
| 456 | p = stpcpy(p, sep); |
| 457 | p = stpcpy(p, gr->gr_mem[ndx]); |
| 458 | sep = ","; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return (line); |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Duplicate a struct group. |