]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-linux.c
Added dependencies on eyefi-config.h
[SourceForge/eyefi-config.git] / eyefi-linux.c
1 #include "eyefi-config.h"
2
3 #include <unistd.h>
4 #include <fcntl.h>
5
6 static int atoo(char o)
7 {
8         if ((o >= '0') && (o <= '7'))
9                 return atoh(o);
10         return -1;
11 }
12
13 static int octal_esc_to_chr(char *input)
14 {
15         int i=0;
16         int ret = 0;
17         int len = strlen(input);
18
19         //intf("%s('%s')\n", __func__, input);
20         if (input[0] != '\\')
21                 return -1;
22         if (len < 4)
23                 return -1;
24
25         for (i=1; i < len ; i++) {
26                 if (i > 3)
27                         break;
28                 int tmp = atoo(input[i]);
29                 //intf("tmp: %d\n", tmp);
30                 if (tmp < 0)
31                         return tmp;
32                 ret <<= 3;
33                 ret += tmp;
34         }
35         return ret;
36 }
37
38 static char *replace_escapes(char *str)
39 {
40         int i;
41         int output = 0;
42         debug_printf(4, "%s(%s)\n", __func__, str);
43         for (i=0; i < strlen(str); i++) {
44                 int esc = octal_esc_to_chr(&str[i]);
45                 if (esc >= 0) {
46                         str[output++] = esc;
47                         i += 3;
48                         continue;
49                 }
50                 str[output++] = str[i];
51         }
52         str[output] = '\0';
53         debug_printf(4, "replaced escapes in: '%s' bytes of output: %d\n", str, output);
54         return str;
55 }
56
57 int fd_flush(int fd)
58 {
59         int ret;
60         ret = posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
61         if (ret)
62                 perror("posix_fadvise() failed");
63         return ret;
64 }
65
66
67 #define LINEBUFSZ 1024
68 char *locate_eyefi_mount(void)
69 {
70         static char eyefi_mount[PATHNAME_MAX]; // PATH_MAX anyone?
71         char line[LINEBUFSZ];
72         FILE *mounts = fopen("/proc/mounts", "r");
73
74         char dev[LINEBUFSZ];
75         char mnt[LINEBUFSZ];
76         char fs[LINEBUFSZ];
77         char opt[LINEBUFSZ];
78         int foo;
79         int bar;
80         
81         if (strlen(eyefi_mount))
82                 return &eyefi_mount[0];
83
84         while (fgets(&line[0], 1023, mounts)) {
85                 int read;
86                 read = sscanf(&line[0], "%s %s %s %s %d %d",
87                                 &dev[0], &mnt[0], &fs[0], &opt[0],
88                                 &foo, &bar);
89                 // only look at fat filesystems:
90                 if (strcmp(fs, "msdos") && strcmp(fs, "vfat")) {
91                         debug_printf(2, "fs at '%s' is not fat, skipping...\n", mnt);
92                         continue;
93                 }
94                 // Linux's /proc/mounts has spaces like this \040
95                 replace_escapes(&mnt[0]);
96                 char *file = eyefi_file_on(REQM, &mnt[0]);
97                 debug_printf(2, "looking for EyeFi file here: '%s'\n", file);
98
99                 struct stat statbuf;
100                 int statret;
101                 statret = stat(file, &statbuf);
102                 free(file);
103                 if (statret) {
104                         debug_printf(2, "fs at: %s is not an Eye-Fi card, skipping...\n",
105                                         eyefi_mount);
106                         continue;
107                 }
108                 strcpy(&eyefi_mount[0], &mnt[0]);
109                 debug_printf(1, "located EyeFi card at: '%s'\n", eyefi_mount);
110                 break;
111         }
112         fclose(mounts);
113
114         if (strlen(eyefi_mount))
115                 return &eyefi_mount[0];
116
117         debug_printf(0, "unable to locate Eye-Fi card\n");
118         if (eyefi_debug_level < 5) {
119                 debug_printf(0, "Please check that your card is inserted and mounted\n");
120                 debug_printf(0, "If you still have issues, please re-run with the '-d5' option and report the output\n");
121         } else {
122                 debug_printf(0, "----------------------------------------------\n");
123                 debug_printf(0, "Debug information:\n");
124                 system("cat /proc/mounts >&2");
125         }
126         exit(1);
127         return NULL;
128 }