TPCCLIB
Loading...
Searching...
No Matches
pathexist.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include "tpcextensions.h"
8/*****************************************************************************/
9#include "tpcfileutil.h"
10/*****************************************************************************/
11
12/*****************************************************************************/
19 const char *pathname
20) {
21 if(pathname==NULL) return(0);
22 int len=strlen(pathname);
23 if(len<1) return(0);
24 char *str=strdup(pathname);
25 if(len>1 && str[len-1]=='/') {
26 if(len<2) {free(str); return(0);}
27 str[len-1]='\0';
28 }
29 struct stat fst;
30 if(stat(str, &fst)!=0) {free(str); return(0);}
31 free(str);
32 if(S_ISDIR(fst.st_mode)) return(1);
33 return(0);
34}
35/*****************************************************************************/
36
37/*****************************************************************************/
45 const char *pathname
46) {
47 if(fileExist(pathname)) return(1);
48 if(!pathExist(pathname)) return(0);
49 if(rmdir(pathname)==0) return(0); else return(1);
50}
51/*****************************************************************************/
52
53/*****************************************************************************/
60 const char *pathname
61) {
62 if(fileExist(pathname)) return(1); // Error if filename was given
63 if(!pathExist(pathname)) return(0); // No error if path does not exist
64 IFT ift; iftInit(&ift);
65 unsigned short int n=pathFileList(pathname, &ift);
66 if(n==0) {iftFree(&ift); return(0);}
67 for(int i=0; i<ift.keyNr; i++) {
68 if(remove(ift.item[i].value)) {iftFree(&ift); return(1);}
69 }
70 iftFree(&ift);
71 return(0);
72}
73/*****************************************************************************/
74
75/*****************************************************************************/
83 const char *pathname
84) {
85 //printf("pathname='%s'\n", pathname);
86 if(pathname==NULL || strnlen(pathname, 2)<1) return(1);
87 if(fileExist(pathname)) return(1);
88 if(pathExist(pathname)) return(0); // exists already
89 // check if this is a subfolder
90 char *lpath=strdup(pathname);
91 char *cptr=strrchr(lpath, '/'); if(cptr==NULL) cptr=strrchr(lpath, '\\');
92 if(cptr!=NULL) { // it is a subfolder, so call recursively with the parent folder name
93 *cptr=(char)0;
94 int ret=pathCreate(lpath);
95 if(ret!=0) {free(lpath); return(1);}
96 }
97 free(lpath);
98 // now try to create the folder
99#ifndef HAVE__MKDIR
100 // other than Windows
101 if(mkdir(pathname, 0777)!=0 && errno!=EEXIST) return(1); else return(0);
102#else
103 // Windows (MinGW)
104 if(_mkdir(pathname)!=0 && errno!=EEXIST) return(1); else return(0);
105#endif
106}
107/*****************************************************************************/
108
109/*****************************************************************************/
unsigned short int pathFileList(const char *pathname, IFT *ift)
Definition filexist.c:83
int fileExist(const char *filename)
Definition filexist.c:17
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
int pathExist(const char *pathname)
Definition pathexist.c:17
int pathCreate(const char *pathname)
Definition pathexist.c:81
int pathRemoveFiles(const char *pathname)
Definition pathexist.c:58
int pathRemove(const char *pathname)
Definition pathexist.c:43
char * strdup(const char *s)
Definition stringext.c:185
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
char * value
Definition tpcift.h:37
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47
Header file for library libtpcextensions.
Header file for libtpcfileutil.