real_dir_from_tilde() - Rewritten using getpwent (suggected by Adam)
git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@498 35c25a1d-7b9e-4130-9fde-d3aeb78583b8master
parent
60cd6aa04f
commit
ee733e6d4c
|
@ -12,6 +12,8 @@ General
|
||||||
discovered by David Sobon).
|
discovered by David Sobon).
|
||||||
username_tab_completion()
|
username_tab_completion()
|
||||||
- Optimization and removal of useless vars (Rocco).
|
- Optimization and removal of useless vars (Rocco).
|
||||||
|
real_dir_from_tilde()
|
||||||
|
- Rewritten using getpwent (suggested by Adam).
|
||||||
- global.c:
|
- global.c:
|
||||||
- Don't define toggles global or toggle_init_one if using --tiny.
|
- Don't define toggles global or toggle_init_one if using --tiny.
|
||||||
- nano.c:
|
- nano.c:
|
||||||
|
|
49
files.c
49
files.c
|
@ -29,6 +29,7 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "proto.h"
|
#include "proto.h"
|
||||||
|
@ -581,8 +582,9 @@ int do_writeout_void(void)
|
||||||
*/
|
*/
|
||||||
char *real_dir_from_tilde(char *buf)
|
char *real_dir_from_tilde(char *buf)
|
||||||
{
|
{
|
||||||
char *dirtmp = NULL, *line = NULL, byte[1], *lineptr;
|
char *dirtmp = NULL;
|
||||||
int fd, i, status, searchctr = 1;
|
int searchctr = 1;
|
||||||
|
struct passwd *userdata;
|
||||||
|
|
||||||
if (buf[0] == '~') {
|
if (buf[0] == '~') {
|
||||||
if (buf[1] == '~')
|
if (buf[1] == '~')
|
||||||
|
@ -599,51 +601,22 @@ char *real_dir_from_tilde(char *buf)
|
||||||
}
|
}
|
||||||
} else if (buf[1] != 0) {
|
} else if (buf[1] != 0) {
|
||||||
|
|
||||||
if ((fd = open("/etc/passwd", O_RDONLY)) == -1)
|
|
||||||
goto abort;
|
|
||||||
|
|
||||||
/* Figure how how much of of the str we need to compare */
|
/* Figure how how much of of the str we need to compare */
|
||||||
for (searchctr = 1; buf[searchctr] != '/' &&
|
for (searchctr = 1; buf[searchctr] != '/' &&
|
||||||
buf[searchctr] != 0; searchctr++);
|
buf[searchctr] != 0; searchctr++);
|
||||||
|
|
||||||
do {
|
for (userdata = getpwent(); userdata != NULL &&
|
||||||
i = 0;
|
strncmp(userdata->pw_name, &buf[1], searchctr - 1);
|
||||||
line = nmalloc(1);
|
userdata = getpwent());
|
||||||
while ((status = read(fd, byte, 1)) != 0
|
|
||||||
&& byte[0] != '\n') {
|
|
||||||
|
|
||||||
line[i] = byte[0];
|
if (userdata == NULL) /* No such user or getpwent() failed */
|
||||||
i++;
|
|
||||||
line = nrealloc(line, i + 1);
|
|
||||||
}
|
|
||||||
line[i] = 0;
|
|
||||||
|
|
||||||
if (i == 0)
|
|
||||||
goto abort;
|
|
||||||
|
|
||||||
line[i] = 0;
|
|
||||||
lineptr = strtok(line, ":");
|
|
||||||
|
|
||||||
if (!strncmp(lineptr, &buf[1], searchctr - 1)) {
|
|
||||||
|
|
||||||
/* Okay, skip to the password portion now */
|
|
||||||
for (i = 0; i <= 4 && lineptr != NULL; i++)
|
|
||||||
lineptr = strtok(NULL, ":");
|
|
||||||
|
|
||||||
if (lineptr == NULL)
|
|
||||||
goto abort;
|
goto abort;
|
||||||
|
|
||||||
/* Else copy the new string into the new buf */
|
/* Else copy the new string into the new buf */
|
||||||
dirtmp = nmalloc(strlen(buf) + 2 + strlen(lineptr));
|
dirtmp = nmalloc(strlen(buf) + 2 + strlen(userdata->pw_dir));
|
||||||
|
|
||||||
sprintf(dirtmp, "%s%s", lineptr, &buf[searchctr]);
|
sprintf(dirtmp, "%s%s", userdata->pw_dir, &buf[searchctr]);
|
||||||
free(line);
|
endpwent();
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(line);
|
|
||||||
|
|
||||||
} while (status != 0);
|
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
dirtmp = mallocstrcpy(dirtmp, buf);
|
dirtmp = mallocstrcpy(dirtmp, buf);
|
||||||
|
|
Loading…
Reference in New Issue