1 #define _GNU_SOURCE /* For strcasestr */
4 #include "../helper/locales.h"
14 # include <sys/utime.h>
27 #include "../helper/formats.h"
28 #include "../helper/printing.h"
29 #include "../helper/string.h"
32 * Displays status of filesystem (if available).
34 static GSM_Error
PrintFileSystemStatus(void)
36 GSM_FileSystemStatus Status
;
39 error
= GSM_GetFileSystemStatus(gsm
, &Status
);
40 if (error
== ERR_NOTSUPPORTED
|| error
== ERR_NOTIMPLEMENTED
) {
48 printf(LISTFORMAT
, _("Phone memory"));
49 printf(_("%i bytes (free %i bytes, used %i bytes)"),
50 Status
.Free
+ Status
.Used
, Status
.Free
, Status
.Used
);
53 if (Status
.UsedImages
!= 0 || Status
.UsedSounds
!= 0
54 || Status
.UsedThemes
!= 0) {
55 printf(LISTFORMAT
, _("Usage details"));
56 printf(_("images: %i, sounds: %i, themes: %i"),
57 Status
.UsedImages
, Status
.UsedSounds
, Status
.UsedThemes
);
64 void GetFileSystemStatus(int argc UNUSED
, char *argv
[]UNUSED
)
69 error
= PrintFileSystemStatus();
75 void GetFileSystem(int argc
, char *argv
[])
78 gboolean Start
= TRUE
, MemoryCard
= FALSE
;
81 long usedphone
= 0, usedcard
= 0;
82 char FolderName
[256], IDUTF
[200];
87 error
= GSM_GetNextFileFolder(gsm
, &Files
, Start
);
88 if (error
== ERR_EMPTY
)
90 if (error
!= ERR_FOLDERPART
)
94 if (GSM_IsPhoneFeatureAvailable
95 (GSM_GetModelInfo(gsm
), F_FILES2
)) {
96 if (DecodeUnicodeString(Files
.ID_FullName
)[0] ==
99 usedcard
+= Files
.Used
;
101 usedphone
+= Files
.Used
;
104 usedphone
+= Files
.Used
;
108 if (argc
<= 2 || !strcasecmp(argv
[2], "-flatall") == 0) {
109 /* Nokia filesystem 1 */
110 if (UnicodeLength(Files
.ID_FullName
) != 0 &&
111 (DecodeUnicodeString(Files
.ID_FullName
)[0] == 'C' ||
112 DecodeUnicodeString(Files
.ID_FullName
)[0] ==
115 DecodeUnicodeString(Files
.ID_FullName
));
117 if (Files
.Protected
) {
118 /* l10n: One char to indicate protected file */
119 printf("%s", _("P"));
123 if (Files
.ReadOnly
) {
124 /* l10n: One char to indicate read only file */
125 printf("%s", _("R"));
130 /* l10n: One char to indicate hidden file */
131 printf("%s", _("H"));
136 /* l10n: One char to indicate system file */
137 printf("%s", _("S"));
141 if (argc
> 2 && strcasecmp(argv
[2], "-flat") == 0) {
143 if (strcasecmp(argv
[2], "-flatall") ==
145 if (!Files
.ModifiedEmpty
) {
151 printf(" %30c", 0x20);
152 printf(" %9li", (long)Files
.Used
);
157 if (error
== ERR_FOLDERPART
) {
158 printf("%s ", _("Part of folder"));
160 printf("%s ", _("Folder"));
164 if (Files
.Level
!= 1) {
165 for (j
= 0; j
< Files
.Level
- 2; j
++)
170 if (error
== ERR_FOLDERPART
) {
171 printf("%s ", _("Part of folder"));
173 printf("%s ", _("Folder"));
177 printf("\"%s\"\n", DecodeUnicodeConsole(Files
.Name
));
178 } else if (argc
> 2 && strcasecmp(argv
[2], "-flatall") == 0) {
179 /* format for a folder ID;Folder;FOLDER_NAME;[FOLDER_PARAMETERS]
180 * format for a file ID;File;FOLDER_NAME;FILE_NAME;DATESTAMP;FILE_SIZE;[FILE_PARAMETERS] */
181 EncodeUTF8QuotedPrintable(IDUTF
, Files
.ID_FullName
);
182 printf("%s;", IDUTF
);
184 printf("%s", _("File;"));
185 printf("\"%s\";", FolderName
);
187 DecodeUnicodeConsole(Files
.Name
));
188 if (!Files
.ModifiedEmpty
) {
190 OSDateTime(Files
.Modified
,
193 printf("\"%c\";", 0x20);
194 printf("%ld;", (long)Files
.Used
);
196 if (error
== ERR_FOLDERPART
) {
197 printf("%s;", _("Part of folder"));
199 printf("%s;", _("Folder"));
202 DecodeUnicodeConsole(Files
.Name
));
204 DecodeUnicodeConsole(Files
.Name
));
208 printf("%s", _("P"));
210 printf("%s", _("R"));
212 printf("%s", _("H"));
214 printf("%s", _("S"));
220 error
= PrintFileSystemStatus();
222 if (error
== ERR_NOTSUPPORTED
|| error
== ERR_NOTIMPLEMENTED
) {
224 printf(_("Used in phone: %li bytes"), usedphone
);
226 printf(_(", used in card: %li bytes"), usedcard
);
233 void SetFileAttrib(int argc
, char *argv
[])
239 Files
.ReadOnly
= FALSE
;
240 Files
.Protected
= FALSE
;
241 Files
.System
= FALSE
;
242 Files
.Hidden
= FALSE
;
244 DecodeUTF8QuotedPrintable(Files
.ID_FullName
, argv
[2], strlen(argv
[2]));
246 for (i
= 3; i
< argc
; i
++) {
247 if (strcasecmp(argv
[i
], "-readonly") == 0) {
248 Files
.ReadOnly
= TRUE
;
249 } else if (strcasecmp(argv
[i
], "-protected") == 0) {
250 Files
.Protected
= TRUE
;
251 } else if (strcasecmp(argv
[i
], "-system") == 0) {
253 } else if (strcasecmp(argv
[i
], "-hidden") == 0) {
256 fprintf(stderr
, _("Unknown attribute (%s)\n"), argv
[i
]);
262 error
= GSM_SetFileAttributes(gsm
, &Files
);
268 void GetRootFolders(int argc UNUSED
, char *argv
[]UNUSED
)
275 File
.ID_FullName
[0] = 0;
276 File
.ID_FullName
[1] = 0;
279 if (GSM_GetNextRootFolder(gsm
, &File
) != ERR_NONE
)
281 EncodeUTF8QuotedPrintable(IDUTF
, File
.ID_FullName
);
282 printf("%s ", IDUTF
);
283 printf("- %s\n", DecodeUnicodeString(File
.Name
));
289 void GetFolderListing(int argc UNUSED
, char *argv
[])
292 gboolean Start
= TRUE
;
298 DecodeUTF8QuotedPrintable(Files
.ID_FullName
, argv
[2], strlen(argv
[2]));
301 error
= GSM_GetFolderListing(gsm
, &Files
, Start
);
302 if (error
== ERR_EMPTY
)
304 if (error
!= ERR_FOLDERPART
) {
307 printf("%s\n\n", _("Part of folder only"));
310 /* format for a folder ID;Folder;[FOLDER_PARAMETERS]
311 * format for a file ID;File;FILE_NAME;DATESTAMP;FILE_SIZE;[FILE_PARAMETERS] */
312 EncodeUTF8QuotedPrintable(IDUTF
, Files
.ID_FullName
);
313 printf("%s;", IDUTF
);
315 printf("%s", _("File;"));
316 printf("\"%s\";", DecodeUnicodeConsole(Files
.Name
));
317 if (!Files
.ModifiedEmpty
) {
319 OSDateTime(Files
.Modified
, FALSE
));
321 printf("\"%c\";", 0x20);
322 printf("%ld;", (long)Files
.Used
);
324 printf("%s", _("Folder"));
325 printf(";\"%s\";", DecodeUnicodeConsole(Files
.Name
));
329 printf("%s", _("P"));
331 printf("%s", _("R"));
333 printf("%s", _("H"));
335 printf("%s", _("S"));
344 static void GetOneFile(GSM_File
* File
, gboolean newtime
, int i
)
349 unsigned char buffer
[5000];
350 struct utimbuf filedate
;
351 int Handle
, Size
, p
, q
, j
, old1
;
352 time_t t_time1
, t_time2
;
355 if (File
->Buffer
!= NULL
) {
362 t_time1
= time(NULL
);
366 while (error
== ERR_NONE
) {
367 error
= GSM_GetFilePart(gsm
, File
, &Handle
, &Size
);
368 if (error
== ERR_NONE
|| error
== ERR_EMPTY
369 || error
== ERR_WRONGCRC
) {
371 printf(_("Getting \"%s\"\n"),
372 DecodeUnicodeConsole(File
->Name
));
379 _ ("Is a folder. Please give only file names."));
383 fprintf(stderr
, "*");
385 fprintf(stderr
, "\r");
386 fprintf(stderr
, _("%i percent"),
387 (int)(File
->Used
* 100 / Size
));
388 if (File
->Used
* 100 / Size
>= 2) {
389 t_time2
= time(NULL
);
390 diff
= t_time2
- t_time1
;
392 File
->Used
) / File
->Used
;
399 (" (%02i:%02i minutes left)"),
402 fprintf(stderr
, "%30c", 0x20);
406 if (error
== ERR_EMPTY
)
408 if (error
== ERR_WRONGCRC
) {
410 _("File checksum calculated by phone doesn't match with value calculated by Gammu. File is damaged or there is a error in Gammu."));
416 t_time2
= time(NULL
);
417 diff
= t_time2
- t_time1
;
418 if ((diff
> 0) && (File
->Used
> 0)) {
419 fprintf(stderr
, "\r");
421 fprintf(stderr
, _("%i percent done."), (int)(File
->Used
* 100 / Size
));
423 fprintf(stderr
, _("%lu Bytes in %li seconds, %lu Bytes/sec"),
424 (long unsigned int) File
->Used
, (long unsigned int) diff
, (long unsigned int) File
->Used
/ diff
);
426 fprintf(stderr
, "\n");
428 if (error
== ERR_NONE
|| error
== ERR_EMPTY
|| error
== ERR_WRONGCRC
) {
429 if (File
->Used
!= 0) {
430 sprintf(buffer
, "%s", DecodeUnicodeConsole(File
->Name
));
431 for (j
= strlen(buffer
) - 1; j
> 0; j
--) {
432 if (buffer
[j
] == '\\' || buffer
[j
] == '/')
435 if (buffer
[j
] == '\\' || buffer
[j
] == '/') {
436 sprintf(buffer
, "%s",
437 DecodeUnicodeConsole(File
->Name
+
440 file
= fopen(buffer
, "wb");
442 sprintf(buffer
, "file%s",
443 DecodeUnicodeString(File
->ID_FullName
));
444 file
= fopen(buffer
, "wb");
447 sprintf(buffer
, "file%i", i
);
448 file
= fopen(buffer
, "wb");
451 Print_Error(ERR_CANTOPENFILE
);
452 printf(_(" Saving to %s\n"), buffer
);
453 if (fwrite(File
->Buffer
, 1, File
->Used
, file
) != File
->Used
) {
454 printf_err("%s", _("Error while writing file!\n"));
459 if (!newtime
&& !File
->ModifiedEmpty
) {
461 filedate
.actime
= Fill_Time_T(File
->Modified
);
462 /* modification time */
463 filedate
.modtime
= Fill_Time_T(File
->Modified
);
464 smprintf(gsm
, "Setting date of %s\n", buffer
);
465 utime(buffer
, &filedate
);
471 void GetFiles(int argc
, char *argv
[])
475 gboolean newtime
= FALSE
;
481 for (i
= 2; i
< argc
; i
++) {
482 if (strcasecmp(argv
[i
], "-newtime") == 0) {
487 DecodeUTF8QuotedPrintable(File
.ID_FullName
, argv
[i
],
489 smprintf(gsm
, "grabbing '%s' '%s'\n",
490 DecodeUnicodeString(File
.ID_FullName
), argv
[i
]);
491 GetOneFile(&File
, newtime
, i
);
497 void GetFileFolder(int argc
, char *argv
[])
500 gboolean Start
= TRUE
;
502 int level
= 0, allnum
= 0, num
= 0, filelevel
= 0, i
= 0;
503 gboolean newtime
= FALSE
, found
;
504 unsigned char IDUTF
[200];
510 for (i
= 2; i
< argc
; i
++) {
511 if (strcasecmp(argv
[i
], "-newtime") == 0) {
518 while (allnum
!= num
) {
519 error
= GSM_GetNextFileFolder(gsm
, &File
, Start
);
520 if (error
== ERR_EMPTY
)
525 /* We search for file or folder */
527 for (i
= 2; i
< argc
; i
++) {
528 if (strcasecmp(argv
[i
], "-newtime") == 0) {
531 smprintf(gsm
, "comparing %s %s\n",
532 DecodeUnicodeString(File
.ID_FullName
),
534 DecodeUTF8QuotedPrintable(IDUTF
, argv
[i
],
536 if (mywstrncasecmp(File
.ID_FullName
, IDUTF
, 0)) {
537 smprintf(gsm
, "found folder");
541 filelevel
= File
.Level
+ 1;
549 if (found
&& File
.Folder
)
554 smprintf(gsm
, "%i %i\n", File
.Level
, filelevel
);
555 if (File
.Level
!= filelevel
) {
561 if (level
!= 0 && !File
.Folder
) {
562 GetOneFile(&File
, newtime
, i
);
578 void AddOneFile(GSM_File
* File
, const char *text
, const gboolean send
)
581 int Pos
, Handle
, i
, j
, old1
;
582 time_t t_time1
, t_time2
;
586 GSM_GetCurrentDateTime(&dt
);
587 t_time1
= Fill_Time_T(dt
);
590 smprintf(gsm
, "Adding file to filesystem now\n");
593 while (error
== ERR_NONE
) {
595 error
= GSM_SendFilePart(gsm
, File
, &Pos
, &Handle
);
597 error
= GSM_AddFilePart(gsm
, File
, &Pos
, &Handle
);
599 if (error
!= ERR_EMPTY
&& error
!= ERR_WRONGCRC
)
601 if (File
->Used
!= 0) {
602 fprintf(stderr
, "\r");
603 fprintf(stderr
, "%s ", text
);
604 fprintf(stderr
, _("%i percent"),
605 (int)(Pos
* 100 / File
->Used
));
606 if (Pos
* 100 / File
->Used
>= 2) {
607 GSM_GetCurrentDateTime(&dt
);
608 t_time2
= Fill_Time_T(dt
);
609 diff
= t_time2
- t_time1
;
610 i
= diff
* (File
->Used
- Pos
) / Pos
;
616 _(" (%02i:%02i minutes left)"),
619 fprintf(stderr
, "%30c", ' ');
624 fprintf(stderr
, "\n");
625 if (error
== ERR_WRONGCRC
) {
627 _("File checksum calculated by phone doesn't match with value calculated by Gammu. File is damaged or there is a error in Gammu."));
631 void AddSendFile(int argc
, char *argv
[])
637 gboolean sendfile
= FALSE
;
640 if (strcasestr(argv
[1], "sendfile") != NULL
) {
646 DecodeUTF8QuotedPrintable(File
.ID_FullName
, argv
[optint
],
647 strlen(argv
[optint
]));
650 error
= GSM_ReadFile(argv
[optint
], &File
);
652 EncodeUnicode(File
.Name
, argv
[optint
], strlen(argv
[optint
]));
653 for (i
= strlen(argv
[optint
]) - 1; i
> 0; i
--) {
654 if (argv
[optint
][i
] == '\\' || argv
[optint
][i
] == '/')
657 if (argv
[optint
][i
] == '\\' || argv
[optint
][i
] == '/') {
658 EncodeUnicode(File
.Name
, argv
[optint
] + i
+ 1,
659 strlen(argv
[optint
]) - i
- 1);
663 GSM_IdentifyFileFormat(&File
);
665 File
.Protected
= FALSE
;
666 File
.ReadOnly
= FALSE
;
672 for (i
= optint
; i
< argc
; i
++) {
675 if (strcasecmp(argv
[i
], "-type") == 0) {
679 if (strcasecmp(argv
[i
], "-protected") ==
681 File
.Protected
= TRUE
;
684 if (strcasecmp(argv
[i
], "-readonly") ==
686 File
.ReadOnly
= TRUE
;
689 if (strcasecmp(argv
[i
], "-hidden") == 0) {
693 if (strcasecmp(argv
[i
], "-system") == 0) {
697 if (strcasecmp(argv
[i
], "-newtime") ==
699 File
.ModifiedEmpty
= TRUE
;
702 printf(_("Parameter \"%s\" unknown\n"),
706 if (strcasecmp(argv
[i
], "JAR") == 0) {
707 File
.Type
= GSM_File_Java_JAR
;
708 } else if (strcasecmp(argv
[i
], "JPG") ==
710 File
.Type
= GSM_File_Image_JPG
;
711 } else if (strcasecmp(argv
[i
], "BMP") ==
713 File
.Type
= GSM_File_Image_BMP
;
714 } else if (strcasecmp(argv
[i
], "WBMP")
716 File
.Type
= GSM_File_Image_WBMP
;
717 } else if (strcasecmp(argv
[i
], "GIF") ==
719 File
.Type
= GSM_File_Image_GIF
;
720 } else if (strcasecmp(argv
[i
], "PNG") ==
722 File
.Type
= GSM_File_Image_PNG
;
723 } else if (strcasecmp(argv
[i
], "MIDI")
725 File
.Type
= GSM_File_Sound_MIDI
;
726 } else if (strcasecmp(argv
[i
], "AMR") ==
728 File
.Type
= GSM_File_Sound_AMR
;
729 } else if (strcasecmp(argv
[i
], "NRT") ==
731 File
.Type
= GSM_File_Sound_NRT
;
732 } else if (strcasecmp(argv
[i
], "3GP") ==
734 File
.Type
= GSM_File_Video_3GP
;
737 ("What file type (\"%s\") ?\n"),
746 printf_err("%s\n", _("Parameter missing!"));
753 AddOneFile(&File
, _("Writing:"), sendfile
);
754 EncodeUTF8QuotedPrintable(IDUTF
, File
.ID_FullName
);
755 printf(_("ID of new file is \"%s\"\n"), IDUTF
);
761 void AddFolder(int argc UNUSED
, char *argv
[])
767 DecodeUTF8QuotedPrintable(File
.ID_FullName
, argv
[2], strlen(argv
[2]));
768 EncodeUnicode(File
.Name
, argv
[3], strlen(argv
[3]));
769 File
.ReadOnly
= FALSE
;
773 error
= GSM_AddFolder(gsm
, &File
);
775 EncodeUTF8QuotedPrintable(IDUTF
, File
.ID_FullName
);
776 printf(_("ID of new folder is \"%s\"\n"), IDUTF
);
781 void DeleteFolder(int argc UNUSED
, char *argv
[]UNUSED
)
784 unsigned char buffer
[500];
788 DecodeUTF8QuotedPrintable(buffer
, argv
[2], strlen(argv
[2]));
790 error
= GSM_DeleteFolder(gsm
, buffer
);
796 void DeleteFiles(int argc
, char *argv
[])
800 unsigned char buffer
[500];
804 for (i
= 2; i
< argc
; i
++) {
805 DecodeUTF8QuotedPrintable(buffer
, argv
[i
], strlen(argv
[i
]));
806 error
= GSM_DeleteFile(gsm
, buffer
);
813 /* How should editor hadle tabs in this file? Add editor commands here.
814 * vim: noexpandtab sw=8 ts=8 sts=8: