diff options
Diffstat (limited to 'libc-0.0.4/pwd')
-rw-r--r-- | libc-0.0.4/pwd/Config | 1 | ||||
-rw-r--r-- | libc-0.0.4/pwd/Makefile | 40 | ||||
-rw-r--r-- | libc-0.0.4/pwd/__getpwent.c | 152 | ||||
-rw-r--r-- | libc-0.0.4/pwd/fgetpwent.c | 35 | ||||
-rw-r--r-- | libc-0.0.4/pwd/getpw.c | 51 | ||||
-rw-r--r-- | libc-0.0.4/pwd/getpwnam.c | 52 | ||||
-rw-r--r-- | libc-0.0.4/pwd/getpwuid.c | 44 | ||||
-rw-r--r-- | libc-0.0.4/pwd/putpwent.c | 39 | ||||
-rw-r--r-- | libc-0.0.4/pwd/pwent.c | 62 | ||||
-rwxr-xr-x | libc-0.0.4/pwd/test_pwd | bin | 0 -> 6436 bytes | |||
-rw-r--r-- | libc-0.0.4/pwd/test_pwd.c | 91 |
11 files changed, 567 insertions, 0 deletions
diff --git a/libc-0.0.4/pwd/Config b/libc-0.0.4/pwd/Config new file mode 100644 index 0000000..b5aaad0 --- /dev/null +++ b/libc-0.0.4/pwd/Config @@ -0,0 +1 @@ +pwd: /etc/passwd managment diff --git a/libc-0.0.4/pwd/Makefile b/libc-0.0.4/pwd/Makefile new file mode 100644 index 0000000..df90f89 --- /dev/null +++ b/libc-0.0.4/pwd/Makefile @@ -0,0 +1,40 @@ +# Copyright (C) 1996 Nat Friedman <ndf@aleph1.mit.edu> +# This file is part of the Linux-8086 C library and is distributed +# under the GNU Library General Public License. + +TOP=.. +include $(TOP)/Make.defs + +ifeq ($(PLATFORM),i386-Linux) +CFLAGS+=$(WALL) +else # ifeq ($(PLATFORM),i86-ELKS) +CFLAGS=$(CCFLAGS) $(LIBDEFS) -ansi +endif + +PSRC=__getpwent.c pwent.c getpwnam.c getpwuid.c putpwent.c getpw.c fgetpwent.c +POBJ=__getpwent.o pwent.o getpwnam.o getpwuid.o putpwent.o getpw.o fgetpwent.o + +all: $(POBJ) + +%.o: %.c + $(CC) $(CFLAGS) -o $@ $< -c + +$(LIBC): $(POBJ) + ar r ../$(LIBC) $(POBJ) + @touch libc.a + +test: test_pwd.c libpwd.a + $(CC) $(CFLAGS) test_pwd.c -o test_pwd -L. -lpwd + +libpwd.a: $(POBJ) + ar r libpwd.a $(POBJ) + ranlib libpwd.a + +libpwd: libpwd.a + +clean: + rm -f $(POBJ) libc.a libpwd.a + + + + diff --git a/libc-0.0.4/pwd/__getpwent.c b/libc-0.0.4/pwd/__getpwent.c new file mode 100644 index 0000000..1dfc87e --- /dev/null +++ b/libc-0.0.4/pwd/__getpwent.c @@ -0,0 +1,152 @@ +/* + * __getpwent.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <pwd.h> + +#define __PWD_LINE_BUFFER_SIZE 256 + +struct passwd * +__getpwent(int passwd_fd) +{ + static char pwd_line[__PWD_LINE_BUFFER_SIZE]; + unsigned short curr_index; + + /* field_num is the current field number, 0 through 6 */ + unsigned short field_num; + + /* field_length is the length of the current field */ + unsigned short field_length; + + /* field_begin is a pointer to the first character of the current field */ + char * field_begin; + + /* endptr is used to error-check the call to strtol() */ + char * endptr; + + /* the only static passwd structure. we return a pointer to it */ + static struct passwd passwd; + + /* Gross, but small... */ + goto readline_start; + +readline_restart: + /* Advance to the end of the line */ + while (pwd_line[curr_index]!='\n' && + read(passwd_fd, &pwd_line[curr_index], 1)==1); + +readline_start: + /* Initialize ourselves at the beginning of the line */ + curr_index=field_num=field_length=0; + field_begin=pwd_line; + while (read(passwd_fd, &pwd_line[curr_index], 1)==1) + { + if (curr_index==0) + if (*pwd_line=='#' || *pwd_line==' ' || *pwd_line=='\n' || + *pwd_line=='\t') + goto readline_restart; + + field_length++; + if (pwd_line[curr_index]==':' || pwd_line[curr_index]=='\n') + { + pwd_line[curr_index]='\0'; + switch (field_num) + { + case 0: /* login */ + passwd.pw_name=field_begin; + break; + case 1: /* passwd */ + passwd.pw_passwd=field_begin; + break; + case 2: /* GID */ + case 3: /* UID */ + if (field_num==2) + passwd.pw_uid=strtol(field_begin, &endptr, 10); + else + passwd.pw_gid=strtol(field_begin, &endptr, 10); + if (endptr!=(pwd_line+curr_index)) /* there were invalid + characters */ + goto readline_restart; + + curr_index-=field_length; + break; + case 4: /* GECOS field */ + passwd.pw_gecos=field_begin; + break; + case 5: /* home directory */ + passwd.pw_dir=field_begin; + break; + case 6: /* shell */ + passwd.pw_shell=field_begin; + return &passwd; + break; + default: /* too many fields -- invalid line */ + goto readline_restart; + } + if (field_num!=2 && field_num!=3) + field_begin=pwd_line+curr_index+1; + field_num++; + field_length=0; + } + curr_index++; + if (curr_index>=__PWD_LINE_BUFFER_SIZE) + { + if (field_num<=3) + goto readline_start; + + if (*passwd.pw_gecos) + { + if (field_num==4) /* gecos.. least important */ + { + /* erase this field */ + curr_index-=field_length; + field_num++; + passwd.pw_gecos=field_begin-1; + /* read until the next delimiter */ + while ((read(passwd_fd, &pwd_line[curr_index], 1)==1) && + pwd_line[curr_index]!=':'); + + } + else if (field_num>4) /* move everything back to the gecos... */ + { + unsigned short shift; + if (field_num==5) + passwd.pw_dir=field_begin; + + shift=passwd.pw_dir-passwd.pw_gecos; + memcpy(passwd.pw_gecos, + passwd.pw_dir, pwd_line+curr_index-passwd.pw_dir); + passwd.pw_gecos--; + passwd.pw_dir=passwd.pw_gecos; + field_begin-=shift; + passwd.pw_shell-=shift; + curr_index-=shift; + } + } + else + goto readline_restart; + } + } + return NULL; +} + diff --git a/libc-0.0.4/pwd/fgetpwent.c b/libc-0.0.4/pwd/fgetpwent.c new file mode 100644 index 0000000..e12b890 --- /dev/null +++ b/libc-0.0.4/pwd/fgetpwent.c @@ -0,0 +1,35 @@ +/* + * fgetpwent.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <errno.h> +#include <stdio.h> +#include <pwd.h> + +struct passwd * +fgetpwent(FILE * file) +{ + if (file==NULL) + { + errno=EINTR; + return NULL; + } + + return __getpwent(fileno(file)); +} diff --git a/libc-0.0.4/pwd/getpw.c b/libc-0.0.4/pwd/getpw.c new file mode 100644 index 0000000..4f4e390 --- /dev/null +++ b/libc-0.0.4/pwd/getpw.c @@ -0,0 +1,51 @@ +/* + * getpw.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <sys/types.h> +#include <errno.h> +#include <stdio.h> +#include <pwd.h> + +int +getpw(uid_t uid, char *buf) +{ + struct passwd * passwd; + + if (buf==NULL) + { + errno=EINVAL; + return -1; + } + if ((passwd=getpwuid(uid))==NULL) + return -1; + + if (sprintf(buf, "%s:%s:%u:%u:%s:%s:%s", passwd->pw_name, passwd->pw_passwd, + passwd->pw_gid, passwd->pw_uid, passwd->pw_gecos, + passwd->pw_dir, passwd->pw_shell)<0) + { + errno=ENOBUFS; + return -1; + } + + return 0; +} + + + diff --git a/libc-0.0.4/pwd/getpwnam.c b/libc-0.0.4/pwd/getpwnam.c new file mode 100644 index 0000000..85dbc8e --- /dev/null +++ b/libc-0.0.4/pwd/getpwnam.c @@ -0,0 +1,52 @@ +/* + * getpwnam.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <pwd.h> + + +struct passwd * +getpwnam(const char * name) +{ + int passwd_fd; + struct passwd * passwd; + + if (name==NULL) + { + errno=EINVAL; + return NULL; + } + + if ((passwd_fd=open("/etc/passwd", O_RDONLY))<0) + return NULL; + + while ((passwd=__getpwent(passwd_fd))!=NULL) + if (!strcmp(passwd->pw_name, name)) + { + close(passwd_fd); + return passwd; + } + + close(passwd_fd); + return NULL; +} diff --git a/libc-0.0.4/pwd/getpwuid.c b/libc-0.0.4/pwd/getpwuid.c new file mode 100644 index 0000000..ffd58c1 --- /dev/null +++ b/libc-0.0.4/pwd/getpwuid.c @@ -0,0 +1,44 @@ +/* + * getpwuid.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <pwd.h> + +struct passwd * +getpwuid(uid_t uid) +{ + int passwd_fd; + struct passwd * passwd; + + if ((passwd_fd=open("/etc/passwd", O_RDONLY))<0) + return NULL; + + while ((passwd=__getpwent(passwd_fd))!=NULL) + if (passwd->pw_uid==uid) + { + close(passwd_fd); + return passwd; + } + + close (passwd_fd); + return NULL; +} diff --git a/libc-0.0.4/pwd/putpwent.c b/libc-0.0.4/pwd/putpwent.c new file mode 100644 index 0000000..a0035ea --- /dev/null +++ b/libc-0.0.4/pwd/putpwent.c @@ -0,0 +1,39 @@ +/* + * putpwent.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <stdio.h> +#include <errno.h> +#include <pwd.h> + +int +putpwent(const struct passwd * passwd, FILE * f) +{ + if (passwd == NULL || f == NULL) + { + errno=EINVAL; + return -1; + } + if (fprintf(f, "%s:%s:%u:%u:%s:%s:%s\n", passwd->pw_name, passwd->pw_passwd, + passwd->pw_gid, passwd->pw_uid, passwd->pw_gecos, + passwd->pw_dir, passwd->pw_shell)<0) + return -1; + + return 0; +} diff --git a/libc-0.0.4/pwd/pwent.c b/libc-0.0.4/pwd/pwent.c new file mode 100644 index 0000000..dc40a08 --- /dev/null +++ b/libc-0.0.4/pwd/pwent.c @@ -0,0 +1,62 @@ +/* + * pwent.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include <pwd.h> +#include <fcntl.h> + +/* + * setpwent(), endpwent(), and getpwent() are included in the same object + * file, since one cannot be used without the other two, so it makes sense to + * link them all in together. + */ + +/* file descriptor for the password file currently open */ +static int pw_fd; + +void +setpwent(void) +{ + if (pw_fd!=-1) + close(pw_fd); + + pw_fd=open("/etc/passwd", O_RDONLY); +} + +void +endpwent(void) +{ + if (pw_fd!=-1) + close(pw_fd); + pw_fd=-1; +} + +struct passwd * +getpwent(void) +{ + if (pw_fd!=-1) + return __getpwent(pw_fd); + return NULL; +} + + + diff --git a/libc-0.0.4/pwd/test_pwd b/libc-0.0.4/pwd/test_pwd Binary files differnew file mode 100755 index 0000000..bac2f1e --- /dev/null +++ b/libc-0.0.4/pwd/test_pwd diff --git a/libc-0.0.4/pwd/test_pwd.c b/libc-0.0.4/pwd/test_pwd.c new file mode 100644 index 0000000..3279fb2 --- /dev/null +++ b/libc-0.0.4/pwd/test_pwd.c @@ -0,0 +1,91 @@ +/* + * test_pwd.c - This file is part of the libc-8086/pwd package for ELKS, + * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <unistd.h> +#include <stdio.h> +#include <fcntl.h> +#include <pwd.h> + +int +main(int argc, char ** argv) +{ + struct passwd * passwd; + int test_uid; + + fprintf(stderr, "Beginning test of libc/pwd...\n"); + + fprintf(stderr, "=> Testing setpwent(), getpwent(), endpwent()...\n"); + fprintf(stderr, "-> setpwent()...\n"); + setpwent(); + fprintf(stderr, "-> getpwent()...\n"); + printf("********************************************************************************\n"); + while ((passwd=getpwent())!=NULL) + { + printf("pw_name\t\t: %s\n", passwd->pw_name); + printf("pw_passwd\t: %s\n", passwd->pw_passwd); + printf("pw_uid\t\t: %d\n", (int) passwd->pw_uid); + printf("pw_gid\t\t: %d\n", (int) passwd->pw_gid); + printf("pw_gecos\t: %s\n", passwd->pw_gecos); + printf("pw_dir\t\t: %s\n", passwd->pw_dir); + printf("pw_shell\t: %s\n", passwd->pw_shell); + printf("********************************************************************************\n"); + } + fprintf(stderr, "-> endpwent()...\n"); + endpwent(); + fprintf(stderr, "=> Test of setpwent(), getpwent(), endpwent() complete.\n"); + fprintf(stderr, "=> Testing getpwuid(), getpwnam()...\n"); + fprintf(stderr, "-> getpwuid()...\n"); + printf("********************************************************************************\n"); + for(test_uid=0;test_uid<100;test_uid++) + { + fprintf(stderr, "-> getpwuid(%d)...\n", test_uid); + passwd=getpwuid((uid_t) test_uid); + if (passwd!=NULL) + { + printf("pw_name\t\t: %s\n", passwd->pw_name); + printf("pw_passwd\t: %s\n", passwd->pw_passwd); + printf("pw_uid\t\t: %d\n", (int) passwd->pw_uid); + printf("pw_gid\t\t: %d\n", (int) passwd->pw_gid); + printf("pw_gecos\t: %s\n", passwd->pw_gecos); + printf("pw_dir\t\t: %s\n", passwd->pw_dir); + printf("pw_shell\t: %s\n", passwd->pw_shell); + } + printf("********************************************************************************\n"); + } + fprintf(stderr, "-> getpwnam()...\n"); + passwd=getpwnam("root"); + if (passwd==NULL) + { + printf(">NULL<\n"); + } + else + { + printf("pw_name\t\t: %s\n", passwd->pw_name); + printf("pw_passwd\t: %s\n", passwd->pw_passwd); + printf("pw_uid\t\t: %d\n", (int) passwd->pw_uid); + printf("pw_gid\t\t: %d\n", (int) passwd->pw_gid); + printf("pw_gecos\t: %s\n", passwd->pw_gecos); + printf("pw_dir\t\t: %s\n", passwd->pw_dir); + printf("pw_shell\t: %s\n", passwd->pw_shell); + } + return 0; +} + + |