Fix from Matt Kraai -- a better way to NULL terminate strings for the
authorEric Andersen <andersen@codepoet.org>
Wed, 13 Dec 2000 01:52:39 +0000 (01:52 -0000)
committerEric Andersen <andersen@codepoet.org>
Wed, 13 Dec 2000 01:52:39 +0000 (01:52 -0000)
my_* passwd and group routines.  I should have thought of doing it
this way...

13 files changed:
archival/tar.c
coreutils/id.c
coreutils/logname.c
coreutils/ls.c
coreutils/whoami.c
id.c
logname.c
ls.c
procps/ps.c
ps.c
tar.c
utility.c
whoami.c

index 8c2650e2b65a43b2605dade856b70f4ca791de4d..3a3b7f14394abaa193ebbc40260af5952d2732d2 100644 (file)
@@ -691,13 +691,11 @@ extern int readTarFile(int tarFd, int extractFlag, int listFlag,
                                struct tm *tm = localtime (&(header.mtime));
 
                                len=printf("%s ", mode_string(header.mode));
-                               memset(buf, 0, 8*sizeof(char));
                                my_getpwuid(buf, header.uid);
                                if (! *buf)
                                        len+=printf("%d", header.uid);
                                else
                                        len+=printf("%s", buf);
-                               memset(buf, 0, 8*sizeof(char));
                                my_getgrgid(buf, header.gid);
                                if (! *buf)
                                        len+=printf("/%-d ", header.gid);
index 86667f516d463862a87bdc015c0141401caebd09..3a8e77a1f0be0cfbdd9f5e746ad07e68812fa54f 100644 (file)
 extern int id_main(int argc, char **argv)
 {
        int no_user = 0, no_group = 0, print_real = 0;
-       char *cp, *user, *group;
+       char user[9], group[9];
        long gid;
        long pwnam, grnam;
        int opt;
        
-       cp = user = group = NULL;
        gid = 0;
 
        while ((opt = getopt(argc, argv, "ugr")) > 0) {
@@ -57,11 +56,7 @@ extern int id_main(int argc, char **argv)
 
        if (no_user && no_group) usage(id_usage);
 
-       user = argv[optind];
-
-       if (user == NULL) {
-               user = xcalloc(9, sizeof(char));
-               group = xcalloc(9, sizeof(char));
+       if (argv[optind] == NULL) {
                if (print_real) {
                        my_getpwuid(user, getuid());
                        my_getgrgid(group, getgid());
@@ -70,7 +65,8 @@ extern int id_main(int argc, char **argv)
                        my_getgrgid(group, getegid());
                }
        } else {
-               group = xcalloc(9, sizeof(char));
+               strncpy(user, argv[optind], 8);
+               user[8] = '\0';
            gid = my_getpwnamegid(user);
                my_getgrgid(group, gid);
        }
index 1fc518bfce36135cfff7201a3dedc766426aa270..d614e85f14d3cff7515614487af8e1ff57d67857 100644 (file)
 
 extern int logname_main(int argc, char **argv)
 {
-       char *user = xmalloc(9);
+       char user[9];
 
        if (argc > 1)
                usage(logname_usage);
 
        my_getpwuid(user, geteuid());
-       if (user) {
+       if (*user) {
                puts(user);
                return EXIT_SUCCESS;
        }
index 94c73b377433ab8c1a8984069f7f75028f09b1dd..655dd7ff400dcebb19f2d7a26a6e686489ddda47 100644 (file)
@@ -600,13 +600,11 @@ int list_single(struct dnode *dn)
                                break;
                        case LIST_ID_NAME:
 #ifdef BB_FEATURE_LS_USERNAME
-                               memset(scratch, 0, sizeof(scratch));
                                my_getpwuid(scratch, dn->dstat.st_uid);
                                if (*scratch)
                                        fprintf(stdout, "%-8.8s ", scratch);
                                else
                                        fprintf(stdout, "%-8d ", dn->dstat.st_uid);
-                               memset(scratch, 0, sizeof(scratch));
                                my_getgrgid(scratch, dn->dstat.st_gid);
                                if (*scratch)
                                        fprintf(stdout, "%-8.8s", scratch);
index 6a5dd2c04ef9b5bfe37ce5bd252b41ee80ef1cec..38a2b30787f02c3a321306fcc2dd8b29320d9738 100644 (file)
 
 extern int whoami_main(int argc, char **argv)
 {
-       char *user = xmalloc(9);
+       char user[9];
        uid_t uid = geteuid();
 
        if (argc > 1)
                usage(whoami_usage);
 
        my_getpwuid(user, uid);
-       if (user) {
+       if (*user) {
                puts(user);
                return EXIT_SUCCESS;
        }
diff --git a/id.c b/id.c
index 86667f516d463862a87bdc015c0141401caebd09..3a8e77a1f0be0cfbdd9f5e746ad07e68812fa54f 100644 (file)
--- a/id.c
+++ b/id.c
 extern int id_main(int argc, char **argv)
 {
        int no_user = 0, no_group = 0, print_real = 0;
-       char *cp, *user, *group;
+       char user[9], group[9];
        long gid;
        long pwnam, grnam;
        int opt;
        
-       cp = user = group = NULL;
        gid = 0;
 
        while ((opt = getopt(argc, argv, "ugr")) > 0) {
@@ -57,11 +56,7 @@ extern int id_main(int argc, char **argv)
 
        if (no_user && no_group) usage(id_usage);
 
-       user = argv[optind];
-
-       if (user == NULL) {
-               user = xcalloc(9, sizeof(char));
-               group = xcalloc(9, sizeof(char));
+       if (argv[optind] == NULL) {
                if (print_real) {
                        my_getpwuid(user, getuid());
                        my_getgrgid(group, getgid());
@@ -70,7 +65,8 @@ extern int id_main(int argc, char **argv)
                        my_getgrgid(group, getegid());
                }
        } else {
-               group = xcalloc(9, sizeof(char));
+               strncpy(user, argv[optind], 8);
+               user[8] = '\0';
            gid = my_getpwnamegid(user);
                my_getgrgid(group, gid);
        }
index 1fc518bfce36135cfff7201a3dedc766426aa270..d614e85f14d3cff7515614487af8e1ff57d67857 100644 (file)
--- a/logname.c
+++ b/logname.c
 
 extern int logname_main(int argc, char **argv)
 {
-       char *user = xmalloc(9);
+       char user[9];
 
        if (argc > 1)
                usage(logname_usage);
 
        my_getpwuid(user, geteuid());
-       if (user) {
+       if (*user) {
                puts(user);
                return EXIT_SUCCESS;
        }
diff --git a/ls.c b/ls.c
index 94c73b377433ab8c1a8984069f7f75028f09b1dd..655dd7ff400dcebb19f2d7a26a6e686489ddda47 100644 (file)
--- a/ls.c
+++ b/ls.c
@@ -600,13 +600,11 @@ int list_single(struct dnode *dn)
                                break;
                        case LIST_ID_NAME:
 #ifdef BB_FEATURE_LS_USERNAME
-                               memset(scratch, 0, sizeof(scratch));
                                my_getpwuid(scratch, dn->dstat.st_uid);
                                if (*scratch)
                                        fprintf(stdout, "%-8.8s ", scratch);
                                else
                                        fprintf(stdout, "%-8d ", dn->dstat.st_uid);
-                               memset(scratch, 0, sizeof(scratch));
                                my_getgrgid(scratch, dn->dstat.st_gid);
                                if (*scratch)
                                        fprintf(stdout, "%-8.8s", scratch);
index 79910fe34f097ebbe4596465363b192cb591e1f6..357ece3835a2ed1507bb004056c9f95c8f5525aa 100644 (file)
@@ -121,8 +121,8 @@ extern int ps_main(int argc, char **argv)
        FILE *file;
        struct dirent *entry;
        char path[32], sbuf[512];
-       char uidName[10] = "";
-       char groupName[10] = "";
+       char uidName[9];
+       char groupName[9];
        int len, i, c;
 #ifdef BB_FEATURE_AUTOWIDTH
        struct winsize win = { 0, 0, 0, 0 };
@@ -146,9 +146,6 @@ extern int ps_main(int argc, char **argv)
        fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
                        "State", "Command");
        while ((entry = readdir(dir)) != NULL) {
-               uidName[0] = '\0';
-               groupName[0] = '\0';
-
                if (!isdigit(*entry->d_name))
                        continue;
                sprintf(path, "/proc/%s/status", entry->d_name);
@@ -204,8 +201,8 @@ extern int ps_main(int argc, char **argv)
        pid_t num_pids;
        pid_t* pid_array = NULL;
        struct pid_info info;
-       char uidName[10] = "";
-       char groupName[10] = "";
+       char uidName[9];
+       char groupName[9];
 #ifdef BB_FEATURE_AUTOWIDTH
        struct winsize win = { 0, 0, 0, 0 };
        int terminal_width = TERMINAL_WIDTH;
@@ -247,8 +244,6 @@ extern int ps_main(int argc, char **argv)
                        "State", "Command");
 
        for (i=1; i<pid_array[0] ; i++) {
-               uidName[0] = '\0';
-               groupName[0] = '\0';
            info.pid = pid_array[i];
 
            if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
diff --git a/ps.c b/ps.c
index 79910fe34f097ebbe4596465363b192cb591e1f6..357ece3835a2ed1507bb004056c9f95c8f5525aa 100644 (file)
--- a/ps.c
+++ b/ps.c
@@ -121,8 +121,8 @@ extern int ps_main(int argc, char **argv)
        FILE *file;
        struct dirent *entry;
        char path[32], sbuf[512];
-       char uidName[10] = "";
-       char groupName[10] = "";
+       char uidName[9];
+       char groupName[9];
        int len, i, c;
 #ifdef BB_FEATURE_AUTOWIDTH
        struct winsize win = { 0, 0, 0, 0 };
@@ -146,9 +146,6 @@ extern int ps_main(int argc, char **argv)
        fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
                        "State", "Command");
        while ((entry = readdir(dir)) != NULL) {
-               uidName[0] = '\0';
-               groupName[0] = '\0';
-
                if (!isdigit(*entry->d_name))
                        continue;
                sprintf(path, "/proc/%s/status", entry->d_name);
@@ -204,8 +201,8 @@ extern int ps_main(int argc, char **argv)
        pid_t num_pids;
        pid_t* pid_array = NULL;
        struct pid_info info;
-       char uidName[10] = "";
-       char groupName[10] = "";
+       char uidName[9];
+       char groupName[9];
 #ifdef BB_FEATURE_AUTOWIDTH
        struct winsize win = { 0, 0, 0, 0 };
        int terminal_width = TERMINAL_WIDTH;
@@ -247,8 +244,6 @@ extern int ps_main(int argc, char **argv)
                        "State", "Command");
 
        for (i=1; i<pid_array[0] ; i++) {
-               uidName[0] = '\0';
-               groupName[0] = '\0';
            info.pid = pid_array[i];
 
            if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
diff --git a/tar.c b/tar.c
index 8c2650e2b65a43b2605dade856b70f4ca791de4d..3a3b7f14394abaa193ebbc40260af5952d2732d2 100644 (file)
--- a/tar.c
+++ b/tar.c
@@ -691,13 +691,11 @@ extern int readTarFile(int tarFd, int extractFlag, int listFlag,
                                struct tm *tm = localtime (&(header.mtime));
 
                                len=printf("%s ", mode_string(header.mode));
-                               memset(buf, 0, 8*sizeof(char));
                                my_getpwuid(buf, header.uid);
                                if (! *buf)
                                        len+=printf("%d", header.uid);
                                else
                                        len+=printf("%s", buf);
-                               memset(buf, 0, 8*sizeof(char));
                                my_getgrgid(buf, header.gid);
                                if (! *buf)
                                        len+=printf("/%-d ", header.gid);
index 0e170d1c182e69321b05114ac4ab583776ff4455..61e5f7a736a0a589d9dd8d7cd95a50b04ef2ef5e 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -957,12 +957,14 @@ long my_getgrnam(char *name)
 /* gets a username given a uid */
 void my_getpwuid(char *name, long uid)
 {
+       name[0] = '\0';
        my_getid("/etc/passwd", name, uid, NULL);
 }
 
 /* gets a groupname given a gid */
 void my_getgrgid(char *group, long gid)
 {
+       group[0] = '\0';
        my_getid("/etc/group", group, gid, NULL);
 }
 
index 6a5dd2c04ef9b5bfe37ce5bd252b41ee80ef1cec..38a2b30787f02c3a321306fcc2dd8b29320d9738 100644 (file)
--- a/whoami.c
+++ b/whoami.c
 
 extern int whoami_main(int argc, char **argv)
 {
-       char *user = xmalloc(9);
+       char user[9];
        uid_t uid = geteuid();
 
        if (argc > 1)
                usage(whoami_usage);
 
        my_getpwuid(user, uid);
-       if (user) {
+       if (*user) {
                puts(user);
                return EXIT_SUCCESS;
        }