Skip to content

Commit

Permalink
Merge pull request #868 from kernelkit/remove-duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
troglobit authored Dec 12, 2024
2 parents 76ab0fe + f06a429 commit 6d6f4e6
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 217 deletions.
2 changes: 1 addition & 1 deletion src/confd/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ PKG_PROG_PKG_CONFIG
PKG_CHECK_MODULES([crypt], [libxcrypt >= 4.4.27])
PKG_CHECK_MODULES([glib], [glib-2.0 >= 2.50 gio-2.0 gio-unix-2.0])
PKG_CHECK_MODULES([jansson], [jansson >= 2.0.0])
PKG_CHECK_MODULES([libite], [libite >= 2.5.0])
PKG_CHECK_MODULES([libite], [libite >= 2.6.1])
PKG_CHECK_MODULES([sysrepo], [sysrepo >= 2.2.36])
PKG_CHECK_MODULES([libsrx], [libsrx >= 1.0.0])

Expand Down
195 changes: 0 additions & 195 deletions src/libsrx/src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,201 +20,6 @@ int debug; /* Sets debug level (0:off) */
int vasprintf(char **strp, const char *fmt, va_list ap);
#endif

/*
* Run cmd in background after delay microseconds.
*/
int runbg(char *const args[], int delay)
{
int pid = fork();

if (!pid) {
usleep(delay * 300);
_exit(execvp(args[0], args));
}

return pid;
}

/*
* Reap forked child from runbg()
*/
int run_status(int pid)
{
int rc;

/* WNOHANG */
if (waitpid(pid, &rc, 0) == -1)
return -1;

if (WIFEXITED(rc)) {
errno = 0;
rc = WEXITSTATUS(rc);
} else if (WIFSIGNALED(rc)) {
errno = EINTR;
rc = -1;
}

return rc;
}

int fexistf(const char *fmt, ...)
{
va_list ap;
char *file;
int len;

va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);

file = alloca(len + 1);
if (!file) {
errno = ENOMEM;
return -1;
}

va_start(ap, fmt);
vsnprintf(file, len + 1, fmt, ap);
va_end(ap);

return fexist(file);
}

FILE *popenf(const char *type, const char *cmdf, ...)
{
va_list ap;
char *cmd;
FILE *fp;
int len;

va_start(ap, cmdf);
len = vasprintf(&cmd, cmdf, ap);
va_end(ap);

if (len < 0) {
errno = ENOMEM;
return NULL;
}

fp = popen(cmd, type);
free(cmd);
return fp;
}

/* XXX: -lite v2.6.0 has vfopenf() to replace this. */
static FILE *open_file(const char *mode, const char *fmt, va_list ap)
{
va_list apc;
char *file;
int len;

va_copy(apc, ap);
len = vsnprintf(NULL, 0, fmt, apc);
va_end(apc);

file = alloca(len + 1);
if (!file) {
errno = ENOMEM;
return NULL;
}

va_copy(apc, ap);
vsnprintf(file, len + 1, fmt, apc);
va_end(apc);

return fopen(file, mode);
}

int vreadllf(long long *value, const char *fmt, va_list ap)
{
char line[0x100];
FILE *fp;

fp = open_file("r", fmt, ap);
if (!fp)
return -1;

if (!fgets(line, sizeof(line), fp)) {
fclose(fp);
return -1;
}

fclose(fp);

errno = 0;
*value = strtoll(line, NULL, 0);

return errno ? -1 : 0;
}

int readllf(long long *value, const char *fmt, ...)
{
va_list ap;
int rc;

va_start(ap, fmt);
rc = vreadllf(value, fmt, ap);
va_end(ap);

return rc;
}

int readdf(int *value, const char *fmt, ...)
{
long long tmp;
va_list ap;
int rc;

va_start(ap, fmt);
rc = vreadllf(&tmp, fmt, ap);
va_end(ap);

if (rc)
return rc;

if (tmp < INT_MIN || tmp > INT_MAX)
return -1;

*value = tmp;
return 0;
}

/*
* Write interger value to a file composed from fmt and optional args.
*/
int writedf(int value, const char *mode, const char *fmt, ...)
{
va_list ap;
FILE *fp;

va_start(ap, fmt);
fp = open_file(mode, fmt, ap);
va_end(ap);
if (!fp)
return -1;

fprintf(fp, "%d\n", value);
return fclose(fp);
}

/*
* Write str to a file composed from fmt and optional args.
*/
int writesf(const char *str, const char *mode, const char *fmt, ...)
{
va_list ap;
FILE *fp;

va_start(ap, fmt);
fp = open_file(mode, fmt, ap);
va_end(ap);
if (!fp)
return -1;

fprintf(fp, "%s\n", str);
return fclose(fp);
}

char *unquote(char *buf)
{
char q = buf[0];
Expand Down
19 changes: 0 additions & 19 deletions src/libsrx/src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,6 @@

int vasprintf(char **strp, const char *fmt, va_list ap);

int runbg(char *const args[], int delay);
int run_status(int pid);

int fexistf(const char *fmt, ...)
__attribute__ ((format (printf, 1, 2)));
FILE *popenf(const char *type, const char *cmdf, ...)
__attribute__ ((format (printf, 2, 3)));

int vreadllf(long long *value, const char *fmt, va_list ap);
int readllf(long long *value, const char *fmt, ...)
__attribute__ ((format (printf, 2, 3)));
int readdf(int *value, const char *fmt, ...)
__attribute__ ((format (printf, 2, 3)));

int writedf(int value, const char *mode, const char *fmt, ...)
__attribute__ ((format (printf, 3, 4)));
int writesf(const char *str, const char *mode, const char *fmt, ...)
__attribute__ ((format (printf, 3, 4)));

char *unquote(char *buf);
char *fgetkey(const char *file, const char *key);

Expand Down
2 changes: 1 addition & 1 deletion src/statd/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ AM_CONDITIONAL(CONTAINERS, [test "x$enable_containers" != "xno"])
PKG_PROG_PKG_CONFIG

PKG_CHECK_MODULES([jansson], [jansson >= 2.0.0])
PKG_CHECK_MODULES([libite], [libite >= 2.5.0])
PKG_CHECK_MODULES([libite], [libite >= 2.6.1])
PKG_CHECK_MODULES([libyang], [libyang >= 2.1.80])
PKG_CHECK_MODULES([sysrepo], [sysrepo >= 2.2.36])
PKG_CHECK_MODULES([libsrx], [libsrx >= 1.0.0])
Expand Down
2 changes: 1 addition & 1 deletion src/statd/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <stdlib.h>
#include <jansson.h>
#include <net/if.h>
#include <libite/lite.h>

#include <srx/common.h>
#include <srx/helpers.h>

json_t *json_get_output(const char *cmd)
{
Expand Down

0 comments on commit 6d6f4e6

Please sign in to comment.