Skip to content

Latest commit

 

History

History
88 lines (78 loc) · 2.03 KB

sysutil.c

File metadata and controls

88 lines (78 loc) · 2.03 KB
 
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
/*
sysutil: generic utilities to interact with the OS (signals, paths)
copyright ?-2014 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Michael Hipp (dissected/renamed by Thomas Orgis)
*/
#include "mpg123app.h"
#include <sys/stat.h>
#include "sysutil.h"
#include "debug.h"
#if 0
/* removed the strndup for better portability */
/*
* Allocate space for a new string containing the first
* "num" characters of "src". The resulting string is
* always zero-terminated. Returns NULL if malloc fails.
*/
char *strndup (const char *src, int num)
{
char *dst;
if (!(dst = (char *) malloc(num+1)))
return (NULL);
dst[num] = '\0';
return (strncpy(dst, src, num));
}
#endif
size_t dir_length(const char *path)
{
char * slashpos = strrchr(path, '/');
return (slashpos ? slashpos-path : 0);
}
/*
* Split "path" into directory and filename components.
*
* Return value is 0 if no directory was specified (i.e.
* "path" does not contain a '/'), OR if the directory
* is the same as on the previous call to this function.
*
* Return value is 1 if a directory was specified AND it
* is different from the previous one (if any).
*/
int split_dir_file (const char *path, char **dname, char **fname)
{
static char *lastdir = NULL;
char *slashpos;
if ((slashpos = strrchr(path, '/'))) {
*fname = slashpos + 1;
*dname = compat_strdup(path); /* , 1 + slashpos - path); */
if(!(*dname)) {
perror("failed to allocate memory for dir name");
return 0;
}
(*dname)[1 + slashpos - path] = 0;
if (lastdir && !strcmp(lastdir, *dname)) {
/*** same as previous directory ***/
free (*dname);
*dname = lastdir;
return 0;
}
else {
/*** different directory ***/
if (lastdir)
free (lastdir);
lastdir = *dname;
return 1;
}
}
else {
/*** no directory specified ***/
if (lastdir) {
free (lastdir);
lastdir = NULL;
};
*dname = NULL;
*fname = (char *)path;
return 0;
}
}