mirror of
https://github.com/swaywm/sway.git
synced 2025-10-31 22:25:26 -04:00
Parse command line args for swaymsg
This commit is contained in:
parent
d69cbeabc0
commit
9a15371ba3
3 changed files with 92 additions and 2 deletions
39
common/readline.c
Normal file
39
common/readline.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include "readline.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char *read_line(FILE *file) {
|
||||
int length = 0, size = 128;
|
||||
char *string = malloc(size);
|
||||
if (!string) {
|
||||
return NULL;
|
||||
}
|
||||
while (1) {
|
||||
int c = getc(file);
|
||||
if (c == EOF || c == '\n' || c == '\0') {
|
||||
break;
|
||||
}
|
||||
if (c == '\r') {
|
||||
continue;
|
||||
}
|
||||
if (length == size) {
|
||||
char *new_string = realloc(string, size *= 2);
|
||||
if (!new_string) {
|
||||
free(string);
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue