-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifr_common.c
158 lines (123 loc) · 2.76 KB
/
ifr_common.c
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
/* =================================================== */
/* Intelligent FRAC. (C) 2000 Michael Glickman */
/* --------------------------------------------------- */
/* See LICENSE regarding distribution policy and */
/* conditions of use. */
/* =================================================== */
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "ifr_autoconf.h"
#include "ifr_common.h"
const char *get_full_fname(const char *filename)
{
struct passwd *pwent;
char *fname;
char *home = NULL;
int l;
if (!filename) return NULL;
l = 0;
if (*filename == '~')
{ int ul;
filename++;
fname = strchr(filename, '/');
if (!fname) goto Adelante;
ul = fname - filename;
home = NULL;
if (!ul) {
fname = getenv("LOGNAME");
if (!fname) fname = getlogin();
}
else {
fname = malloc(ul+1);
if (fname) {
memcpy(fname, filename, ul);
fname[ul] = '\0';
}
filename += ul;
}
pwent = NULL;
if (fname) pwent = getpwnam(fname);
if(ul) free(fname);
if (pwent)
home = pwent->pw_dir;
else
home = getenv("HOME");
if (home) l=strlen(home);
}
Adelante:
l += strlen(filename) + 1;
fname = (char *) malloc(l);
if (fname)
{ if (home)
{ strcpy(fname, home);
strcat(fname, filename);
}
else
strcpy(fname, filename);
}
return fname;
}
/* A fixed version of strtok, based on strpbrk */
char *strtokm(char *line, const char *delim)
{ static char *nextpos = NULL;
if (line == NULL) line = nextpos;
if (line)
{ nextpos = strpbrk(line, delim);
if (nextpos) *nextpos++ = '\0';
}
return line;
}
int sign(int val)
{
return (val > 0) ? 1 : (val < 0) ? -1 : 0;
}
#if !HAVE_STRPBRK
char *strpbrk(const char *s, const char *accept)
{ char c;
while ((c=*s)!='\0' && strchr(accept, c)==NULL) s++;
return (c=='\0') ? NULL : (char *)s;
}
#endif
#if !HAVE_STRSPN
size_t strpspn(const char *s, const char *accept)
{ char c;
const char *s1;
s1 = s;
while((c=*s1) != '\0' && strchr(accept, c) != NULL) s1++;
return (s1-s);
}
#endif
#if !HAVE_STRDUP
char *strdup(const char *s)
{ char *s1;
int len;
len = strlen(s)+1;
s1 = malloc(len);
if (s1 != NULL) memcpy(s1, s, len);
return s1;
}
#endif
#if !HAVE_STRCASECMP
int strcasecmp(const char *s1, const char *s2)
{ int dif;
char c;
do dif = toupper(c=*s1++) - toupper(*s2++);
while ((dif == 0) && c != '\0');
return dif;
}
#endif
#if !HAVE_STRNCASECMP
int strncasecmp(const char *s1, const char *s2, size_t n)
{ int dif = 0;
char c;
while (n > 0)
{ dif = toupper(c=*s1++) - toupper(*s2++);
if((dif != 0) || c == '\0') break;
n--;
}
return dif;
}
#endif