Annotation of researchv10no/cmd/odist/tar/id.c, revision 1.1.1.1

1.1       root        1: #include <limits.h>
                      2: #include <stddef.h>
                      3: #include <stdio.h>
                      4: #include <pwd.h>
                      5: #include <grp.h>
                      6: #include "misc.h"
                      7: 
                      8: struct idmap {
                      9:        char *str;
                     10:        int num;
                     11:        struct idmap *next;
                     12: };
                     13: 
                     14: static struct idmap *umap, *gmap;
                     15: 
                     16: static struct idmap *
                     17: idnum(struct idmap **map, char *str)
                     18: {
                     19:        while (*map) {
                     20:                if (strcmp((*map)->str, str) == 0)
                     21:                        return *map;
                     22:                map = &(*map)->next;
                     23:        }
                     24:        return 0;
                     25: }
                     26: 
                     27: static struct idmap *
                     28: idstr(struct idmap **map, int num)
                     29: {
                     30:        while (*map) {
                     31:                if ((*map)->num == num)
                     32:                        return *map;
                     33:                map = &(*map)->next;
                     34:        }
                     35:        return 0;
                     36: }
                     37: 
                     38: static struct idmap *
                     39: idset(struct idmap **map, char *str, int num)
                     40: {
                     41:        struct idmap *mapent;
                     42: 
                     43:        mapent = xmalloc(sizeof (struct idmap));
                     44:        if (str)
                     45:                mapent->str = xstrdup(str);
                     46:        else
                     47:                mapent->str = 0;
                     48:        mapent->num = num;
                     49:        mapent->next = *map;
                     50:        *map = mapent;
                     51:        return mapent;
                     52: }
                     53: 
                     54: int
                     55: uidnum(char *str)
                     56: {
                     57:        struct idmap *id;
                     58:        struct passwd *pw;
                     59:        int num;
                     60: 
                     61:        id = idnum(&umap, str);
                     62:        if (!id) {
                     63:                pw = getpwnam(str);
                     64:                id = idset(&umap, str, pw ? pw->pw_uid : -1);
                     65:        }
                     66:        return id->num;
                     67: }
                     68: 
                     69: char *
                     70: uidstr(int num)
                     71: {
                     72:        struct idmap *id;
                     73:        struct passwd *pw;
                     74:        char *str;
                     75: 
                     76:        id = idstr(&umap, num);
                     77:        if (!id) {
                     78:                pw = getpwuid(num);
                     79:                id = idset(&umap, pw ? pw->pw_name : 0, num);
                     80:        }
                     81:        return id->str;
                     82: }
                     83: 
                     84: int
                     85: gidnum(char *str)
                     86: {
                     87:        struct idmap *id;
                     88:        struct group *gr;
                     89:        int num;
                     90: 
                     91:        id = idnum(&gmap, str);
                     92:        if (!id) {
                     93:                gr = getgrnam(str);
                     94:                id = idset(&gmap, str, gr ? gr->gr_gid : -1);
                     95:        }
                     96:        return id->num;
                     97: }
                     98: 
                     99: char *
                    100: gidstr(int num)
                    101: {
                    102:        struct idmap *id;
                    103:        struct group *gr;
                    104:        char *str;
                    105: 
                    106:        id = idstr(&gmap, num);
                    107:        if (!id) {
                    108:                gr = getgrgid(num);
                    109:                id = idset(&gmap, gr ? gr->gr_name : 0, num);
                    110:        }
                    111:        return id->str;
                    112: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.