tools: add shared NCP22 name helper

Move the common NCP22 DOS name buffer helper into tools.c and declare it in
net.h.

Replace the local copy/uppercase implementations in CREATOR, FLAG, FLAGDIR,
NDIR and NWTESTS with tool_copy_ncp22_name(). The helper keeps the existing
behavior: uppercase conversion, path separator rejection and the 1..12 byte
DOS name length limit.

NCOPY is intentionally left untouched while its NCP copy path remains under
investigation.
This commit is contained in:
Mario Fetka
2026-05-29 09:26:28 +02:00
parent 0362cc7810
commit fc91f3f274
7 changed files with 47 additions and 129 deletions

View File

@@ -312,24 +312,6 @@ static void print_dos_time(uint16 time)
fprintf(stdout, "%02d:%02d:%02d", hour, min, sec);
}
static int copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out)
{
char tmp[260];
int len;
if (!dst || !len_out) return(-1);
if (!src) src = "";
tool_upcopy(tmp, src, sizeof(tmp));
if (strchr(tmp, '\\') || strchr(tmp, '/') || strchr(tmp, ':'))
return(-1);
len = strlen(tmp);
if (len < 1 || len > 12)
return(-1);
memcpy(dst, tmp, len);
*len_out = (uint8)len;
return(0);
}
static int split_path(char *path, char *parent, int parent_size, char *name, int name_size)
{
char tmp[260];
@@ -407,7 +389,7 @@ static int set_info(uint8 dhandle, char *name, uint32 bits,
U16_TO_16(mdate, req.updated_date);
U32_TO_BE32(mid, req.updated_id);
if (copy_ncp22_name(req.name, name, &req.namlen))
if (tool_copy_ncp22_name(req.name, name, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);

28
flag.c
View File

@@ -138,30 +138,6 @@ static int flag_ncp87_obtain_attrs(char *name, uint32 *attrs)
}
static int flag_copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out)
{
char tmp[260];
int len;
if (!dst || !len_out)
return(-1);
if (!src)
src = "";
tool_upcopy(tmp, src, sizeof(tmp));
if (strchr(tmp, '\\') || strchr(tmp, '/') || strchr(tmp, ':'))
return(-1);
len = strlen(tmp);
if (len < 1 || len > 12)
return(-1);
memcpy(dst, tmp, len);
*len_out = (uint8)len;
return(0);
}
static int flag_ncp22_1e_obtain_attrs(char *name, uint32 *attrs)
{
struct {
@@ -187,7 +163,7 @@ static int flag_ncp22_1e_obtain_attrs(char *name, uint32 *attrs)
memset(&req, 0, sizeof(req));
memset(&repl, 0, sizeof(repl));
if (flag_copy_ncp22_name(req.name, name, &namlen))
if (tool_copy_ncp22_name(req.name, name, &namlen))
return(-1);
req.func = 0x1e; /* Scan directory */
@@ -252,7 +228,7 @@ static int flag_ncp22_25_modify_attrs(char *name, uint32 attrs)
U32_TO_32(FLAG_DM_ATTRIBUTES, req.change_bits);
U32_TO_32(attrs, req.attributes);
if (flag_copy_ncp22_name(req.name, name, &req.namlen))
if (tool_copy_ncp22_name(req.name, name, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);

View File

@@ -56,29 +56,6 @@
static int fd_copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out)
{
char tmp[260];
int len;
if (!dst || !len_out)
return(-1);
if (!src)
src = "";
tool_upcopy(tmp, src, sizeof(tmp));
if (strchr(tmp, '\\') || strchr(tmp, '/') || strchr(tmp, ':'))
return(-1);
len = strlen(tmp);
if (len < 1 || len > 12)
return(-1);
memcpy(dst, tmp, len);
*len_out = (uint8)len;
return(0);
}
static int fd_ncp22_1e_obtain_attrs(char *name, uint32 *attrs)
{
struct {
@@ -104,7 +81,7 @@ static int fd_ncp22_1e_obtain_attrs(char *name, uint32 *attrs)
memset(&req, 0, sizeof(req));
memset(&repl, 0, sizeof(repl));
if (fd_copy_ncp22_name(req.name, name, &namlen))
if (tool_copy_ncp22_name(req.name, name, &namlen))
return(-1);
req.func = 0x1e; /* Scan directory */
@@ -163,7 +140,7 @@ static int fd_ncp22_25_modify_attrs(char *name, uint32 attrs)
U32_TO_32(FD_DM_ATTRIBUTES, req.change_bits);
tool_put_dword_lh(req.attributes, attrs);
if (fd_copy_ncp22_name(req.name, name, &req.namlen))
if (tool_copy_ncp22_name(req.name, name, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);

25
ndir.c
View File

@@ -503,29 +503,6 @@ static void ndir_old_rights_string(uint8 old_rights, char *out)
static int ndir_copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out)
{
char tmp[260];
int len;
if (!dst || !len_out)
return(-1);
if (!src)
src = "";
tool_upcopy(tmp, src, sizeof(tmp));
if (strchr(tmp, '\\') || strchr(tmp, '/') || strchr(tmp, ':'))
return(-1);
len = strlen(tmp);
if (len < 1 || len > 12)
return(-1);
memcpy(dst, tmp, len);
*len_out = (uint8)len;
return(0);
}
static int ndir_ncp22_scan_entry(char *name, int want_dir, uint32 *attrs)
{
struct {
@@ -551,7 +528,7 @@ static int ndir_ncp22_scan_entry(char *name, int want_dir, uint32 *attrs)
memset(&req, 0, sizeof(req));
memset(&repl, 0, sizeof(repl));
if (ndir_copy_ncp22_name(req.name, name, &namlen))
if (tool_copy_ncp22_name(req.name, name, &namlen))
return(-1);
req.func = 0x1e; /* NCP22/30 Scan Directory */

1
net.h
View File

@@ -175,6 +175,7 @@ extern void tool_put_word_hl(uint8 *p, uint16 v);
extern void tool_put_dword_hl(uint8 *p, uint32 v);
extern uint16 tool_get_word_hl(uint8 *p);
extern uint32 tool_get_dword_hl(uint8 *p);
extern int tool_copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out);
extern int tool_strsame(char *a, char *b);
extern int tool_is_help_arg(char *s);
extern int tool_is_option(char *s);

View File

@@ -382,32 +382,6 @@ static int tests_parse_dword_arg(char *s, uint32 *value_out)
}
static int tests_copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out)
{
char tmp[260];
char *p;
int len;
if (!dst || !len_out)
return(-1);
if (!src)
src = "";
tool_upcopy(tmp, src, sizeof(tmp));
if (strchr(tmp, '\\') || strchr(tmp, '/') || strchr(tmp, ':'))
return(-1);
src = tmp;
len = strlen(src);
if (len < 1 || len > 12)
return(-1);
memcpy(dst, src, len);
*len_out = (uint8)len;
return(0);
}
static int tests_read_attr_by_ncp87(uint8 dhandle, char *path, uint8 *attr_out)
{
uint32 attr = 0;
@@ -457,7 +431,7 @@ static int tests_ncp22_25_set_attr(uint8 dhandle, char *path, uint8 attr)
U32_TO_BE32(0xffffffffUL, req.searchsequence);
U32_TO_32(TEST_DM_ATTRIBUTES, req.change_bits);
U32_TO_32((uint32)attr, req.attributes);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -609,7 +583,7 @@ static int tests_ncp22_25_set_mtime(uint8 dhandle, char *path, uint16 dos_date,
U32_TO_32(TEST_DM_MODIFY_DATE | TEST_DM_MODIFY_TIME, req.change_bits);
U16_TO_16(dos_time, req.updated_time);
U16_TO_16(dos_date, req.updated_date);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -662,7 +636,7 @@ static int tests_ncp22_25_set_archive(uint8 dhandle, char *path,
U16_TO_16(dos_time, req.archived_time);
U16_TO_16(dos_date, req.archived_date);
U32_TO_BE32(archiver_id, req.archived_id);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -714,7 +688,7 @@ static int tests_ncp22_25_set_modifier(uint8 dhandle, char *path, uint32 modifie
U32_TO_BE32(0xffffffffUL, req.searchsequence);
U32_TO_32(TEST_DM_MODIFIER_ID, req.change_bits);
U32_TO_BE32(modifier_id, req.updated_id);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -762,7 +736,7 @@ static int tests_ncp22_25_set_create(uint8 dhandle, char *path,
U16_TO_16(dos_time, req.created_time);
U16_TO_16(dos_date, req.created_date);
U32_TO_BE32(creator_id, req.created_id);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -815,7 +789,7 @@ static int tests_ncp22_25_set_adate(uint8 dhandle, char *path, uint16 dos_date)
U32_TO_BE32(0xffffffffUL, req.searchsequence);
U32_TO_32(TEST_DM_LAST_ACCESS_DATE, req.change_bits);
U16_TO_16(dos_date, req.last_access_date);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -869,7 +843,7 @@ static int tests_ncp22_25_set_maxspace(uint8 dhandle, char *path, uint32 max_spa
U32_TO_32(TEST_DM_MAXIMUM_SPACE, req.change_bits);
U32_TO_32(0x10UL, req.attributes);
U32_TO_BE32(max_space, req.max_space);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -950,7 +924,7 @@ static int tests_ncp22_1e_read_dir_maxspace(uint8 dhandle, char *path, uint32 *m
req.dirhandle = dhandle;
req.search_attributes = 0x10; /* directory */
U32_TO_BE32(0xffffffffUL, req.searchsequence);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);
@@ -1033,7 +1007,7 @@ static int tests_ncp221einfo(char *path)
req.dirhandle = dhandle;
req.search_attributes = 0x10; /* directory */
U32_TO_BE32(0xffffffffUL, req.searchsequence);
if (tests_copy_ncp22_name(req.name, path, &req.namlen))
if (tool_copy_ncp22_name(req.name, path, &req.namlen))
return(-1);
req.len = sizeof(req) - sizeof(req.len);

31
tools.c
View File

@@ -127,6 +127,37 @@ uint32 tool_get_dword_hl(uint8 *p)
(uint32)p[3]);
}
/*
* tool_copy_ncp22_name
*
* Purpose:
* Converts one DOS filename component into the uppercase NCP22 name format.
* The helper rejects paths and keeps the historical 12-byte DOS namespace
* component limit used by the old directory-entry NCPs.
*/
int tool_copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out)
{
char tmp[260];
int len;
if (!dst || !len_out)
return(-1);
if (!src)
src = "";
tool_upcopy(tmp, src, sizeof(tmp));
if (strchr(tmp, '\\') || strchr(tmp, '/') || strchr(tmp, ':'))
return(-1);
len = strlen(tmp);
if (len < 1 || len > 12)
return(-1);
memcpy(dst, tmp, len);
*len_out = (uint8)len;
return(0);
}
#ifdef __WATCOMC__
/*
* Borland C compatibility wrappers used by the historical mars-nwe DOS tools.