Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.

Commit 2988f5a

Browse files
author
Stephen Mathieson
committed
src: Refactor to use littlstar/asprintf.c
significant code reduction and cleanup. improves memory cleanup slightly (rel. #1).
1 parent dc8f3b0 commit 2988f5a

File tree

5 files changed

+153
-54
lines changed

5 files changed

+153
-54
lines changed

deps/asprintf/asprintf.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/**
3+
* `asprintf.c' - asprintf
4+
*
5+
* copyright (c) 2014 joseph werle <[email protected]>
6+
*/
7+
8+
#ifndef HAVE_ASPRINTF
9+
10+
#include <stdlib.h>
11+
#include <stdio.h>
12+
#include <stdarg.h>
13+
14+
#include "asprintf.h"
15+
16+
int
17+
asprintf (char **str, const char *fmt, ...) {
18+
int size = 0;
19+
va_list args;
20+
21+
// init variadic argumens
22+
va_start(args, fmt);
23+
24+
// format and get size
25+
size = vasprintf(str, fmt, args);
26+
27+
// toss args
28+
va_end(args);
29+
30+
return size;
31+
}
32+
33+
int
34+
vasprintf (char **str, const char *fmt, va_list args) {
35+
int size = 0;
36+
va_list tmpa;
37+
38+
// copy
39+
va_copy(tmpa, args);
40+
41+
// apply variadic arguments to
42+
// sprintf with format to get size
43+
size = vsnprintf(NULL, size, fmt, tmpa);
44+
45+
// toss args
46+
va_end(tmpa);
47+
48+
// return -1 to be compliant if
49+
// size is less than 0
50+
if (size < 0) { return -1; }
51+
52+
// alloc with size plus 1 for `\0'
53+
*str = (char *) malloc(size + 1);
54+
55+
// return -1 to be compliant
56+
// if pointer is `NULL'
57+
if (NULL == *str) { return -1; }
58+
59+
// format string with original
60+
// variadic arguments and set new size
61+
size = vsprintf(*str, fmt, args);
62+
return size;
63+
}
64+
65+
#endif

deps/asprintf/asprintf.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
/**
3+
* `asprintf.h' - asprintf.c
4+
*
5+
* copyright (c) 2014 joseph werle <[email protected]>
6+
*/
7+
8+
#ifndef HAVE_ASPRINTF
9+
#ifndef ASPRINTF_H
10+
#define ASPRINTF_H 1
11+
12+
#include <stdarg.h>
13+
14+
/**
15+
* Sets `char **' pointer to be a buffer
16+
* large enough to hold the formatted string
17+
* accepting a `va_list' args of variadic
18+
* arguments.
19+
*/
20+
21+
int
22+
vasprintf (char **, const char *, va_list);
23+
24+
/**
25+
* Sets `char **' pointer to be a buffer
26+
* large enough to hold the formatted
27+
* string accepting `n' arguments of
28+
* variadic arguments.
29+
*/
30+
31+
int
32+
asprintf (char **, const char *, ...);
33+
34+
#endif
35+
#endif

deps/asprintf/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "asprintf",
3+
"version": "0.0.3",
4+
"repo": "littlstar/asprintf.c",
5+
"description": "asprintf() implementation",
6+
"keywords": ["asprintf", "sprintf", "alloc", "string"],
7+
"src": ["asprintf.h", "asprintf.c"]
8+
}
9+

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"Constellation/console-colors.c": "1.0.0",
1212
"strdup": "*",
1313
"logger": "*",
14+
"littlstar/asprintf.c": "0.0.3",
1415
"stephenmathieson/substr.c": "0.1.2",
1516
"stephenmathieson/parse-repo.c": "1.1.1"
1617
}

src/clib-uninstall.c

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,106 +11,95 @@
1111
#include "http-get/http-get.h"
1212
#include "parson/parson.h"
1313
#include "logger/logger.h"
14+
#include "asprintf/asprintf.h"
1415
#include "clib-uninstall.h"
1516

17+
1618
static char *
1719
get_tarball_url(const char *owner, const char *name, const char *version) {
18-
size_t size = 19 // https://github.com/
19-
+ strlen(owner)
20-
+ 1 // /
21-
+ strlen(name)
22-
+ 9 // /archive/
23-
+ strlen(version)
24-
+ 7; // .tar.gz
25-
char *tarball = malloc(size * sizeof(char *));
26-
if (tarball) {
27-
sprintf(tarball
20+
char *tarball = NULL;
21+
int size = 0;
22+
23+
size = asprintf(
24+
&tarball
2825
, "https://github.com/%s/%s/archive/%s.tar.gz"
2926
, owner
3027
, name
31-
, version);
32-
}
28+
, version
29+
);
30+
31+
if (-1 == size) return NULL;
3332
return tarball;
3433
}
3534

3635
static char *
3736
get_tar_filepath(const char *name, const char *version) {
38-
size_t size = 1 // null
39-
+ strlen(name)
40-
+ 1 // -
41-
+ strlen(version)
42-
+ 7; // .tar.gz
43-
char *file = malloc(size * sizeof(char *));
44-
if (file) sprintf(file, "%s-%s.tar.gz", name, version);
37+
char *file = NULL;
38+
int size = asprintf(&file, "%s-%s.tar.gz", name, version);
39+
if (-1 == size) return NULL;
4540
return file;
4641
}
4742

4843
static char *
4944
get_tmp_tarball(const char *file) {
50-
size_t size = 5 // /tmp/ + null
51-
+ strlen(file);
52-
char *tmp = malloc(size * sizeof(char *));
53-
if (tmp) sprintf(tmp, "/tmp/%s", file);
45+
char *tmp = NULL;
46+
int size = asprintf(&tmp, "/tmp/%s", file);
47+
if (-1 == size) return NULL;
5448
return tmp;
5549
}
5650

5751
static char *
5852
get_untar_command(const char *file) {
59-
size_t size = 19 // cd /tmp && tar -xf
60-
+ strlen(file);
61-
char *command = malloc(size * sizeof(char *));
62-
if (command) sprintf(command, "cd /tmp && tar -xf %s", file);
63-
return command;
53+
char *cmd = NULL;
54+
int size = 0;
55+
size = asprintf(&cmd, "cd /tmp && tar -xf %s", file);
56+
if (-1 == size) return NULL;
57+
return cmd;
6458
}
6559

6660
static char *
6761
get_uninstall_target(const char *name, const char *version) {
68-
size_t size = 5 // /tmp/
69-
+ strlen(name)
70-
+ 1 // /
71-
+ strlen(version);
62+
int size = 0;
7263
char *target = NULL;
73-
char *dir = malloc(size * sizeof(char *));
74-
char *package_json = malloc((size + 13) * sizeof(char));
64+
char *dir = NULL;
65+
char *pkg = NULL;
66+
const char *val = NULL;
7567
JSON_Value *root = NULL;
76-
JSON_Object *object = NULL;
68+
JSON_Object *obj = NULL;
7769

78-
if (!dir || !package_json) goto cleanup;
70+
size = asprintf(&dir, "/tmp/%s-%s", name, version);
71+
if (-1 == size) return NULL;
7972

80-
sprintf(dir, "/tmp/%s-%s", name, version);
81-
sprintf(package_json, "%s/package.json", dir);
73+
size = asprintf(&pkg, "%s/package.json", dir);
74+
if (-1 == size) goto done;
8275

83-
root = json_parse_file(package_json);
84-
if (!root) goto cleanup;
76+
root = json_parse_file(pkg);
77+
if (!root) goto done;
8578

86-
object = json_value_get_object(root);
87-
if (!object) goto cleanup;
79+
obj = json_value_get_object(root);
80+
if (!obj) goto done;
8881

89-
const char *value = json_object_get_string(object, "uninstall");
90-
if (!value) {
82+
val = json_object_get_string(obj, "uninstall");
83+
if (!val) {
9184
logger_warn(
9285
"warning"
9386
, "No uninstall target specified. Defaulting to '%s'."
9487
, CLIB_UNINSTALL_DEFAULT_TARGET
9588
);
9689
// default to "make uninstall"
97-
value = CLIB_UNINSTALL_DEFAULT_TARGET;
90+
val = CLIB_UNINSTALL_DEFAULT_TARGET;
9891
}
9992

100-
size = 3 // 'cd '
101-
+ strlen(dir)
102-
+ 4 // ' && '
103-
+ strlen(value);
104-
target = malloc(size * sizeof(char *));
105-
if (target) sprintf(target, "cd %s && %s", dir, value);
93+
size = asprintf(&target, "cd %s && %s", dir, val);
10694

107-
cleanup:
95+
done:
10896
if (root) json_value_free(root);
109-
if (dir) free(dir);
110-
if (package_json) free(package_json);
97+
free(dir);
98+
free(pkg);
11199
return target;
112100
}
113101

102+
114103
int
115104
clib_uninstall(const char *owner, const char *name, const char *version) {
116105
char *tarball = NULL;

0 commit comments

Comments
 (0)