💾 Archived View for gemini.rmf-dev.com › repo › Vaati › mz › files › e2398b97322b4447b73a3d5041b078a… captured on 2023-01-29 at 17:35:54. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

0 /*

1 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>

2 *

3 * Permission to use, copy, modify, and distribute this software for any

4 * purpose with or without fee is hereby granted, provided that the above

5 * copyright notice and this permission notice appear in all copies.

6 *

7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES

8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF

9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR

10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES

11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN

12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF

13 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

14 */

15 #include <unistd.h>

16 #include <fcntl.h>

17 #include <dirent.h>

18 #include <stdlib.h>

19 #include <string.h>

20 #include "view.h"

21 #include "file.h"

22 #include "strlcpy.h"

23 #include "client.h"

24 #include "util.h"

25

26 int file_init(struct view *view) {

27

28 PZERO(view);

29 if (view->path != getcwd(V(view->path)))

30 return -1;

31

32 view->fd = open(view->path, O_DIRECTORY);

33 if (view->fd < 0)

34 return -1;

35

36 return 0;

37 }

38

39 int file_up(struct view *view) {

40 return file_cd(view, "..");

41 }

42

43 int file_cd(struct view *view, const char *path) {

44 int fd = openat(view->fd, path, O_DIRECTORY);

45 if (fd < 0) return -1;

46 close(view->fd);

47 view->fd = fd;

48 fchdir(fd);

49 getcwd(V(view->path));

50 view->selected = 0;

51 return 0;

52 }

53

54 void file_free(struct view *view) {

55 free(view->entries);

56 view->length = 0;

57 view->entries = NULL;

58 }

59

60 int file_ls(struct view *view) {

61 struct dirent *entry;

62 DIR *dp;

63 int length, i, fd;

64

65 fd = dup(view->fd);

66 dp = fdopendir(fd);

67 if (dp == NULL)

68 return -1;

69

70 /* make a array of entry, sort it */

71 length = 0;

72 while ((entry = readdir(dp))) {

73 if (!view->showhidden && entry->d_name[0] == '.')

74 continue;

75 if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))

76 length++;

77 }

78 if (length == 0) {

79 file_free(view);

80 return 0;

81 }

82

83 rewinddir(dp);

84 file_free(view);

85 view->entries = malloc(sizeof(struct entry) * length);

86 i = 0;

87 while ((entry = readdir(dp))) {

88 if (!strcmp(entry->d_name, ".") ||

89 !strcmp(entry->d_name, ".."))

90 continue;

91 if (!view->showhidden && entry->d_name[0] == '.')

92 continue;

93 strlcpy(view->entries[i].name, entry->d_name,

94 sizeof(view->entries[i].name));

95 view->entries[i].type = entry->d_type;

96 i++;

97 }

98

99 closedir(dp);

100 close(fd);

101 view->length = length;

102 view->scroll = 0;

103 lseek(view->fd, 0, SEEK_SET);

104 return 0;

105 }

106

107 int file_select(struct view *view, const char *path) {

108 size_t i = 0;

109 while (i < view->length) {

110 if (!strncmp(view->entries[i].name, path,

111 sizeof(view->entries[i].name))) {

112 view->selected = i;

113 return 0;

114 }

115 i++;

116 }

117 return -1;

118 }

119