c程序獲取所有許可權
1. 怎樣用c語言編一個程序,使其只要運行即獲得管理員許可權。(在win7系統下)
emmm,我記得microsoft三件套就有這個特性
給一個從CSDN找到的代碼
#include windows.h//這里自己加上括弧
VOID ManagerRun(LPCSTR exe,LPCSTR param,INT nShow=SW_SHOW)
{ //注意:會跳出提示。
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "runas";
ShExecInfo.lpFile = exe;
ShExecInfo.lpParameters = param;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = nShow;
ShExecInfo.hInstApp = NULL;
BOOL ret = ShellExecuteEx(&ShExecInfo);
//等不及了,不等了。
CloseHandle(ShExecInfo.hProcess);
return;
}
int main(int argc,char *argv[])
{
if(argc == 1) //初次運行,即雙擊EXE
{
ShowWindow(GetConsoleWindow(),SW_HIDE);
ManagerRun(argv[0],"2");
return 1;
}else if(argc == 2) //再次運行,即上面那個ManagerRun
{
/*你的程序主代碼在此*/
}
return 0;
}
2. C語言的命令行程序的許可權
這就看你編譯器的許可權了,編譯器有管理員許可權,對應的控制台程序也就具有管理員許可權~
3. linux C語言如何得到一個文件的許可權並輸出
參考ln -l命令的輸出結果,編寫了以下程序(即輸出結果和ls -l命令的輸出結果相似),通過命令行傳入要查看的目錄,如果沒有傳入參數,則顯出當前目錄:
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <time.h>
char *getmod(mode_t mode,char *line) /*生成許可權描述字元串*/
{
memset(line,0,sizeof(char)*11);
strcat(line,S_ISDIR(mode)?"d":"-");
strcat(line,(mode&S_IRWXU)&S_IRUSR?"r":"-");
strcat(line,(mode&S_IRWXU)&S_IWUSR?"w":"-");
strcat(line,(mode&S_IRWXU)&S_IXUSR?"x":"-");
strcat(line,(mode&S_IRWXG)&S_IRGRP?"r":"-");
strcat(line,(mode&S_IRWXG)&S_IWGRP?"w":"-");
strcat(line,(mode&S_IRWXG)&S_IXGRP?"x":"-");
strcat(line,(mode&S_IRWXO)&S_IROTH?"r":"-");
strcat(line,(mode&S_IRWXO)&S_IWOTH?"w":"-");
strcat(line,(mode&S_IRWXO)&S_IXOTH?"x":"-");
return line;
}
char *directory(char *argv) /*從程序參數取出目錄*/
{
int i;
for (i=strlen(argv)-1;i;--i)
if (argv[i]=='/'){
argv[i+1]='\0';
break;
}
return argv;
}
int main(int argc,char *argv[])
{
DIR *dirp;
struct dirent *dirst;
struct stat finfo;
char *path,fname[512],mod[11],ctm[10];
struct passwd *user=NULL;
struct tm *ltm;
if (argc==1) path=directory(argv[0]);
else path=argv[1];
dirp=opendir(path);
if (!dirp)
{
fprintf(stderr,"ERROR\n");
exit(-1);
}
for (dirst=readdir(dirp);dirst;dirst=readdir(dirp))
{
strcpy(fname,path);
lstat(strcat(strcat(fname,"/"),dirst->d_name),&finfo);
user=getpwuid(finfo.st_uid);
printf("%s\t%10s\t",getmod(finfo.st_mode,mod),user->pw_name);
printf("%10d\t%9d\t",finfo.st_ino,finfo.st_size);
ltm=localtime(&finfo.st_mtime);
strftime(ctm,9,"%b",ltm);
printf("%5s",ctm);
strftime(ctm,9,"%d",ltm);
printf("%3s",ctm);
strftime(ctm,9,"%Y",ltm);
printf("%5s\t",ctm);
printf("%s\n",dirst->d_name);
}
closedir(dirp);
return 0;
}
4. C語言 讀取訪問許可權沖突 怎麼解決
運行三次抄後, 最後一次 找abcd沒找到, 返回的q是NULL
導致p=q + strlen("abcd");
指向出錯。
這個可以找不到後退出。
while(*p!='