// SPDX-License-Identifier: WTFPL #include #include #include #define MAX_ID 65536 static char *loadfile(char *filename) { FILE *fp; size_t size; char *buf; fp = fopen(filename, "r"); if (fp == NULL) return NULL; fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); // treat as string, add '\0' at last buf = calloc(size + 1, 1); if (buf == NULL) goto fin0; fread(buf, size, 1, fp); fin0: fclose(fp); return buf; } static void tokenize(char *table[], char *buf) { int i, id; char *p, *n, *d; d = NULL; for (i = 1, p = buf; p != NULL && *p; i++, p = n) { n = strchr(p, ':'); if (n != NULL) { *n = '\0'; n++; } // extract 3rd (ID) field if (i == 3) { d = p; break; // no need to tokenize more } } if (d != NULL) { id = atoi(d); if (id >= 0 && id < MAX_ID) table[id] = buf; // 1st field: name } } static void parse_lines(char *table[], char *buf) { char *p, *n; for (p = buf; p != NULL && *p; p = n) { n = strchr(p, '\n'); if (n != NULL) { *n = '\0'; n++; } tokenize(table, p); } } static void match_tables(char *src[], char *dest[], int entry) { int i, j; // NFS mapping for client // uid/gid remote local for (i = 0; i < entry; i++) { if (src[i] == NULL) continue; for (j = 0; j < entry; j++) { if (dest[j] == NULL) continue; if (!strcmp(src[i], dest[j])) { printf("gid %5d %5d # %s\n", j, i, src[i]); break; } } } } int main(int argc, char *argv[]) { char *local, *remote; char *local_id[MAX_ID]; char *remote_id[MAX_ID]; memset(local_id, 0, sizeof(local_id)); memset(remote_id, 0, sizeof(remote_id)); if (argc < 3) { printf("%s [local] [remote]\n", argv[0]); return 0; } local = loadfile(argv[1]); if (local == NULL) { printf("file open error(local)\n"); goto fin0; } remote = loadfile(argv[2]); if (remote == NULL) { printf("file open error(remote)\n"); goto fin1; } parse_lines(local_id, local); parse_lines(remote_id, remote); match_tables(local_id, remote_id, MAX_ID); //fin2: free(remote); fin1: free(local); fin0: return 0; }