alias.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 1983-2013 Martin Atkins, Richard Dobson and Composers Desktop Project Ltd
  3. * http://people.bath.ac.uk/masrwd
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /*alias.c: WIN32 only */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "alias.h"
  26. #ifndef _MAX_PATH
  27. #define _MAX_PATH (255)
  28. #endif
  29. #ifdef TESTING
  30. int main(int argc, char **argv)
  31. {
  32. char linkfile[_MAX_PATH];
  33. char path[_MAX_PATH];
  34. char desc[_MAX_PATH];
  35. char tmp[_MAX_PATH];
  36. char *dotp = NULL, *targetname;
  37. if(argc < 2){
  38. printf("\nusage: alias file.lnk");
  39. return 1;
  40. }
  41. linkfile[0] = path[0] = desc[0] = tmp[0] = '\0';
  42. strcpy(linkfile,argv[1]);
  43. targetname = linkfile;
  44. /*does name have .lnk extension?*/
  45. dotp = strrchr(linkfile, '.');
  46. if(dotp == NULL || (dotp != NULL && _stricmp(dotp, ".lnk") != 0)) {
  47. //add .lnk extension
  48. strcpy(tmp,linkfile);
  49. strcat(tmp,".lnk"); /*TODO: check strlen(linkfile) !*/
  50. targetname = tmp;
  51. }
  52. /* check if the file exists */
  53. if(!fileExists(targetname)) {
  54. printf("\ncannot find a link file with that name!");
  55. return 1;
  56. }
  57. /*and get the target */
  58. if(!getAliasInfo(targetname,path,desc)){
  59. printf("\nfailed to get link info: is it a real shortcut?");
  60. return 1;
  61. }
  62. printf("\nPath = %s\nDescr = %s\n",path,desc);
  63. return 0;
  64. }
  65. #endif
  66. /*return 0 for failure, 1 for success*/
  67. int getAliasName(const char *name, char *newname)
  68. {
  69. char *dotp = NULL, *targetname = (char *) name;
  70. char tmp[_MAX_PATH],desc[_MAX_PATH];
  71. if(name==NULL || newname==NULL)
  72. return 0;
  73. tmp[0] = '\0';
  74. dotp = strrchr(targetname, '.');
  75. if(dotp == NULL || (dotp != NULL && _stricmp(dotp, ".lnk") != 0)) {
  76. /*add .lnk extension */
  77. strcpy(tmp,name);
  78. strcat(tmp,".lnk"); /*TODO: check strlen(linkfile) !*/
  79. targetname = tmp;
  80. }
  81. /* check if the file exists */
  82. if(!fileExists(targetname)) {
  83. newname=NULL;
  84. return 0;
  85. }
  86. /*and get the target */
  87. if(!getAliasInfo(targetname,newname,desc)){
  88. newname[0] = '\0';
  89. return 0;
  90. }
  91. return 1;
  92. }