1
1

* screen.c (string_file_*): Don't use static buffers.

* screen.c (format_file): Provide a buffer for the string_file_*
	functions.
Этот коммит содержится в:
Roland Illig 2005-02-08 12:28:17 +00:00
родитель f629c91ded
Коммит c6a72952ac
2 изменённых файлов: 103 добавлений и 114 удалений

Просмотреть файл

@ -1,3 +1,9 @@
2005-02-08 Roland Illig <roland.illig@gmx.de>
* screen.c (string_file_*): Don't use static buffers.
* screen.c (format_file): Provide a buffer for the string_file_*
functions.
2005-02-08 Roland Illig <roland.illig@gmx.de> 2005-02-08 Roland Illig <roland.illig@gmx.de>
* widget.h: Renamed define_label to buttonbar_set_label. Renamed * widget.h: Renamed define_label to buttonbar_set_label. Renamed

Просмотреть файл

@ -70,9 +70,14 @@
#define MARKED_SELECTED 3 #define MARKED_SELECTED 3
#define STATUS 5 #define STATUS 5
typedef void (*info_fn) (file_entry *, char * buf, size_t bufsize);
/* /*
* This describes a format item. The parse_display_format routine parses * This describes a format item. The parse_display_format routine parses
* the user specified format and creates a linked list of format_e structures. * the user specified format and creates a linked list of format_e structures.
*
* The string_fn functions may assume that the size of the passed buffer is
* at least 1, to allow storing the trailing '\0'.
*/ */
typedef struct format_e { typedef struct format_e {
struct format_e *next; struct format_e *next;
@ -80,7 +85,7 @@ typedef struct format_e {
int field_len; int field_len;
int just_mode; int just_mode;
int expand; int expand;
const char *(*string_fn)(file_entry *, int len); info_fn string_fn;
const char *title; const char *title;
const char *id; const char *id;
} format_e; } format_e;
@ -167,29 +172,23 @@ add_permission_string (char *dest, int width, file_entry *fe, int attr, int colo
} }
/* String representations of various file attributes */ /* String representations of various file attributes */
/* name */ /* name */
static const char * static void
string_file_name (file_entry *fe, int len) string_file_name (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer [BUF_SMALL];
size_t i; size_t i;
char c;
for (i = 0; i < sizeof(buffer) - 1; i++) { for (i = 0; i < bufsize - 1; i++) {
char c;
c = fe->fname[i]; c = fe->fname[i];
if (!c) if (!c)
break; break;
if (!is_printable(c)) if (!is_printable(c))
c = '?'; c = '?';
buffer[i] = c; buffer[i] = c;
} }
buffer[i] = '\0';
buffer[i] = 0;
return buffer;
} }
static inline int ilog10(dev_t n) static inline int ilog10(dev_t n)
@ -205,8 +204,8 @@ static void format_device_number (char *buf, size_t bufsize, dev_t dev)
{ {
dev_t major_dev = major(dev); dev_t major_dev = major(dev);
dev_t minor_dev = minor(dev); dev_t minor_dev = minor(dev);
int major_digits = ilog10(major_dev); size_t major_digits = ilog10(major_dev);
int minor_digits = ilog10(minor_dev); size_t minor_digits = ilog10(minor_dev);
g_assert(bufsize >= 1); g_assert(bufsize >= 1);
if (major_digits + 1 + minor_digits + 1 <= bufsize) { if (major_digits + 1 + minor_digits + 1 <= bufsize) {
@ -218,48 +217,44 @@ static void format_device_number (char *buf, size_t bufsize, dev_t dev)
} }
/* size */ /* size */
static const char * static void
string_file_size (file_entry *fe, int len) string_file_size (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer [BUF_TINY];
/* Don't ever show size of ".." since we don't calculate it */ /* Don't ever show size of ".." since we don't calculate it */
if (!strcmp (fe->fname, "..")) { if (!strcmp (fe->fname, "..")) {
return _("UP--DIR"); g_strlcpy (buffer, _("UP--DIR"), bufsize);
} } else
#ifdef HAVE_STRUCT_STAT_ST_RDEV #ifdef HAVE_STRUCT_STAT_ST_RDEV
if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode)) if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
format_device_number (buffer, len + 1, fe->st.st_rdev); format_device_number (buffer, bufsize, fe->st.st_rdev);
else else
#endif #endif
{ {
size_trunc_len (buffer, len, fe->st.st_size, 0); size_trunc_len (buffer, bufsize - 1, fe->st.st_size, 0);
} }
return buffer;
} }
/* bsize */ /* bsize */
static const char * static void
string_file_size_brief (file_entry *fe, int len) string_file_size_brief (file_entry *fe, char *buffer, size_t bufsize)
{ {
if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) { if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir) {
return _("SYMLINK"); g_strlcpy (buffer, _("SYMLINK"), bufsize);
} else if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..") != 0) {
g_strlcpy (buffer, _("SUB-DIR"), bufsize);
} else {
string_file_size (fe, buffer, bufsize);
} }
if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, "..")) {
return _("SUB-DIR");
}
return string_file_size (fe, len);
} }
/* This functions return a string representation of a file entry */
/* type */ /* type */
static const char * static void
string_file_type (file_entry *fe, int len) string_file_type (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer[2]; if (bufsize == 1) {
buffer[0] = '\0';
return;
}
if (S_ISDIR (fe->st.st_mode)) if (S_ISDIR (fe->st.st_mode))
buffer[0] = PATH_SEP; buffer[0] = PATH_SEP;
@ -289,132 +284,119 @@ string_file_type (file_entry *fe, int len)
else else
buffer[0] = ' '; buffer[0] = ' ';
buffer[1] = '\0'; buffer[1] = '\0';
return buffer;
} }
/* mtime */ /* mtime */
static const char * static void
string_file_mtime (file_entry *fe, int len) string_file_mtime (file_entry *fe, char *buffer, size_t bufsize)
{ {
if (!strcmp (fe->fname, "..")) { if (str_cmp (fe->fname, ==, ".."))
return ""; g_strlcpy (buffer, "", bufsize);
} else
return file_date (fe->st.st_mtime); g_strlcpy (buffer, file_date (fe->st.st_mtime), bufsize);
} }
/* atime */ /* atime */
static const char * static void
string_file_atime (file_entry *fe, int len) string_file_atime (file_entry *fe, char *buffer, size_t bufsize)
{ {
if (!strcmp (fe->fname, "..")) { if (str_cmp (fe->fname, ==, ".."))
return ""; g_strlcpy (buffer, "", bufsize);
} else
return file_date (fe->st.st_atime); g_strlcpy (buffer, file_date (fe->st.st_atime), bufsize);
} }
/* ctime */ /* ctime */
static const char * static void
string_file_ctime (file_entry *fe, int len) string_file_ctime (file_entry *fe, char *buffer, size_t bufsize)
{ {
if (!strcmp (fe->fname, "..")) { if (str_cmp (fe->fname, ==, ".."))
return ""; g_strlcpy (buffer, "", bufsize);
} else
return file_date (fe->st.st_ctime); g_strlcpy (buffer, file_date (fe->st.st_ctime), bufsize);
} }
/* perm */ /* perm */
static const char * static void
string_file_permission (file_entry *fe, int len) string_file_permission (file_entry *fe, char *buffer, size_t bufsize)
{ {
return string_perm (fe->st.st_mode); g_strlcpy (buffer, string_perm (fe->st.st_mode), bufsize);
} }
/* mode */ /* mode */
static const char * static void
string_file_perm_octal (file_entry *fe, int len) string_file_perm_octal (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer [10]; char buf[8];
const int digits = bufsize - 1;
g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode); g_snprintf (buf, sizeof(buf), "0%06lo", (unsigned long) fe->st.st_mode);
return buffer; g_strlcpy (buffer, buf + ((digits < 7) ? (7 - digits) : 0), bufsize);
} }
/* nlink */ /* nlink */
static const char * static void
string_file_nlinks (file_entry *fe, int len) string_file_nlinks (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer[BUF_TINY]; g_snprintf (buffer, bufsize, "%16d", (int) fe->st.st_nlink);
g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
return buffer;
} }
/* inode */ /* inode */
static const char * static void
string_inode (file_entry *fe, int len) string_inode (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer [10]; g_snprintf (buffer, bufsize, "%lu", (unsigned long) fe->st.st_ino);
g_snprintf (buffer, sizeof (buffer), "%lu",
(unsigned long) fe->st.st_ino);
return buffer;
} }
/* nuid */ /* nuid */
static const char * static void
string_file_nuid (file_entry *fe, int len) string_file_nuid (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer [10]; g_snprintf (buffer, bufsize, "%lu", (unsigned long) fe->st.st_uid);
g_snprintf (buffer, sizeof (buffer), "%lu",
(unsigned long) fe->st.st_uid);
return buffer;
} }
/* ngid */ /* ngid */
static const char * static void
string_file_ngid (file_entry *fe, int len) string_file_ngid (file_entry *fe, char *buffer, size_t bufsize)
{ {
static char buffer [10]; g_snprintf (buffer, bufsize, "%lu", (unsigned long) fe->st.st_gid);
g_snprintf (buffer, sizeof (buffer), "%lu",
(unsigned long) fe->st.st_gid);
return buffer;
} }
/* owner */ /* owner */
static const char * static void
string_file_owner (file_entry *fe, int len) string_file_owner (file_entry *fe, char *buffer, size_t bufsize)
{ {
return get_owner (fe->st.st_uid); g_strlcpy (buffer, get_owner (fe->st.st_uid), bufsize);
} }
/* group */ /* group */
static const char * static void
string_file_group (file_entry *fe, int len) string_file_group (file_entry *fe, char *buffer, size_t bufsize)
{ {
return get_group (fe->st.st_gid); g_strlcpy (buffer, get_group (fe->st.st_gid), bufsize);
} }
/* mark */ /* mark */
static const char * static void
string_marked (file_entry *fe, int len) string_marked (file_entry *fe, char *buffer, size_t bufsize)
{ {
return fe->f.marked ? "*" : " "; g_strlcpy (buffer, fe->f.marked ? "*" : " ", bufsize);
} }
/* space */ /* space */
static const char * static void
string_space (file_entry *fe, int len) string_space (file_entry *fe, char *buffer, size_t bufsize)
{ {
return " "; (void) fe;
g_strlcpy (buffer, " ", bufsize);
} }
/* dot */ /* dot */
static const char * static void
string_dot (file_entry *fe, int len) string_dot (file_entry *fe, char *buffer, size_t bufsize)
{ {
return "."; (void) fe;
g_strlcpy (buffer, ".", bufsize);
} }
#define GT 1 #define GT 1
@ -426,7 +408,7 @@ static struct {
int default_just; int default_just;
const char *title; const char *title;
int use_in_gui; int use_in_gui;
const char *(*string_fn)(file_entry *, int); info_fn string_fn;
sortfn *sort_routine; sortfn *sort_routine;
} formats [] = { } formats [] = {
{ "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name }, { "name", 12, 1, J_LEFT_FIT, N_("Name"), 1, string_file_name, (sortfn *) sort_name },
@ -539,8 +521,9 @@ file_compute_color (int attr, file_entry *fe)
/* Formats the file number file_index of panel in the buffer dest */ /* Formats the file number file_index of panel in the buffer dest */
static void static void
format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus) format_file (char *dest, size_t destsize, WPanel *panel, int file_index, int width, int attr, int isstatus)
{ {
char buffer[BUF_1K];
int color, length, empty_line; int color, length, empty_line;
const char *txt; const char *txt;
char *old_pos; char *old_pos;
@ -567,20 +550,20 @@ format_file (char *dest, int limit, WPanel *panel, int file_index, int width, in
int len; int len;
if (empty_line) if (empty_line)
txt = " "; strcpy (buffer, " ");
else else
txt = (*format->string_fn)(fe, format->field_len); (*format->string_fn) (fe, buffer, min(format->field_len, sizeof(buffer) - 1) + 1);
old_pos = cdest; old_pos = cdest;
len = format->field_len; len = format->field_len;
if (len + length > width) if (len + length > width)
len = width - length; len = width - length;
if (len + (cdest - dest) > limit) if (len + (cdest - dest) > destsize)
len = limit - (cdest - dest); len = destsize - (cdest - dest);
if (len <= 0) if (len <= 0)
break; break;
cdest = to_buffer (cdest, format->just_mode, len, txt); cdest = to_buffer (cdest, format->just_mode, len, buffer);
length += len; length += len;
attrset (color); attrset (color);