home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / dos-chmod < prev    next >
Text File  |  1989-02-03  |  8KB  |  320 lines

  1. Path: xanth!mcnc!rutgers!ucsd!ames!necntc!ncoast!allbery
  2. From: pozar@hoptoad.UUCP (Tim Pozar)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i078: chmod for MS-DOS
  5. Message-ID: <4854@hoptoad.uucp>
  6. Date: 11 Jul 88 20:56:49 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: pozar@hoptoad.UUCP (Tim Pozar)
  9. Organization: Syncstream/Widget Systems (San Francisco)
  10. Lines: 307
  11. Approved: allbery@ncoast.UUCP
  12.  
  13. Posting-number: Volume 3, Issue 78
  14. Submitted-by: "Tim Pozar" <pozar@hoptoad.UUCP>
  15. Archive-name: dos-chmod
  16.  
  17. [Not shar'ed; I assume that unshar'ers for MS-DOS are not in common use. ++bsa]
  18.  
  19. /*
  20.     CHMOD 
  21.  
  22.     For MS-DOS.
  23.  
  24.  
  25.     11.July.1988
  26.  
  27.     This program is Freeware.  You can use it to your hearts content without
  28.     the feeling you are ripping me off.  If you redistribute this source or
  29.     binary, the copywrite statement must be included.  Please see the help
  30.     display for more info.
  31.  
  32.     I ripped off some of the "interface" from the UN*X CHMOD.  I am also 
  33.     waiting for AT&T to sue me for "look-and-feel".  
  34.  
  35.     I am giving out the source so you can compile it your-self, or as an
  36.     education on how to do certain hacks.
  37.  
  38.     Some things you may want to check out are how to do file searches without
  39.     going through Microsoft C 5.x's brain damaged _dos_findfirst() and 
  40.     _dos_findnext() functions.  Also you may want to rip off the code to get 
  41.     the time and date out of MS-DOS's packed time and date ints.
  42.  
  43.                  Tim
  44.  
  45.     If you want to send me fan mail:
  46.  
  47.     Tim Pozar
  48.     KKSF
  49.     77 Maiden Lane 
  50.     San Francisco CA 94131
  51.     (415) 788-2022
  52.  
  53.     FidoNet  1:125/406
  54.     Internet Tim_Pozar@fidogate.fidonet.org
  55.     Usenet   {lll-winken, hoptoad}!fidogate!pozar
  56. */
  57.  
  58. /*
  59.     Makefile:
  60. --- cut here ---
  61. chmod.exe:    chmod.c
  62.     cl /Zp chmod.c
  63. --- cut here ---
  64.  
  65. */
  66.  
  67. #include <string.h>
  68. #include <stdio.h>
  69. #include <dos.h>
  70.  
  71.     /* Attribute bits */
  72. #define RO 0x01        /* Read only attribute. */
  73. #define HI 0x02        /* Hidden attribute, */
  74. #define SY 0x04        /* System attribute, */
  75. #define VO 0x08        /* Volume ID */
  76. #define DI 0x10        /* Subdirectory */
  77. #define AR 0x20        /* Archive, */
  78. #define U1 0x40        /* Unused */
  79. #define U2 0x80        /* Unused */
  80.  
  81. #define SS 80        /* A string length */
  82.  
  83. /* Structure for find first/next (find()).
  84. NOTE: the DPB pointer is a double word 
  85. character pointer; here it is declared as a
  86. long, to avoid problems with the various
  87. size of char *'s.
  88. */
  89.  
  90. struct _xfbuf {
  91.  
  92. char s_attrib;        /* Search attribute */
  93.  
  94. /* These do not need to be initialized */
  95.  
  96. char drive;        /* 0 == current, 1 == A: ... */
  97. char fcbname[11];    /* FCB name, */
  98. unsigned ent;
  99. long dpb_ptr;
  100. unsigned dirstart;
  101.  
  102. /* These are returned by DOS */
  103. char f_attrib;        /* found attribute, */
  104. unsigned time;        /* Packed time, */
  105. unsigned date;        /* Packed date */
  106. long fsize;        /* file size, */
  107. char name[13];        /* found name */
  108. };
  109.  
  110. int ver = 1;        /* version number */
  111. int rev = 0;        /* revision number */
  112.  
  113. int findflag = 0;
  114. int year,month,day,hour,minute,second;
  115.  
  116. main(argc,argv)
  117. int argc;
  118. char *argv[];
  119. {
  120. char findname[SS];        /* Search name, may contain wild cards */
  121. char filename[SS];        /* Specific file name, no wild cards */
  122. int att;
  123. int i;
  124. int result;
  125. struct _xfbuf xfbuf;
  126.  
  127. i = 0;
  128.  
  129. printf("CHMOD %d.%d    Copyright 1988 Tim Pozar    FidoNet 1:125/406\n\n",ver,rev);
  130.  
  131. if (argc <2){
  132.   printf(" Syntax:\n");
  133.   printf("%s [+-][ahsw] filename\n",argv[0]);
  134.   printf(" Where\n");
  135.   printf("   + = set bit or \"turn-on\"\n");
  136.   printf("   - = reset bit or \"turn-off\"\n");
  137.   printf("   a = Archive - Indicates file has NOT been backed up.\n");
  138.   printf("   h = Hidden  - Hides file from directory listings.\n");
  139.   printf("   s = System  - A hold over from CP/M.  Will hide files too.\n");
  140.   printf("   w = Write   - Turns off or on Write permission.\n");
  141.   printf("                 All files have Read permission.\n");
  142.   printf("   Only one bit can be toggled at once, but \"filename\" can \n");
  143.   printf("   contain wildcards for global settings.\n");
  144.   printf("\n");
  145.   printf("   If you only specify the file name, you will get\n");
  146.   printf("   a listing of the files with their attributes.\n");
  147.   printf("\n");
  148.   printf("   If you feel like you have to give money to some one for this\n");
  149.   printf("   program, please send money to your local AIDS support group.\n");
  150.   exit(1);    /* Thanks to Wynn Wagner for the idea. */
  151. }else
  152.   if (argc <3){
  153.     strcpy(findname,argv[1]);
  154.   } else {
  155.     strcpy(findname,argv[2]);
  156.   }
  157.  
  158. printf("A = Archive, H = Hidden, S = System, R = Read Only\n");
  159. printf("    Filename    Atrb   Size     Date       Time\n");
  160. printf("  ------------  ----  ------  --------  ----------\n");
  161.  
  162. while (find(findname,&xfbuf)) {
  163.   strcpy(filename,xfbuf.name);       /* get the found file name */
  164.   printf("  %s",filename);
  165.  
  166.   for(i=0;i <= 13-strlen(filename);i++){
  167.     printf(" ");            /* make the table nice and pretty */
  168.   }
  169.  
  170.   att = getatt(filename);        /* get the attribute byte */
  171.  
  172.   if (argc == 3){
  173.     switch (argv[1][0]){
  174.       case '+':
  175.         if (argv[1][1] == 'a'){
  176.           putatt(filename, att | AR);
  177.         }
  178.         if (argv[1][1] == 'h'){
  179.           putatt(filename, att | HI);
  180.         }
  181.         if (argv[1][1] == 's'){
  182.           putatt(filename, att | SY);
  183.         }
  184.         if (argv[1][1] == 'w'){
  185.           putatt(filename, att & (~RO));
  186.         }
  187.         break;
  188.  
  189.       case '-':
  190.         if (argv[1][1] == 'a'){
  191.           putatt(filename, att & (~AR));
  192.         }
  193.         if (argv[1][1] == 'h'){
  194.           putatt(filename, att & (~HI));
  195.         }
  196.         if (argv[1][1] == 's'){
  197.           putatt(filename, att & (~SY));
  198.         }
  199.         if (argv[1][1] == 'w'){
  200.           putatt(filename, att | RO);
  201.         }
  202.         break;
  203.  
  204.       default:
  205.         printf("I don't reconize %s as a valid change switch.",argv[1]);
  206.         exit(1);
  207.  
  208.     }
  209.   }
  210.  
  211.   att = getatt(filename);
  212.  
  213.   if (att & AR) printf("A"); else printf(".");
  214.   if (att & HI) printf("H"); else printf(".");
  215.   if (att & SY) printf("S"); else printf(".");
  216.   if (att & RO) printf("R"); else printf(".");
  217.  
  218.   printf(" %7lu ",xfbuf.fsize);
  219.  
  220.   /* This is the unpacker for Messy-DOS packed time and date ints. */
  221.   year = xfbuf.date >> 9;
  222.   month = (xfbuf.date >> 5) & 15;
  223.   day = xfbuf.date & 31;
  224.   printf(" %2u-%02u-%02u ",month,day,year+80);
  225.  
  226.   hour = xfbuf.time >> 11;
  227.   minute = (xfbuf.time >> 5) & 63;
  228.   second = (xfbuf.time & 31) << 1;
  229.   if (hour > 12){
  230.     printf(" %2u:%02u:%02upm ",hour-12,minute,second);
  231.   }else{
  232.     printf(" %2u:%02u:%02uam ",hour,minute,second);
  233.   }
  234.  
  235.   printf("\n");
  236.  
  237. }
  238.  
  239. exit(0);
  240.  
  241. } /* end main */
  242.  
  243.  
  244. find(pathname,pxfbuf)
  245.  
  246. char *pathname;
  247. struct _xfbuf *pxfbuf;
  248.  
  249. {
  250. union REGS inregs, outregs;
  251. unsigned int seg_val;
  252. unsigned int off_val;
  253. int found;
  254.  
  255. seg_val = FP_SEG(pxfbuf);
  256. off_val = FP_OFF(pxfbuf);    /* get the address of the structure */
  257.  
  258. inregs.x.dx = off_val;
  259. inregs.h.ah = 26;
  260. intdos(&inregs,&outregs);       /* set up DTA */
  261.  
  262. inregs.x.dx = FP_OFF(pathname);
  263. inregs.x.cx = RO+HI+SY+AR;    /* the search atributes */
  264. if (findflag == 0) {
  265.     inregs.h.ah = 78;     /* if this is the first time FIND FIRST */
  266.     findflag = 1;
  267. } else {
  268.     inregs.h.ah = 79;     /* if this is not the first time FIND NEXT */
  269. }
  270.  
  271. intdos(&inregs,&outregs);
  272.  
  273. found = outregs.x.ax;
  274.  
  275. if (found)
  276.   return (0);
  277.  
  278. return(1);
  279.  
  280. }
  281.  
  282. /*
  283.    Get attribute byte from filename.
  284.  */
  285. getatt(name)
  286. char *name;
  287. {
  288. union REGS inregs, outregs;
  289.  
  290. inregs.x.dx = FP_OFF(name);
  291. inregs.h.ah = 67;
  292. inregs.h.al = 0;
  293. intdos(&inregs,&outregs);
  294.  
  295. return(outregs.x.cx);
  296. }
  297.  
  298. /*
  299.    Put attibute bye with filename.
  300. */
  301. putatt(name,attribute)
  302. char *name;
  303. int attribute;
  304. {
  305. union REGS inregs, outregs;
  306.  
  307. inregs.x.dx = FP_OFF(name);
  308. inregs.x.cx = attribute;
  309. inregs.h.ah = 67;
  310. inregs.h.al = 1;
  311. intdos(&inregs,&outregs);
  312.  
  313. return(outregs.x.ax);
  314. }
  315. -- 
  316.  ...sun!hoptoad!\                                     Tim Pozar
  317.                  >fidogate!pozar               Fido:  1:125/406
  318.   ...lll-winken!/                            PaBell:  (415) 788-3904
  319.        USNail:  KKSF / 77 Maiden Lane /  San Francisco CA 94108
  320.