libcdio-paranoia 10.2+2.0.2
src/utils.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2004, 2005, 2008, 2014 Rocky Bernstein <rocky@gnu.org>
3 Copyright (C) 1998 Monty <xiphmont@mit.edu>
4*/
5
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include <limits.h>
10
11#define copystring(s) (s) ? s : NULL;
12
13static inline char *
14catstring(char *buff, const char *s)
15{
16 if(s){
17 if(buff)
18 buff=realloc(buff,strlen(buff)+strlen(s)+1);
19 else
20 buff=calloc(strlen(s)+1,1);
21 strcat(buff,s);
22 }
23 return(buff);
24}
25
29static inline char *
30split_base_dir(char *fullpath, char *path, unsigned int max)
31{
32 char *post = strrchr(fullpath, '/');
33 int pos = (post ? post-fullpath+1 : 0);
34 path[0]='\0';
35 if (pos>max) return NULL;
36 if (fullpath[pos] == '/') pos++;
37 if (pos) strncat(path, fullpath, pos);
38 return fullpath + pos;
39}
40
41/* By renaming this to utils.c and compiling like this:
42 gcc -DSTANDALONE -o utils utils.h
43you can demo this code.
44*/
45#ifdef STANDALONE
46int main(int argc, char **argv)
47{
48 int i;
49 const char *paths[] = {"/abc/def", "hij/klm"};
50 char path[10];
51 for (i=0; i<2; i++) {
52 char *fullpath = strdup(paths[i]);
53 char *base = split_base_dir(fullpath, path, sizeof(path));
54 printf("dirname of %s is %s; basename is %s\n", fullpath, path, base);
55 if (fullpath) free(fullpath);
56 }
57}
58#endif
int main(int argc, char *argv[])
Definition: cd-paranoia.c:704
#define NULL
Definition: getopt1.c:58
#define max(x, y)
Definition: p_block.h:37
char * catstring(char *buff, const char *s)
Definition: utils.c:160