1 /**
2  * \file format.c
3  * Example program that formats the device storage.
4  *
5  * Copyright (C) 2006-2007 Linus Walleij <triad@df.lth.se>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 #include "common.h"
23 
24 /**
25  * Returns 0 if OK (yes), 1 if not OK (no)
26  */
prompt()27 static int prompt()
28 {
29   char buff[2];
30 
31   while (1) {
32     fprintf(stdout, "> ");
33     if ( fgets(buff, sizeof(buff), stdin) == NULL ) {
34       if (ferror(stdin)) {
35         fprintf(stderr, "File error on stdin\n");
36       } else {
37         fprintf(stderr, "EOF on stdin\n");
38       }
39       return 1;
40     }
41     if (buff[0] == 'y') {
42       return 0;
43     } else if (buff[0] == 'n') {
44       return 1;
45     }
46   }
47 }
48 
main(int argc,char ** argv)49 int main (int argc, char **argv)
50 {
51   LIBMTP_mtpdevice_t *device;
52   int ret;
53 
54   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
55 
56   LIBMTP_Init();
57   device = LIBMTP_Get_First_Device();
58   if (device == NULL) {
59     printf("No devices.\n");
60     return 0;
61   }
62 
63   printf("I will now format your device. This means that\n");
64   printf("all content (and licenses) will be lost forever.\n");
65   printf("you will not be able to undo this operation.\n");
66   printf("Continue? (y/n)\n");
67   if (prompt() == 0) {
68     // This will just format the first storage.
69     ret = LIBMTP_Format_Storage(device, device->storage);
70   } else {
71     printf("Aborted.\n");
72     ret = 0;
73   }
74 
75   if ( ret != 0 ) {
76     printf("Failed to format device.\n");
77     LIBMTP_Dump_Errorstack(device);
78     LIBMTP_Clear_Errorstack(device);
79     LIBMTP_Release_Device(device);
80     return 1;
81   }
82 
83   LIBMTP_Release_Device(device);
84   printf("OK.\n");
85   return 0;
86 }
87