Skip to content

Commit b4eb524

Browse files
committed
Some more string utils and doc updates
1 parent faa2a4a commit b4eb524

File tree

6 files changed

+135
-23
lines changed

6 files changed

+135
-23
lines changed

src/procutils.c

+36-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <unistd.h>
10+
#include <fcntl.h>
11+
712
#include "procutils.h"
813

914
int read_pid(const char *file, int *pid)
@@ -44,21 +49,40 @@ int write_pid(const char *file)
4449
return 0;
4550
}
4651

47-
int redirect_output_to_log_file(const char *file)
52+
int o_redirect(int mode, const char *file)
4853
{
49-
int log_file = open(file, O_RDWR | O_CREAT | O_APPEND, 0600);
54+
int fd = -1;
55+
int has_err = 0;
5056

51-
if (log_file == -1) {
52-
perror("opening log_file");
53-
return -1;
57+
if (file != NULL) {
58+
fd = open(file, O_RDWR | O_CREAT | O_APPEND, 0644);
59+
if (fd == -1) {
60+
perror("opening log_file");
61+
return -1;
62+
}
5463
}
55-
if (dup2(log_file, fileno(stdout)) == -1) {
56-
perror("cannot redirect stdout to log_file");
57-
return -1;
64+
65+
if (mode & 0x01) {
66+
if (fd != -1) {
67+
if (dup2(fd, fileno(stdout)) == -1) {
68+
perror("cannot redirect stdout to log_file");
69+
has_err = -1;
70+
}
71+
} else {
72+
close(fileno(stdout));
73+
}
5874
}
59-
if (dup2(log_file, fileno(stderr)) == -1) {
60-
perror("cannot redirect stderr to log_file");
61-
return -1;
75+
76+
if (mode & 0x02) {
77+
if (fd != -1) {
78+
if (dup2(fd, fileno(stderr)) == -1) {
79+
perror("cannot redirect stderr to log_file");
80+
has_err = -1;
81+
}
82+
} else {
83+
close(fileno(stderr));
84+
}
6285
}
63-
return 0;
86+
87+
return has_err;
6488
}

src/procutils.h

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
#ifndef _UTIL_PROCUTILS_H_
22
#define _UTIL_PROCUTILS_H_
33

4-
#include <stdio.h>
5-
#include <stdlib.h>
6-
#include <unistd.h>
7-
#include <fcntl.h>
4+
/*
5+
* read_pid() - Reads PID from `file`. The file's content must just be
6+
* be PID number, as written by a call to write_pid(). On success, copies
7+
* the PID read from file into `pid`.
8+
*
9+
* Returns:
10+
* 0 - Success
11+
* -1 - Failure
12+
*/
13+
int read_pid(const char *file, int *pid);
814

15+
/*
16+
* write_pid() - Writes the calling process's PID to the file specified.
17+
*
18+
* Returns:
19+
* 0 - Success
20+
* -1 - Failure
21+
*/
22+
int write_pid(const char *file);
923

24+
/*
25+
* o_redirect() - Redirects stdout/stderr to file specified.
26+
*
27+
* Params:
28+
* mode - Bit mask.
29+
* bit-0: STDOUT
30+
* bit-1: STDERR
31+
* file - Path to log file. if null, close() is called on the
32+
* corresponding FDs (from mode above).
33+
* Examples:
34+
* o_redirect(1, "/tmp/my_log.txt"); // redirects stdout to "/tmp/my_log.txt"
35+
* o_redirect(3, "/tmp/my_log.txt"); // Same as above; but redirects stdout and strerr
36+
* o_redirect(3, NULL); // close both stdout and strerr. Same as `> /dev/null 1>&2`
37+
*/
38+
int o_redirect(int mode, const char *file);
1039

1140
#endif /* _UTIL_PROCUTILS_H_ */

src/strlib.c

-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,3 @@ int str_copy(string_t *s, const char *mode, const char *str, size_t len)
4343
s->buf[s->len] = 0;
4444
return len;
4545
}
46-

src/strlib.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int str_printf(string_t *s, const char *mode, const char *fmt, ...);
8383
* Use `mode` as defined above in "String Ops Modes" section.
8484
*
8585
* Example:
86-
* str_copyf(&my_string, "c", "Hello", 5);
86+
* str_copy(&my_string, "c", "Hello", 5);
8787
* // now, my_string == "Hello"
8888
* str_copy(&my_string, "a", " World", 6);
8989
* // now, my_string == "Hello World"

src/strutils.c

+39-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ int safe_atoi(const char *a, int *i)
6767
return 0;
6868
}
6969

70+
char *safe_strncpy(char* dest, const char* src, size_t size)
71+
{
72+
strncpy(dest, src, size - 1);
73+
dest[size - 1] = '\0';
74+
return dest;
75+
}
76+
7077
int trim_suffix(char *str, const char *suffix)
7178
{
7279
int i, j;
@@ -84,5 +91,36 @@ int trim_suffix(char *str, const char *suffix)
8491
i--; j--;
8592
}
8693

87-
return str[i] = 0;
94+
return str[i] = '\0';
95+
}
96+
97+
void rstrip(char *str)
98+
{
99+
int i;
100+
101+
i = strlen(str);
102+
while (str[--i] == ' ')
103+
str[i] = '\0';
104+
}
105+
106+
void lstrip(char *str)
107+
{
108+
int i = 0, j = 0;
109+
110+
while (str[i] && str[i] == ' ')
111+
i++;
112+
113+
while (i && str[i]) {
114+
str[j] = str[i];
115+
i++; j++;
116+
}
117+
118+
if (j != 0)
119+
str[j] = '\0';
120+
}
121+
122+
void strip(char *str)
123+
{
124+
lstrip(str);
125+
rstrip(str);
88126
}

src/strutils.h

+26-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#include <string.h>
1212

1313
/*
14-
* atohstr - Array to Hex String
14+
* atohstr() - Converts an array of bytes to its hexadecimal string
15+
* representation.
1516
*
1617
* Usage:
1718
* uint8_t arr[4] = { 0xca, 0xfe, 0xba, 0xbe };
@@ -21,12 +22,13 @@
2122
* }
2223
*
2324
* Note:
24-
* The passed char *hstr, has to be 2 * arr_len.
25+
* sizeof passed char *hstr, has to be atleast (2 * arr_len) + 1.
2526
*/
2627
int atohstr(char *hstr, const uint8_t *arr, const int arr_len);
2728

2829
/*
29-
* hstrtoa - Hex String to Array
30+
* hstrtoa() - Converts a hexadecimal string to it's value as array of
31+
* bytes.
3032
*
3133
* Usage:
3234
* uint8_t arr[4];
@@ -36,10 +38,30 @@ int atohstr(char *hstr, const uint8_t *arr, const int arr_len);
3638
* }
3739
*
3840
* Note:
39-
* uint8_t *arr has to be atleast half of strlen(hstr)
41+
* sizeof uint8_t *arr has to be atleast half of strlen(hstr)
4042
*/
4143
int hstrtoa(uint8_t *arr, const char *hstr);
4244

45+
/*
46+
* safe_atoi() - A wrapper for atoi() that returns -ve on non number-ish
47+
* strings. This can be used to distinguish "0" from "A" as both would
48+
* return 0 by the bare atoi()'s contract.
49+
*/
4350
int safe_atoi(const char *a, int *i);
4451

52+
/*
53+
* safe_strncpy() - A wrapper for strnlen() that guarantees null
54+
* terminated copy to dest. If strlen(src) > size, then only size-1
55+
* chars are copied to dest and a terminating '\0' is added.
56+
*/
57+
char *safe_strncpy(char* dest, const char* src, size_t size);
58+
59+
/*
60+
* rstrip(), lstrip(), strip() - String space trim methods, as defined
61+
* in python3 programming language.
62+
*/
63+
void rstrip(char *str);
64+
void lstrip(char *str);
65+
void strip(char *str);
66+
4567
#endif /* _UTIL_STRUTIL_H_ */

0 commit comments

Comments
 (0)