Hi, I'm fairly new to C, and I wanted some feedback on a new project I made. It is a line-oriented text editor inspired (not based on, so functionality is not the same) by the 'ed' utility on UNIX systems. Here is the Github link: https://github.com/TSOA2/Blob
Any advice would be appreciated!
#Line-oriented text editor
28 messages · Page 1 of 1 (latest)
https://github.com/TSOA2/Blob/blob/c025c71c03e17a1fee4a3f6d25c3fa97db9dfeca/blob.c#L21C1-L28C14 these macros aren't really worth it, they're not used anywhere near enough to justify their own existence
also one of the reasons to use do while(0) in a macro is to force the user to add a semicolon at the end
you shouldn't add that yourself, if you used a macro
also get rid of the PROMPT macro
macros are just evil in general
also you're relying on C99 since not every variable is declared at the start of a block, so commit to the style
Thank you for your help! Should I have PROMPT in a constant, and then just remove the linked list macros then?
Additionally, looking at the man page for signal (https://man7.org/linux/man-pages/man2/signal.2.html), it says that signal() is not portable. Should I be using it?
FILE *create_empty_file(const char *fname)
{
- FILE *create_file;
- FILE *read_file;
- create_file = fopen(fname, "w");
+ FILE *file = fopen(fname, "w+");
- if (create_file == NULL) {
+ if (file == NULL) {
perror("ed: create_empty_file");
exit(EXIT_FAILURE);
}
- (void) fclose(create_file);
- read_file = fopen(fname, "r");
- FILE *read_file = freopen(fname, "r", create_file);
- if (read_file == NULL) {
- perror("ed: create_empty_file");
- exit(EXIT_FAILURE);
- }
-
- return read_file;
+ return file;
}
or you could freopen or something
in any case, it's probably not good to open the same file twice with manual closing
Thanks, I didn't know that!
it's in the standard library, so it's decently portable
every platform will have slightly different signals and behavior around them though
just don't rely on it working in any particular way
Sounds good.
also you should be using const a lot more, especially in longer functions
lots of mutable variables make it very difficult to reason about code
Am I making any mistakes with memory allocation/freeing linked lists?
it's overall pretty clean though
idk, you can never tell with manual memory management at first glance
you should run this through valgrind or an address sanitizer to be somewhat sure
Thank you for your help!
B - baffling
l - line
o - oriented
b - baboon?
@ebon bay why not, will add that