home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / src / attrib.c next >
C/C++ Source or Header  |  1999-01-04  |  3KB  |  103 lines

  1. /* attrib command for OS/2
  2.  
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.  
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  
  17. */
  18.  
  19. #include <config.h>
  20.  
  21. #define INCL_DOSFILEMGR
  22. #include <os2.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. /* for chmod and stat */
  26. #include <io.h>
  27. #include <sys\types.h>
  28. #include <sys\stat.h>
  29.  
  30. int get_attrib (char *filename)
  31. {
  32.     mode_t st;
  33.  
  34.     HFILE       fHandle    = 0L;
  35.     ULONG       fInfoLevel = 1;  /* 1st Level Info: Standard attributs */
  36.     FILESTATUS3 fInfoBuf;
  37.     ULONG       fInfoBufSize;
  38.     ULONG      fAction    = 0;
  39.     APIRET      rc;
  40.  
  41.     fInfoBufSize = sizeof(FILESTATUS3);
  42.     rc = DosOpen((PSZ) filename,
  43.                  &fHandle,
  44.                  &fAction,
  45.                  (ULONG) 0,
  46.                  FILE_NORMAL,
  47.                  OPEN_ACTION_OPEN_IF_EXISTS,
  48.                  (OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE),
  49.                  (PEAOP2) NULL);
  50.     if (rc != 0) {
  51.        return -1;
  52.     }
  53.  
  54.     rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
  55.     DosClose(fHandle);
  56.     if (rc != 0) {
  57.        return -1;  /* error ! */
  58.     } else {
  59.        st = fInfoBuf.attrFile;
  60.     }
  61.  
  62.     if (st & FILE_DIRECTORY)
  63.         st = -1;
  64.     return st;
  65. }
  66.  
  67.  
  68. int set_attrib (char *filename, ULONG st)
  69. {
  70.     HFILE       fHandle    = 0L;
  71.     ULONG       fInfoLevel = 1;  /* 1st Level Info: Standard attributs */
  72.     FILESTATUS3 fInfoBuf;
  73.     ULONG       fInfoBufSize;
  74.     ULONG       fAction    = 0L;
  75.     APIRET      rc;
  76.  
  77.     if (!(st & FILE_READONLY))
  78.        chmod(filename, (S_IWRITE | S_IREAD));
  79.     fInfoBufSize = sizeof(FILESTATUS3);
  80.     rc = DosOpen((PSZ) filename,
  81.                  &fHandle,
  82.                  &fAction,
  83.                  (ULONG) 0,
  84.                  FILE_NORMAL,
  85.                  OPEN_ACTION_OPEN_IF_EXISTS,
  86.                  (OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE),
  87.                  0L);
  88.     if (rc != 0) {
  89.        return rc;
  90.     }
  91.  
  92.     rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
  93.     if (rc!=0) {
  94.        DosClose(fHandle);
  95.        return rc;
  96.     }
  97.     fInfoBuf.attrFile = st;
  98.     rc = DosSetFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
  99.     rc = DosClose(fHandle);
  100.     return rc;
  101. }
  102.  
  103.