home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / ATTR2.ZIP / ATTR2.C next >
C/C++ Source or Header  |  1989-09-09  |  4KB  |  121 lines

  1. /***********************************************************************
  2. *  Attr2 - Modify file attributes for OS/2                             *
  3. *  Copyright (C) 1989 FreeLance Programming.  All rights reserved.     *
  4. ***********************************************************************/
  5.  
  6. void parse_a_parm(char *argv);
  7. void set_masks(unsigned short flag, char on_off);
  8.  
  9. #define INCL_DOS 1
  10.  
  11. #include <os2.h>
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h> 
  15. #include <string.h>
  16.  
  17. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  18.  
  19. unsigned short tot_files = 0, ANDMASK = 0xffff, ORMASK = 0, temp;
  20. char inspec[127], inpath[127], work[127];
  21.  
  22. HFILE                     dirhand;
  23. struct _FILEFINDBUF       dir_entry;
  24.  
  25. #define DIRSIZE (sizeof(struct _FILEFINDBUF))
  26.  
  27. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  28.  
  29. void main(int argc, char **argv)
  30. /***********************************************************************
  31. *  Attr2 - Modify file attributes for OS/2                             *
  32. ***********************************************************************/
  33. {
  34. register unsigned short x;
  35.  
  36. puts("Attr2 - Modify file attributes for OS/2  -  Version 1.0  - 09/09/89"
  37.      "\nCopyright (c) 1989 FreeLance Programming.  All rights reserved.\n");
  38.  
  39. if (argc < 2)
  40.   {
  41.   puts("Usage: Attr2 filename.ext [/R+] [/R-] [/H+] [H-] [S+] [/S-]\n\n"
  42.        "filename.ext may be any valid file specifier, including wildcards.\n"
  43.        "Any combination of switches may be used.  In the event of conflicting\n"
  44.        "specifications, the last one(s) specified will be used\n.");
  45.   exit(1);
  46.   }
  47.  
  48. for (x = 1; x < argc; x++)  parse_a_parm(argv[x]);
  49.  
  50. dirhand = temp = 1;
  51. if (DosFindFirst(&inspec[0], &dirhand, 0x0007, &dir_entry,
  52.                      (USHORT)DIRSIZE, (PUSHORT)&temp, 0L))
  53.   {
  54.   puts("Error - no files found to process.\n");  exit(1);
  55.   }
  56.  
  57. while (-1)
  58.   {
  59.   tot_files++;
  60.   sprintf(work, "%s%s", inpath, dir_entry.achName);
  61.  
  62.   x = (dir_entry.attrFile & ANDMASK) | ORMASK;
  63.   x = DosSetFileMode(work, (unsigned int)x, 0L);
  64.  
  65.   printf("%s - %s\n", dir_entry.achName, (x) ? "error" : "ok");
  66.  
  67.   if (DosFindNext(dirhand, &dir_entry,
  68.                   (USHORT)DIRSIZE, (PUSHORT)&temp))  break;
  69.   }
  70.  
  71. printf("\n%u files found.\n", tot_files);  exit(0);
  72. }
  73.  
  74. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  75.  
  76. void parse_a_parm(char *argv)
  77. /***********************************************************************
  78. *  Parse a single parmameter entry                                     *
  79. ***********************************************************************/
  80. {
  81. register int x;
  82.  
  83. if ((argv[0] == '-') || (argv[0] == '/'))
  84.   {
  85.   switch (toupper(argv[1]))
  86.     {
  87.     case '\x00': break;       /* End of parm */
  88.  
  89.     default : puts("Invalid option. Run Attr2 with no parameters "
  90.                    "for a list of valid options.");
  91.               exit(1);
  92.  
  93.     case 'R': set_masks(1, argv[2]);  break; 
  94.     case 'H': set_masks(2, argv[2]);  break; 
  95.     case 'S': set_masks(4, argv[2]);  break; 
  96.     }
  97.   }
  98. else
  99.   {
  100.   strcpy(inspec, argv);   x = strlen(inspec) - 1;
  101.   while ((x >= 0) &&
  102.          (inspec[x] != '\\') && (inspec[x] != ':')) x--;
  103.   strcpy(inpath, inspec);    inpath[++x] = '\0';
  104.   }
  105. }
  106.  
  107. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  108.  
  109. void set_masks(unsigned short flag, char on_off)
  110. /***********************************************************************
  111. *  Setup the ANDMASK and ORMASK fields based on user parms             *
  112. ***********************************************************************/
  113. {
  114. switch (on_off)
  115.   {
  116.   default:  printf("Uknown option : %c\n", on_off);  exit(1);
  117.   case '+': ORMASK |= flag;  break;
  118.   case '-': ORMASK &= ~flag;  ANDMASK &= ~flag;  break;
  119.   }
  120. }
  121.