]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-linux.c
Add some new WEP code and some experimental non-working
[SourceForge/eyefi-config.git] / eyefi-linux.c
1 #include "eyefi-config.h"
2
3 // Geez there has to be a better way to do this
4
5 #ifdef __i386
6 #define O_DIRECT        00040000        /* direct disk access hint */
7 #else
8 #define O_DIRECT        0200000 /* direct disk access hint - currently ignored */
9 #endif
10
11 static int atoo(char o)
12 {
13         if ((o >= '0') && (o <= '7'))
14                 return atoh(o);
15         return -1;
16 }
17
18 static int octal_esc_to_chr(char *input)
19 {
20         int i=0;
21         int ret = 0;
22         int len = strlen(input);
23
24         //intf("%s('%s')\n", __func__, input);
25         if (input[0] != '\\')
26                 return -1;
27         if (len < 4)
28                 return -1;
29
30         for (i=1; i < len ; i++) {
31                 if (i > 3)
32                         break;
33                 int tmp = atoo(input[i]);
34                 //intf("tmp: %d\n", tmp);
35                 if (tmp < 0)
36                         return tmp;
37                 ret <<= 3;
38                 ret += tmp;
39         }
40         return ret;
41 }
42
43 static char *replace_escapes(char *str)
44 {
45         int i;
46         int output = 0;
47         debug_printf(4, "%s(%s)\n", __func__, str);
48         for (i=0; i < strlen(str); i++) {
49                 int esc = octal_esc_to_chr(&str[i]);
50                 if (esc >= 0) {
51                         str[output++] = esc;
52                         i += 3;
53                         continue;
54                 }
55                 str[output++] = str[i];
56         }
57         str[output] = '\0';
58         debug_printf(4, "replaced escapes in: '%s' bytes of output: %d\n", str, output);
59         return str;
60 }
61
62 int fd_dont_cache(int fd)
63 {
64         return fcntl(fd, F_SETFL, O_DIRECT);
65 }
66
67
68 #define LINEBUFSZ 1024
69 char *locate_eyefi_mount(void)
70 {
71         static char eyefi_mount[PATHNAME_MAX]; // PATH_MAX anyone?
72         char line[LINEBUFSZ];
73         FILE *mounts = fopen("/proc/mounts", "r");
74
75         char dev[LINEBUFSZ];
76         char mnt[LINEBUFSZ];
77         char fs[LINEBUFSZ];
78         char opt[LINEBUFSZ];
79         int foo;
80         int bar;
81         
82         if (strlen(eyefi_mount))
83                 return &eyefi_mount[0];
84
85         while (fgets(&line[0], 1023, mounts)) {
86                 int read;
87                 read = sscanf(&line[0], "%s %s %s %s %d %d",
88                                 &dev[0], &mnt[0], &fs[0], &opt[0],
89                                 &foo, &bar);
90                 // only look at fat filesystems:
91                 if (strcmp(fs, "msdos") && strcmp(fs, "vfat")) {
92                         debug_printf(2, "fs at '%s' is not fat, skipping...\n", mnt);
93                         continue;
94                 }
95                 // Linux's /proc/mounts has spaces like this \040
96                 replace_escapes(&mnt[0]);
97                 char *file = eyefi_file_on(REQM, &mnt[0]);
98                 debug_printf(2, "looking for EyeFi file here: '%s'\n", file);
99
100                 struct stat statbuf;
101                 int statret;
102                 statret = stat(file, &statbuf);
103                 free(file);
104                 if (statret) {
105                         debug_printf(2, "fs at: %s is not an Eye-Fi card, skipping...\n",
106                                         eyefi_mount);
107                         continue;
108                 }
109                 strcpy(&eyefi_mount[0], &mnt[0]);
110                 debug_printf(1, "located EyeFi card at: '%s'\n", eyefi_mount);
111                 break;
112         }
113         fclose(mounts);
114
115         if (strlen(eyefi_mount))
116                 return &eyefi_mount[0];
117
118         debug_printf(0, "unable to locate Eye-Fi card\n");
119         if (eyefi_debug_level < 5)
120                 debug_printf(0, "please run with '-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 }