/*- * Copyright (c) 2012 Diane Bruce * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer, * without modification, immediately at the beginning of the file. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* Compile with cc -o gr_testjig gr_testjig.c -lutil */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void gr_print(struct group *gr); static void gr_dissect(struct group *gr); int main() { struct group *gr; struct group *newgr; printf("Getting group wheel\n"); gr = getgrnam("wheel"); if (gr != NULL) { gr_print(gr); gr_dissect(gr); } printf("Duplicating group\n"); newgr = gr_dup(gr); if (newgr != NULL) { gr_print(newgr); gr_dissect(newgr); } printf("Adding new user 'sys' to group 'wheel'\n"); newgr = gr_add(gr, "sys"); if (newgr != NULL) { gr_print(newgr); gr_dissect(newgr); } } static void gr_dissect(struct group *gr) { int i; printf("group is at %p\n", gr); printf("Address of gr->gr_name %p\n", gr->gr_name); hexdump(gr->gr_name, strlen(gr->gr_name) + 1, NULL, 0); printf("Address of gr->gr_passwd %p\n", gr->gr_passwd); hexdump(gr->gr_passwd, strlen(gr->gr_passwd) + 1, NULL, 0); printf("gr->gr_mem %p\n", gr->gr_mem); if (gr->gr_mem != NULL) { printf("Address of gr->gr_mem %p\n", gr->gr_mem); for (i = 0; gr->gr_mem[i] != NULL; i++) { printf("Address of gr->gr_mem[%d] %p\n", i, gr->gr_mem[i]); hexdump(gr->gr_mem[i], strlen(gr->gr_mem[i]) + 1, NULL, 0); } } } static void gr_print(struct group *gr) { int i; printf("name = %s\n", gr->gr_name); printf("passwd = %s\n", gr->gr_passwd); printf("gid = %d\n", gr->gr_gid); if (gr->gr_mem != NULL) { printf("Members***\n\n"); for (i = 0; gr->gr_mem[i] != NULL; i++) printf("%s\n", gr->gr_mem[i]); } #if 0 printf("Hexdump at %p\n\n",gr); hexdump(gr, sizeof(*gr) + 128, NULL, 0); #endif }