removed pathname()

moved pstrncpy() to cutils.h/cutils.c
allow NULL pointers in splitpath for extraction for dirname or basename alone
changed sizes of filename or pathname buffers to MAX_FILENAME_SIZE
This commit is contained in:
Charlie Gordon 2005-05-10 18:53:03 +00:00
commit 33c43fccbf
4 changed files with 67 additions and 87 deletions

View file

@ -93,7 +93,7 @@ void pstrcpy(char *buf, int buf_size, const char *str)
if (buf_size <= 0)
return;
for(;;) {
for (;;) {
c = *str++;
if (c == 0 || q >= buf + buf_size - 1)
break;
@ -112,6 +112,28 @@ char *pstrcat(char *buf, int buf_size, const char *s)
return buf;
}
/* copy the n first char of a string and truncate it. */
char *pstrncpy(char *buf, int buf_size, const char *s, int len)
{
char *q;
int c;
if (buf_size > 0) {
q = buf;
if (len >= buf_size)
len = buf_size - 1;
while (len > 0) {
c = *s++;
if (c == '\0')
break;
*q++ = c;
len--;
}
*q = '\0';
}
return buf;
}
#endif
/**

View file

@ -7,6 +7,7 @@ int strstart(const char *str, const char *val, const char **ptr);
int stristart(const char *str, const char *val, const char **ptr);
void pstrcpy(char *buf, int buf_size, const char *str);
char *pstrcat(char *buf, int buf_size, const char *s);
char *pstrncpy(char *buf, int buf_size, const char *s, int len);
/* simple dynamic strings wrappers. The strings are always terminated
by zero except if they are empty. */
@ -76,7 +77,7 @@ static inline void __list_del(struct list_head *prev, struct list_head *next)
#define list_for_each_safe(elem, elem1, head) \
for (elem = (void *)(head)->next, elem1 = elem->next; elem != (void *)(head); \
elem = elem1, elem1 = elem->next)
elem = elem1, elem1 = elem->next)
#define list_for_each_prev(elem, head) \
for (elem = (void *)(head)->prev; elem != (void *)(head); elem = elem->prev)

46
qe.h
View file

@ -80,14 +80,12 @@ void canonize_path(char *buf, int buf_size, const char *path);
void canonize_absolute_path(char *buf, int buf_size, const char *path1);
const char *basename(const char *filename);
const char *extension(const char *filename);
char *pathname(char *buf, int buf_size, const char *filename);
char *makepath(char *buf, int buf_size, const char *path, const char *filename);
void splitpath(char *dirname, int dirname_size,
char *filename, int filename_size, const char *pathname);
char *filename, int filename_size, const char *pathname);
int find_resource_file(char *path, int path_size, const char *pattern);
int strfind(const char *keytable, const char *str, int casefold);
char *pstrncpy(char *buf, int buf_size, const char *s, int len);
void umemmove(unsigned int *dest, unsigned int *src, int len);
void skip_spaces(const char **pp);
int ustristart(const unsigned int *str, const char *val, const unsigned int **ptr);
@ -331,13 +329,13 @@ enum QEEventType {
#define KEY_RET KEY_CTRL('m')
#define KEY_ESC KEY_CTRL('[')
#define KEY_SPC 0x0020
#define KEY_DEL 127 // kbs
#define KEY_BS KEY_CTRL('h') // kbs
#define KEY_DEL 127 // kbs
#define KEY_BS KEY_CTRL('h') // kbs
#define KEY_UP KEY_ESC1('A') // kcuu1
#define KEY_DOWN KEY_ESC1('B') // kcud1
#define KEY_RIGHT KEY_ESC1('C') // kcuf1
#define KEY_LEFT KEY_ESC1('D') // kcub1
#define KEY_UP KEY_ESC1('A') // kcuu1
#define KEY_DOWN KEY_ESC1('B') // kcud1
#define KEY_RIGHT KEY_ESC1('C') // kcuf1
#define KEY_LEFT KEY_ESC1('D') // kcub1
#define KEY_CTRL_UP KEY_ESC1('a')
#define KEY_CTRL_DOWN KEY_ESC1('b')
#define KEY_CTRL_RIGHT KEY_ESC1('c')
@ -346,13 +344,13 @@ enum QEEventType {
#define KEY_CTRL_HOME KEY_ESC1('h')
#define KEY_CTRL_PAGEUP KEY_ESC1('i')
#define KEY_CTRL_PAGEDOWN KEY_ESC1('j')
#define KEY_SHIFT_TAB KEY_ESC1('Z') // kcbt
#define KEY_HOME KEY_ESC1(1) // khome
#define KEY_INSERT KEY_ESC1(2) // kich1
#define KEY_DELETE KEY_ESC1(3) // kdch1
#define KEY_END KEY_ESC1(4) // kend
#define KEY_PAGEUP KEY_ESC1(5) // kpp
#define KEY_PAGEDOWN KEY_ESC1(6) // knp
#define KEY_SHIFT_TAB KEY_ESC1('Z') // kcbt
#define KEY_HOME KEY_ESC1(1) // khome
#define KEY_INSERT KEY_ESC1(2) // kich1
#define KEY_DELETE KEY_ESC1(3) // kdch1
#define KEY_END KEY_ESC1(4) // kend
#define KEY_PAGEUP KEY_ESC1(5) // kpp
#define KEY_PAGEDOWN KEY_ESC1(6) // knp
#define KEY_F1 KEY_ESC1(11)
#define KEY_F2 KEY_ESC1(12)
#define KEY_F3 KEY_ESC1(13)
@ -617,10 +615,10 @@ extern EditBufferDataType raw_data_type;
/* dynamic module case */
#define qe_module_init(fn) \
int __qe_module_init(void) { return fn(); }
int __qe_module_init(void) { return fn(); }
#define qe_module_exit(fn) \
void __qe_module_exit(void) { fn(); }
void __qe_module_exit(void) { fn(); }
#else /* QE_MODULE */
@ -630,21 +628,21 @@ extern EditBufferDataType raw_data_type;
#undef __attribute__
/* same method as the linux kernel... */
#define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
#define __exit_call __attribute__ ((unused,__section__ (".exitcall.exit")))
#define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
#define __exit_call __attribute__ ((unused,__section__ (".exitcall.exit")))
#define qe_module_init(fn) \
static int (*__initcall_##fn)(void) __init_call = fn
static int (*__initcall_##fn)(void) __init_call = fn
#define qe_module_exit(fn) \
static void (*__exitcall_##fn)(void) __exit_call = fn
static void (*__exitcall_##fn)(void) __exit_call = fn
#else
#define __init_call
#define __exit_call
#define qe_module_init(fn) \
int module_ ## fn (void) { return fn(); }
int module_ ## fn (void) { return fn(); }
#define qe_module_exit(fn)
@ -1214,6 +1212,6 @@ int gxml_mode_init(EditState *s,
void fill_border(EditState *s, int x, int y, int w, int h, int color);
int qe_bitmap_format_to_pix_fmt(int format);
#define QASSERT(e) do { if (!(e)) fprintf(stderr, "%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #e); } while (0)
#define QASSERT(e) do { if (!(e)) fprintf(stderr, "%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #e); } while (0)
#endif

81
util.c
View file

@ -36,9 +36,9 @@ int fnmatch(const char *pattern, const char *string, int flags)
#endif
struct FindFileState {
char path[1024];
char dirpath[1024]; /* current dir path */
char pattern[1024]; /* search pattern */
char path[MAX_FILENAME_SIZE];
char dirpath[MAX_FILENAME_SIZE]; /* current dir path */
char pattern[MAX_FILENAME_SIZE]; /* search pattern */
const char *bufptr;
DIR *dir;
};
@ -67,7 +67,7 @@ int find_file_next(FindFileState *s, char *filename, int filename_size_max)
if (s->dir == NULL)
goto redo;
for(;;) {
for (;;) {
dirent = readdir(s->dir);
if (dirent == NULL) {
redo:
@ -130,15 +130,15 @@ static void canonize_path1(char *buf, int buf_size, const char *path)
const char *p;
char *q, *q1;
int c, abs_path;
char file[1024];
char file[MAX_FILENAME_SIZE];
p = path;
abs_path = (p[0] == '/');
buf[0] = '\0';
for(;;) {
for (;;) {
/* extract file */
q = file;
for(;;) {
for (;;) {
c = *p;
if (c == '\0')
break;
@ -226,8 +226,8 @@ static int is_abs_path(const char *path)
/* canonize the path and make it absolute */
void canonize_absolute_path(char *buf, int buf_size, const char *path1)
{
char cwd[1024];
char path[1024];
char cwd[MAX_FILENAME_SIZE];
char path[MAX_FILENAME_SIZE];
char *homedir;
if (!is_abs_path(path1)) {
@ -289,27 +289,6 @@ restart:
return ext ? ext : p;
}
/* extract the pathname (and the trailing '/') */
char *pathname(char *buf, int buf_size, const char *filename)
{
const char *p;
int len;
p = strrchr(filename, '/');
if (!p) {
/* CG: this is bogus: path is not filename! */
pstrcpy(buf, buf_size, filename);
} else {
/* CG: pstrncpy(buf, buf_size, filename, p + 1 - filename) */
len = p - filename + 1;
if (len > buf_size - 1)
len = buf_size - 1;
memcpy(buf, filename, len);
buf[len] = '\0';
}
return buf;
}
char *makepath(char *buf, int buf_size, const char *path,
const char *filename)
{
@ -330,30 +309,10 @@ void splitpath(char *dirname, int dirname_size,
const char *base;
base = basename(pathname);
pstrncpy(dirname, dirname_size, pathname, base - pathname);
pstrcpy(filename, filename_size, base);
}
/* copy the n first char of a string and truncate it. */
char *pstrncpy(char *buf, int buf_size, const char *s, int len)
{
char *q;
int c;
if (buf_size > 0) {
q = buf;
if (len >= buf_size)
len = buf_size - 1;
while (len > 0) {
c = *s++;
if (c == '\0')
break;
*q++ = c;
len--;
}
*q = '\0';
}
return buf;
if (dirname)
pstrncpy(dirname, dirname_size, pathname, base - pathname);
if (filename)
pstrcpy(filename, filename_size, base);
}
/* find a word in a list using '|' as separator,
@ -461,7 +420,7 @@ int css_get_enum(const char *str, const char *enum_str)
s = enum_str;
val = 0;
len = strlen(str);
for(;;) {
for (;;) {
s1 = strchr(s, ',');
if (!s1) {
if (!strcmp(s, str))
@ -778,15 +737,15 @@ int css_get_color(int *color_ptr, const char *p)
p++;
parse_num:
len = strlen(p);
switch(len) {
switch (len) {
case 3:
for(i=0;i<3;i++) {
for (i = 0; i < 3; i++) {
v = to_hex(*p++);
rgba[i] = v | (v << 4);
}
break;
case 6:
for(i=0;i<3;i++) {
for (i = 0; i < 3; i++) {
v = to_hex(*p++) << 4;
v |= to_hex(*p++);
rgba[i] = v;
@ -803,7 +762,7 @@ int css_get_color(int *color_ptr, const char *p)
/* extension for alpha */
n = 4;
parse_rgba:
for(i=0;i<n;i++) {
for (i = 0; i < n; i++) {
/* XXX: floats ? */
skip_spaces(&p);
v = strtol(p, (char **)&p, 0);
@ -927,7 +886,7 @@ void free_strings(StringArray *cs)
{
int i;
for(i=0;i<cs->nb_items;i++)
for (i = 0; i < cs->nb_items; i++)
free(cs->items[i]);
free(cs->items);
memset(cs, 0, sizeof(StringArray));
@ -938,7 +897,7 @@ void set_color(unsigned int *buf, int len, int style)
int i;
style <<= STYLE_SHIFT;
for(i=0;i<len;i++)
for (i = 0; i < len; i++)
buf[i] |= style;
}