source: rtems/cpukit/libcsupport/src/getgrent.c @ e5e58da

Last change on this file since e5e58da was e5e58da, checked in by Ryan Long <ryan.long@…>, on 02/19/21 at 22:30:13

getgrent.c: Fix Unchecked return value error (CID #1459004)

CID 1459004: Unchecked return value in endgrent().

Closes #4261

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief User Database Access Routines
5 *  @ingroup libcsupport
6 */
7
8/*
9 *  Copyright (c) 1999-2009 Ralf Corsepius <corsepiu@faw.uni-ulm.de>
10 *  Copyright (c) 1999-2013 Joel Sherrill <joel.sherrill@OARcorp.com>
11 *  Copyright (c) 2000-2001 Fernando Ruiz Casas <fernando.ruiz@ctv.es>
12 *  Copyright (c) 2002 Eric Norum <eric.norum@usask.ca>
13 *  Copyright (c) 2003 Till Straumann <strauman@slac.stanford.edu>
14 *  Copyright (c) 2012 Alex Ivanov <alexivanov97@gmail.com>
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include "pwdgrp.h"
26
27#include <stdlib.h>
28#include <pthread.h>
29
30typedef struct {
31  FILE *fp;
32  char buf[256];
33  struct group grp;
34} grp_context;
35
36static pthread_once_t grp_once = PTHREAD_ONCE_INIT;
37
38static pthread_key_t grp_key;
39
40static void grp_init(void)
41{
42  pthread_key_create(&grp_key, free);
43}
44
45static grp_context *grp_get_context(void)
46{
47  pthread_once(&grp_once, grp_init);
48
49  return pthread_getspecific(grp_key);
50}
51
52struct group *getgrent(void)
53{
54  grp_context *ctx = grp_get_context();
55
56  if (ctx == NULL)
57    return NULL;
58
59  if (ctx->fp == NULL)
60    return NULL;
61
62  if (!_libcsupport_scangr(ctx->fp, &ctx->grp, ctx->buf, sizeof(ctx->buf)))
63    return NULL;
64
65  return &ctx->grp;
66}
67
68void setgrent(void)
69{
70  grp_context *ctx = grp_get_context();
71
72  if (ctx == NULL) {
73    int eno;
74
75    ctx = calloc(1, sizeof(*ctx));
76    if (ctx == NULL)
77      return;
78
79    eno = pthread_setspecific(grp_key, ctx);
80    if (eno != 0) {
81      free(ctx);
82
83      return;
84    }
85  }
86
87  _libcsupport_pwdgrp_init();
88
89  if (ctx->fp != NULL)
90    fclose(ctx->fp);
91
92  ctx->fp = fopen("/etc/group", "r");
93}
94
95void endgrent(void)
96{
97  grp_context *ctx = grp_get_context();
98  int          sc;
99
100  if (ctx == NULL)
101    return;
102
103  if (ctx->fp != NULL) {
104    fclose(ctx->fp);
105  }
106
107  free(ctx);
108  sc = pthread_setspecific(grp_key, NULL);
109  _Assert_Unused_variable_equals(sc, 0);
110}
Note: See TracBrowser for help on using the repository browser.