chmod.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:6k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
- // Copyright 1996 by Jarle Aase. All rights reserved.
- // See the "War Software Series Licende Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : chmod.cpp
- // PURPOSE : extended UNIX chmod command
- // PROGRAM :
- // DATE : Oct. 13 1996
- // AUTHOR : Jarle Aase
- // ---
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include "WarDaemon.h"
- #include "Unix.h"
- #include "FsysSecurity.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CChmod::CChmod()
- {
- set = NULL;
- m_Owner = -1;
- m_Group = -1;
- m_Comment = NULL;
- m_DefDirPerms = -1;
- m_DefFilePerms = -1;
- omode = -1;
- m_SetMode = FALSE;
- }
- CChmod::~CChmod()
- {
- if (set)
- free(set);
- }
- int CChmod::main(int argc, char **argv)
- {
- char *ep, *mode;
- BOOL Alternate = FALSE;
- set = NULL;
- omode = 0;
- Hflag = Lflag = Pflag = Rflag = fflag = hflag = 0;
- while ((ch = getopt(argc, argv, "O:G:C:D:F:HLPRXfgorstuwx")) != EOF)
- switch (ch)
- {
- case 'O': // chown
- if (!optarg || !*optarg)
- goto badarg;
- if ((m_Owner = CUsr::FindUser(UT_USER, optarg)) == 0)
- {
- printf("Unknown user name '%s'n", optarg);
- return -1;
- }
- Alternate = TRUE;
- break;
- case 'G': // chown
- if (!optarg || !*optarg)
- goto badarg;
- if ((m_Group = CUsr::FindUser(UT_CLASS, optarg)) == 0)
- {
- printf("Unknown class name '%s'n", optarg);
- return -1;
- }
- Alternate = TRUE;
- break;
- case 'C': // change comment
- if (!optarg)
- goto badarg;
- m_Comment = optarg;
- Alternate = TRUE;
- break;
- case 'D':
- if (!optarg || !*optarg || (*optarg < '0') || (*optarg > '8'))
- goto badarg;
- m_DefDirPerms = strtol(optarg, &ep, 8);
- Alternate = TRUE;
- break;
- case 'F':
- if (!optarg || !*optarg || (*optarg < '0') || (*optarg > '8'))
- goto badarg;
- m_DefFilePerms = strtol(optarg, &ep, 8);
- Alternate = TRUE;
- break;
- case 'H':
- Hflag = 1;
- Lflag = Pflag = 0;
- break;
- case 'L':
- Lflag = 1;
- Hflag = Pflag = 0;
- break;
- case 'P':
- Pflag = 1;
- Hflag = Lflag = 0;
- break;
- case 'R':
- Rflag = 1;
- break;
- case 'f': /* XXX: undocumented. */
- fflag = 1;
- break;
- case 'h':
- /*
- * In System V (and probably POSIX.2) the -h option
- * causes chmod to change the mode of the symbolic
- * link. 4.4BSD's symbolic links don't have modes,
- * so it's an undocumented noop. Do syntax checking,
- * though.
- */
- hflag = 1;
- break;
- /*
- * XXX
- * "-[rwx]" are valid mode commands. If they are the entire
- * argument, getopt has moved past them, so decrement optind.
- * Regardless, we're done argument processing.
- */
- case 'g': case 'o': case 'r': case 's':
- case 't': case 'u': case 'w': case 'X': case 'x':
- if (argv[optind - 1][0] == '-' &&
- argv[optind - 1][1] == ch &&
- argv[optind - 1][2] == ' ')
- --optind;
- Alternate = FALSE;
- goto done;
- case '?':
- default:
- return usage();
- }
- done:
- argv += optind;
- argc -= optind;
- if (Rflag)
- {
- if (hflag)
- {
- printf("the -R and -h options may not be specified together.");
- return usage();
- }
- }
- if (!Alternate)
- {
- if (argc < 2)
- return usage();
- m_SetMode = TRUE;
- mode = *argv;
- --argc;
- ++argv;
- if (*mode >= '0' && *mode <= '7')
- {
- val = strtol(mode, &ep, 8);
- if (val > INT_MAX || val < 0)
- {
- printf("invalid file mode: %s", mode);
- return 1;
- }
- if (*ep)
- {
- printf("invalid file mode: %s", mode);
- return 1;
- }
- omode = val;
- oct = 1;
- }
- else
- {
- if ((set = setmode(mode)) == NULL)
- {
- printf("invalid file mode: %s", mode);
- return 1;
- }
- oct = 0;
- }
- }
- return Process(argc, argv);
- badarg:
- return usage();
- }
- int CChmod::Process(int argc, char **argv)
- {
- BOOL UsePrint = TRUE;
- CString cShowPath, cCurrentPath;
- LPSTR p;
- ASSERT(argc >= 1);
- for(;argc ; argc--, argv++)
- {
- CFileInfoList FileList;
- if (!strcmp(*argv,".\") || !strcmp(*argv,"."))
- {
- cCurrentPath = ".";
- cShowPath = "";
- }
- else
- cCurrentPath = cShowPath = *argv;
- p = cShowPath.GetBuffer(1);
- while (*p)
- {
- if (*p == '\')
- *p = '/';
- ++p;
- }
- LPSTR xpath;
- xpath = strrchr(cShowPath,'\');
- if (xpath)
- *xpath++= 0;
- cShowPath.ReleaseBuffer();
- ASSERT(AfxIsValidString(*argv));
- BldFileInfoList(FileList, *argv, FALSE);
- if (!ChmodFileList(FileList, xpath ? cShowPath : NULL))
- return -1;
- if (Rflag)
- {
- // Grab subdirs too
- CFileInfo *Info;
- LPSTR buf = new char[MAX_PATH];
- *buf = 0;
- int Rval;
- for(Info = NULL; Info = FileList.GetNext(Info); )
- {
- if (IS_DIRECTORY(Info) && strcmp(Info->FileName(),".") && strcmp(Info->FileName(),".."))
- {
- CString cBuf;
- cBuf.Format("%s\%s", cCurrentPath, Info->FileName());
- LPSTR MyArgs[1];
- MyArgs[0] = cBuf.GetBuffer(1);
- if (Rval = Process(1, MyArgs))
- return Rval; // Error
- }
- }
- }
- }
- return 0;
- }
- BOOL CChmod::ChmodFileList(CFileInfoList& FileList, LPCSTR Path)
- {
- CFileInfo *Info;
- LPCSTR FileName;
- char MyPath[MAX_PATH * 2];
- for(Info = NULL; Info = FileList.GetNext(Info);)
- {
- FileName = Info->FileName();
- ASSERT(AfxIsValidString(FileName));
- if (!strcmp(FileName,".") || !strcmp(FileName,".."))
- continue;
- //MkPath(Path,FileName,MyPath);
- if (chmod(MyPath,
- m_SetMode ? oct ? omode : getmode(set,Info->m_Flags) : -1,
- m_Owner, m_Group, m_Comment, m_DefDirPerms, m_DefFilePerms))
- {
- printf("chmod: %s - mode change failed %sn", Path, GetLastErrorText());
- }
- }
- return TRUE;
- }
- int CChmod::usage()
- {
- printf("usage: chmod [-R [-H | -L | -P]] mode file ...n");
- printf(" or chmod [-O Owner] [-G Class] [-C "Comment"] [-D DefDirPerms] [-F DefFilePerms] file ...n");
- return 1;
- }