OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [pwd_grp/] [pwd_grp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*  Copyright (C) 2003     Manuel Novoa III
2
 *
3
 *  This library is free software; you can redistribute it and/or
4
 *  modify it under the terms of the GNU Library General Public
5
 *  License as published by the Free Software Foundation; either
6
 *  version 2 of the License, or (at your option) any later version.
7
 *
8
 *  This library is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 *  Library General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU Library General Public
14
 *  License along with this library; if not, write to the Free
15
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
17
 
18
/*  Nov 6, 2003  Initial version.
19
 *
20
 *  NOTE: This implementation is quite strict about requiring all
21
 *    field seperators.  It also does not allow leading whitespace
22
 *    except when processing the numeric fields.  glibc is more
23
 *    lenient.  See the various glibc difference comments below.
24
 *
25
 *  TODO:
26
 *    Move to dynamic allocation of (currently staticly allocated)
27
 *      buffers; especially for the group-related functions since
28
 *      large group member lists will cause error returns.
29
 *
30
 */
31
 
32
#define _GNU_SOURCE
33
#include <features.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <stdint.h>
37
#include <string.h>
38
#include <stddef.h>
39
#include <errno.h>
40
#include <assert.h>
41
#include <ctype.h>
42
#include <pwd.h>
43
#include <grp.h>
44
#include <shadow.h>
45
#ifdef __UCLIBC_HAS_THREADS__
46
#include <pthread.h>
47
#endif
48
 
49
/**********************************************************************/
50
/* Sizes for staticly allocated buffers. */
51
 
52
#define PWD_BUFFER_SIZE 256
53
#define GRP_BUFFER_SIZE 256
54
 
55
/**********************************************************************/
56
/* Prototypes for internal functions. */
57
 
58
extern int __parsepwent(void *pw, char *line);
59
extern int __parsegrent(void *gr, char *line);
60
extern int __parsespent(void *sp, char *line);
61
 
62
extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
63
                                           char *__restrict line_buff, size_t buflen, FILE *f);
64
 
65
/**********************************************************************/
66
/* For the various fget??ent_r funcs, return
67
 *
68
 *  0: success
69
 *  ENOENT: end-of-file encountered
70
 *  ERANGE: buflen too small
71
 *  other error values possible. See __pgsreader.
72
 *
73
 * Also, *result == resultbuf on success and NULL on failure.
74
 *
75
 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
76
 *   We do not, as it really isn't an error if we reach the end-of-file.
77
 *   Doing so is analogous to having fgetc() set errno on EOF.
78
 */
79
/**********************************************************************/
80
#ifdef L_fgetpwent_r
81
 
82
int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
83
                                char *__restrict buffer, size_t buflen,
84
                                struct passwd **__restrict result)
85
{
86
        int rv;
87
 
88
        *result = NULL;
89
 
90
        if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
91
                *result = resultbuf;
92
        }
93
 
94
        return rv;
95
}
96
 
97
#endif
98
/**********************************************************************/
99
#ifdef L_fgetgrent_r
100
 
101
int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
102
                                char *__restrict buffer, size_t buflen,
103
                                struct group **__restrict result)
104
{
105
        int rv;
106
 
107
        *result = NULL;
108
 
109
        if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
110
                *result = resultbuf;
111
        }
112
 
113
        return rv;
114
}
115
 
116
#endif
117
/**********************************************************************/
118
#ifdef L_fgetspent_r
119
 
120
int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
121
                                char *__restrict buffer, size_t buflen,
122
                                struct spwd **__restrict result)
123
{
124
        int rv;
125
 
126
        *result = NULL;
127
 
128
        if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) {
129
                *result = resultbuf;
130
        }
131
 
132
        return rv;
133
}
134
 
135
#endif
136
/**********************************************************************/
137
/* For the various fget??ent funcs, return NULL on failure and a
138
 * pointer to the appropriate struct (staticly allocated) on success.
139
 */
140
/**********************************************************************/
141
#ifdef L_fgetpwent
142
 
143
struct passwd *fgetpwent(FILE *stream)
144
{
145
        static char buffer[PWD_BUFFER_SIZE];
146
        static struct passwd resultbuf;
147
        struct passwd *result;
148
 
149
        fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
150
        return result;
151
}
152
 
153
#endif
154
/**********************************************************************/
155
#ifdef L_fgetgrent
156
 
157
struct group *fgetgrent(FILE *stream)
158
{
159
        static char buffer[GRP_BUFFER_SIZE];
160
        static struct group resultbuf;
161
        struct group *result;
162
 
163
        fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
164
        return result;
165
}
166
 
167
#endif
168
/**********************************************************************/
169
#ifdef L_fgetspent
170
 
171
struct spwd *fgetspent(FILE *stream)
172
{
173
        static char buffer[PWD_BUFFER_SIZE];
174
        static struct spwd resultbuf;
175
        struct spwd *result;
176
 
177
        fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
178
        return result;
179
}
180
 
181
#endif
182
/**********************************************************************/
183
#ifdef L_sgetspent_r
184
 
185
int sgetspent_r(const char *string, struct spwd *result_buf,
186
                                char *buffer, size_t buflen, struct spwd **result)
187
{
188
        int rv = ERANGE;
189
 
190
        *result = NULL;
191
 
192
        if (buflen < PWD_BUFFER_SIZE) {
193
        DO_ERANGE:
194
                __set_errno(rv);
195
                goto DONE;
196
        }
197
 
198
        if (string != buffer) {
199
                if (strlen(string) >= buflen) {
200
                        goto DO_ERANGE;
201
                }
202
                strcpy(buffer, string);
203
        }
204
 
205
        if (!(rv = __parsespent(result_buf, buffer))) {
206
                *result = result_buf;
207
        }
208
 
209
 DONE:
210
        return rv;
211
}
212
 
213
#endif
214
/**********************************************************************/
215
 
216
#ifdef GETXXKEY_R_FUNC
217
#error GETXXKEY_R_FUNC is already defined!
218
#endif
219
 
220
#ifdef L_getpwnam_r
221
#define GETXXKEY_R_FUNC                 getpwnam_r
222
#define GETXXKEY_R_PARSER       __parsepwent
223
#define GETXXKEY_R_ENTTYPE              struct passwd
224
#define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->pw_name, key))
225
#define DO_GETXXKEY_R_KEYTYPE   const char *__restrict
226
#define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
227
#endif
228
 
229
#ifdef L_getgrnam_r
230
#define GETXXKEY_R_FUNC                 getgrnam_r
231
#define GETXXKEY_R_PARSER       __parsegrent
232
#define GETXXKEY_R_ENTTYPE              struct group
233
#define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->gr_name, key))
234
#define DO_GETXXKEY_R_KEYTYPE   const char *__restrict
235
#define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
236
#endif
237
 
238
#ifdef L_getspnam_r
239
#define GETXXKEY_R_FUNC                 getspnam_r
240
#define GETXXKEY_R_PARSER       __parsespent
241
#define GETXXKEY_R_ENTTYPE              struct spwd
242
#define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->sp_namp, key))
243
#define DO_GETXXKEY_R_KEYTYPE   const char *__restrict
244
#define DO_GETXXKEY_R_PATHNAME  _PATH_SHADOW
245
#endif
246
 
247
#ifdef L_getpwuid_r
248
#define GETXXKEY_R_FUNC                 getpwuid_r
249
#define GETXXKEY_R_PARSER       __parsepwent
250
#define GETXXKEY_R_ENTTYPE              struct passwd
251
#define GETXXKEY_R_TEST(ENT)    ((ENT)->pw_uid == key)
252
#define DO_GETXXKEY_R_KEYTYPE   uid_t
253
#define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
254
#endif
255
 
256
#ifdef L_getgrgid_r
257
#define GETXXKEY_R_FUNC                 getgrgid_r
258
#define GETXXKEY_R_PARSER       __parsegrent
259
#define GETXXKEY_R_ENTTYPE              struct group
260
#define GETXXKEY_R_TEST(ENT)    ((ENT)->gr_gid == key)
261
#define DO_GETXXKEY_R_KEYTYPE   gid_t
262
#define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
263
#endif
264
 
265
/**********************************************************************/
266
#ifdef GETXXKEY_R_FUNC
267
 
268
int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
269
                                        GETXXKEY_R_ENTTYPE *__restrict resultbuf,
270
                                        char *__restrict buffer, size_t buflen,
271
                                        GETXXKEY_R_ENTTYPE **__restrict result)
272
{
273
        FILE *stream;
274
        int rv;
275
 
276
        *result = NULL;
277
 
278
        if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
279
                rv = errno;
280
        } else {
281
                __STDIO_SET_USER_LOCKING(stream);
282
                do {
283
                        if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
284
                                                                   buffer, buflen, stream))
285
                                ) {
286
                                if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
287
                                        *result = resultbuf;
288
                                        break;
289
                                }
290
                        } else {
291
                                if (rv == ENOENT) {     /* end-of-file encountered. */
292
                                        rv = 0;
293
                                }
294
                                break;
295
                        }
296
                } while (1);
297
                fclose(stream);
298
        }
299
 
300
        return rv;
301
}
302
 
303
#endif
304
/**********************************************************************/
305
#ifdef L_getpwuid
306
 
307
struct passwd *getpwuid(uid_t uid)
308
{
309
        static char buffer[PWD_BUFFER_SIZE];
310
        static struct passwd resultbuf;
311
        struct passwd *result;
312
 
313
        getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
314
        return result;
315
}
316
 
317
#endif
318
/**********************************************************************/
319
#ifdef L_getgrgid
320
 
321
struct group *getgrgid(gid_t gid)
322
{
323
        static char buffer[GRP_BUFFER_SIZE];
324
        static struct group resultbuf;
325
        struct group *result;
326
 
327
        getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
328
        return result;
329
}
330
 
331
#endif
332
/**********************************************************************/
333
#ifdef L_getspuid_r
334
 
335
/* This function is non-standard and is currently not built.  It seems
336
 * to have been created as a reentrant version of the non-standard
337
 * functions getspuid.  Why getspuid was added, I do not know. */
338
 
339
int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
340
                       char *__restrict buffer, size_t buflen,
341
                       struct spwd **__restrict result)
342
{
343
        int rv;
344
        struct passwd *pp;
345
        struct passwd password;
346
        char pwd_buff[PWD_BUFFER_SIZE];
347
 
348
        *result = NULL;
349
        if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
350
                rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
351
        }
352
 
353
        return rv;
354
}
355
 
356
#endif
357
/**********************************************************************/
358
#ifdef L_getspuid
359
 
360
/* This function is non-standard and is currently not built.
361
 * Why it was added, I do not know. */
362
 
363
struct spwd *getspuid(uid_t uid)
364
{
365
        static char buffer[PWD_BUFFER_SIZE];
366
        static struct spwd resultbuf;
367
        struct spwd *result;
368
 
369
        getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
370
        return result;
371
}
372
 
373
#endif
374
/**********************************************************************/
375
#ifdef L_getpwnam
376
 
377
struct passwd *getpwnam(const char *name)
378
{
379
        static char buffer[PWD_BUFFER_SIZE];
380
        static struct passwd resultbuf;
381
        struct passwd *result;
382
 
383
        getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
384
        return result;
385
}
386
 
387
#endif
388
/**********************************************************************/
389
#ifdef L_getgrnam
390
 
391
struct group *getgrnam(const char *name)
392
{
393
        static char buffer[GRP_BUFFER_SIZE];
394
        static struct group resultbuf;
395
        struct group *result;
396
 
397
        getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
398
        return result;
399
}
400
 
401
#endif
402
/**********************************************************************/
403
#ifdef L_getspnam
404
 
405
struct spwd *getspnam(const char *name)
406
{
407
        static char buffer[PWD_BUFFER_SIZE];
408
        static struct spwd resultbuf;
409
        struct spwd *result;
410
 
411
        getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
412
        return result;
413
}
414
 
415
#endif
416
/**********************************************************************/
417
#ifdef L_getpw
418
 
419
int getpw(uid_t uid, char *buf)
420
{
421
        struct passwd resultbuf;
422
        struct passwd *result;
423
        char buffer[PWD_BUFFER_SIZE];
424
 
425
        if (!buf) {
426
                __set_errno(EINVAL);
427
        } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
428
                if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
429
                                        resultbuf.pw_name, resultbuf.pw_passwd,
430
                                        (unsigned long)(resultbuf.pw_uid),
431
                                        (unsigned long)(resultbuf.pw_gid),
432
                                        resultbuf.pw_gecos, resultbuf.pw_dir,
433
                                        resultbuf.pw_shell) >= 0
434
                        ) {
435
                        return 0;
436
                }
437
        }
438
 
439
        return -1;
440
}
441
 
442
#endif
443
/**********************************************************************/
444
#ifdef L_getpwent_r
445
 
446
#ifdef __UCLIBC_HAS_THREADS__
447
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
448
# define LOCK           __pthread_mutex_lock(&mylock)
449
# define UNLOCK         __pthread_mutex_unlock(&mylock);
450
#else       
451
# define LOCK           ((void) 0)
452
# define UNLOCK         ((void) 0)
453
#endif      
454
 
455
static FILE *pwf /*= NULL*/;
456
 
457
void setpwent(void)
458
{
459
        LOCK;
460
        if (pwf) {
461
                rewind(pwf);
462
        }
463
        UNLOCK;
464
}
465
 
466
void endpwent(void)
467
{
468
        LOCK;
469
        if (pwf) {
470
                fclose(pwf);
471
                pwf = NULL;
472
        }
473
        UNLOCK;
474
}
475
 
476
 
477
int getpwent_r(struct passwd *__restrict resultbuf,
478
                           char *__restrict buffer, size_t buflen,
479
                           struct passwd **__restrict result)
480
{
481
        int rv;
482
 
483
        LOCK;
484
 
485
        *result = NULL;                         /* In case of error... */
486
 
487
        if (!pwf) {
488
                if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
489
                        rv = errno;
490
                        goto ERR;
491
                }
492
                __STDIO_SET_USER_LOCKING(pwf);
493
        }
494
 
495
        if (!(rv = __pgsreader(__parsepwent, resultbuf,
496
                                                   buffer, buflen, pwf))) {
497
                *result = resultbuf;
498
        }
499
 
500
 ERR:
501
        UNLOCK;
502
 
503
        return rv;
504
}
505
 
506
#endif
507
/**********************************************************************/
508
#ifdef L_getgrent_r
509
 
510
#ifdef __UCLIBC_HAS_THREADS__
511
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
512
# define LOCK           __pthread_mutex_lock(&mylock)
513
# define UNLOCK         __pthread_mutex_unlock(&mylock);
514
#else       
515
# define LOCK           ((void) 0)
516
# define UNLOCK         ((void) 0)
517
#endif      
518
 
519
static FILE *grf /*= NULL*/;
520
 
521
void setgrent(void)
522
{
523
        LOCK;
524
        if (grf) {
525
                rewind(grf);
526
        }
527
        UNLOCK;
528
}
529
 
530
void endgrent(void)
531
{
532
        LOCK;
533
        if (grf) {
534
                fclose(grf);
535
                grf = NULL;
536
        }
537
        UNLOCK;
538
}
539
 
540
int getgrent_r(struct group *__restrict resultbuf,
541
                           char *__restrict buffer, size_t buflen,
542
                           struct group **__restrict result)
543
{
544
        int rv;
545
 
546
        LOCK;
547
 
548
        *result = NULL;                         /* In case of error... */
549
 
550
        if (!grf) {
551
                if (!(grf = fopen(_PATH_GROUP, "r"))) {
552
                        rv = errno;
553
                        goto ERR;
554
                }
555
                __STDIO_SET_USER_LOCKING(grf);
556
        }
557
 
558
        if (!(rv = __pgsreader(__parsegrent, resultbuf,
559
                                                   buffer, buflen, grf))) {
560
                *result = resultbuf;
561
        }
562
 
563
 ERR:
564
        UNLOCK;
565
 
566
        return rv;
567
}
568
 
569
#endif
570
/**********************************************************************/
571
#ifdef L_getspent_r
572
 
573
#ifdef __UCLIBC_HAS_THREADS__
574
static pthread_mutex_t mylock =  PTHREAD_MUTEX_INITIALIZER;
575
# define LOCK           __pthread_mutex_lock(&mylock)
576
# define UNLOCK         __pthread_mutex_unlock(&mylock);
577
#else       
578
# define LOCK           ((void) 0)
579
# define UNLOCK         ((void) 0)
580
#endif      
581
 
582
static FILE *spf /*= NULL*/;
583
 
584
void setspent(void)
585
{
586
        LOCK;
587
        if (spf) {
588
                rewind(spf);
589
        }
590
        UNLOCK;
591
}
592
 
593
void endspent(void)
594
{
595
        LOCK;
596
        if (spf) {
597
                fclose(spf);
598
                spf = NULL;
599
        }
600
        UNLOCK;
601
}
602
 
603
int getspent_r(struct spwd *resultbuf, char *buffer,
604
                           size_t buflen, struct spwd **result)
605
{
606
        int rv;
607
 
608
        LOCK;
609
 
610
        *result = NULL;                         /* In case of error... */
611
 
612
        if (!spf) {
613
                if (!(spf = fopen(_PATH_SHADOW, "r"))) {
614
                        rv = errno;
615
                        goto ERR;
616
                }
617
                __STDIO_SET_USER_LOCKING(spf);
618
        }
619
 
620
        if (!(rv = __pgsreader(__parsespent, resultbuf,
621
                                                   buffer, buflen, spf))) {
622
                *result = resultbuf;
623
        }
624
 
625
 ERR:
626
        UNLOCK;
627
 
628
        return rv;
629
}
630
 
631
#endif
632
/**********************************************************************/
633
#ifdef L_getpwent
634
 
635
struct passwd *getpwent(void)
636
{
637
        static char line_buff[PWD_BUFFER_SIZE];
638
        static struct passwd pwd;
639
        struct passwd *result;
640
 
641
        getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
642
        return result;
643
}
644
 
645
#endif
646
/**********************************************************************/
647
#ifdef L_getgrent
648
 
649
struct group *getgrent(void)
650
{
651
        static char line_buff[GRP_BUFFER_SIZE];
652
        static struct group gr;
653
        struct group *result;
654
 
655
        getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
656
        return result;
657
}
658
 
659
#endif
660
/**********************************************************************/
661
#ifdef L_getspent
662
 
663
struct spwd *getspent(void)
664
{
665
        static char line_buff[PWD_BUFFER_SIZE];
666
        static struct spwd spwd;
667
        struct spwd *result;
668
 
669
        getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
670
        return result;
671
}
672
 
673
#endif
674
/**********************************************************************/
675
#ifdef L_sgetspent
676
 
677
struct spwd *sgetspent(const char *string)
678
{
679
        static char line_buff[PWD_BUFFER_SIZE];
680
        static struct spwd spwd;
681
        struct spwd *result;
682
 
683
        sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
684
        return result;
685
}
686
 
687
#endif
688
/**********************************************************************/
689
#ifdef L_initgroups
690
 
691
int initgroups(const char *user, gid_t gid)
692
{
693
        FILE *grf;
694
        gid_t *group_list;
695
        int num_groups, rv;
696
        char **m;
697
        struct group group;
698
        char buff[PWD_BUFFER_SIZE];
699
 
700
        rv = -1;
701
 
702
        /* We alloc space for 8 gids at a time. */
703
        if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
704
                && ((grf = fopen(_PATH_GROUP, "r")) != NULL)
705
                ) {
706
 
707
                __STDIO_SET_USER_LOCKING(grf);
708
 
709
                *group_list = gid;
710
                num_groups = 1;
711
 
712
                while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) {
713
                        assert(group.gr_mem); /* Must have at least a NULL terminator. */
714
                        if (group.gr_gid != gid) {
715
                                for (m=group.gr_mem ; *m ; m++) {
716
                                        if (!strcmp(*m, user)) {
717
                                                if (!(num_groups & 7)) {
718
                                                        gid_t *tmp = (gid_t *)
719
                                                                realloc(group_list,
720
                                                                                (num_groups+8) * sizeof(gid_t *));
721
                                                        if (!tmp) {
722
                                                                rv = -1;
723
                                                                goto DO_CLOSE;
724
                                                        }
725
                                                        group_list = tmp;
726
                                                }
727
                                                group_list[num_groups++] = group.gr_gid;
728
                                                break;
729
                                        }
730
                                }
731
                        }
732
                }
733
 
734
                rv = setgroups(num_groups, group_list);
735
        DO_CLOSE:
736
                fclose(grf);
737
        }
738
 
739
        /* group_list will be NULL if initial malloc failed, which may trigger
740
         * warnings from various malloc debuggers. */
741
        free(group_list);
742
        return rv;
743
}
744
 
745
#endif
746
/**********************************************************************/
747
#ifdef L_putpwent
748
 
749
int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
750
{
751
        int rv = -1;
752
 
753
        if (!p || !f) {
754
                __set_errno(EINVAL);
755
        } else {
756
                /* No extra thread locking is needed above what fprintf does. */
757
                if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
758
                                        p->pw_name, p->pw_passwd,
759
                                        (unsigned long)(p->pw_uid),
760
                                        (unsigned long)(p->pw_gid),
761
                                        p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
762
                        ) {
763
                        rv = 0;
764
                }
765
        }
766
 
767
        return rv;
768
}
769
 
770
#endif
771
/**********************************************************************/
772
#ifdef L_putgrent
773
 
774
int putgrent(const struct group *__restrict p, FILE *__restrict f)
775
{
776
        static const char format[] = ",%s";
777
        char **m;
778
        const char *fmt;
779
        int rv = -1;
780
 
781
        if (!p || !f) {                         /* Sigh... glibc checks. */
782
                __set_errno(EINVAL);
783
        } else {
784
                __STDIO_THREADLOCK(f);
785
 
786
                if (fprintf(f, "%s:%s:%lu:",
787
                                        p->gr_name, p->gr_passwd,
788
                                        (unsigned long)(p->gr_gid)) >= 0
789
                        ) {
790
 
791
                        fmt = format + 1;
792
 
793
                        assert(p->gr_mem);
794
                        m = p->gr_mem;
795
 
796
                        do {
797
                                if (!*m) {
798
                                        if (fputc_unlocked('\n', f) >= 0) {
799
                                                rv = 0;
800
                                        }
801
                                        break;
802
                                }
803
                                if (fprintf(f, fmt, *m) < 0) {
804
                                        break;
805
                                }
806
                                ++m;
807
                                fmt = format;
808
                        } while (1);
809
 
810
                }
811
 
812
                __STDIO_THREADUNLOCK(f);
813
        }
814
 
815
        return rv;
816
}
817
 
818
#endif
819
/**********************************************************************/
820
#ifdef L_putspent
821
 
822
static const unsigned char sp_off[] = {
823
        offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
824
        offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
825
        offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
826
        offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
827
        offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
828
        offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
829
};
830
 
831
int putspent(const struct spwd *p, FILE *stream)
832
{
833
        static const char ld_format[] = "%ld:";
834
        const char *f;
835
        long int x;
836
        int i;
837
        int rv = -1;
838
 
839
        /* Unlike putpwent and putgrent, glibc does not check the args. */
840
 
841
        __STDIO_THREADLOCK(stream);
842
 
843
        if (fprintf(stream, "%s:%s:", p->sp_namp,
844
                                (p->sp_pwdp ? p->sp_pwdp : "")) < 0
845
                ) {
846
                goto DO_UNLOCK;
847
        }
848
 
849
        for (i=0 ; i < sizeof(sp_off) ; i++) {
850
                f = ld_format;
851
                if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) {
852
                        f += 3;
853
                }
854
                if (fprintf(stream, f, x) < 0) {
855
                        goto DO_UNLOCK;
856
                }
857
        }
858
 
859
        if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
860
                goto DO_UNLOCK;
861
        }
862
 
863
        if (fputc_unlocked('\n', stream) > 0) {
864
                rv = 0;
865
        }
866
 
867
 DO_UNLOCK:
868
        __STDIO_THREADUNLOCK(stream);
869
 
870
        return rv;
871
}
872
 
873
#endif
874
/**********************************************************************/
875
/* Internal uClibc functions.                                         */
876
/**********************************************************************/
877
#ifdef L___parsepwent
878
 
879
static const unsigned char pw_off[] = {
880
        offsetof(struct passwd, pw_name),       /* 0 */
881
        offsetof(struct passwd, pw_passwd),     /* 1 */
882
        offsetof(struct passwd, pw_uid),        /* 2 - not a char ptr */
883
        offsetof(struct passwd, pw_gid),        /* 3 - not a char ptr */
884
        offsetof(struct passwd, pw_gecos),      /* 4 */
885
        offsetof(struct passwd, pw_dir),        /* 5 */
886
        offsetof(struct passwd, pw_shell)       /* 6 */
887
};
888
 
889
int __parsepwent(void *data, char *line)
890
{
891
        char *endptr;
892
        char *p;
893
        int i;
894
 
895
        i = 0;
896
        do {
897
                p = ((char *) ((struct passwd *) data)) + pw_off[i];
898
 
899
                if ((i & 6) ^ 2) {      /* i!=2 and i!=3 */
900
                        *((char **) p) = line;
901
                        if (i==6) {
902
                                return 0;
903
                        }
904
                        /* NOTE: glibc difference - glibc allows omission of
905
                         * ':' seperators after the gid field if all remaining
906
                         * entries are empty.  We require all separators. */
907
                        if (!(line = strchr(line, ':'))) {
908
                                break;
909
                        }
910
                } else {
911
                        unsigned long t = strtoul(line, &endptr, 10);
912
                        /* Make sure we had at least one digit, and that the
913
                         * failing char is the next field seperator ':'.  See
914
                         * glibc difference note above. */
915
                        /* TODO: Also check for leading whitespace? */
916
                        if ((endptr == line) || (*endptr != ':')) {
917
                                break;
918
                        }
919
                        line = endptr;
920
                        if (i & 1) {            /* i == 3 -- gid */
921
                                *((gid_t *) p) = t;
922
                        } else {                        /* i == 2 -- uid */
923
                                *((uid_t *) p) = t;
924
                        }
925
                }
926
 
927
                *line++ = 0;
928
                ++i;
929
        } while (1);
930
 
931
        return -1;
932
}
933
 
934
#endif
935
/**********************************************************************/
936
#ifdef L___parsegrent
937
 
938
static const unsigned char gr_off[] = {
939
        offsetof(struct group, gr_name),        /* 0 */
940
        offsetof(struct group, gr_passwd),      /* 1 */
941
        offsetof(struct group, gr_gid)          /* 2 - not a char ptr */
942
};
943
 
944
int __parsegrent(void *data, char *line)
945
{
946
        char *endptr;
947
        char *p;
948
        int i;
949
        char **members;
950
        char *end_of_buf;
951
 
952
        end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
953
        i = 0;
954
        do {
955
                p = ((char *) ((struct group *) data)) + gr_off[i];
956
 
957
                if (i < 2) {
958
                        *((char **) p) = line;
959
                        if (!(line = strchr(line, ':'))) {
960
                                break;
961
                        }
962
                        *line++ = 0;
963
                        ++i;
964
                } else {
965
                        *((gid_t *) p) = strtoul(line, &endptr, 10);
966
 
967
                        /* NOTE: glibc difference - glibc allows omission of the
968
                         * trailing colon when there is no member list.  We treat
969
                         * this as an error. */
970
 
971
                        /* Make sure we had at least one digit, and that the
972
                         * failing char is the next field seperator ':'.  See
973
                         * glibc difference note above. */
974
                        if ((endptr == line) || (*endptr != ':')) {
975
                                break;
976
                        }
977
 
978
                        i = 1;                          /* Count terminating NULL ptr. */
979
                        p = endptr;
980
 
981
                        if (p[1]) { /* We have a member list to process. */
982
                                /* Overwrite the last ':' with a ',' before counting.
983
                                 * This allows us to test for initial ',' and adds
984
                                 * one ',' so that the ',' count equals the member
985
                                 * count. */
986
                                *p = ',';
987
                                do {
988
                                        /* NOTE: glibc difference - glibc allows and trims leading
989
                                         * (but not trailing) space.  We treat this as an error. */
990
                                        /* NOTE: glibc difference - glibc allows consecutive and
991
                                         * trailing commas, and ignores "empty string" users.  We
992
                                         * treat this as an error. */
993
                                        if (*p == ',') {
994
                                                ++i;
995
                                                *p = 0;  /* nul-terminate each member string. */
996
                                                if (!*++p || (*p == ',') || isspace(*p)) {
997
                                                        goto ERR;
998
                                                }
999
                                        }
1000
                                } while (*++p);
1001
                        }
1002
 
1003
                        /* Now align (p+1), rounding up. */
1004
                        /* Assumes sizeof(char **) is a power of 2. */
1005
                        members = (char **)( (((intptr_t) p) + sizeof(char **))
1006
                                                                 & ~((intptr_t)(sizeof(char **) - 1)) );
1007
 
1008
                        if (((char *)(members + i)) > end_of_buf) {     /* No space. */
1009
                                break;
1010
                        }
1011
 
1012
                        ((struct group *) data)->gr_mem = members;
1013
 
1014
                        if (--i) {
1015
                                p = endptr;     /* Pointing to char prior to first member. */
1016
                                do {
1017
                                        *members++ = ++p;
1018
                                        if (!--i) break;
1019
                                        while (*++p) {}
1020
                                } while (1);
1021
                        }
1022
                        *members = NULL;
1023
 
1024
                        return 0;
1025
                }
1026
        } while (1);
1027
 
1028
 ERR:
1029
        return -1;
1030
}
1031
 
1032
#endif
1033
/**********************************************************************/
1034
#ifdef L___parsespent
1035
 
1036
static const unsigned char sp_off[] = {
1037
        offsetof(struct spwd, sp_namp),         /* 0 */
1038
        offsetof(struct spwd, sp_pwdp),         /* 1 */
1039
        offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
1040
        offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
1041
        offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
1042
        offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
1043
        offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
1044
        offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
1045
        offsetof(struct spwd, sp_flag)          /* 8 - not a char ptr */
1046
};
1047
 
1048
int __parsespent(void *data, char * line)
1049
{
1050
        char *endptr;
1051
        char *p;
1052
        int i;
1053
 
1054
        i = 0;
1055
        do {
1056
                p = ((char *) ((struct spwd *) data)) + sp_off[i];
1057
                if (i < 2) {
1058
                        *((char **) p) = line;
1059
                        if (!(line = strchr(line, ':'))) {
1060
                                break;
1061
                        }
1062
                } else {
1063
#if 0
1064
                        if (i==5) {                     /* Support for old format. */
1065
                                while (isspace(*line)) ++line; /* glibc eats space here. */
1066
                                if (!*line) {
1067
                                        ((struct spwd *) data)->sp_warn = -1;
1068
                                        ((struct spwd *) data)->sp_inact = -1;
1069
                                        ((struct spwd *) data)->sp_expire = -1;
1070
                                        ((struct spwd *) data)->sp_flag = ~0UL;
1071
                                        return 0;
1072
                                }
1073
                        }
1074
#endif
1075
 
1076
                        *((long *) p) = (long) strtoul(line, &endptr, 10);
1077
 
1078
                        if (endptr == line) {
1079
                                *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
1080
                        }
1081
 
1082
                        line = endptr;
1083
 
1084
                        if (i == 8) {
1085
                                if (!*endptr) {
1086
                                        return 0;
1087
                                }
1088
                                break;
1089
                        }
1090
 
1091
                        if (*endptr != ':') {
1092
                                break;
1093
                        }
1094
 
1095
                }
1096
 
1097
                *line++ = 0;
1098
                ++i;
1099
        } while (1);
1100
 
1101
        return EINVAL;
1102
}
1103
 
1104
#endif
1105
/**********************************************************************/
1106
#ifdef L___pgsreader
1107
 
1108
/* Reads until if EOF, or until if finds a line which fits in the buffer
1109
 * and for which the parser function succeeds.
1110
 *
1111
 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1112
 */
1113
 
1114
int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
1115
                                char *__restrict line_buff, size_t buflen, FILE *f)
1116
{
1117
        int line_len;
1118
        int skip;
1119
        int rv = ERANGE;
1120
 
1121
        if (buflen < PWD_BUFFER_SIZE) {
1122
                __set_errno(rv);
1123
        } else {
1124
                __STDIO_THREADLOCK(f);
1125
 
1126
                skip = 0;
1127
                do {
1128
                        if (!fgets_unlocked(line_buff, buflen, f)) {
1129
                                if (feof_unlocked(f)) {
1130
                                        rv = ENOENT;
1131
                                }
1132
                                break;
1133
                        }
1134
 
1135
                        line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1136
                        if (line_buff[line_len] == '\n') {
1137
                                line_buff[line_len] = 0;
1138
                        } else if (line_len + 2 == buflen) { /* line too long */
1139
                                ++skip;
1140
                                continue;
1141
                        }
1142
 
1143
                        if (skip) {
1144
                                --skip;
1145
                                continue;
1146
                        }
1147
 
1148
                        /* NOTE: glibc difference - glibc strips leading whitespace from
1149
                         * records.  We do not allow leading whitespace. */
1150
 
1151
                        /* Skip empty lines, comment lines, and lines with leading
1152
                         * whitespace. */
1153
                        if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1154
                                if (__parserfunc == __parsegrent) {     /* Do evil group hack. */
1155
                                        /* The group entry parsing function needs to know where
1156
                                         * the end of the buffer is so that it can construct the
1157
                                         * group member ptr table. */
1158
                                        ((struct group *) data)->gr_name = line_buff + buflen;
1159
                                }
1160
 
1161
                                if (!__parserfunc(data, line_buff)) {
1162
                                        rv = 0;
1163
                                        break;
1164
                                }
1165
                        }
1166
                } while (1);
1167
 
1168
                __STDIO_THREADUNLOCK(f);
1169
        }
1170
 
1171
        return rv;
1172
}
1173
 
1174
#endif
1175
/**********************************************************************/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.