aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authoremersion <contact@emersion.fr>2019-01-08 10:05:37 +0100
committerGitHub <noreply@github.com>2019-01-08 10:05:37 +0100
commit140bc2dd5b81205df58bf06e695788e689fae397 (patch)
tree6a88913630734736763b12ec0b10da68ef413256 /common
parent353d9ed74f3560e12500c08920cf8a1ca3344cc2 (diff)
parent5bef06adfdac4ce9940dcaf2d90a4bdffae7bba9 (diff)
Merge pull request #3275 from ianyfan/remove-readline
Rewrite strip_whitespace and remove readline.c
Diffstat (limited to 'common')
-rw-r--r--common/ipc-client.c11
-rw-r--r--common/meson.build1
-rw-r--r--common/readline.c72
-rw-r--r--common/stringop.c25
-rw-r--r--common/util.c4
5 files changed, 17 insertions, 96 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c
index 496fd131..3515ef0a 100644
--- a/common/ipc-client.c
+++ b/common/ipc-client.c
@@ -7,7 +7,6 @@
#include <sys/un.h>
#include <unistd.h>
#include "ipc-client.h"
-#include "readline.h"
#include "log.h"
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
@@ -18,28 +17,30 @@ char *get_socketpath(void) {
if (swaysock) {
return strdup(swaysock);
}
+ char *line = NULL;
+ size_t line_size = 0;
FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r");
if (fp) {
- char *line = read_line(fp);
+ getline(&line, &line_size, fp);
pclose(fp);
if (line && *line) {
return line;
}
- free(line);
}
const char *i3sock = getenv("I3SOCK");
if (i3sock) {
+ free(line);
return strdup(i3sock);
}
fp = popen("i3 --get-socketpath 2>/dev/null", "r");
if (fp) {
- char *line = read_line(fp);
+ getline(&line, &line_size, fp);
pclose(fp);
if (line && *line) {
return line;
}
- free(line);
}
+ free(line);
return NULL;
}
diff --git a/common/meson.build b/common/meson.build
index 224a9c3f..4ad872d1 100644
--- a/common/meson.build
+++ b/common/meson.build
@@ -8,7 +8,6 @@ lib_sway_common = static_library(
'loop.c',
'list.c',
'pango.c',
- 'readline.c',
'stringop.c',
'unicode.c',
'util.c'
diff --git a/common/readline.c b/common/readline.c
deleted file mode 100644
index 58652429..00000000
--- a/common/readline.c
+++ /dev/null
@@ -1,72 +0,0 @@
-#define _POSIX_C_SOURCE 200809L
-#include "readline.h"
-#include "log.h"
-#include <stdlib.h>
-#include <stdio.h>
-
-char *read_line(FILE *file) {
- size_t length = 0, size = 128;
- char *string = malloc(size);
- char lastChar = '\0';
- if (!string) {
- wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
- return NULL;
- }
- while (1) {
- int c = getc(file);
- if (c == '\n' && lastChar == '\\'){
- --length; // Ignore last character.
- lastChar = '\0';
- continue;
- }
- if (c == EOF || c == '\n' || c == '\0') {
- break;
- }
- if (c == '\r') {
- continue;
- }
- lastChar = c;
- if (length == size) {
- char *new_string = realloc(string, size *= 2);
- if (!new_string) {
- free(string);
- wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
- return NULL;
- }
- string = new_string;
- }
- string[length++] = c;
- }
- if (length + 1 == size) {
- char *new_string = realloc(string, length + 1);
- if (!new_string) {
- free(string);
- return NULL;
- }
- string = new_string;
- }
- string[length] = '\0';
- return string;
-}
-
-char *peek_line(FILE *file, int line_offset, long *position) {
- long pos = ftell(file);
- size_t length = 0;
- char *line = NULL;
- for (int i = 0; i <= line_offset; i++) {
- ssize_t read = getline(&line, &length, file);
- if (read < 0) {
- free(line);
- line = NULL;
- break;
- }
- if (read > 0 && line[read - 1] == '\n') {
- line[read - 1] = '\0';
- }
- }
- if (position) {
- *position = ftell(file);
- }
- fseek(file, pos, SEEK_SET);
- return line;
-}
diff --git a/common/stringop.c b/common/stringop.c
index 4b8c9a38..8af0d60f 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -9,24 +9,17 @@
#include "string.h"
#include "list.h"
-const char whitespace[] = " \f\n\r\t\v";
+static const char whitespace[] = " \f\n\r\t\v";
-char *strip_whitespace(char *_str) {
- if (*_str == '\0')
- return _str;
- char *strold = _str;
- while (*_str == ' ' || *_str == '\t') {
- _str++;
+void strip_whitespace(char *str) {
+ size_t len = strlen(str);
+ size_t start = strspn(str, whitespace);
+ memmove(str, &str[start], len + 1 - start);
+
+ if (*str) {
+ for (len -= start + 1; isspace(str[len]); --len) {}
+ str[len + 1] = '\0';
}
- char *str = strdup(_str);
- free(strold);
- int i;
- for (i = 0; str[i] != '\0'; ++i);
- do {
- i--;
- } while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
- str[i + 1] = '\0';
- return str;
}
void strip_quotes(char *str) {
diff --git a/common/util.c b/common/util.c
index 40c64230..d66058a6 100644
--- a/common/util.c
+++ b/common/util.c
@@ -13,7 +13,6 @@
#include <xkbcommon/xkbcommon-names.h>
#include <wlr/types/wlr_keyboard.h>
#include "log.h"
-#include "readline.h"
#include "util.h"
int wrap(int i, int max) {
@@ -87,11 +86,12 @@ pid_t get_parent_pid(pid_t child) {
char *token = NULL;
const char *sep = " ";
FILE *stat = NULL;
+ size_t buf_size = 0;
sprintf(file_name, "/proc/%d/stat", child);
if ((stat = fopen(file_name, "r"))) {
- if ((buffer = read_line(stat))) {
+ if (getline(&buffer, &buf_size, stat) != -1) {
token = strtok(buffer, sep); // pid
token = strtok(NULL, sep); // executable name
token = strtok(NULL, sep); // state