FireBirdLib - Topfield TMS PVR TAP Programming Library
GetPathType.c
Go to the documentation of this file.
1#include <sys/stat.h>
2#include <string.h>
3#include "FBLib_hdd.h"
4
5tPathFormat GetPathType(const char *Source)
6{
8
9 byte Result;
10 tPathFormat ret;
11 char AbsFileName[FBLIB_DIR_SIZE];
12 tstat64 statbuf;
13
14 if((Source == NULL) || (*Source == '\0'))
15 {
16 TRACEEXIT();
17 return 0;
18 }
19
20 // PF_FileNameOnly xyz.tap
21 // PF_TAPPathOnly /ProgramFiles/Settings/
22 // PF_LinuxPathOnly /mnt/hd/ProgramFiles/Settings/
23 // PF_FullTAPPath /ProgramFiles/Settings/xyz.tap
24 // PF_FullLinuxPath /mnt/hd/ProgramFiles/Settings/xyz.tap
25
26 //Catch . and / entries
27 if((Source[0] == '.') || (strcmp(Source, "/") == 0))
28 {
29 TRACEEXIT();
30 return PF_TAPPathOnly;
31 }
32
33 //If lstat() returns a result, we already have a Linux path
34 if(!lstat64(Source, &statbuf))
35 {
36 if(S_ISDIR(statbuf.st_mode))
37 Result = PF_LinuxPathOnly;
38 else
39 Result = PF_FullLinuxPath;
40
41 TRACEEXIT();
42 return Result;
43 }
44
45 Result = 0;
46
47 if(strchr(Source, '/') != NULL) Result |= 1;
48 if(Source[strlen(Source) - 1] == '/') Result |= 2;
49 if(strncmp(Source, "/mnt", 4) == 0) Result |= 4;
50
51 //Special case: a simple name without a / might point to a subdirectory. Find out
52 strcpy(AbsFileName, "/mnt/hd");
53 if(Source[0] == '/')
54 {
55 //Add to the mount point
56 strcat(AbsFileName, Source);
57 }
58 else
59 {
60 //As the path is relative, we need to get the current path to make it absolute
61 HDD_TAP_GetCurrentDir(&AbsFileName[7]);
62 if(!StringEndsWith(AbsFileName, "/")) strcat(AbsFileName, "/");
63 strcat(AbsFileName, Source);
64 }
65
66 if(!lstat64(AbsFileName, &statbuf) && S_ISDIR(statbuf.st_mode)) Result |= 3;
67
68 //Now interpret the collected information
69 ret = PF_FileNameOnly;
70 switch(Result)
71 {
72 case 0: //In theory, results 2, 4 and 6 may never appear.
73 case 2:
74 case 4:
75 case 6: ret = PF_FileNameOnly; break;
76
77 case 1: ret = PF_FullTAPPath; break;
78 case 3: ret = PF_TAPPathOnly; break;
79 case 5: ret = PF_FullLinuxPath; break;
80 case 7: ret = PF_LinuxPathOnly; break;
81 }
82
83 TRACEEXIT();
84 return ret;
85}
int lstat64(__const char *__restrict __file, tstat64 *__restrict __buf) __THROW
tPathFormat GetPathType(const char *Source)
Definition: GetPathType.c:5
int HDD_TAP_GetCurrentDir(char *CurrentDir)
#define FBLIB_DIR_SIZE
Definition: libFireBird.h:1871
#define TRACEEXIT()
Definition: libFireBird.h:1244
#define TRACEENTER()
Definition: libFireBird.h:1243
bool StringEndsWith(const char *text, const char *postfix)
Definition: StringEndsWith.c:4
tPathFormat
Definition: libFireBird.h:1921
@ PF_FullTAPPath
Definition: libFireBird.h:1925
@ PF_LinuxPathOnly
Definition: libFireBird.h:1924
@ PF_FullLinuxPath
Definition: libFireBird.h:1926
@ PF_TAPPathOnly
Definition: libFireBird.h:1923
@ PF_FileNameOnly
Definition: libFireBird.h:1922
__mode_t st_mode
Definition: FBLib_hdd.h:77