File: map.c

package info (click to toggle)
lsh 0.70-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 488 kB
  • ctags: 303
  • sloc: ansic: 6,713; makefile: 73; sh: 15
file content (560 lines) | stat: -rw-r--r-- 10,971 bytes parent folder | download | duplicates (3)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/* This is copyrighted software but you may use/distribute it     *
 * in accordance with the conditions set out in the file COPYING  */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "defs.h"

int
pathinit ()
{
  int i;

  for (i = 0; i < DRIVES; i++)
    {
      drivetab[i].path = NULL;
      drivetab[i].plen = 0;
    }

  return 0;
}

/* receives unix path and drive - returns 0 if ok */

int
map (int drive, char *str)
{
  int retval = 0;

/* before we reallocate we might as well zap the old stuff */

  if (drivetab[drive].plen)
    {
      free (drivetab[drive].path);
      drivetab[drive].path = NULL;
      drivetab[drive].plen = 0;

#ifdef DEBUG
      fprintf (stderr, "In map() : Cleaning up before reallocating \n");
#endif

    }

/* have some margin for cds */

  drivetab[drive].plen = CEM (strlen (str), 16) + 32;
  drivetab[drive].path = (char *) malloc (drivetab[drive].plen * sizeof (char));

  if (drivetab[drive].path != NULL)
    {
      strcpy (drivetab[drive].path, str);
      drivetab[drive].slide = strlen (str);
      drivetab[drive].valid = 1;
      if (drivetab[drive].path[drivetab[drive].slide - 1] != '/')
	{
	  drivetab[drive].path[drivetab[drive].slide] = '/';
	  drivetab[drive].path[drivetab[drive].slide + 1] = '\0';
	  drivetab[drive].slide++;

#ifdef DEBUG
	  fprintf (stderr, "In map() : Appended a trailing / \n");
#endif

	}
/* if nothing else has been allocated, then make this the default drive */
      if (drivecur == (-1))
	{
	  drivecur = drive;
	  if (chdir (str))
	    retval = 1;
	}
    }
  else
    {
      drivetab[drive].valid = 0;
      drivetab[drive].plen = 0;
#ifdef DEBUG
      fprintf (stderr, "Memory allocation failed in map()\n");
#endif
      retval = 1;
    }

  return retval;
}

/* receives drive  -  returns 0 if ok */

int
zap (int drive)
{
  drivetab[drive % DRIVES].valid = 0;

  if (drivetab[drive % DRIVES].plen)
    {
      free (drivetab[drive % DRIVES].path);
      drivetab[drive % DRIVES].plen = 0;
#ifdef DEBUG
      fprintf (stderr, "Zapped RAM in drive %c:\n", 'A' + drive);
#endif
    }

  return 0;
}

/* receives unix path and returns 0 if ok */

int
setlo (int drive, char *str)
{
  int retval = 0;
  char *tptr;

#ifdef DEBUG
  fprintf (stderr, "Setlo received <%s>\n", str);
#endif

  if (drivetab[drive].valid)
    {
      if ((!(strncmp (drivetab[drive].path, str, drivetab[drive].slide))) ||
	  (
	(!strncmp (drivetab[drive].path, str, drivetab[drive].slide - 1)) &&
	    (strlen (str) < drivetab[drive].slide) &&
	    (drivetab[drive].slide - 1)
	  ))
	{
	  if (strlen (str) > drivetab[drive].plen)
	    {
	      if ((tptr = realloc (drivetab[drive].path, (CEM (strlen (str), 16) + 16) * sizeof (char))) == NULL)
		{
		  return 1;
#ifdef DEBUG
		  fprintf (stderr, "realloc failed in setlo()\n");
#endif
		}
	      else
		{
		  drivetab[drive].plen = strlen (str) + strlen (str) % 16;
		  drivetab[drive].path = tptr;
#ifdef DEBUG
		  fprintf (stderr, "realloc ok in setlo()\n");
#endif
		}
	    }
	  strcpy (drivetab[drive].path, str);
	  if (drivetab[drive].path[strlen (str) - 1] != '/')
	    {
#ifdef DEBUG
	      fprintf (stderr, "In setlo() : last char is <%c>\n", drivetab[drive].path[strlen (str) - 1]);
	      fprintf (stderr, "In setlo() : appending trailing / to path %s\n", drivetab[drive].path);
#endif
	      strcat (drivetab[drive].path, "/");
	    }
	}
      else
	{
	  retval = 1;
#ifdef DEBUG
	  fprintf (stderr, "In setlo() : paths did not match\n");
#endif
	}
    }
  else
    {
      retval = 1;

#ifdef DEBUG
      fprintf (stderr, "In setlo() : attempting to use invalid drive\n");
#endif

    }

#ifdef DEBUG
  fprintf (stderr, "Setlo returned <%s>\n", str);
#endif

  return retval;
}

/* construct a unix path from drivetab path */

int
fromlo (char *str, int len)
{
  int retval = 0;
  int drive;
  int i = 0;
  int j;
  struct stat fst;

  char *tptr;

  drive = drivecur;

#ifdef DEBUG
  fprintf (stderr, "Fromlo receives <%s>\n", str);
#endif

/* a x: is the only difference between unix an los */

  if (str[1] == ':')
    {
      drive = toupper (str[0]) - 'A';
      if (drivetab[drive].valid)
	{
	  if (str[2] == '\\')
	    {
	      if ((drivetab[drive].slide + strlen (str)) < len)
		{
		  if (drivetab[drive].slide == 1)
		    {
		      for (i = 0; i < strlen (str); i++)
			{
			  str[i] = str[i + 2];
			}
		      retval = 0;
		    }
		  else
		    {
		      j = drivetab[drive].slide - 3;
		      for (i = strlen (str); i >= 0; i--)
			{
			  str[i + j] = str[i];
			}
		      strncpy (str, drivetab[drive].path, j + 2);
		      retval = 0;
		    }
		}
	      else
		{
		  retval = 2;
#ifdef DEBUG
		  fprintf (stderr, "In fromlo : buffer passed too small (%d bytes)\n", len);
#endif
		}
	    }


	  else
	    {
	      if ((strlen (drivetab[drive].path) + strlen (str)) < len)
		{
		  if (drivetab[drive].path[1] == '\0')
		    {
		      for (i = 0; i < strlen (str); i++)
			{
			  str[i + 1] = str[i + 2];
			}
		      str[0] = '/';
		    }
		  else
		    {
		      j = strlen (drivetab[drive].path) - 2;
		      for (i = strlen (str); i >= 0; i--)
			{
			  str[i + j] = str[i];
			}
		      strncpy (str, drivetab[drive].path, strlen (drivetab[drive].path));
		    }
		}
	      else
		{
		  retval = 1;
#ifdef DEBUG
		  fprintf (stderr, "In fromlo : buffer passed too small [%d bytes]\n", len);
#endif
		}
	    }
	}
      else
	{
	  retval = 1;
#ifdef DEBUG
	  fprintf (stderr, "In fromlo : path not valid\n");
#endif
	}
    }
  else
    {
      if (str[0] == '\\')
	{
	  if (drivetab[drivecur].valid)
	    {
	      if ((strlen (str) + drivetab[drivecur].slide) >= len)
		{
#ifdef DEBUG
		  fprintf (stderr, "In fromlo : buffer passed from calling function too small\n");
#endif
		  retval = 1;
		}
	      else
		{
#ifdef DEBUG
		  fprintf (stderr, "In fromlo : about to move things back\n");
#endif
		  for (i = strlen (str); i >= 0; i--)
		    {
		      str[drivetab[drivecur].slide - 1 + i] = str[i];
		    }
#ifdef DEBUG
		  fprintf (stderr, "In fromlo : about to copy over\n");
#endif
		  strncpy (str, drivetab[drivecur].path, drivetab[drivecur].slide - 1);
		}
	    }
	  else
	    {
	      retval = 1;
	    }
	}
    }

/* clean up slashes */
  i = 0;
  while (!((isspace (str[i])) || (iscntrl (str[i]))))
    {
      if (str[i] == '\\')
	str[i] = '/';
      i++;
    }
  str[i] = '\0';


  if (!retval && (drivetab[drive].slide > 1))
    {
      i = strlen (str);
      j = stat (str, &fst);
#ifdef DEBUG
      fprintf (stderr, "In fromlo : <%s> canditate for a check\n", str);
#endif

      if ((!(fst.st_mode & S_IFDIR)) || j)
	{
	  while (i && str[i] != '/')
	    i--;
#ifdef DEBUG
	  fprintf (stderr, "In fromlo : <%s> is not a dir -- it has to be part of one\n", str);
#endif
	}
#ifdef DEBUG
      else
	fprintf (stderr, "In fromlo : <%s> is a dir\n", str);
#endif
      if (i)
	{
#ifdef DEBUG
	  fprintf (stderr, "In fromlo() : we are not in the root directory\n");
#endif
	  j = CEM (strlen (str) + strlen (drivetab[drive].path), 16);
#ifdef DEBUG
	  fprintf (stderr, "In fromlo() : we are allocating %d bytes\n", j);
#endif
	  tptr = (char *) malloc (j * sizeof (char));
	  if (tptr != NULL)
	    {
	      strncpy (tptr, str, i);
	      tptr[i] = '\0';
#ifdef DEBUG
	      fprintf (stderr, "In fromlo() : going to change to <%s>\n", tptr);
#endif
	      if (chdir (tptr))
		{
#ifdef DEBUG
		  fprintf (stderr, "In fromlo() : chdir <%s> failed\n", tptr);
#endif
		  retval = 1;
		}
	      else
		{
		  if (NULL != getcwd (tptr, j))
		    {
		      if (strncmp (tptr, drivetab[drive].path, (drivetab[drive].slide - 1)) ||
			  (tptr[drivetab[drive].slide - 1] != '\0' && tptr[drivetab[drive].slide - 1] != '/')
			)
			{
			  retval = 1;
#ifdef DEBUG
			  fprintf (stderr, "Request failed since <%s> does not match <%s> in first %d locations\n", tptr, drivetab[drive].path, drivetab[drive].slide - 1);
#endif
			}
#ifdef DEBUG
		      else
			{
			  fprintf (stderr, "In fromlo() : Request ok <%s> matches <%s> in first %d places\n", tptr, drivetab[drive].path, drivetab[drive].slide - 1);
			}
#endif
		    }
		  else
		    {
		      retval = 1;
#ifdef DEBUG
		      perror ("In fromlo() : could not aquire cwd");
#endif
		    }
		  chdir (drivetab[drivecur].path);
		}
	      free (tptr);
	    }
	}
    }

#ifdef DEBUG
  fprintf (stderr, "Fromlo() returns %s\n", str);
#endif

  return retval;
}

/* constuct a los path from unix path */

/* untested */

int
tolo (int drive, char *str, int len)
{
  int retval = 1;
  int i;

#ifdef DEBUG
  fprintf (stderr, "Tolo() received <%s>\n", str);
#endif

  if (drivetab[drive].valid)
    {
      if ((!strncmp (drivetab[drive].path, str, drivetab[drive].slide)) ||
	  (!strncmp (drivetab[drive].path, str, drivetab[drive].slide - 1) &&
	   (strlen (drivetab[drive].path) >= (strlen (str))) &&
	   (drivetab[drive].slide - 1)))
	{
	  if (drivetab[drive].slide > 2)
	    {
#ifdef DEBUG
	      fprintf (stderr, "In the flaky bit\n");
#endif
	      str[0] = 'A' + drive;
	      str[1] = ':';
	      str[2] = '/';
	      if (drivetab[drive].slide < strlen (str))
		{
		  for (i = drivetab[drive].slide; str[i] != '\0'; i++)
		    {
		      str[3 + i - drivetab[drive].slide] = str[i];
		    }
		  str[3 + i - drivetab[drive].slide] = '\0';
		}
	      else
		{
		  str[3] = '\0';
		}
	    }
	  else
	    {
	      for (i = strlen (str); i >= 0; i--)
		{
		  str[i + 2] = str[i];
		}
	      str[0] = 'A' + drive;
	      str[1] = ':';
	      str[2] = '/';
	    }
	  retval = 0;
	}
#ifdef DEBUG
      else
	{
	  fprintf (stderr, "In tolo() : input string %s did not match drive base\n", str);
	}
#endif
    }

  /* remember to convert / to \ down here */

  for (i = 0; i < strlen (str); i++)
    {
      if (str[i] == '/')
	{
	  str[i] = '\\';
	}
      else
	{
	  str[i] = toupper (str[i]);
	}
    }

#ifdef DEBUG
  fprintf (stderr, "Tolo() returned <%s>\n", str);
#endif

  return retval;
}

/* free up the ram */

int
pathexit ()
{
  int i;
  for (i = 0; i < DRIVES; i++)
    {
      zap (i);
    }
  return 0;
}

/* diagnostics */

int
pathdump (FILE * out)
{
  int i;
  for (i = 0; i < 26; i++)
    {
      if (drivetab[i].valid)
	{
	  fprintf (out, "%c:=%s\n", i + 'A', drivetab[i].path);
	}
    }
  return 0;
}


#ifdef OODEBUG
int
main ()
{
  char t[70], r[70], s[70];
  pathinit ();
  while (t[0] != 'q')
    {

      scanf ("%s", t);
      if (fromlo (t, 70))
	{
	  fprintf (stderr, "In main() : Conversion by fromlo() failed \n");
	}
      getcwd (r, 70);
      if (chdir (t))
	{
	  fprintf (stderr, "In main() : chdir() failed with request %s\n", t);
	}
      getcwd (s, 70);
      if (setlo (2, s))
	{
	  fprintf (stderr, "In main() : Setlo failed with s=%s\n", s);
	  chdir (r);
	}
      pathdump (stderr);
      if (tolo (2, s, 70))
	{
	  fprintf (stderr, "In main() : tolo() failed\n");
	}
      printf ("%s>", s);
    }
  pathdump (stderr);
  return 0;
}
#endif