Commit 63146072 authored by Jonas Termansen's avatar Jonas Termansen

Fix getline(3) and getdelim(3) usage.

parent 8d5599ba
......@@ -715,9 +715,9 @@ static bool lookup_fstab_by_blockdevice(struct fstab* out_fsent,
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, fstab_fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fstab_fp)) )
{
if ( line_length && line[line_length - 1] == '\n' )
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
if ( !scanfsent(line, out_fsent) )
continue;
......@@ -730,8 +730,6 @@ static bool lookup_fstab_by_blockdevice(struct fstab* out_fsent,
}
free(line);
fclose(fstab_fp);
if ( errno )
return false;
return false;
}
......@@ -747,9 +745,9 @@ static bool remove_blockdevice_from_fstab(struct blockdevice* bdev)
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, rewr.in)) )
while ( 0 < (line_length = getline(&line, &line_size, rewr.in)) )
{
if ( line_length && line[line_length - 1] == '\n' )
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
char* dup = strdup(line);
if ( !dup )
......@@ -761,7 +759,7 @@ static bool remove_blockdevice_from_fstab(struct blockdevice* bdev)
free(dup);
}
free(line);
if ( errno )
if ( ferror(rewr.in) )
return rewrite_abort(&rewr), false;
return rewrite_finish(&rewr);
}
......@@ -811,9 +809,9 @@ static bool add_blockdevice_to_fstab(struct blockdevice* bdev,
size_t line_size = 0;
ssize_t line_length;
bool found = false;
while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, rewr.in)) )
while ( 0 < (line_length = getline(&line, &line_size, rewr.in)) )
{
if ( line_length && line[line_length - 1] == '\n' )
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
char* dup = strdup(line);
if ( !dup )
......@@ -845,7 +843,7 @@ static bool add_blockdevice_to_fstab(struct blockdevice* bdev,
free(dup);
}
free(line);
if ( errno )
if ( ferror(rewr.in) )
return rewrite_abort(&rewr), false;
if ( !found )
print_blockdevice_fsent(rewr.out, bdev, mountpoint);
......@@ -2820,7 +2818,7 @@ int main(int argc, char* argv[])
break;
}
if ( line_length && line[line_length-1] == '\n' )
if ( line[line_length-1] == '\n' )
line[--line_length] = '\0';
execute(line);
......
......@@ -97,7 +97,7 @@ void editor_load_config_path(struct editor* editor, const char* path)
char* line = NULL;
size_t line_size = 0;
ssize_t line_length = 0;
while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
......@@ -105,7 +105,7 @@ void editor_load_config_path(struct editor* editor, const char* path)
line[line_length] = '\0';
editor_modal_command_config(editor, line);
}
if ( errno != 0 )
if ( ferror(fp) )
error(0, errno, "getline: %s", path);
fclose(fp);
}
......
......@@ -63,7 +63,7 @@ static char* read_single_line(FILE* fp)
free(ret);
return NULL;
}
if ( ret_length && ret[ret_length-1] == '\n' )
if ( ret[ret_length-1] == '\n' )
ret[--ret_length] = '\0';
return ret;
}
......@@ -326,7 +326,7 @@ static void load_fstab(void)
char* line = NULL;
size_t line_size;
ssize_t line_length;
while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
......@@ -354,7 +354,7 @@ static void load_fstab(void)
line = NULL;
line_size = 0;
}
if ( errno )
if ( ferror(fp) )
fatal("/etc/fstab: %m");
free(line);
fclose(fp);
......
......@@ -460,9 +460,9 @@ int main(int argc, char* argv[])
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 <= (line_length = getline(&line, &line_size, input)) )
while ( 0 < (line_length = getline(&line, &line_size, input)) )
{
if ( line_length && line[line_length-1] == '\n' )
if ( line[line_length-1] == '\n' )
line[--line_length] = '\0';
if ( line[0] == '/' && line[1] == '/' )
continue;
......@@ -604,6 +604,8 @@ int main(int argc, char* argv[])
assert(found_key);
}
free(line);
if ( ferror(input) )
error(1, errno, "%s", input_path);
fclose(input);
if ( verbose )
......
......@@ -29,9 +29,9 @@ struct fstab* getfsent(void)
static char* line = NULL;
static size_t line_size = 0;
ssize_t line_length;
while ( 0 <= (line_length = getline(&line, &line_size, __fstab_file)) )
while ( 0 < (line_length = getline(&line, &line_size, __fstab_file)) )
{
if ( line_length && line[line_length - 1] == '\n' )
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
if ( scanfsent(line, &fs) )
return &fs;
......
......@@ -174,12 +174,12 @@ bool AddRulesFromFile(FILE* fp, const char* fpname)
size_t line_num = 0;
char* mem = NULL;
ssize_t line_len;
while ( 0 <= (line_len = getline(&mem, &line_size, fp)) )
while ( 0 < (line_len = getline(&mem, &line_size, fp)) )
{
char* line = mem;
line_num++;
if ( line_len && line[line_len-1] == '\n' )
line[line_len-1] = '\0';
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
line = (char*) SkipWhitespace((char*) line);
if ( IsLineComment(line) )
continue;
......@@ -279,10 +279,10 @@ bool AddManifestFromFile(FILE* fp, const char* fpname)
char* line = NULL;
size_t line_size = 0;
ssize_t line_len;
while ( 0 <= (line_len = getline(&line, &line_size, fp)) )
while ( 0 < (line_len = getline(&line, &line_size, fp)) )
{
if ( line_len && line[line_len-1] == '\n' )
line[line_len-1] = '\0';
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
if ( !AddManifestPath(line) )
return false;
}
......
......@@ -91,8 +91,11 @@ char* search_for_proper_shell(void)
ssize_t result_length = getline(&result, &result_size, fp);
fclose(fp);
if ( result_length < 0 )
{
free(result);
continue;
if ( result_length && result[result_length-1] == '\n' )
}
if ( result[result_length-1] == '\n' )
result[--result_length] = '\0';
if ( !is_existing_shell(result) )
{
......
......@@ -74,7 +74,7 @@ void load_upgrade_conf(struct conf* conf, const char* path)
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
......@@ -105,7 +105,7 @@ void load_upgrade_conf(struct conf* conf, const char* path)
name[name_length] = '\0';
conf_assign(conf, name, value, path);
}
if ( errno )
if ( ferror(fp) )
err(2, "%s", path);
free(line);
fclose(fp);
......
......@@ -272,7 +272,7 @@ bool load_mountpoints(const char* fstab_path,
char* line = NULL;
size_t line_size;
ssize_t line_length;
while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
......@@ -309,7 +309,7 @@ bool load_mountpoints(const char* fstab_path,
line = NULL;
line_size = 0;
}
bool failure = errno;
bool failure = ferror(fp);
free(line);
fclose(fp);
if ( failure )
......
......@@ -97,9 +97,9 @@ void install_manifest(const char* manifest,
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, fpin)) )
while ( 0 < (line_length = getline(&line, &line_size, fpin)) )
{
if ( line_length && line[line_length-1] == '\n' )
if ( line[line_length-1] == '\n' )
line[--line_length] = '\0';
if ( fprintf(fpout, "%s\n", line) < 0 )
{
......@@ -241,7 +241,7 @@ void install_manifest(const char* manifest,
free(out_path);
}
free(line);
if ( errno )
if ( ferror(fpin) )
{
warn("%s", inmanifest);
_exit(2);
......@@ -273,7 +273,7 @@ bool check_installed(const char* path, const char* package)
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length-1] == '\n' )
line[--line_length] = '\0';
......@@ -284,7 +284,7 @@ bool check_installed(const char* path, const char* package)
return true;
}
}
if ( errno != 0 )
if ( ferror(fp) )
warn("%s", path);
free(line);
fclose(fp);
......@@ -363,7 +363,7 @@ void install_ports(const char* from_prefix, const char* to_prefix)
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length-1] == '\n' )
line[--line_length] = '\0';
......@@ -395,7 +395,7 @@ void install_ports(const char* from_prefix, const char* to_prefix)
install_manifest(line, from_prefix, to_prefix);
}
free(line);
if ( errno )
if ( ferror(fp) )
{
warn("%s", cmd);
pclose(fp);
......
......@@ -110,7 +110,7 @@ bool os_release_load(struct release* release,
char* line = NULL;
size_t line_size = 0;
ssize_t line_length;
while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) )
while ( 0 < (line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length-1] == '\n' )
line[--line_length] = '\0';
......@@ -178,7 +178,7 @@ bool os_release_load(struct release* release,
}
}
}
if ( errno )
if ( ferror(fp) )
warn("%s", errpath);
else if ( failure )
;
......
......@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
......@@ -313,7 +314,7 @@ int main(int argc, char* argv[])
ssize_t line_len;
while ( 0 < (line_len = getline(&line, &line_size, tar_fp)) )
{
if ( line_len && line[line_len-1] == '\n' )
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
const char* path = line;
while ( *path && *path != '/' )
......@@ -335,6 +336,8 @@ int main(int argc, char* argv[])
}
}
free(line);
if ( ferror(tar_fp) )
error(1, errno, "getline: tar");
fclose(tar_fp);
int tar_exit_status;
......
......@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
......@@ -154,7 +155,7 @@ int main(int argc, char* argv[])
ssize_t line_len;
while ( 0 < (line_len = getline(&line, &line_size, porttixinfo_fp)) )
{
if ( line_len && line[line_len-1] == '\n' )
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
char* first_space = strchr(line, ' ');
if ( !first_space )
......@@ -271,6 +272,8 @@ int main(int argc, char* argv[])
porttixinfo_path, function);
}
free(line);
if ( ferror(porttixinfo_fp) )
error(1, errno, "%s", porttixinfo_path);
fclose(porttixinfo_fp);
free(porttixinfo_path);
......
......@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
......
......@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
......
......@@ -105,7 +105,7 @@ bool IsPackageInstalled(const char* tixdb_path, const char* package)
ssize_t line_len;
while ( 0 < (line_len = getline(&line, &line_size, installed_list_fp)) )
{
if ( line_len && line[line_len-1] == '\n' )
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
if ( !strcmp(line, package) )
{
......@@ -114,6 +114,8 @@ bool IsPackageInstalled(const char* tixdb_path, const char* package)
}
}
free(line);
if ( ferror(installed_list_fp) )
error(1, errno, "`%s'", installed_list_path);
fclose(installed_list_fp);
free(installed_list_path);
......
......@@ -242,11 +242,12 @@ void string_array_append_file(string_array_t* sa, FILE* fp)
ssize_t entry_length;
while ( 0 < (entry_length = getline(&entry, &entry_size, fp)) )
{
if ( entry_length && entry[entry_length-1] == '\n' )
entry[entry_length-1] = '\0';
if ( entry[entry_length-1] == '\n' )
entry[--entry_length] = '\0';
string_array_append(sa, entry);
}
free(entry);
assert(!ferror(fp));
}
bool string_array_append_file_path(string_array_t* sa, const char* path)
......@@ -330,14 +331,15 @@ void dictionary_append_file(string_array_t* sa, FILE* fp)
ssize_t entry_length;
while ( 0 < (entry_length = getline(&entry, &entry_size, fp)) )
{
if ( entry_length && entry[entry_length-1] == '\n' )
entry[entry_length-1] = '\0';
if ( entry[entry_length-1] == '\n' )
entry[--entry_length] = '\0';
dictionary_normalize_entry(entry);
if ( entry[0] == '#' )
continue;
string_array_append(sa, entry);
}
free(entry);
assert(!ferror(fp));
}
bool dictionary_append_file_path(string_array_t* sa, const char* path)
......@@ -372,8 +374,8 @@ char* read_single_line(FILE* fp)
free(ret);
return NULL;
}
if ( ret_len && ret[ret_len-1] == '\n' )
ret[ret_len-1] = '\0';
if ( ret[ret_len-1] == '\n' )
ret[--ret_len] = '\0';
return ret;
}
......@@ -563,7 +565,7 @@ bool TarContainsFile(const char* archive, const char* file)
bool ret = false;
while ( 0 < (line_len = getline(&line, &line_size, fp)) )
{
if ( line_len && line[line_len-1] == '\n' )
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
if ( strcmp(line, file) == 0 )
{
......@@ -575,6 +577,8 @@ bool TarContainsFile(const char* archive, const char* file)
}
}
free(line);
if ( ferror(fp) )
error(1, errno, "getline: tar");
fclose(fp);
int tar_exit_status;
......
......@@ -97,9 +97,12 @@ int main(int argc, char* argv[])
mode = next_mode;
}
free(line);
int stdin_errno = errno;
int status;
waitpid(child_pid, &status, 0);
printf("\e[m");
fflush(stdout);
if ( ferror(stdin) )
error(1, stdin_errno, "stdin");
return WEXITSTATUS(status);
}
......@@ -95,14 +95,14 @@ bool append_lines_from_file(FILE* fp,
if ( string_length < 0 )
{
free(string);
if ( feof(stdin) )
return true;
if ( errno == 0 )
return true;
error(0, errno, "getline: `%s'", fpname);
return false;
if ( ferror(fp) )
{
error(0, errno, "getline: `%s'", fpname);
return false;
}
return true;
}
if ( string_length && string[string_length-1] == '\n' )
if ( string[string_length-1] == '\n' )
string[--string_length] = '\0';
size_t display_width = measure_line_display_width(string);
if ( display_width == 0 && !flag_empty )
......
......@@ -105,7 +105,7 @@ char* read_line(FILE* fp, const char* fpname, int delim)
error(0, errno, "read: `%s'", fpname);
return NULL;
}
if ( amount && (unsigned char) line[amount-1] == (unsigned char) delim )
if ( (unsigned char) line[amount-1] == (unsigned char) delim )
line[amount-1] = '\0';
return line;
}
......
......@@ -67,9 +67,10 @@ bool processfp(const char* inputname, FILE* fp)
ssize_t linelen = getline(&line, &linesize, fp);
if ( linelen < 0 )
{
if ( ferror(fp) )
error(1, errno, "%s", inputname);
free(line);
if ( feof(fp) ) { break; }
error(1, errno, "%s", inputname);
break;
}
if ( specialleading )
{
......
......@@ -41,7 +41,7 @@ char* read_line(FILE* fp, const char* fpname, int delim)
error(0, errno, "read: `%s'", fpname);
return NULL;
}
if ( amount && (unsigned char) line[amount-1] == (unsigned char) delim )
if ( (unsigned char) line[amount-1] == (unsigned char) delim )
line[amount-1] = '\0';
return line;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment