File: mlabel.c

package info (click to toggle)
mtools 3.8-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,116 kB
  • ctags: 1,306
  • sloc: ansic: 11,489; sh: 2,052; makefile: 223; sed: 8
file content (191 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * mlabel.c
 * Make an MSDOS volume label
 */

#include "sysincludes.h"
#include "msdos.h"
#include "mainloop.h"
#include "vfat.h"
#include "mtools.h"
#include "nameclash.h"

char *label_name(char *filename, int verbose, 
		 int *mangled, char *ans)
{
	int len;
	int i;
	int have_lower, have_upper;

	strcpy(ans,"           ");
	len = strlen(filename);
	if(len > 11){
		*mangled = 1;
		len = 11;
	} else
		*mangled = 0;
	strncpy(ans, filename, len);
	have_lower = have_upper = 0;
	for(i=0; i<11; i++){
		if(islower(ans[i]))
			have_lower = 1;
		if(isupper(ans[i]))
			have_upper = 1;
		ans[i] = toupper(ans[i]);

		if(strchr("^+=/[]:,?*\\<>|\".", ans[i])){
			*mangled = 1;
			ans[i] = '~';
		}
	}
	if (have_lower && have_upper)
		*mangled = 1;
	return ans;
}

struct directory *labelit(char *dosname,
			  char *longname,
			  void *arg0,
			  struct directory *dir)
{
	time_t now;

	/* find out current time */
	time(&now);
	mk_entry(dosname, 0x8, 0, 0, now, dir);
	return dir;
}

static void usage(void)
{
	fprintf(stderr, "Mtools version %s, dated %s\n",
		mversion, mdate);
	fprintf(stderr, "Usage: %s [-vscV] drive:\n", progname);
	exit(1);
}


void mlabel(int argc, char **argv, int type)
{
	int entry, verbose, clear, interactive, show, open_mode;
	struct directory dir;
	int result=0;
	char longname[VBUFSIZE],filename[VBUFSIZE];
	char shortname[13];
	ClashHandling_t ch;
	struct MainParam_t mp;
	Stream_t *RootDir;
	int c;
	int mangled;
	
	init_clash_handling(&ch);
	ch.name_converter = label_name;
	ch.ignore_entry = -2;

	verbose = 0;
	clear = 0;
	show = 0;

	while ((c = getopt(argc, argv, "vcs")) != EOF) {
		switch (c) {
			case 'v':
				verbose = 1;
				break;
			case 'c':
				clear = 1;
				break;
			case 's':
				show = 1;
				break;
			default:
				usage();
			}
	}

	if (argc - optind != 1 ||
	    !argv[optind][0] || argv[optind][1] != ':') 
		usage();

	init_mp(&mp);
	get_name(argv[optind], filename, mp.mcwd);
	interactive = !filename[0] && !show && !clear;
	if (filename[0] || clear || interactive)
		open_mode = O_RDWR;
	else
		open_mode = O_RDONLY;

	RootDir = open_subdir(&mp, argv[optind], open_mode, 0, 0);
	if(!RootDir && open_mode == O_RDWR && !clear && !filename[0] &&
	   ( errno == EACCES || errno == EPERM) ) {
		show = 1;
		interactive = 0;
		RootDir = open_subdir(&mp, argv[optind], O_RDONLY, 0, 0);
	}	    
	if(!RootDir) {
		fprintf(stderr, "%s: Cannot initialize drive\n", argv[0]);
		exit(1);
	}

	entry = 0;
	vfat_lookup(RootDir, &dir, &entry, 0, 0, ACCEPT_LABEL | MATCH_ANY,
		    NULL, shortname, longname);
	
	if(show || interactive){
		if(entry == -1)
			printf(" Volume has no label\n");
		else if (*longname)
			printf(" Volume label is %s (abbr=%s)\n",
			       longname, shortname);
		else
			printf(" Volume label is %s\n", shortname);

	}

	/* ask for new label */
	if(interactive){
		fprintf(stderr,"Enter the new volume label : ");
		fgets(filename, VBUFSIZE, stdin);
		if(filename[0])
			filename[strlen(filename)-1] = '\0';
	}

	if(!show && entry != -1){
		/* if we have a label, wipe it out before putting new one */
		if(interactive && filename[0] == '\0')
			if(ask_confirmation("Delete volume label (y/n): ",0,0)){
				FREE(&RootDir);
				exit(0);
			}
		dir.name[0] = DELMARK;
		dir.attr = 0; /* for old mlabel */
		dir_write(RootDir, entry-1, &dir);
	}

	if (!show && filename[0] != '\0') {
		ch.ignore_entry = 1;
		result = mwrite_one(RootDir,filename,0,labelit,NULL,&ch) ? 
		  0 : 1;
	}

	if(!show){
		struct bootsector boot;
		Stream_t *Fs = GetFs(RootDir);

		if(!filename[0])
			strncpy(shortname, "NO NAME    ",11);
		else
			label_name(filename, verbose, &mangled, shortname);

		if(force_read(Fs,(char *)&boot,0,sizeof(boot)) == 
		   sizeof(boot) &&
		   boot.descr >= 0xf0 &&
		   boot.ext.old.dos4 == 0x29 &&
		   _WORD(boot.fatlen)){
			strncpy(boot.ext.old.label, shortname, 11);
			force_write(Fs, (char *)&boot, 0, sizeof(boot));
		}
	}

	FREE(&RootDir);
	exit(result);
}