DOSnaming 21 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*> c.DOSnaming <*/
/*---------------------------------------------------------------------------*/
/* MSDOS to RISCOS name conversion                Copyright (c) 1989 JGSmith */
/*---------------------------------------------------------------------------*/

#include <stdlib.h>
#include <string.h>
Robert Sprowson's avatar
Robert Sprowson committed
22
#include <ctype.h>
Neil Turton's avatar
Neil Turton committed
23
#include "kernel.h"
Robert Sprowson's avatar
Robert Sprowson committed
24 25 26
#include "DebugLib/DebugLib.h"

#include "DOSFS.h"
Ben Avison's avatar
Ben Avison committed
27
#include "TIMEconv.h"
Robert Sprowson's avatar
Robert Sprowson committed
28 29 30
#include "ADFSshape.h"
#include "Helpers.h"
#include "MsgTrans.h"
Ben Avison's avatar
Ben Avison committed
31
#include "DOSnaming.h"
Robert Sprowson's avatar
Robert Sprowson committed
32 33 34
#include "DOSshape.h"
#include "DOSdirs.h"
#include "debug.h"
Neil Turton's avatar
Neil Turton committed
35

Robert Sprowson's avatar
Robert Sprowson committed
36 37 38 39 40 41 42
/*!
 * \brief  Validate that the given character appears in the string
 * \param  string Table to use
 * \param  init Character to check if it is in it
 * \return -1 if the character is in the table
 */
int validchar(char *string, char init)
Neil Turton's avatar
Neil Turton committed
43
{
Robert Sprowson's avatar
Robert Sprowson committed
44
  while (*string != '\0')
Neil Turton's avatar
Neil Turton committed
45 46 47 48 49 50 51 52 53
  {
    if (*string == init)
      return -1;
    string++;
  }

  return 0;
}

Robert Sprowson's avatar
Robert Sprowson committed
54 55 56 57 58 59 60 61
/*!
 * \brief  Map certain characters between filing systems
 * \param  char Character to map
 * \param  fromlist Input table
 * \param  tolist Output table
 * \return Remapped character
 */
char mapchar(char cchr, char *fromlist, char *tolist)
Neil Turton's avatar
Neil Turton committed
62
{
Robert Sprowson's avatar
Robert Sprowson committed
63
  while (*fromlist != '\0')
Neil Turton's avatar
Neil Turton committed
64 65 66 67 68 69 70 71 72
  {
    if (cchr == *fromlist)
      return *tolist;
    fromlist++;
    tolist++;
  }
  return cchr;
}

Robert Sprowson's avatar
Robert Sprowson committed
73 74 75 76 77 78 79 80
/*!
 * \brief  Return the character position of the passed character (or 0 if none)
 * \param  text String to search
 * \param  marker fromlist Input table
 * \param  tolist Output table
 * \return Remapped character
 */
static int chr_pos(char *text, char marker)
Neil Turton's avatar
Neil Turton committed
81
{
Robert Sprowson's avatar
Robert Sprowson committed
82
// int index;
83
//
Robert Sprowson's avatar
Robert Sprowson committed
84
// for (index = 0; (text[index] != '\0'); index++)
85 86
//  if (text[index] == marker)
//   return(index) ;
Robert Sprowson's avatar
Robert Sprowson committed
87 88
  char *index = strrchr(text, marker);
  return (index ? (int)(index - text) : 0);
Neil Turton's avatar
Neil Turton committed
89 90
}

Robert Sprowson's avatar
Robert Sprowson committed
91 92 93 94 95 96 97 98 99
/*!
 * \brief  Return a string containing the text before the given character
 * \param  newptr Pointer to storage for the substring
 * \param  text String to use
 * \param  marker Character to split at
 * \param  npad Terminate with 0 if zero
 * \return The new string
 */
char *before(char *newptr, char *text, char marker, int npad)
Neil Turton's avatar
Neil Turton committed
100
{
Robert Sprowson's avatar
Robert Sprowson committed
101 102 103
  int cpos = chr_pos(text, marker);
  if (cpos == 0)
    cpos = strlen(text);
Neil Turton's avatar
Neil Turton committed
104

Robert Sprowson's avatar
Robert Sprowson committed
105 106 107
  strncpy(newptr, text, cpos);
  if (npad == 0)
    newptr[cpos] = '\0';
Neil Turton's avatar
Neil Turton committed
108

Robert Sprowson's avatar
Robert Sprowson committed
109
  return newptr;
Neil Turton's avatar
Neil Turton committed
110 111
}

Robert Sprowson's avatar
Robert Sprowson committed
112 113 114 115 116 117 118 119 120
/*!
 * \brief  Return a string containing the text after the given character
 * \param  newptr Pointer to storage for the substring
 * \param  text String to use
 * \param  marker Character to split at
 * \param  npad Terminate with 0 if zero
 * \return The new string
 */
char *after(char *newptr, char *text, char marker, int npad)
Neil Turton's avatar
Neil Turton committed
121
{
Robert Sprowson's avatar
Robert Sprowson committed
122
  int cpos = chr_pos(text, marker);
Neil Turton's avatar
Neil Turton committed
123

Robert Sprowson's avatar
Robert Sprowson committed
124
  if (cpos != 0)
Neil Turton's avatar
Neil Turton committed
125
  {
Robert Sprowson's avatar
Robert Sprowson committed
126 127 128 129 130 131 132 133 134
    if (npad == 0)
      strcpy(newptr, &text[cpos + 1]);
    else
      strncpy(newptr, &(text[cpos + 1]), 4);
  }
  else
  {
    if (npad == 0)
      strcpy(newptr, "");
Neil Turton's avatar
Neil Turton committed
135 136
  }

Robert Sprowson's avatar
Robert Sprowson committed
137
  return (newptr);
Neil Turton's avatar
Neil Turton committed
138 139
}

Robert Sprowson's avatar
Robert Sprowson committed
140 141 142 143 144 145 146 147 148 149
/* RISC OS name:
 *      [$.]<path>
 * RISC OS path:
 *      <filename (max. 10 chars)>[.<path element>]
 *
 * DOS name:
 *      [\]<path>
 * DOS path:
 *      <filename (max. 8 chars)>[.<extension (max. 3 chars)>[\<path element>]
 */
Neil Turton's avatar
Neil Turton committed
150

Robert Sprowson's avatar
Robert Sprowson committed
151 152 153 154 155 156
/*!
 * \brief  Check that the given DOS names are identical
 * \param  wcname Compare name, possibly including wild cards
 * \param  fname File name
 * \return TRUE when there is a match
 */
Neil Turton's avatar
Neil Turton committed
157 158
int namematch(char *wcname,char *fname)
{
Robert Sprowson's avatar
Robert Sprowson committed
159 160 161 162 163 164 165 166 167
  char string1[257];
  char string2[257];

  /* Code assumes characters upto (and including) "file_sep" will always fit
   * in RISC OS names.
   */
  before(string1, fname, file_sep, 0);
  before(string2, wcname, file_sep, 0);
  if (wild_card_compare(string1, string2, DOSwcmult, DOSwcsing) == TRUE)
Neil Turton's avatar
Neil Turton committed
168
  {
Robert Sprowson's avatar
Robert Sprowson committed
169 170 171 172
    /* "string1" is the full (non-wildcarded) filename we have matched with */
    /* "string2" is the original (wildcarded) filename we were given on entry */
    after(string1, fname, file_sep, 0);
    after(string2, wcname, file_sep, 0);
Neil Turton's avatar
Neil Turton committed
173

Robert Sprowson's avatar
Robert Sprowson committed
174 175
    if (wild_card_compare(string1, string2, DOSwcmult, DOSwcsing) == TRUE)
      return TRUE;
Neil Turton's avatar
Neil Turton committed
176 177
  }

Robert Sprowson's avatar
Robert Sprowson committed
178
  return FALSE;
Neil Turton's avatar
Neil Turton committed
179 180
}

Robert Sprowson's avatar
Robert Sprowson committed
181 182 183 184 185
/*!
 * \brief  Translate a standard RISC OS name into a MS-DOS one
 * \param  source The RISC OS name
 * \param  dest Buffer to receive the DOS one
 * \return Destination buffer pointer
Neil Turton's avatar
Neil Turton committed
186
 */
Robert Sprowson's avatar
Robert Sprowson committed
187
char *convertRISCOStoDOS(char *source, char *dest)
Neil Turton's avatar
Neil Turton committed
188
{
Robert Sprowson's avatar
Robert Sprowson committed
189 190 191 192 193 194 195 196 197
  char *csptr = source;
  char *cdptr = dest;
  int   loop;
  int   filesepseen = FALSE; /* if we have seen the file seperator */
  char *cptr;      /* string pointer */
  char  lchr;      /* last character seen */
  int   point = 0; /* position where file extension started */

  if ((source == NULL) || (*source == '\0'))
Neil Turton's avatar
Neil Turton committed
198
  {
Robert Sprowson's avatar
Robert Sprowson committed
199 200 201
    dprintf(("", "DOSFS: convertRISCOStoDOS: NULL name\n"));
    *dest = '\0';
    return dest;
Neil Turton's avatar
Neil Turton committed
202 203
  }

Robert Sprowson's avatar
Robert Sprowson committed
204
  dprintf(("","DOSFS: convertRISCOStoDOS: \"%s\"", source));
Neil Turton's avatar
Neil Turton committed
205

Robert Sprowson's avatar
Robert Sprowson committed
206 207 208
  lchr = '\0'; /* no last character */
  cptr = csptr;
  do
Neil Turton's avatar
Neil Turton committed
209
  {
Robert Sprowson's avatar
Robert Sprowson committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    /* Ensure that "/" characters do not appear at the start or end of the name
     * and that "//" sequences are trapped.
     */
    if ((*cptr == '/' && (lchr == '\0' || lchr == *cptr || lchr == '.')) ||
        ((lchr == '/') && ((*cptr == '\0') || (*cptr == '.')))
       )
      return_error1(char *, err_invalidname, source);

    lchr = *cptr++; /* remember this character */
  } while (lchr);

  if (*csptr == '$')             /* ROOT directory specifier */
  {
    csptr++;
    if (*csptr == '.')           /* RISC OS directory seperator */
Neil Turton's avatar
Neil Turton committed
225
    {
Robert Sprowson's avatar
Robert Sprowson committed
226 227
      *cdptr++ = dir_sep;        /* MSDOS directory seperator */
      csptr++;
Neil Turton's avatar
Neil Turton committed
228 229
    }
    else
Robert Sprowson's avatar
Robert Sprowson committed
230 231 232 233
      if (*csptr == '\0')
        *cdptr++ = dir_sep;
      else
        *cdptr++ = '$';
Neil Turton's avatar
Neil Turton committed
234 235
  }

Robert Sprowson's avatar
Robert Sprowson committed
236
  for (loop = 0;;)       /* convert the remainder of the pathname */
Neil Turton's avatar
Neil Turton committed
237
  {
Robert Sprowson's avatar
Robert Sprowson committed
238
    if (*csptr == '\0')  /* end of the source pathname */
Neil Turton's avatar
Neil Turton committed
239
    {
Robert Sprowson's avatar
Robert Sprowson committed
240 241
      *cdptr = '\0';     /* terminate the destination pathname */
      break;
Neil Turton's avatar
Neil Turton committed
242 243
    }

Robert Sprowson's avatar
Robert Sprowson committed
244
    switch (*csptr)
Neil Turton's avatar
Neil Turton committed
245
    {
Robert Sprowson's avatar
Robert Sprowson committed
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
      case '.':
        /* RISC OS to directory seperator */
        *cdptr++ = dir_sep;
        csptr++;
        loop = 0;
        filesepseen = FALSE; /* for this leafname */
        break;

      case '/':
        /* convert to file seperator */
        if (filesepseen)
          return_error1(char *, err_invalidname, source);
        *cdptr++ = file_sep;
        csptr++;
        loop++;
        point = loop;
        filesepseen = TRUE;
        break;

      default:
        /* perform standard name mapping */
        if (filesepseen)
        {
          char c = *csptr++;
          /* should never need to truncate the extension */
//        if ((loop - point) >= extsize)
//          return_error1(char *, err_invalidname, source);
//        if (islower(c))
//          c = toupper(c);
          *cdptr++ = mapchar(c, ROmapping, DOSmapping);
        }
        else
        {
          char c = *csptr;
//        if (islower(c))
//          c = toupper(c);
          *cdptr++ = mapchar(c, ROmapping, DOSmapping);
          csptr++;      /* step over this DOS character */
        }
        loop++;
        break;
287 288 289
    }
  }

Robert Sprowson's avatar
Robert Sprowson committed
290
  dprintf(("", " converted to \"%s\"\n", dest));
291

Robert Sprowson's avatar
Robert Sprowson committed
292 293 294
  for (cptr = dest; *cptr; cptr++)
    if (!validchar(valchars, *cptr))
      return_error1(char *, err_invalidname, source);
295

Robert Sprowson's avatar
Robert Sprowson committed
296
  return dest;
297 298
}

Robert Sprowson's avatar
Robert Sprowson committed
299 300 301 302 303 304
/*!
 * \brief  Convert a dir entry into a DOS short filename
 * \param  dentry The dir entry to consider
 * \param  name Buffer to receive the name, at least 8+1+3+1 long
 * \return Name buffer pointer
 */
Robert Sprowson's avatar
Robert Sprowson committed
305 306
char *buildFILEname(DOS_direntry * dentry,char *name)
{
Robert Sprowson's avatar
Robert Sprowson committed
307 308 309 310 311 312 313 314 315 316 317 318 319
  char *cptr = (char *)&(dentry->FILE_status);
  int   index;
  int   loop;

  dprintf(("", "buildFILEname: dentry:%p\n", dentry));

  /* "dentry" should contain a valid filename */
  /* copy prefix characters (or upto a space) into the filename buffer */
  for (index=0; ((cptr[index] > ' ') && (index < namsize)); index++)
    name[index] = cptr[index];

  /* copy suffix characters (or upto a space) into the filename buffer */
  for (loop=0; ((dentry->FILE_extension[loop] > ' ') && (loop < extsize)); loop++)
Robert Sprowson's avatar
Robert Sprowson committed
320
  {
Robert Sprowson's avatar
Robert Sprowson committed
321 322 323
    if (loop == 0) /* the first character of the extension */
      name[index++] = file_sep; /* then place in the file_seperator */
    name[index++] = dentry->FILE_extension[loop];
Robert Sprowson's avatar
Robert Sprowson committed
324 325
  }

Robert Sprowson's avatar
Robert Sprowson committed
326 327 328
  /* terminate the name */
  name[index] = '\0';
  dprintf(("", "buildFILEname: got:%s\n", name));
Robert Sprowson's avatar
Robert Sprowson committed
329

Robert Sprowson's avatar
Robert Sprowson committed
330
  return name;
Robert Sprowson's avatar
Robert Sprowson committed
331 332
}

Robert Sprowson's avatar
Robert Sprowson committed
333 334 335 336 337 338
/*!
 * \brief  Calculates the LFN checksum of an 11 bytes DOS filename
 * \param  filename The filename to use
 * \return The checksum
 */
byte lfnchecksum(char *filename)
Robert Sprowson's avatar
Robert Sprowson committed
339
{
Robert Sprowson's avatar
Robert Sprowson committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353
  byte checksum = 0;
  byte lsb;
  int  i;

  for (i = 0; i < 11; i++)
  {
    lsb = (checksum & 0x1); /* Save the lsb */
    checksum = checksum >> 1; /* Shift the byte */
    lsb = lsb << 7; /* Turn saved lsb into msb */
    checksum |= lsb;
    checksum += filename[i];
  }

  return checksum;
Robert Sprowson's avatar
Robert Sprowson committed
354 355
}

Robert Sprowson's avatar
Robert Sprowson committed
356 357 358 359 360 361 362 363 364
/*!
 * \brief  Formulate a DOS short filename from a long one
 * \param  longfname The filename to start with
 * \param  shortname Pointer to a buffer for the short result
 * \param  shortnamebuff A temporary buffer
 * \param  cdir The current working directory, used to check for duplicates of the shortened name
 * \return Zero if the short name ends up being the same as the long one
 */
int shorten_lfn(char * longfname, char * shortname, char * shortnamebuff, DIR_info * cdir)
Robert Sprowson's avatar
Robert Sprowson committed
365
{
Robert Sprowson's avatar
Robert Sprowson committed
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
  // char * longfname : pointer to a null terminated file name
  // char * shortnamebuff : pointer to a 13 byte buffer to store newly created short file name
  // DIR_info * cdir : pointer the dir info structure for the target directory
  int i, j, o, retval, dotseen=0;
  char * extension,c;
  /* check if already a valid short file name */
  memset(shortnamebuff,' ',12);shortnamebuff[12]=0;
  dprintf(("","shorten_lfn: start with -%s-\n",longfname));
  for(i=0,j=0;(i<=12)&&(j<=12);i++,j++)
  {
    c=longfname[i];
    if(c=='.')
    {
      if(!dotseen)
      {
        dotseen=i+1;
        while(j<8)
        {
         shortnamebuff[j++]=' ';  // spacefill to 8
        }
      }
      if(i>8) break; // too many chars
    }
  dprintf(("","shorten_lfn: check -%x-%c- toupper(c) -%c-\n",c,c,toupper(c)));
    if(c && ((c=='.')||((c==toupper(c)) && (c!=' '))))
    { /* copy over */
      shortnamebuff[j]=c;
  dprintf(("","shorten_lfn: copied -%c-\n",c));
    }
    else
    {
      dprintf(("","shorten_lfn: %d %d %d\n",c,strlen(shortnamebuff),strlen(longfname)));
      if(!c && /*strlen(shortnamebuff)==strlen(longfname) && */((!dotseen && (i<9)) || dotseen))
      { /* end of string .. and identical */
        dprintf(("","shorten_lfn: its a valid DOS SFN\n"));
        retval= 0;
        goto slnfn1;
      }
      break;
    }
  }
Robert Sprowson's avatar
Robert Sprowson committed
407 408 409



Robert Sprowson's avatar
Robert Sprowson committed
410 411 412 413
  memset(shortnamebuff,'_',8);
  memset(&shortnamebuff[8],' ',4);
  shortnamebuff[12]=NULL;
  for(i=0,o=0;o<6;i++)
Robert Sprowson's avatar
Robert Sprowson committed
414
  {
Robert Sprowson's avatar
Robert Sprowson committed
415
   if(longfname[i]==NULL)
Robert Sprowson's avatar
Robert Sprowson committed
416
   {
Robert Sprowson's avatar
Robert Sprowson committed
417
    o=6;
Robert Sprowson's avatar
Robert Sprowson committed
418 419
    continue;
   }
Robert Sprowson's avatar
Robert Sprowson committed
420 421
   if(longfname[i]==' ') continue;
   if(longfname[i]=='.')
Robert Sprowson's avatar
Robert Sprowson committed
422
   {
Robert Sprowson's avatar
Robert Sprowson committed
423 424 425 426 427 428 429 430 431
    if( &(longfname[i]) == strrchr(longfname,'.') )
    {//If we are currently at the last 'dot' terminator in the filename...
     o=6;      //Ensuring we
     continue; //Drop out of the loop
    }else
    {
     i++;
     continue;
    }
Robert Sprowson's avatar
Robert Sprowson committed
432
   }
433
   if(strchr("+,;=[]", longfname[i]) != NULL)
Robert Sprowson's avatar
Robert Sprowson committed
434 435 436 437
   {
    shortnamebuff[o] = '_';
   }else
   {
Robert Sprowson's avatar
Robert Sprowson committed
438
    shortnamebuff[o] = toupper(longfname[i]);
Robert Sprowson's avatar
Robert Sprowson committed
439 440 441 442
   }
   o++;
  }

Robert Sprowson's avatar
Robert Sprowson committed
443 444 445 446 447 448 449 450 451 452 453 454
  dprintf(("","shorten_lfn: strchr(longfname,'.') = %p, c='%c'\n",strchr(longfname,'.'),*strchr(longfname,'.')));
  //We now have collected the first 6 characters of the short file name.
  if(extension=strrchr(longfname,'.'), extension != NULL) //There is a dot extension
  {//char * 'extension' points to the dot extension
   for(i=0,o=8;o<12;i++)
   {
    if(extension[i]==NULL)
    {
     o=12; //Break out of loop
     continue;
    }
    if(extension[i]==' ') continue;
455
    if(strchr("+,;=[]", extension[i]) != NULL)
Robert Sprowson's avatar
Robert Sprowson committed
456 457 458 459 460 461 462 463 464 465 466 467 468
    {
     shortnamebuff[o] = '_';
    }else
    {
     shortnamebuff[o] = toupper(extension[i]);
    }
    o++;
   }
  }
  shortnamebuff[6]='~';
  shortnamebuff[7]='1';
  shortnamebuff[12]=0;
  if(shortnamebuff[9]==' ') shortnamebuff[8]='\0';
Robert Sprowson's avatar
Robert Sprowson committed
469

Robert Sprowson's avatar
Robert Sprowson committed
470
  dprintf(("","Here file name is '%s'\n",shortnamebuff));
Robert Sprowson's avatar
Robert Sprowson committed
471

Robert Sprowson's avatar
Robert Sprowson committed
472 473 474 475
  DOS_direntry * fentry;
  int index;

  for(i=1;i<99;i++)
Robert Sprowson's avatar
Robert Sprowson committed
476
  {
Robert Sprowson's avatar
Robert Sprowson committed
477 478 479 480 481 482 483 484 485
   shortnamebuff[7]='0'+(i%10);
   if((i / 10) > 0)
   {
    shortnamebuff[6]='0'+(i/10);
    shortnamebuff[5]='~';
   }
   index=0;
   fentry=findDIRentry(shortnamebuff,cdir,cdir->dir_size,&index);
   if(fentry==NULL)
Robert Sprowson's avatar
Robert Sprowson committed
486
   {
Robert Sprowson's avatar
Robert Sprowson committed
487
    i=99;
Robert Sprowson's avatar
Robert Sprowson committed
488 489 490
    continue;
   }
  }
Robert Sprowson's avatar
Robert Sprowson committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511


  dprintf(("","shorten_lfn: Started with lfn \"%s\"\n",longfname));
  dprintf(("","shorten_lfn: Created short name \"%s\", char[8]= %x, char[9]= %x, char[10]= %x, char[11]= %x, char[12]= %x, char[13]= %x\n",shortnamebuff,shortnamebuff[8],shortnamebuff[9],shortnamebuff[10],shortnamebuff[11],shortnamebuff[12],shortnamebuff[13]));
  retval=1;
 slnfn1:
   memset(shortname,' ',11);
   memcpy(shortname,shortnamebuff,8);
   memcpy(&shortname[8],&shortnamebuff[9],3);
   if(shortnamebuff[8]=='.') i=8;
   else i=0;
   for(;i<11;i++)
   {
    if(shortnamebuff[i] == ' ')
    {
     shortnamebuff[i]=0;
     i=11;
     continue;
    }
   }
  return retval;
Robert Sprowson's avatar
Robert Sprowson committed
512 513
}

Robert Sprowson's avatar
Robert Sprowson committed
514 515 516 517 518 519 520
/*!
 * \brief  Build an array of long filename entries from its name and short name
 * \param  lfn Allocated array of dir entries
 * \param  numreq Number of dir entries allocated
 * \param  leafname The long name to encode
 * \param  shortname The short name to encode
 */
Robert Sprowson's avatar
Robert Sprowson committed
521 522
void MakeLFNEntries(DOS_direntry * lfn[],int numreq,char* leafname,char* shortname)
{
Robert Sprowson's avatar
Robert Sprowson committed
523 524 525 526 527 528 529 530 531 532 533
  int lfnnum, i;
  DOS_lfnentry *lfnentry;
  int charnum = 0;
  int nullreached = 0;

  for (i = (numreq - 2), lfnnum = 0; i >= 0; i--, lfnnum++)
  {
    /* Create the long file name structures */
    lfnentry = (DOS_lfnentry *)lfn[i];
    lfnentry->FILE_Ordinal = ((lfnnum & 0x3F) + 1);
    if (i == 0) lfnentry->FILE_Ordinal |= 0x40; /* Last entry */
534
    lfnentry->FILE_attribute = FILE_win95;
Robert Sprowson's avatar
Robert Sprowson committed
535 536

    /* Weave the name */
Robert Sprowson's avatar
Robert Sprowson committed
537 538
    lfnentry->FILE_uchar0_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar0    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
539
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
540 541 542
    charnum++;
    lfnentry->FILE_uchar1_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar1    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
543
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
544 545 546
    charnum++;
    lfnentry->FILE_uchar2_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar2    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
547
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
548 549 550
    charnum++;
    lfnentry->FILE_uchar3_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar3    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
551
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
552 553 554
    charnum++;
    lfnentry->FILE_uchar4_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar4    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
555
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
556 557 558
    charnum++;
    lfnentry->FILE_uchar5_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar5    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
559
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
560 561 562
    charnum++;
    lfnentry->FILE_uchar6_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar6    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
563
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
564 565 566
    charnum++;
    lfnentry->FILE_uchar7_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar7    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
567
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
568 569 570
    charnum++;
    lfnentry->FILE_uchar8_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar8    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
571
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
572 573 574
    charnum++;
    lfnentry->FILE_uchar9_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar9    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
575
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
576 577 578
    charnum++;
    lfnentry->FILE_uchar10_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar10    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
579
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
580 581 582
    charnum++;
    lfnentry->FILE_uchar11_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar11    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
583
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
584 585 586
    charnum++;
    lfnentry->FILE_uchar12_hi = (nullreached)?0xFF:0;
    lfnentry->FILE_uchar12    = (nullreached)?0xFF:leafname[charnum];
Robert Sprowson's avatar
Robert Sprowson committed
587
    if (leafname[charnum] == '\0') nullreached = 1;
Robert Sprowson's avatar
Robert Sprowson committed
588
    charnum++;
Robert Sprowson's avatar
Robert Sprowson committed
589

Robert Sprowson's avatar
Robert Sprowson committed
590
    lfnentry->FILE_checksum = lfnchecksum(shortname);
Robert Sprowson's avatar
Robert Sprowson committed
591
  }
Robert Sprowson's avatar
Robert Sprowson committed
592 593
}

Robert Sprowson's avatar
Robert Sprowson committed
594 595 596 597 598
/*!
 * \brief  Translate a standard RISC OS name into an extended DOS one
 * \param  source The RISC OS name
 * \param  dest Buffer to receive the DOS one
 * \return Destination buffer pointer
599
 */
Robert Sprowson's avatar
Robert Sprowson committed
600
char *convertRISCOStoLFN(char *source, char *dest)
601
{
Robert Sprowson's avatar
Robert Sprowson committed
602 603 604 605 606 607 608 609 610
  char *csptr = source;
  char *cdptr = dest;
  int   loop;
  int   filesepseen = FALSE; /* if we have seen the file seperator */
  char *cptr;      /* string pointer */
  char  lchr;      /* last character seen */
  int   point = 0; /* position where file extension started */

  if ((source == NULL) || (*source == '\0'))
611
  {
Robert Sprowson's avatar
Robert Sprowson committed
612 613 614
    dprintf(("", "DOSFS: convertRISCOStoLFN: NULL name\n"));
    *dest = '\0';
    return dest;
615 616
  }

Robert Sprowson's avatar
Robert Sprowson committed
617
  dprintf(("", "DOSFS: convertRISCOStoLFN: \"%s\"",source));
618

Robert Sprowson's avatar
Robert Sprowson committed
619 620 621
  lchr = '\0'; /* no last character */
  cptr = csptr;
  do
622
  {
Robert Sprowson's avatar
Robert Sprowson committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    /* Ensure that "/" characters do not appear at the start or end of the name
     * and that "//" sequences are trapped.
     */
//   if ((*cptr == '/' && (lchr == '\0' || lchr == *cptr || lchr == '.')) ||
//       ((lchr == '/') && ((*cptr == '\0') || (*cptr == '.')))
//      )
//     return_error1(char *,err_invalidname,source) ;

    lchr = *cptr++; /* remember this character */
  } while (lchr);

  if (*csptr == '$')             /* ROOT directory specifier */
  {
    csptr++;
    if (*csptr == '.')           /* RISC OS directory seperator */
638
    {
Robert Sprowson's avatar
Robert Sprowson committed
639 640
      *cdptr++ = dir_sep;        /* MSDOS directory seperator */
      csptr++;
641 642
    }
    else
Robert Sprowson's avatar
Robert Sprowson committed
643 644 645 646
      if (*csptr == '\0')
        *cdptr++ = dir_sep;
      else
        *cdptr++ = '$';
647 648
  }

Robert Sprowson's avatar
Robert Sprowson committed
649
  for (loop = 0;;)       /* convert the remainder of the pathname */
650
  {
Robert Sprowson's avatar
Robert Sprowson committed
651
    if (*csptr == '\0')  /* end of the source pathname */
652
    {
Robert Sprowson's avatar
Robert Sprowson committed
653 654
      *cdptr = '\0';     /* terminate the destination pathname */
      break;
655 656
    }

Robert Sprowson's avatar
Robert Sprowson committed
657
    switch (*csptr)
658
    {
Robert Sprowson's avatar
Robert Sprowson committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
      case '.':
        /* RISC OS to directory seperator */
        *cdptr++ = dir_sep;
        csptr++;
        loop = 0;
        filesepseen = FALSE; /* for this leafname */
        break;

      case '/':
        /* convert to file seperator */
//      if (filesepseen)
//        return_error1(char *, err_invalidname, source);
        *cdptr++ = file_sep;
        csptr++;
        loop++;
        point = loop;
//      filesepseen = TRUE;
        break;

      default:
        /* perform standard name mapping */
//      if (filesepseen)
//      {
//        char c = *csptr++;
        /* should never need to truncate the extension */
//        if ((loop - point) >= extsize)
//         return_error1(char *, err_invalidname, source);
//        if (islower(c))
//         c = toupper(c);
//        *cdptr++ = mapchar(c, ROmapping, DOSmapping);
//      }
//      else
//      {
        if (loop < 255)    /* characters left */
        {
          char c = *csptr;
//        if (islower(c))
//          c = toupper(c);
          *cdptr++ = mapchar(c, ROmapping, DOSmapping);
        }
        else
        {
          if (!(module_flags & TRUNCATE_NAMES))
            return_errorT(char *, err_nametoolong, tok_nametoolong, source, "255");
        }
        csptr++;      /* step over this DOS character */
//      }
      loop++;
      break;
     }
Neil Turton's avatar
Neil Turton committed
709 710
  }

Robert Sprowson's avatar
Robert Sprowson committed
711
  dprintf(("", " converted to \"%s\"\n", dest));
Neil Turton's avatar
Neil Turton committed
712

713
// for (cptr = dest; *cptr; cptr++)
Robert Sprowson's avatar
Robert Sprowson committed
714 715
//   if (!validchar(valchars,*cptr))
//     return_error1(char *, err_invalidname, source);
Neil Turton's avatar
Neil Turton committed
716

Robert Sprowson's avatar
Robert Sprowson committed
717
  return dest;
Neil Turton's avatar
Neil Turton committed
718 719
}

Robert Sprowson's avatar
Robert Sprowson committed
720 721 722 723 724
/*!
 * \brief  Translate an extended DOS name into a RISC OS one
 * \param  source The DOS name
 * \param  dest Buffer to receive the RISC OS one
 * \return Destination buffer pointer
Neil Turton's avatar
Neil Turton committed
725
 */
Robert Sprowson's avatar
Robert Sprowson committed
726
char *convertDOStoRISCOS(char *source, char *dest)
Neil Turton's avatar
Neil Turton committed
727
{
Robert Sprowson's avatar
Robert Sprowson committed
728 729 730
  char *csptr = source;
  char *cdptr = dest;
  int   loop;
Neil Turton's avatar
Neil Turton committed
731

Robert Sprowson's avatar
Robert Sprowson committed
732
  dprintf(("", "DOSFS: convertDOStoRISCOS: \"%s\" ",source));
Neil Turton's avatar
Neil Turton committed
733

Robert Sprowson's avatar
Robert Sprowson committed
734 735
  if (*csptr == dir_sep)
    *cdptr++ = '$';
Neil Turton's avatar
Neil Turton committed
736

Robert Sprowson's avatar
Robert Sprowson committed
737
  for (loop = 0;;)
Neil Turton's avatar
Neil Turton committed
738
  {
Robert Sprowson's avatar
Robert Sprowson committed
739
    if (*csptr =='\0')   /* end of the source pathname */
Neil Turton's avatar
Neil Turton committed
740
    {
Robert Sprowson's avatar
Robert Sprowson committed
741 742
      *cdptr = '\0';     /* terminate filename */
      break;             /* the for loop */
Neil Turton's avatar
Neil Turton committed
743 744
    }

Robert Sprowson's avatar
Robert Sprowson committed
745
    switch (*csptr)
Neil Turton's avatar
Neil Turton committed
746
    {
Robert Sprowson's avatar
Robert Sprowson committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
      case file_sep:
        /* convert the character to "/" */
        *cdptr++ = '/';
        csptr++;
        loop++;
        break;

      case dir_sep:
        /* convert to RISC OS directory seperator */
        *cdptr++ = '.';
        loop = 0;
        csptr++;
        break;

      default:
        /* perform standard name mapping */
        /* we never truncate here, it's up to the outside world */
        *cdptr++ = mapchar(*csptr, DOSmapping, ROmapping);
        csptr++;
        loop++;
        break;
Neil Turton's avatar
Neil Turton committed
768 769 770
    }
  }

Robert Sprowson's avatar
Robert Sprowson committed
771 772
  dprintf(("", "converted to \"%s\"\n", dest));
  return dest;
Neil Turton's avatar
Neil Turton committed
773
}