use asprintf in place of malloc/sprintf as suggested by solar

This commit is contained in:
Mike Frysinger 2005-04-25 05:24:35 +00:00
parent 7eb6457c4c
commit c238a97a9e
2 changed files with 13 additions and 13 deletions

View File

@ -167,11 +167,9 @@ static int chattr_dir_proc(const char *dir_name, struct dirent *de,
/*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/ /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/
if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \ if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \
(de->d_name[1] == '.' && de->d_name[2] == '\0'))) { (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
char *path;
char *path = malloc(strlen(dir_name) + 1 + strlen(de->d_name) + 1); if (asprintf(&path, "%s/%s", dir_name, de->d_name) == -1)
if (!path) bb_error_msg_and_die("asprintf failed");
bb_error_msg_and_die("Couldn't allocate path variable in chattr_dir_proc");
sprintf(path, "%s/%s", dir_name, de->d_name);
change_attributes(path); change_attributes(path);
free(path); free(path);
} }

View File

@ -101,29 +101,31 @@ static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
{ {
STRUCT_STAT st; STRUCT_STAT st;
char *path; char *path;
int dir_len = strlen(dir_name); int i = strlen(dir_name);
path = malloc(dir_len + strlen(de->d_name) + 2); if (i && dir_name[i-1] == '/')
i = asprintf(&path, "%s%s", dir_name, de->d_name);
if (dir_len && dir_name[dir_len-1] == '/')
sprintf(path, "%s%s", dir_name, de->d_name);
else else
sprintf(path, "%s/%s", dir_name, de->d_name); i = asprintf(&path, "%s/%s", dir_name, de->d_name);
if (i == -1)
bb_perror_msg_and_die("asprintf failed");
if (LSTAT(path, &st) == -1) if (LSTAT(path, &st) == -1)
bb_perror_msg(path); bb_perror_msg(path);
else { else {
if (de->d_name[0] != '.' || (flags & OPT_ALL)) { if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
list_attributes(path); list_attributes(path);
if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) && if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
strcmp(de->d_name, ".") && strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
strcmp(de->d_name, "..")) {
printf("\n%s:\n", path); printf("\n%s:\n", path);
iterate_on_dir(path, lsattr_dir_proc, NULL); iterate_on_dir(path, lsattr_dir_proc, NULL);
printf("\n"); printf("\n");
} }
} }
} }
free(path); free(path);
return 0; return 0;
} }