MCPcopy Index your code
hub / github.com/F-Stack/f-stack / gr_make

Function gr_make

tools/libutil/gr_util.c:428–463  ·  view source on GitHub ↗

* Make a group line out of a struct group. */

Source from the content-addressed store, hash-verified

426 * Make a group line out of a struct group.
427 */
428char *
429gr_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.

Callers 2

gr_copyFunction · 0.85
mainFunction · 0.85

Calls 3

snprintfFunction · 0.85
mallocFunction · 0.85
sprintfFunction · 0.85

Tested by 1

mainFunction · 0.68