aboutsummaryrefslogtreecommitdiffstats
path: root/flx/utils.c
blob: 16aaad24eddc0931e0a88ad6bc1b9ab862dd3119 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdlib.h>

#include "utils.h"

static char bigbuffer[BUFFLEN];

POOL_INIT(p2void);

// fonction to return write in 'ls -l' format
char *right(mode_t m) {
    static char dsc[10];
    dsc[0]=(m&S_IRUSR)?'r':'-';
    dsc[1]=(m&S_IWUSR)?'w':'-';
    dsc[2]=(m&S_IXUSR)?'x':'-';
    dsc[3]=(m&S_IRGRP)?'r':'-';
    dsc[4]=(m&S_IWGRP)?'w':'-';
    dsc[5]=(m&S_IXGRP)?'x':'-';
    dsc[6]=(m&S_IROTH)?'r':'-';
    dsc[7]=(m&S_IWOTH)?'w':'-';
    dsc[8]=(m&S_IXOTH)?'x':'-';
    dsc[9]=0;
    return(dsc);
}

// return directory name of a [path/]filename string
// the return value is in static value
char *dirname(char *str) {
    static char tmp[8192],*p;
    strcpy(tmp,str);
    if ((p=strrchr(tmp,'/'))) return (*p=0,tmp);
    return (NULL);
}

// return the filename of a [path/]filename string
char *basename(char *str) {
    static char *p;
    if (!(p=strrchr(str,'/'))) return str;
    return (p+1);
}

int fatal_error(char *message, ...) {
    va_list argp;
    char    buff[BUFFLEN];
    
    va_start(argp,message);
    vsnprintf(buff,BUFFLEN,message,argp);
    va_end(argp);
    write(2, "Fatal: ", 7);
    write(2, buff, strlen(buff));
    write(2, "\n", 1);
    exit (3);
}

int error(char *message, ...) {
    va_list argp;
    char    buff[BUFFLEN];
    
    va_start(argp,message);
    vsnprintf(buff,BUFFLEN,message,argp);
    va_end(argp);
    write(2, "Error: ", 7);
    write(2,buff,strlen(buff));
    write(2, "\n", 1);
    return (2);
}

void warning(char *message, ...) {
    va_list argp;
    char    buff[BUFFLEN];
    
    va_start(argp,message);
    vsnprintf(buff,BUFFLEN,message,argp);
    va_end(argp);
    write(2, "Warning: ", sizeof("Warning: "));
    write(2, buff, strlen(buff));
    write(2, "\n", 1);
    return ;
}

int pferror(char *message, ...) {
    va_list argp;
    char    buff[BUFFLEN];
    
    va_start(argp,message);
    vsnprintf(buff,BUFFLEN,message,argp);
    perror(buff);
    va_end(argp);
    return (2);
}

/* strchr but bybass backslash '\\' characters */
char *backslashed_strchr(char *s, char c) {
    while (*s) {
	if (*s == '\\' && *(s+1)) s++;
	else if (*s == c) return (s);
	s++;
    }
    return (NULL);
}

/* strchr with multiple characters but bypass '\\' characters */
char *backslashed_strmchr(char *s, char *mc) {
    char  *c;
    
    while (*s) {
	if (*s == '\\' && *(s+1)) s++;
	else 
	    for (c = mc; *c ; c++)
		if (*s == *c) return (s);
	s++;
    }
    return (NULL);
}

/* return the string with characters 'toback' backslashed */
char *backslashed_str(char *s, char *toback) {
    char        *pbuff = bigbuffer;
	int         len = 0;
    
    while (*s && len < (BUFFLEN-1)) {
	if (strchr(toback, *s)) {
	    *pbuff++ = '\\';
	}
	*pbuff++ = *s++;
    }
	bigbuffer[len] = 0;
    return (bigbuffer);
}

/* escape unprintable char with its hexadecimal value (url encode form) */
char *escape_str(char *s) {
	char        *pbuff = bigbuffer;
	int         len = 0;

	while (*s && len < (BUFFLEN-4)) {
		if (flx_is_graph(*s)) {
			*pbuff++ = *s++;
			len++;
		}
		else {
			*pbuff++ = '%';
			*pbuff++ = DEC2HEX((*s >> 4) & 0xF);
			*pbuff++ = DEC2HEX(*s & 0xF);
			s++;
			len += 3;
		}
	}
	bigbuffer[len] = 0;
	return (bigbuffer);
}

/* unescape string from %xx string form */
char *unescape_str(char *s) {
	char        *pbuff = bigbuffer;
	int         len = 0;

	while (*s && len < (BUFFLEN-1)) {
		if (*s == '\\') {
			s++;
			*pbuff++ = *s++;
		}
		else if (*s == '%' && isxdigit(*(s+1)) && isxdigit(*(s+2))) {
			*pbuff++ = HEX2DEC(s[1])*16+HEX2DEC(s[2]);
			s += 3;
		}
		else {
			*pbuff++ = *s++;
		}
		len++;
	}
	bigbuffer[len] = 0;
	return (bigbuffer);
}

void *push_str_sorted(void *ptr, char *str) {
    void *c;
    
    if (!ptr || strcmp(str, SIMPLE_LIST_PTR(ptr)) < 0)
	SIMPLE_LIST_PUSH(ptr, str);
    else {
      for (c = ptr; SIMPLE_LIST_NEXT(c) &&
	     strcmp(SIMPLE_LIST_NEXT_PTR(c), str) < 0 ;
	  c = SIMPLE_LIST_NEXT(c));
      SIMPLE_LIST_PUSH(SIMPLE_LIST_NEXT(c), str);
    }
    return (ptr);
}