File indexing completed on 2025-05-11 08:24:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #ifdef HAVE_CONFIG_H
0022 #include "config.h"
0023 #endif
0024
0025 #include "pwdgrp.h"
0026
0027 #include <stdlib.h>
0028 #include <pthread.h>
0029 #include <rtems/score/assert.h>
0030
0031 typedef struct {
0032 FILE *fp;
0033 char buf[256];
0034 struct group grp;
0035 } grp_context;
0036
0037 static pthread_once_t grp_once = PTHREAD_ONCE_INIT;
0038
0039 static pthread_key_t grp_key;
0040
0041 static void grp_init(void)
0042 {
0043 pthread_key_create(&grp_key, free);
0044 }
0045
0046 static grp_context *grp_get_context(void)
0047 {
0048 pthread_once(&grp_once, grp_init);
0049
0050 return pthread_getspecific(grp_key);
0051 }
0052
0053 struct group *getgrent(void)
0054 {
0055 grp_context *ctx = grp_get_context();
0056
0057 if (ctx == NULL)
0058 return NULL;
0059
0060 if (ctx->fp == NULL)
0061 return NULL;
0062
0063 if (!_libcsupport_scangr(ctx->fp, &ctx->grp, ctx->buf, sizeof(ctx->buf)))
0064 return NULL;
0065
0066 return &ctx->grp;
0067 }
0068
0069 void setgrent(void)
0070 {
0071 grp_context *ctx = grp_get_context();
0072
0073 if (ctx == NULL) {
0074 int eno;
0075
0076 ctx = calloc(1, sizeof(*ctx));
0077 if (ctx == NULL)
0078 return;
0079
0080 eno = pthread_setspecific(grp_key, ctx);
0081 if (eno != 0) {
0082 free(ctx);
0083
0084 return;
0085 }
0086 }
0087
0088 _libcsupport_pwdgrp_init();
0089
0090 if (ctx->fp != NULL)
0091 fclose(ctx->fp);
0092
0093 ctx->fp = fopen("/etc/group", "r");
0094 }
0095
0096 void endgrent(void)
0097 {
0098 grp_context *ctx = grp_get_context();
0099 int sc;
0100
0101 if (ctx == NULL)
0102 return;
0103
0104 if (ctx->fp != NULL) {
0105 fclose(ctx->fp);
0106 }
0107
0108 free(ctx);
0109 sc = pthread_setspecific(grp_key, NULL);
0110 _Assert_Unused_variable_equals(sc, 0);
0111 }