aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJason Woodward2004-10-16 06:46:45 +0000
committerJason Woodward2004-10-16 06:46:45 +0000
commit651e62a784c5ca6349ed5a1039b21c9c76e77657 (patch)
tree75d0876f683e562793a09bf8777fd059a680f472 /src
parent99569241cd210a2ef27d363dc7d329d685f9d39d (diff)
downloadslapt-get-651e62a784c5ca6349ed5a1039b21c9c76e77657.tar.gz
misc code cleanups
Diffstat (limited to 'src')
-rw-r--r--src/action.c28
-rw-r--r--src/common.c14
-rw-r--r--src/configuration.c14
-rw-r--r--src/curl.c3
-rw-r--r--src/package.c114
-rw-r--r--src/transaction.c2
6 files changed, 64 insertions, 111 deletions
diff --git a/src/action.c b/src/action.c
index 57d7bec..cbeb993 100644
--- a/src/action.c
+++ b/src/action.c
@@ -45,9 +45,7 @@ void pkg_action_install(const rc_config *global_config,const pkg_action_args_t *
/* If so, parse it out and try to get that version only */
if( pkg_regex.reg_return == 0 ){
-
- char pkg_name[NAME_LEN];
- char pkg_version[VERSION_LEN];
+ char *pkg_name,*pkg_version;
if( (pkg_regex.pmatch[1].rm_eo - pkg_regex.pmatch[1].rm_so) > NAME_LEN ){
fprintf(stderr,"Package name exceeds NAME_LEN: %d\n",NAME_LEN);
@@ -58,21 +56,19 @@ void pkg_action_install(const rc_config *global_config,const pkg_action_args_t *
exit(1);
}
- strncpy(
- pkg_name,
+ pkg_name = strndup(
action_args->pkgs[i] + pkg_regex.pmatch[1].rm_so,
pkg_regex.pmatch[1].rm_eo - pkg_regex.pmatch[1].rm_so
);
- pkg_name[ pkg_regex.pmatch[1].rm_eo - pkg_regex.pmatch[1].rm_so ] = '\0';
- strncpy(
- pkg_version,
+ pkg_version = strndup(
action_args->pkgs[i] + pkg_regex.pmatch[2].rm_so,
pkg_regex.pmatch[2].rm_eo - pkg_regex.pmatch[2].rm_so
);
- pkg_version[ pkg_regex.pmatch[2].rm_eo - pkg_regex.pmatch[2].rm_so ] = '\0';
pkg = get_exact_pkg(avail_pkgs, pkg_name, pkg_version);
+ free(pkg_name);
+ free(pkg_version);
}
@@ -293,9 +289,7 @@ void pkg_action_show(const char *pkg_name){
/* If so, parse it out and try to get that version only */
if( pkg_regex.reg_return == 0 ){
-
- char p_name[NAME_LEN];
- char p_version[VERSION_LEN];
+ char *p_name,*p_version;
if( (pkg_regex.pmatch[1].rm_eo - pkg_regex.pmatch[1].rm_so) > NAME_LEN ){
fprintf(stderr,"Package name exceeds NAME_LEN: %d\n",NAME_LEN);
@@ -306,19 +300,19 @@ void pkg_action_show(const char *pkg_name){
exit(1);
}
- strncpy(p_name,
+ p_name = strndup(
pkg_name + pkg_regex.pmatch[1].rm_so,
pkg_regex.pmatch[1].rm_eo - pkg_regex.pmatch[1].rm_so
);
- p_name[ pkg_regex.pmatch[1].rm_eo - pkg_regex.pmatch[1].rm_so ] = '\0';
- strncpy(p_version,
+ p_version = strndup(
pkg_name + pkg_regex.pmatch[2].rm_so,
pkg_regex.pmatch[2].rm_eo - pkg_regex.pmatch[2].rm_so
);
- p_version[ pkg_regex.pmatch[2].rm_eo - pkg_regex.pmatch[2].rm_so ] = '\0';
pkg = get_exact_pkg(avail_pkgs, p_name, p_version);
+ free(p_name);
+ free(p_version);
}
@@ -384,7 +378,7 @@ void pkg_action_upgrade_all(const rc_config *global_config){
installed_pkgs->pkgs[i]->name
);
if( update_pkg != NULL ){
- int cmp_r;
+ int cmp_r = 0;
if( is_excluded(global_config,installed_pkgs->pkgs[i]) == 1 ){
add_exclude_to_transaction(&tran,update_pkg);
diff --git a/src/common.c b/src/common.c
index 2966f56..ac8299c 100644
--- a/src/common.c
+++ b/src/common.c
@@ -129,9 +129,10 @@ void create_dir_structure(const char *dir_name){
/* if no more directory delim, then this must be last dir */
if( strstr(dir_name + position,"/" ) == NULL ){
- dir_name_buffer = calloc( strlen(dir_name + position) + 1 , sizeof *dir_name_buffer );
- strncpy(dir_name_buffer,dir_name + position,strlen(dir_name + position));
- dir_name_buffer[ strlen(dir_name + position) ] = '\0';
+ dir_name_buffer = strndup(
+ dir_name + position,
+ strlen(dir_name + position)
+ );
if( strcmp(dir_name_buffer,".") != 0 ){
if( (mkdir(dir_name_buffer,0755)) == -1){
@@ -163,11 +164,10 @@ void create_dir_structure(const char *dir_name){
/* figure our dir name and mk it */
pointer = strchr(dir_name + position,'/');
- dir_name_buffer = calloc(
- strlen(dir_name + position) - strlen(pointer) + 1 , sizeof *dir_name_buffer
+ dir_name_buffer = strndup(
+ dir_name + position,
+ strlen(dir_name + position) - strlen(pointer)
);
- strncpy(dir_name_buffer,dir_name + position, strlen(dir_name + position) - strlen(pointer));
- dir_name_buffer[ (strlen(dir_name + position) - strlen(pointer)) ] = '\0';
if( strcmp(dir_name_buffer,".") != 0 ){
if( (mkdir(dir_name_buffer,0755)) == -1 ){
diff --git a/src/configuration.c b/src/configuration.c
index d4555a4..9c170fa 100644
--- a/src/configuration.c
+++ b/src/configuration.c
@@ -200,9 +200,9 @@ static struct exclude_list *parse_exclude(char *line){
realloc_tmp = realloc( list->excludes, sizeof *list->excludes * (list->count + 1) );
if( realloc_tmp != NULL ){
list->excludes = realloc_tmp;
- list->excludes[ list->count ] = calloc( strlen(pointer) + 1, sizeof *list->excludes[list->count] );
- strncpy(list->excludes[ list->count ], pointer, strlen(pointer) );
- list->excludes[ list->count ][strlen(pointer)] = '\0';
+ list->excludes[ list->count ] = strndup(
+ pointer, strlen(pointer)
+ );
++list->count;
}
@@ -215,15 +215,15 @@ static struct exclude_list *parse_exclude(char *line){
}else{
pointer = strchr(line + position,',');
- buffer = calloc( strlen(line + position) - strlen(pointer) + 1, sizeof *buffer );
- strncpy(buffer,line + position,strlen(line + position) - strlen(pointer) );
- buffer[ strlen(line + position) - strlen(pointer) ] = '\0';
+ buffer = strndup(
+ line + position,
+ strlen(line + position) - strlen(pointer)
+ );
realloc_tmp = realloc( list->excludes, sizeof *list->excludes * (list->count + 1) );
if( realloc_tmp != NULL ){
list->excludes = realloc_tmp;
list->excludes[ list->count ] = buffer;
- list->excludes[ list->count ][strlen(buffer)] = '\0';
}
++list->count;
diff --git a/src/curl.c b/src/curl.c
index f61a748..f5db6da 100644
--- a/src/curl.c
+++ b/src/curl.c
@@ -86,6 +86,7 @@ static size_t write_header_callback(void *buffer, size_t size, size_t nmemb, voi
head_t->data = tmp;
memcpy(&(head_t->data[head_t->size]),buffer,a_size);
head_t->size += a_size;
+ head_t->data[head_t->size] = '\0';
}
return nmemb;
@@ -96,7 +97,7 @@ char *head_request(const char *url){
CURLcode response;
struct head_data_t head_t;
- head_t.data = NULL;
+ head_t.data = malloc( sizeof *head_t.data );
head_t.size = 0;
ch = curl_easy_init();
diff --git a/src/package.c b/src/package.c
index cc1b92e..827e112 100644
--- a/src/package.c
+++ b/src/package.c
@@ -234,19 +234,10 @@ struct pkg_list *parse_packages_txt(FILE *pkg_list_fh){
execute_regex(&size_c_regex,getline_buffer);
if( size_c_regex.reg_return == 0 ){
- size_c = calloc(
- (size_c_regex.pmatch[1].rm_eo - size_c_regex.pmatch[1].rm_so) + 1 , sizeof *size_c
- );
- if( size_c == NULL ){
- fprintf(stderr,_("Failed to calloc %s\n"),"size_c");
- exit(1);
- }
- strncpy(
- size_c,
+ size_c = strndup(
getline_buffer + size_c_regex.pmatch[1].rm_so,
(size_c_regex.pmatch[1].rm_eo - size_c_regex.pmatch[1].rm_so)
);
- size_c[ (size_c_regex.pmatch[1].rm_eo - size_c_regex.pmatch[1].rm_so) ] = '\0';
tmp_pkg->size_c = strtol(size_c, (char **)NULL, 10);
free(size_c);
}else{
@@ -268,21 +259,10 @@ struct pkg_list *parse_packages_txt(FILE *pkg_list_fh){
execute_regex(&size_u_regex,getline_buffer);
if( size_u_regex.reg_return == 0 ){
-
- size_u = calloc(
- (size_u_regex.pmatch[1].rm_eo - size_u_regex.pmatch[1].rm_so) + 1 ,
- sizeof *size_u
- );
-
- if( size_u == NULL ){
- fprintf(stderr,_("Failed to calloc %s\n"),"size_u");
- exit(1);
- }
- strncpy(
- size_u,getline_buffer + size_u_regex.pmatch[1].rm_so,
+ size_u = strndup(
+ getline_buffer + size_u_regex.pmatch[1].rm_so,
(size_u_regex.pmatch[1].rm_eo - size_u_regex.pmatch[1].rm_so)
);
- size_u[ (size_u_regex.pmatch[1].rm_eo - size_u_regex.pmatch[1].rm_so) ] = '\0';
tmp_pkg->size_u = strtol(size_u, (char **)NULL, 10);
free(size_u);
}else{
@@ -438,18 +418,10 @@ char *gen_short_pkg_description(pkg_info_t *pkg){
/* quit now if the description is going to be empty */
if( (int)string_size < 0 ) return NULL;
- short_description = calloc( string_size + 1 , sizeof *short_description );
- if( short_description == NULL ){
- fprintf(stderr,_("Failed to calloc %s\n"),"short_description");
- exit(1);
- }
-
- strncpy(
- short_description,
+ short_description = strndup(
pkg->description + (strlen(pkg->name) + 2),
string_size
);
- short_description[ string_size ] = '\0';
return short_description;
}
@@ -542,33 +514,17 @@ struct pkg_list *get_installed_pkgs(void){
/* ignore unless we matched */
if( compressed_size_reg.reg_return == 0 ){
- char *size_c;
- size_c = calloc( compressed_size_reg.pmatch[1].rm_eo - compressed_size_reg.pmatch[1].rm_so + 1, sizeof *size_c);
- if( size_c == NULL ){
- fprintf(stderr,_("Failed to calloc %s\n"),"size_c");
- exit(1);
- }
-
- strncpy(size_c,
+ char *size_c = strndup(
getline_buffer + compressed_size_reg.pmatch[1].rm_so,
(compressed_size_reg.pmatch[1].rm_eo - compressed_size_reg.pmatch[1].rm_so)
);
-
tmp_pkg->size_c = strtol(size_c,(char **)NULL,10);
free(size_c);
}else if(uncompressed_size_reg.reg_return == 0 ){
- char *size_u;
- size_u = calloc( uncompressed_size_reg.pmatch[1].rm_eo - uncompressed_size_reg.pmatch[1].rm_so + 1, sizeof *size_u);
- if( size_u == NULL ){
- fprintf(stderr,_("Failed to calloc %s\n"),"size_u");
- exit(1);
- }
-
- strncpy(size_u,
+ char *size_u = strndup(
getline_buffer + uncompressed_size_reg.pmatch[1].rm_so,
(uncompressed_size_reg.pmatch[1].rm_eo - uncompressed_size_reg.pmatch[1].rm_so)
);
-
tmp_pkg->size_u = strtol(size_u,(char **)NULL,10);
free(size_u);
}else{
@@ -707,7 +663,7 @@ void free_pkg_list(struct pkg_list *list){
int is_excluded(const rc_config *global_config,pkg_info_t *pkg){
int i,pkg_not_excluded = 0, pkg_is_excluded = 1;
- int name_reg_ret,version_reg_ret;
+ int name_reg_ret = -1,version_reg_ret = -1;
sg_regex exclude_reg;
if( global_config->ignore_excludes == 1 )
@@ -1035,7 +991,7 @@ void write_pkg_data(const char *source_url,FILE *d_file,struct pkg_list *pkgs){
struct pkg_list *search_pkg_list(struct pkg_list *available,const char *pattern){
int i;
- int name_r,desc_r,loc_r,version_r;
+ int name_r = -1,desc_r = -1,loc_r = -1,version_r = -1;
sg_regex search_regex;
struct pkg_list *matches;
@@ -1122,9 +1078,10 @@ int get_pkg_dependencies(const rc_config *global_config,struct pkg_list *avail_p
/* build the buffer to contain the dep entry */
pointer = strchr(pkg->required + position,',');
- buffer = calloc(strlen(pkg->required + position) - strlen(pointer) +1, sizeof *buffer);
- strncpy(buffer,pkg->required + position, strlen(pkg->required + position) - strlen(pointer));
- buffer[ strlen(pkg->required + position) - strlen(pointer) ] = '\0';
+ buffer = strndup(
+ pkg->required + position,
+ strlen(pkg->required + position) - strlen(pointer)
+ );
/* parse the dep entry and try to lookup a package */
if( strchr(buffer,'|') != NULL ){
@@ -1221,9 +1178,10 @@ struct pkg_list *get_pkg_conflicts(struct pkg_list *avail_pkgs,struct pkg_list *
/* build the buffer to contain the conflict entry */
pointer = strchr(pkg->conflicts + position,',');
- buffer = calloc(strlen(pkg->conflicts + position) - strlen(pointer) +1, sizeof *buffer);
- strncpy(buffer,pkg->conflicts + position, strlen(pkg->conflicts + position) - strlen(pointer));
- buffer[ strlen(pkg->conflicts + position) - strlen(pointer) ] = '\0';
+ buffer = strndup(
+ pkg->conflicts + position,
+ strlen(pkg->conflicts + position) - strlen(pointer)
+ );
/* parse the conflict entry and try to lookup a package */
tmp_pkg = parse_meta_entry(avail_pkgs,installed_pkgs,buffer);
@@ -1484,12 +1442,16 @@ pkg_info_t *get_pkg_by_details(struct pkg_list *list,char *name,char *version,ch
/* do a head request on the mirror data to find out if it's new */
static char *head_mirror_data(const char *wurl,const char *file){
- char *request_header,*request_header_ptr,*delim_ptr,*head_data;
- int request_header_len;
+ char *request_header = NULL;
+ char *request_header_ptr = NULL;
+ char *delim_ptr = NULL;
+ char *head_data = NULL;
+ int request_header_len = 0;
char *url;
/* build url */
url = calloc( strlen(wurl) + strlen(file) + 2, sizeof *url );
+ url[0] = '\0';
strncat(url,wurl,strlen(wurl));
strncat(url,"/",1);
strncat(url,file,strlen(file));
@@ -1516,7 +1478,9 @@ static char *head_mirror_data(const char *wurl,const char *file){
free(head_data);
return NULL;
}
- request_header_len = strlen(request_header_ptr) - strlen(delim_ptr);
+ request_header_len =
+ (request_header_ptr == NULL) ? 0 : strlen(request_header_ptr) -
+ (delim_ptr == NULL) ? 0 : strlen(delim_ptr);
request_header = calloc( request_header_len + 1, sizeof *request_header );
memcpy(request_header,request_header_ptr,request_header_len);
@@ -1590,10 +1554,14 @@ static void clear_head_cache(const char *cache_filename){
/* update package data from mirror url */
int update_pkg_cache(const rc_config *global_config){
int i,source_dl_failed = 0;
- FILE *pkg_list_fh_tmp;
+ FILE *pkg_list_fh_tmp = NULL;
/* open tmp pkg list file */
pkg_list_fh_tmp = tmpfile();
+ if( pkg_list_fh_tmp == NULL ){
+ if( errno ) perror("tmpfile");
+ exit(1);
+ }
/* go through each package source and download the meta data */
for(i = 0; i < global_config->sources.count; i++){
@@ -2031,9 +1999,6 @@ void purge_old_cached_pkgs(const rc_config *global_config,char *dir_name,struct
char *tmp_pkg_name,*tmp_pkg_version;
pkg_info_t *tmp_pkg;
- tmp_pkg_name = malloc( sizeof *tmp_pkg_name * NAME_LEN + 1);
- tmp_pkg_version = malloc( sizeof *tmp_pkg_version * VERSION_LEN + 1);
-
if((cached_pkgs_regex.pmatch[1].rm_eo - cached_pkgs_regex.pmatch[1].rm_so) >
NAME_LEN ){
fprintf(stderr,"cached package name too long\n");
@@ -2045,22 +2010,14 @@ void purge_old_cached_pkgs(const rc_config *global_config,char *dir_name,struct
exit(1);
}
- strncpy(
- tmp_pkg_name,
+ tmp_pkg_name = strndup(
file->d_name + cached_pkgs_regex.pmatch[1].rm_so,
cached_pkgs_regex.pmatch[1].rm_eo - cached_pkgs_regex.pmatch[1].rm_so
);
- tmp_pkg_name[
- cached_pkgs_regex.pmatch[1].rm_eo - cached_pkgs_regex.pmatch[1].rm_so
- ] = '\0';
- strncpy(
- tmp_pkg_version,
+ tmp_pkg_version = strndup(
file->d_name + cached_pkgs_regex.pmatch[2].rm_so,
cached_pkgs_regex.pmatch[2].rm_eo - cached_pkgs_regex.pmatch[2].rm_so
);
- tmp_pkg_version[
- cached_pkgs_regex.pmatch[2].rm_eo - cached_pkgs_regex.pmatch[2].rm_so
- ] = '\0';
tmp_pkg = get_exact_pkg(avail_pkgs,tmp_pkg_name,tmp_pkg_version);
if( tmp_pkg == NULL ){
@@ -2156,9 +2113,10 @@ static pkg_info_t *find_or_requirement(struct pkg_list *avail_pkgs,struct pkg_li
next_token = index(required_str + position,'|');
str_len = strlen(required_str + position) - strlen(next_token);
- string = malloc( sizeof *string * str_len + 1 );
- strncpy(string,required_str + position, str_len);
- string[str_len] = '\0';
+ string = strndup(
+ required_str + position,
+ str_len
+ );
pkg = parse_meta_entry(avail_pkgs,installed_pkgs,string);
free(string);
diff --git a/src/transaction.c b/src/transaction.c
index c39db14..85332d5 100644
--- a/src/transaction.c
+++ b/src/transaction.c
@@ -475,7 +475,7 @@ transaction *remove_from_transaction(transaction *tran,pkg_info_t *pkg){
/* parse the dependencies for a package, and add them to the transaction as needed */
/* check to see if a package is conflicted */
int add_deps_to_trans(const rc_config *global_config, transaction *tran, struct pkg_list *avail_pkgs, struct pkg_list *installed_pkgs, pkg_info_t *pkg){
- int c,dep_return;
+ int c,dep_return = -1;
struct pkg_list *deps;
if( global_config->disable_dep_check == 1) return -1;