/* * Reads the next node from the given file. On success, the 'arcv' * array is allocated and it must be freed by the caller. * * Return 1 on success, 0 on failure. * */ static int read_node(FILE *f, struct node *n){ int err; err = read_uint_field(f, &n->num); n->num = n->num - 1; /* avoiding 1-indexing */ if (err) { return 1; } err = read_int_field(f, &n->x); if (err) { return 1; } err = read_int_field(f, &n->y); if (err) { return 1; } err = read_uint_field(f, &n->num_arcs); if (err) { return 1; } n->arcv = xmalloc(n->num_arcs * sizeof(*n->arcv)); err = read_arcs(f, n->arcv, n->num_arcs); if (err) { free(n->arcv); return 1; } return 0; }
static int read_pwd(FILE *stream, char *buf, size_t buflen, struct passwd *pwd) { int res; char *p = buf; size_t llen = buflen; if (0 != (res = read_field(stream, &p, &llen, &pwd->pw_name, ':'))) { return res; } if (0 != (res = read_field(stream, &p, &llen, &pwd->pw_passwd, ':'))) { return res; } if (0 != (res = read_int_field(stream, "%hd", &pwd->pw_uid, ':'))) { return res; } if (0 != (res = read_int_field(stream, "%hd", &pwd->pw_gid, ':'))) { return res; } if (0 != (res = read_field(stream, &p, &llen, &pwd->pw_gecos, ':'))) { return res; } if (0 != (res = read_field(stream, &p, &llen, &pwd->pw_dir, ':'))) { return res; } if (0 != (res = read_field(stream, &p, &llen, &pwd->pw_shell, '\n'))) { return res; } return 0; }
/* * Reads the next node from the given file. On success, the 'arcv' * array is allocated and it must be freed by the caller. * * Return 1 on success, 0 on failure. */ static int read_node(FILE *f, struct node *n) { int err; err = read_uint_field(f, &n->num); n->num = n->num - 1; /* avoiding 1-indexing */ if (err) return 1; err = read_int_field(f, &n->x); if (err) return 1; err = read_int_field(f, &n->y); if (err) return 1; err = read_uint_field(f, &n->num_arcs); if (err) return 1; n->arcv = malloc(n->num_arcs * sizeof(*n->arcv)); if (!(n->arcv)) { fprintf(stderr, "Failed allocating %lu bytes for %u arcs\n", (unsigned long) n->num_arcs * sizeof(*n->arcv), n->num_arcs); perror("malloc failed"); return 1; } err = read_arcs(f, n->arcv, n->num_arcs); if (err) { free(n->arcv); return 1; } return 0; }
int fgetgrent_r(FILE *fp, struct group *gbuf, char *tbuf, size_t buflen, struct group **gbufp) { int res; char *buf = tbuf; size_t buf_len = buflen; char *ch, **pmem; *gbufp = NULL; if (0 != (res = read_field(fp, &buf, &buf_len, &gbuf->gr_name, ':'))) { return res; } if (0 != (res = read_field(fp, &buf, &buf_len, &gbuf->gr_passwd, ':'))) { return res; } if (0 != (res = read_int_field(fp, "%hd", &gbuf->gr_gid, ':'))) { return res; } if (0 != (res = read_field(fp, &buf, &buf_len, &ch, '\n'))) { return res; } gbuf->gr_mem = pmem = (char **)binalign_bound((uintptr_t)buf, sizeof(void *)); buf_len -= (uintptr_t)pmem - (uintptr_t)buf; *pmem = ch; while (NULL != (ch = strchr(ch, ','))) { if (buf_len < sizeof(char *)) { return -ERANGE; } buf_len -= sizeof(char *); *ch++ = '\0'; *(++pmem) = ch; } if (buf_len < sizeof(char *)) { return -ERANGE; } *(++pmem) = NULL; *gbufp = gbuf; return 0; }
int get_defpswd(struct passwd *passwd, char *buf, size_t buf_len) { FILE *passwdf; char *temp; int res = 0; if (NULL == (passwdf = fopen(ADDUSER_FILE, "r"))) { return errno; } while (read_field(passwdf, &buf, &buf_len, &temp, '=') != EOF) { if(0 == strcmp(temp, "GROUP")) { if (0 != read_int_field(passwdf, "%hd", &passwd->pw_gid, '\n')) { res = -1; goto out; } continue; } if(0 == strcmp(temp, "HOME")) { if (0 != read_field(passwdf, &buf, &buf_len, &passwd->pw_dir, '\n')) { res = -1; goto out; } continue; } if(0 == strcmp(temp, "SHELL")) { if (0 != read_field(passwdf, &buf, &buf_len, &passwd->pw_shell, '\n')) { res = -1; goto out; } continue; } } out: fclose(passwdf); return res; }
struct spwd *fgetspent(FILE *file) { int res; char *buf = spwd_buf; size_t buf_len = SHADOW_NAME_BUF_LEN + SHADOW_PSWD_BUF_LEN; if (0 != (res = read_field(file, &buf, &buf_len, &spwd.sp_namp, ':'))) { return NULL; } if (0 != (res = read_field(file, &buf, &buf_len, &spwd.sp_pwdp, ':'))) { return NULL; } if (0 != (res = read_int_field(file, "%ld", &spwd.sp_lstchg, ':'))) { return NULL; } if (0 != (res = read_int_field(file, "%ld", &spwd.sp_min, ':'))) { return NULL; } if (0 != (res = read_int_field(file, "%ld", &spwd.sp_max, ':'))) { return NULL; } if (0 != (res = read_int_field(file, "%ld", &spwd.sp_warn, ':'))) { return NULL; } if (0 != (res = read_int_field(file, "%ld", &spwd.sp_inact, ':'))) { return NULL; } if (0 != (res = read_int_field(file, "%ld", &spwd.sp_expire, ':'))) { return NULL; } if (0 != (res = read_int_field(file, "%ld", &spwd.sp_flag, '\n'))) { return NULL; } return &spwd; }