]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/PosixLib/Glob/DirFunctions.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / PosixLib / Glob / DirFunctions.c
CommitLineData
d7ce7006 1/** @file\r
2 Implement the opendir, closedir, and readdir functions.\r
3\r
4 Copyright (c) 2011, Intel Corporation\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "internal.h"\r
16#include <Library/ShellLib.h>\r
17#include <Library/MemoryAllocationLib.h>\r
18#include <Library/BaseLib.h>\r
19\r
20typedef struct {\r
21 UINT32 Signature;\r
22 SHELL_FILE_HANDLE DirHandle;\r
23 EFI_FILE_INFO *FileInfo;\r
24 struct dirent *DirentStructure;\r
25} DIR_STRUCTURE;\r
26 \r
27DIR * opendir(const char * AsciiFileName)\r
28{\r
29 EFI_STATUS Status;\r
30 DIR_STRUCTURE *DirStruct;\r
31 CHAR16 *FileName;\r
32\r
33 DirStruct = (DIR_STRUCTURE*)AllocateZeroPool(sizeof(DIR_STRUCTURE));\r
34 if (DirStruct == NULL) {\r
35 errno = ENOMEM;\r
36 return NULL;\r
37 }\r
38\r
39 FileName = (CHAR16*)AllocateZeroPool((1+AsciiStrLen(AsciiFileName))*sizeof(CHAR16));\r
40 if (FileName == NULL) {\r
41 FreePool(DirStruct);\r
42 errno = ENOMEM;\r
43 return NULL;\r
44 }\r
45 AsciiStrToUnicodeStr(AsciiFileName, FileName);\r
46\r
47 Status = ShellOpenFileByName(FileName, &DirStruct->DirHandle, EFI_FILE_MODE_READ, 0);\r
48 FreePool(FileName);\r
49 if (EFI_ERROR(Status)) {\r
50 errno = ENOENT;\r
51 FreePool(DirStruct);\r
52 return NULL;\r
53 }\r
54 DirStruct->Signature = 0x08675309;\r
55 return ((DIR*)DirStruct);\r
56}\r
57\r
58int closedir(DIR * DirPointer)\r
59{\r
60 DIR_STRUCTURE *DirStruct;\r
61\r
62 if (DirPointer == NULL) {\r
63 return 0;\r
64 }\r
65\r
66 DirStruct = (DIR_STRUCTURE*)DirPointer;\r
67 if (DirStruct->Signature != 0x08675309) {\r
68 return 0;\r
69 }\r
70\r
71 ShellCloseFile(DirStruct->DirHandle);\r
72 SHELL_FREE_NON_NULL(DirStruct->FileInfo);\r
73 SHELL_FREE_NON_NULL(DirStruct->DirentStructure);\r
74 SHELL_FREE_NON_NULL(DirStruct);\r
75 \r
76 return 0;\r
77}\r
78\r
79struct dirent * readdir(DIR * DirPointer)\r
80{\r
81 DIR_STRUCTURE *DirStruct;\r
82 EFI_STATUS Status;\r
83 BOOLEAN NoFile;\r
84\r
85 NoFile = FALSE;\r
86\r
87 if (DirPointer == NULL) {\r
88 errno = EBADF;\r
89 return NULL;\r
90 }\r
91\r
92 DirStruct = (DIR_STRUCTURE*)DirPointer;\r
93 if (DirStruct->Signature != 0x08675309) {\r
94 errno = EBADF;\r
95 return NULL;\r
96 }\r
97\r
98 if (DirStruct->FileInfo == NULL) {\r
99 Status = ShellFindFirstFile(DirStruct->DirHandle, &(DirStruct->FileInfo));\r
100 } else {\r
101 Status = ShellFindNextFile(DirStruct->DirHandle, DirStruct->FileInfo, &NoFile);\r
102 }\r
103\r
104 if (EFI_ERROR(Status)) {\r
105 errno = ENOENT;\r
106 return NULL;\r
107 }\r
108\r
109 if (NoFile) {\r
110 return (NULL);\r
111 }\r
112\r
113 SHELL_FREE_NON_NULL(DirStruct->DirentStructure);\r
114\r
115 DirStruct->DirentStructure = AllocateZeroPool(sizeof(DIR_STRUCTURE)+(StrSize(DirStruct->FileInfo->FileName)));\r
116 if (DirStruct->DirentStructure == NULL) {\r
117 errno = ENOMEM;\r
118 return NULL;\r
119 }\r
120\r
121 StrCpy(DirStruct->FileInfo->FileName, DirStruct->DirentStructure->FileName);\r
122\r
123 DirStruct->DirentStructure->FileSize = DirStruct->FileInfo->FileSize;\r
124 DirStruct->DirentStructure->PhysicalSize = DirStruct->FileInfo->PhysicalSize;\r
125 DirStruct->DirentStructure->Attribute = DirStruct->FileInfo->Attribute;\r
126 DirStruct->DirentStructure->CreateTime.tv_sec = Efi2Time(&DirStruct->FileInfo->CreateTime);\r
127 DirStruct->DirentStructure->CreateTime.tv_nsec = DirStruct->FileInfo->CreateTime.Nanosecond;\r
128 DirStruct->DirentStructure->LastAccessTime.tv_nsec = Efi2Time(&DirStruct->FileInfo->LastAccessTime);\r
129 DirStruct->DirentStructure->LastAccessTime.tv_sec = DirStruct->FileInfo->LastAccessTime.Nanosecond;\r
130 DirStruct->DirentStructure->ModificationTime.tv_sec = Efi2Time(&DirStruct->FileInfo->ModificationTime);\r
131 DirStruct->DirentStructure->ModificationTime.tv_nsec = DirStruct->FileInfo->ModificationTime.Nanosecond;\r
132 DirStruct->DirentStructure->Size = StrSize(DirStruct->DirentStructure->FileName) + sizeof(DIR_STRUCTURE);\r
133\r
134 return (DirStruct->DirentStructure);\r
135}\r