Cleaning up some static buffers, and adding in dynamic allocation. No more limits!

This commit is contained in:
Pentium44 2021-04-16 01:33:52 -07:00
parent 9c1c11b56d
commit 7f88238255
11 changed files with 62 additions and 54 deletions

View File

@ -177,7 +177,7 @@ V_FUNC x_warn(s,a,b,c,d,e,f,g,h,i,j)
char *s; /* format string */
unsigned long a,b,c,d,e,f,g,h,i,j; /* dummy arguments */
{
z_msg (FALSE, "Warning: ", s,a,b,c,d,e,f,g,h,i,j);
z_msg (FALSE, ":", s,a,b,c,d,e,f,g,h,i,j);
}
/* ------------------------------------------------------------ */
@ -199,7 +199,7 @@ V_FUNC x_error(s,a,b,c,d,e,f,g,h,i,j)
char *s; /* format string */
long a,b,c,d,e,f,g,h,i,j; /* dummy arguments */
{
z_msg (TRUE, "Error: ", s,a,b,c,d,e,f,g,h,i,j);
z_msg (TRUE, ":", s,a,b,c,d,e,f,g,h,i,j);
}
/* ------------------------------------------------------------ */
@ -224,7 +224,7 @@ V_FUNC x_panic (s,a,b,c,d,e,f,g,h,i,j)
char *s; /* format string */
unsigned long a,b,c,d,e,f,g,h,i,j; /* dummy arguments */
{
z_msg (TRUE, "Internal error: ",
z_msg (TRUE, "INTERNAL ERROR! :",
s,a,b,c,d,e,f,g,h,i,j);
}

View File

@ -43,7 +43,7 @@
#define MAX_VAR_NAME_LEN 512
#define MAX_VAR_NAME_BUFSIZE (MAX_VAR_NAME_LEN + 1)
#define MAX_STRING_LEN 65535
#define MAX_STRING_LEN 8128
#define MAX_STRING_BUFSIZE (MAX_STRING_LEN + 1)
#define MAX_VAR_DATA_LEN 65535
#define MAX_DATA_BUFSIZE (MAX_VAR_DATA_LEN + 1)

View File

@ -5,4 +5,9 @@
View README file supplied with this software for more details
*/
#include "x3mem.h"
UX3_EXT QLIST QM_RETURN [ONE]; // Dynamic-memory QLIST
char *process_line(char *line);

View File

@ -30,6 +30,7 @@ THE SOFTWARE
UX3_EXT QLIST QM_TAR [ONE]; // Dynamic-memory QLIST
extern void tar_free_pool (void);
#ifndef _DEFAULT_SOURCE

View File

@ -21,22 +21,21 @@
char *process_line(char *line)
{
char *tok_srch;
char *tok_srch, *retbuf;
//printf("%s", &line[strlen(line)-2]);
static char concatbuf[MAX_CONCAT_BUF+1];
static char filebuf[MAX_READFILE_LEN+1];
static char retbuf[MAX_STRING_BUFSIZE];
static char compbuf[MAX_STRING_BUFSIZE];
static char loopbuf[MAX_STRING_BUFSIZE];
static char countbuf[MAX_STRING_BUFSIZE];
static char dirpathbuf[MAX_STRING_BUFSIZE];
// Make sure static buffers are empty before initialize
// Write with zeros
bzero(filebuf, MAX_READFILE_LEN);
bzero(concatbuf, MAX_CONCAT_BUF);
bzero(retbuf, MAX_STRING_LEN);
tok_srch = strtok(line, "=\" ;");
@ -58,14 +57,14 @@ char *process_line(char *line)
else if(strncmp("version", tok_srch, 7) == 0)
{
sprintf(retbuf, "ss:version: %s, type 'help' for function list\n", VERSION);
return retbuf;
x_warn(":ss:version: %s, type 'help' for function list\n", VERSION);
return NULL;
}
else if(strncmp("help", tok_srch, 4) == 0)
{
sprintf(retbuf, "ss:help:\n%s", FUNCTION_HELP);
return retbuf;
x_warn(":ss:help:\n%s", FUNCTION_HELP);
return NULL;
}
// Lets add if and loop statements, make this
@ -260,11 +259,11 @@ char *process_line(char *line)
else if(strncmp("showpath", tok_srch, 8) == 0 || strncmp("pwd", tok_srch, 3) == 0)
{
// Get current directory, if it errors, return NULL
if (getcwd(retbuf, sizeof(retbuf)) == NULL) {
if (getcwd(retbuf, sizeof(dirpathbuf)) == NULL) {
return NULL;
}
// All good, return path
return retbuf;
return dirpathbuf;
}
// Change directory
@ -317,10 +316,8 @@ char *process_line(char *line)
if(dirname == NULL)
dirname = parse_vars(tok_srch);
sprintf(retbuf, "%s\n", "ss:showdir:");
struct dirent **files;
int nn;
int nn, dirstr_size;
nn = scandir (dirname, &files, NULL, alphasort);
@ -330,15 +327,24 @@ char *process_line(char *line)
return NULL;
}
dirstr_size = 14;
for (int jj = 0; jj < nn; jj++)
{
struct dirent *ent = files [jj];
dirstr_size += (strlen(ent->d_name)+3);
}
retbuf = qmalloc(QM_RETURN, dirstr_size);
sprintf(retbuf, "%s\n", "ss:showdir:");
for (int ii = 0; ii < nn; ii++)
{
struct dirent *ent = files [ii];
if((strlen(retbuf) + strlen(ent->d_name) + 1) < MAX_STRING_LEN)
{
strcat(retbuf, ent->d_name);
strcat(retbuf, "\n");
}
}
for (int ii = 0; ii < nn; ii++) { free (files [ii]); }
free (files);
@ -386,7 +392,7 @@ char *process_line(char *line)
sprintf(origsize, "%ld", fsize(in));
uint32_t deco_return = ss_decompress(filename, filedecout);
retbuf = qmalloc(QM_RETURN, (sizeof(deco_return) + strlen(filename) + strlen(origsize) + 40));
sprintf(retbuf, "ss: %s: decompressed: %s -> %u", filename, origsize, deco_return);
fclose(in);
@ -534,9 +540,12 @@ char *process_line(char *line)
{
uint32_t comp_return = ss_compress(filename, file_comp, i);
uint32_t deco_return = ss_decompress(file_comp, filedecout);
sprintf(retbuf, "ss: %s: compressed: %u -> %u", file_comp, deco_return, comp_return);
if(atoi(comp_size) < (int)comp_return && comp_return != 0 && deco_return != 0 && i != 1)
{
retbuf = qmalloc(QM_RETURN, sizeof(deco_return) + sizeof(comp_return) + strlen(file_comp) + 40);
sprintf(retbuf, "ss: %s: compressed: %u -> %u", file_comp, deco_return, comp_return);
break;
}
sprintf(comp_size, "%u", comp_return);
printf("pass %u decompressed/compressed: %u/%u\n", i, deco_return, comp_return);
@ -1084,9 +1093,10 @@ char *process_line(char *line)
catfile = ss_concat(file_md5_val, file_line);
if(strlen(catfile) < MAX_CONCAT_BUF)
{
strcpy(concatbuf, catfile);
retbuf = qmalloc(QM_RETURN, strlen(catfile)+1);
strcpy(retbuf, catfile);
free(catfile);
return concatbuf;
return retbuf;
}
else
{
@ -1314,40 +1324,26 @@ char *process_line(char *line)
read_size = ftell(read_file);
rewind(read_file);
dynfile = (char *)malloc(read_size + 1);
*dynfile = '\0';
dynfile = qmalloc(QM_RETURN, read_size);
/* Check if file was opened successfully */
if(read_file == NULL)
{
free(dynfile);
x_warn("ss:warn:write, failed open");
return NULL;
}
char *nullbyte = "\0";
strcpy(dynfile, nullbyte);
while(fgets(read_line, sizeof(read_line), read_file) != NULL)
{
strcat(dynfile, read_line);
}
if(read_size <= MAX_READFILE_LEN)
{
// Copy string to end of file, rewrite file
sprintf(filebuf, "%s", dynfile);
free(dynfile);
}
else
{
free(dynfile);
fclose(read_file);
x_warn("ss:warn:write, file too large (2MB max)");
return NULL;
}
fclose(read_file);
return strip_nl(filebuf);
return strip_nl(dynfile);
} /* read function */
/* Cat function, writes to end of file specified */
@ -1427,9 +1423,9 @@ char *process_line(char *line)
read_size = ftell(read_file);
rewind(read_file);
dynfile = (char *)malloc(read_size + 1);
dynfile = qmalloc(QM_RETURN, read_size);
*dynfile = '\0';
strcpy(dynfile, "\0");
while(fgets(read_line, sizeof(read_line), read_file) != NULL)
{
@ -1440,15 +1436,15 @@ char *process_line(char *line)
if((read_size + strlen(parse_vars(tok_srch))) <= MAX_READFILE_LEN)
{
char *filebuf = qmalloc(QM_RETURN, (read_size + strlen(parse_vars(tok_srch))));
read_file = fopen(filename, "w");
sprintf(filebuf, "%s%s", dynfile, parse_vars(tok_srch));
fprintf(read_file, "%s", filebuf);
free(dynfile);
fclose(read_file);
return NULL;
}
else
{
free(dynfile);
fclose(read_file);
x_warn("ss:warn:cat, file too large (2MB max)");
return NULL;

View File

@ -53,7 +53,7 @@ int main(int argc, char **argv)
if (is_interactive)
{
printf ("%s", "ss:prompt: ");
printf ("%s", ":ss:prompt: ");
fflush (stdout);
}
@ -85,7 +85,10 @@ int main(int argc, char **argv)
// set PIPE var back to NULL after use, keep the struct clean
set_var(varc, "\0", "\0");
// Check to see if there's anything to even display
if(return_dat == NULL) break;
if(return_dat == NULL)
{
break;
}
}
}
@ -95,6 +98,9 @@ int main(int argc, char **argv)
printf("%s\n", return_dat);
fflush(stdout);
}
// Free used mallocs within QM_RETURN
qflush(QM_RETURN);
} /* end of while */
} /* file null */