#include "readwrite.h"
#include <stddef.h> /* size_t */
-#include <stdint.h> /* uint8_t, uint16_t, uint32_t */
+#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
#include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
- * fputc(), fwrite(), fclose(), fopen()
+ * fputc(), fwrite(), fclose(), fopen(), clearerr()
*/
+#include <stdlib.h> /* free() */
#include <string.h> /* strlen() */
-#include <unistd.h> /* for access(), unlink() */
+#include <unistd.h> /* access(), unlink() */
#include "rexit.h" /* exit_err(), exit_trouble() */
+#include "try_malloc.h" /* try_malloc() */
extern FILE * try_fopen(char * path, char * mode, char * f)
{
+ char * f_name = "try_fopen()";
char * msg1 = "Trouble in ";
char * msg2 = " with fopen() (mode '";
char * msg3 = "') on path '";
char * msg4 = "'.";
uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
+ strlen(f) + strlen(path) + strlen(mode) + 1;
- char msg[size];
- sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
+ char * msg = try_malloc(size, f_name);
+ int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
+ exit_trouble(test < 0, f_name, "sprintf()");
FILE * file_p = fopen(path, mode);
exit_err(NULL == file_p, msg);
+ free(msg);
return file_p;
}
extern int try_fgetc(FILE * file, char * f)
{
+ clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
int test = fgetc(file);
exit_trouble(EOF == test && ferror(file), f, "fgetc()");
return test;
extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
char * f)
{
+ char * f_name = "try_fclose_unlink_rename()";
try_fclose(file, f);
char * msg1 = "Trouble in ";
char * msg4 = "'.";
char * msg2 = " with unlink() on path '";
uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
+ strlen(f) + strlen(p2) + 1;
- char msg[size];
- sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
+ char * msg = try_malloc(size, f_name);
+ int test = sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
+ exit_trouble(test < 0, f_name, "sprintf()");
exit_err(unlink(p2), msg);
+ free(msg);
}
char * msg2 = " with rename() from '";
char * msg3 = "' to '";
uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
+ strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
- char msg[size];
- sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
+ char * msg = try_malloc(size, f_name);
+ int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,p1,msg3,p2,msg4);
+ exit_trouble(test < 0, f_name, "sprintf()");
exit_err(rename(p1, p2), msg);
+ free(msg);
}
-extern uint32_t textfile_sizes(FILE * file, uint32_t * n_lines_p)
+extern uint32_t textfile_width(FILE * file)
{
- char * f_name = "textfile_sizes()";
+ char * f_name = "textfile_width()";
int c = 0;
uint32_t c_count = 0;
- uint32_t n_lines = 0;
uint32_t linemax = 0;
while (1)
{
break;
}
c_count++;
+ exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
if ('\n' == c)
{
if (c_count > linemax)
linemax = c_count;
}
c_count = 0;
- if (n_lines_p)
- {
- n_lines++;
- }
}
}
if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
linemax = c_count;
}
exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");
- if (n_lines_p)
- {
- * n_lines_p = n_lines;
- }
return linemax;
}