diff --git a/flag.c b/flag.c index c5e6834..36dba7a 100644 --- a/flag.c +++ b/flag.c @@ -381,6 +381,14 @@ static void flag_display_one(char *name, uint32 attr) fprintf(stdout, "\n"); } +static void flag_display_one_paged(char *name, uint32 attr, + int *line_count, int *continuous) +{ + flag_display_one(name, attr); + tool_page_line(line_count, continuous); +} + + static int flag_has_wildcards(char *s) { while (*s) { @@ -395,6 +403,8 @@ static int flag_list(char *pattern) struct find_t ff; unsigned findattr = _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_ARCH; int found = 0; + int line_count = 0; + int continuous = 0; if (_dos_findfirst(pattern, findattr, &ff)) return(-1); @@ -406,7 +416,7 @@ static int flag_list(char *pattern) if (flag_ncp87_obtain_attrs(ff.name, &nwattrs)) nwattrs = (uint32)ff.attrib; - flag_display_one(ff.name, nwattrs); + flag_display_one_paged(ff.name, nwattrs, &line_count, &continuous); found++; } } while (!_dos_findnext(&ff)); diff --git a/flagdir.c b/flagdir.c index 1cb82d1..79c40fc 100644 --- a/flagdir.c +++ b/flagdir.c @@ -1,4 +1,4 @@ -/* flagdir.c - Novell FLAGDIR-like DOS utility, first Client32 version */ +/* flagdir.c - Novell FLAGDIR-like DOS utility, Client32 version */ #include "net.h" #include "c32ncp.h" @@ -43,8 +43,12 @@ static int fd_same(char *a, char *b) static void fd_upcopy(char *dst, char *src, int max) { int i = 0; + + if (!src) src = ""; + while (*src && i < max - 1) { char c = *src++; + if (c == '/') c = '\\'; if (c >= 'a' && c <= 'z') c -= 32; dst[i++] = c; } @@ -75,7 +79,6 @@ static int fd_current_dhandle(uint8 *dhandle) return(0); } - static int fd_current_prefix(char *out, int max) { uint8 connid = 0; @@ -129,6 +132,125 @@ static int fd_current_prefix(char *out, int max) return(0); } +static int fd_current_display_path(uint8 dhandle, char *out, int max) +{ + char path[260]; + char *p; + + if (!out || max < 2) + return(-1); + + out[0] = '\0'; + path[0] = '\0'; + + if (get_dir_path(dhandle, path) || !path[0]) + return(-1); + + p = strchr(path, ':'); + if (p) + p++; + else + p = path; + + if (!*p) + out[0] = '\0'; + else + strmaxcpy(out, p, max - 1); + + return(0); +} + +static int fd_is_current_path(char *path) +{ + if (!path || !*path) return(1); + if (fd_same(path, ".")) return(1); + if (fd_same(path, ".\\")) return(1); + if (fd_same(path, "./")) return(1); + return(0); +} + +static int fd_has_wildcards(char *s) +{ + if (!s) return(0); + while (*s) { + if (*s == '*' || *s == '?') + return(1); + s++; + } + return(0); +} + +static void fd_split_pattern(char *spec, char *dir, char *pat) +{ + char tmp[260]; + char *p; + + if (!spec || !*spec) { + strcpy(dir, "."); + strcpy(pat, "*.*"); + return; + } + + strmaxcpy(tmp, spec, sizeof(tmp) - 1); + p = strrchr(tmp, '\\'); + if (!p) p = strrchr(tmp, '/'); + + if (p) { + *p++ = '\0'; + if (tmp[0]) + strmaxcpy(dir, tmp, 259); + else + strcpy(dir, "."); + strmaxcpy(pat, p, 259); + } else { + strcpy(dir, "."); + strmaxcpy(pat, tmp, 259); + } + + if (!pat[0]) + strcpy(pat, "*.*"); +} + +static void fd_join_path(char *out, char *dir, char *name, int max) +{ + int len; + + out[0] = '\0'; + + if (!dir || !dir[0] || fd_same(dir, ".")) { + strmaxcpy(out, name, max - 1); + return; + } + + strmaxcpy(out, dir, max - 1); + len = strlen(out); + + if (len > 0 && out[len - 1] != '\\' && out[len - 1] != '/' && + out[len - 1] != ':') { + if (len < max - 1) { + out[len++] = '\\'; + out[len] = '\0'; + } + } + + if ((int)(strlen(out) + strlen(name)) < max - 1) + strcat(out, name); +} + +static void fd_basename(char *out, char *path, int max) +{ + char up[260]; + char *p; + + fd_upcopy(up, path, sizeof(up)); + p = strrchr(up, '\\'); + if (!p) p = strrchr(up, ':'); + + if (p) + strmaxcpy(out, p + 1, max - 1); + else + strmaxcpy(out, up, max - 1); +} static void fd_help(void) { @@ -166,21 +288,10 @@ static int fd_attr_mask(char *s, uint32 *setbits, uint32 *clearbits) return(0); } -static void fd_display(char *path, uint32 attrs) +static void fd_print_attrs(uint32 attrs) { - char up[260]; int any = 0; - fd_upcopy(up, path, sizeof(up)); - - { - char prefix[90]; - if (fd_current_prefix(prefix, sizeof(prefix))) - prefix[0] = '\0'; - fprintf(stdout, "%s%s \n", prefix, up); - } - fprintf(stdout, " %-12.12s ", up); - if (!(attrs & FD_DIR_BITS)) { fprintf(stdout, "Normal\n"); return; @@ -210,12 +321,45 @@ static void fd_display(char *path, uint32 attrs) fprintf(stdout, "\n"); } +static void fd_display_header(char *path) +{ + char up[260]; + char prefix[90]; + + fd_upcopy(up, path, sizeof(up)); + + if (fd_current_prefix(prefix, sizeof(prefix))) + prefix[0] = '\0'; + + fprintf(stdout, "%s%s \n", prefix, up); +} + +static void fd_display_row(char *name, uint32 attrs) +{ + char base[260]; + + fd_basename(base, name, sizeof(base)); + fprintf(stdout, " %-12.12s ", base); + fd_print_attrs(attrs); +} + +static void fd_display(char *path, uint32 attrs) +{ + fd_display_header(path); + fd_display_row(path, attrs); +} + static int fd_is_directory(char *path) { struct find_t ff; unsigned attr = _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR | _A_ARCH; + + if (fd_is_current_path(path)) + return(1); + if (_dos_findfirst(path, attr, &ff)) return(0); + return((ff.attrib & _A_SUBDIR) != 0); } @@ -235,14 +379,103 @@ static int fd_modify(char *path, uint8 dhandle, uint32 attrs) return(-1); } +static int fd_process_one(char *path, char *display_path, uint8 dhandle, + uint32 setbits, uint32 clearbits, + int have_change, int show_header) +{ + char *ncp_path; + uint32 attrs; + uint32 newattrs; + + if (!fd_is_directory(path)) { + fprintf(stderr, "Directory %s not found.\n", path); + return(1); + } + + ncp_path = fd_is_current_path(path) ? "" : path; + + if (fd_obtain(ncp_path, dhandle, &attrs)) { + if (fd_is_current_path(path) && display_path && !display_path[0] && + !have_change) { + attrs = 0; + } else { + fprintf(stderr, "Unable to get directory attributes.\n"); + return(1); + } + } + + if (have_change) { + newattrs = (attrs | setbits) & ~clearbits; + if (newattrs != attrs) { + if (fd_modify(ncp_path, dhandle, newattrs)) { + fprintf(stderr, "Unable to change attributes.\n"); + return(1); + } + attrs = newattrs; + fd_obtain(ncp_path, dhandle, &attrs); + } + } + + if (show_header) + fd_display(display_path, attrs); + else + fd_display_row(display_path, attrs); + + return(0); +} + +static int fd_process_wild(char *spec, uint8 dhandle, + uint32 setbits, uint32 clearbits, + int have_change) +{ + struct find_t ff; + char dir[260]; + char pat[260]; + char search[260]; + char full[260]; + int found = 0; + int rc = 0; + int lines = 0; + int continuous = 0; + unsigned attr = _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR | _A_ARCH; + + fd_split_pattern(spec, dir, pat); + fd_join_path(search, dir, pat, sizeof(search)); + + if (_dos_findfirst(search, attr, &ff) != 0) { + fprintf(stderr, "Directory %s not found.\n", spec); + return(1); + } + + fd_display_header(spec); + + do { + if ((ff.attrib & _A_SUBDIR) && strcmp(ff.name, ".") && + strcmp(ff.name, "..")) { + fd_join_path(full, dir, ff.name, sizeof(full)); + if (fd_process_one(full, ff.name, dhandle, setbits, clearbits, + have_change, 0)) + rc = 1; + found++; + tool_page_line(&lines, &continuous); + } + } while (_dos_findnext(&ff) == 0); + + if (!found) { + fprintf(stderr, "Directory %s not found.\n", spec); + return(1); + } + + return(rc); +} + int func_flagdir(int argc, char *argv[], int mode) { char *path = "."; + char display_path[260]; uint8 dhandle = 0; - uint32 attrs = 0; uint32 setbits = 0; uint32 clearbits = 0; - uint32 newattrs; int have_change = 0; int i; @@ -262,35 +495,22 @@ int func_flagdir(int argc, char *argv[], int mode) return(1); } - if (!fd_is_directory(path)) { - fprintf(stderr, "Directory %s not found.\n", path); - return(1); - } - for (i = 2; i < argc; i++) { if (fd_attr_mask(argv[i], &setbits, &clearbits)) return(1); have_change = 1; } - if (fd_obtain(path, dhandle, &attrs)) { - fprintf(stderr, "Unable to get directory attributes.\n"); - return(1); + if (fd_has_wildcards(path)) + return(fd_process_wild(path, dhandle, setbits, clearbits, have_change)); + + if (fd_is_current_path(path)) { + if (fd_current_display_path(dhandle, display_path, sizeof(display_path))) + strcpy(display_path, "."); + } else { + strmaxcpy(display_path, path, sizeof(display_path) - 1); } - if (have_change) { - newattrs = (attrs | setbits) & ~clearbits; - if (newattrs != attrs) { - if (fd_modify(path, dhandle, newattrs)) { - fprintf(stderr, "Unable to change attributes.\n"); - return(1); - } - attrs = newattrs; - /* Try to read back; keep requested value if readback fails. */ - fd_obtain(path, dhandle, &attrs); - } - } - - fd_display(path, attrs); - return(0); + return(fd_process_one(path, display_path, dhandle, + setbits, clearbits, have_change, 1)); } diff --git a/net.h b/net.h index 869cbaa..6047e8b 100644 --- a/net.h +++ b/net.h @@ -17,6 +17,7 @@ #include #ifdef __WATCOMC__ #include +extern int tool_page_line(int *line_count, int *continuous); #endif typedef unsigned int UI; @@ -58,7 +59,7 @@ typedef struct { uint16 fragment_count; /* Anzahl Fragment Buffers */ uint8 *fragment_1; uint16 fragment_1_size; - /* K�nnen auch mehr sein */ + /* K�nnen auch mehr sein */ } ECB; #include "kern.h" diff --git a/slist.c b/slist.c index 18a0f44..76f3b76 100644 --- a/slist.c +++ b/slist.c @@ -55,6 +55,8 @@ int func_slist(int argc, char *argv[], int mode) int found = 0; int result; int i; + int continuous = 0; + int line_count = 0; (void)mode; @@ -66,6 +68,7 @@ int func_slist(int argc, char *argv[], int mode) if (argv[i][0] == '/' || argv[i][0] == '-') { if (same_arg(argv[i], "/C") || same_arg(argv[i], "/CONTINUE") || same_arg(argv[i], "-C") || same_arg(argv[i], "-CONTINUE")) { + continuous = 1; continue; } return(usage()); @@ -101,6 +104,7 @@ int func_slist(int argc, char *argv[], int mode) } fprintf(stdout, "\n"); + tool_page_line(&line_count, &continuous); if (last_id == MAX_U32) break; } diff --git a/tools.c b/tools.c index e4b44a4..d3bf7cf 100644 --- a/tools.c +++ b/tools.c @@ -328,7 +328,7 @@ int putglobenv(char *option) } search=nextp; } - /* nicht gefunden , nun eintragen, falls m�glich */ + /* nicht gefunden , nun eintragen, falls m�glich */ if (*(equal+1) && optionlen < maxenvsize - aktenvsize) { strcpy(search, option); *(search+optionlen+1) = '\0'; /* letzter Eintrag '\0' nicht vergessen */ @@ -339,3 +339,30 @@ int putglobenv(char *option) return(-1); } + +int tool_page_line(int *line_count, int *continuous) +{ + int ch; + + if (!line_count || !continuous) + return(0); + + if (*continuous) + return(0); + + (*line_count)++; + if (*line_count < 24) + return(0); + + fprintf(stdout, "Press any key to continue ('C' for continue)"); + fflush(stdout); + ch = getch(); + fprintf(stdout, "\n"); + + if (ch == 'c' || ch == 'C') + *continuous = 1; + + *line_count = 0; + return(0); +} +