C語言實現可變引數列表的system介面:巨集__VA_ARGS__
目錄
t_shell.h
/* file name: t_shell.h
* author: Rong Tao
* create time: 2018.11.14
*
*/
#ifndef _T_SYS_SHELL_CMD_H_
#define _T_SYS_SHELL_CMD_H_
#ifndef _T_SHELL_STRING_MAXLEN_
#define _T_SHELL_STRING_MAXLEN_ 1024
#endif
#define t_sh_cmd(fmt, ...) _t_execfmt2string_(NULL, fmt, __VA_ARGS__)
/**
* Execute a format string by shell command
* Author: rongtao
* Time: 2018.11.15
*
*/
int _t_execfmt2string_(char *string, const char *fmt, ...);
#endif /*<_T_SYS_SHELL_CMD_H_>*/
t_shell.c
/* file name: t_shell.c
* author: Rong Tao
* create time: 2018.11.15
* create time: 2018.11.17
*
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <libgen.h>
#include "t_shell.h"
#include "t_user.h"
#define t_shell_getlogin() getlogin()
#define t_shell_getcwd(buf,size) getcwd(buf,size)
#ifndef _T_SHELL_COLOR_RED__
#define _T_SHELL_COLOR_RED__ "\033[31m"
#endif
#ifndef _T_SHELL_COLOR_GREEN__
#define _T_SHELL_COLOR_GREEN__ "\033[32m"
#endif
#ifndef _T_SHELL_COLOR_END__
#define _T_SHELL_COLOR_END__ "\033[0m"
#endif
FILE *t_fp_sys_shell;
/**
* Execute a format string by shell command
* Author: rongtao
* Time: 2018.11.15
*
*/
int _t_execfmt2string_(char *string, const char *fmt, ...)
{
if((string = (char*)malloc(_T_SHELL_STRING_MAXLEN_)) == NULL)
{
return -1;
}
va_list arg;
va_start(arg, fmt);
vsprintf(string, fmt, arg);
va_end(arg);
char folder[_T_SHELL_STRING_MAXLEN_];
t_shell_getcwd(folder, _T_SHELL_STRING_MAXLEN_);
char *user = t_getcurrentusername(NULL);
char *grp = t_getcurrentgrpname(NULL);
char *shell = t_getcurrentusershell(NULL);
fprintf(stdout, "%s[%s@%s-%s %s]%c %s",
strcmp(user, "root")==0?_T_SHELL_COLOR_RED__:_T_SHELL_COLOR_GREEN__,
user,grp,
basename(shell),
basename((char*)folder), strcmp(user, "root")==0?'#':'$',
_T_SHELL_COLOR_END__);
fprintf(stdout, "%s\n", string);
system(string);
free(string);
free(user);
free(grp);
free(shell);
return 0;
}
#if 1
int main()
{
t_sh_cmd("ls %s; mkdir %s; rm -rf %s", "/home/rongtao", "1a.out", "1a.out");
}
#endif
效果
$ gcc t_shell.c t_user.c
$ ./a.out
$ ls /home/rongtao; mkdir 1a.out; rm -rf 1a.out
clean.sh 文件
實際上的效果
今天發現個好玩的linux註釋:
相關文章
- C語言怎麼實現可變引數C語言
- C語言可變引數詳解C語言
- C可變引數的實現
- php實現函式可變引數列表PHP函式
- C可變引數函式 實現函式
- C語言預設引數值的實現C語言
- PHP 函式可變數量的引數列表PHP函式變數
- c語言巨集的使用C語言
- 關於C99可變引數巨集的例項程式碼講解
- 遞迴函式,可變引數列表遞迴函式
- C++反射機制:可變引數模板實現C++反射C++反射
- C語言可變引數以及printf()、sprintf()、vsprintf() 的區別與聯絡C語言
- [ASM C/C++] C語言函式的可選性自變數ASMC++C語言函式變數
- C++反射機制:可變引數模板實現C++反射薦C++反射
- C++反射機制:可變引數模板實現C++反射(二)C++反射
- C語言(巨集定義)C語言
- C++ 函式的可變引數C++函式
- C 可變長引數 VS C++11 可變長模板C++
- C語言巨集中"#"和"##"的用法C語言
- C語言異常與斷言介面的實現C語言
- C++逆向 可變引數HookC++Hook
- C++11 可變引數模板C++
- java基礎(九) 可變引數列表介紹Java
- 對 “C語言指標變數作為函式引數” 的個人理解C語言指標變數函式
- C語言巨集的高階應用C語言
- 淺談C#可變引數paramsC#
- C#實現多語言介面程式的方法介紹C#
- C語言_結構體變數指標做函式引數的使用案例C語言結構體變數指標函式
- C語言sizeof()變數、字元、字串C語言變數字元字串
- iOS可變引數(不定引數)的用法iOS
- 如何設計和實現一個C庫 -- 《C語言介面與實現:建立可重用軟體的技術 》書評C語言
- 複數的四則運算(C語言實現)C語言
- ORACLE 數字演算法的C語言實現Oracle演算法C語言
- C 語言是怎樣實現儲存一個 PHP5 的變數?PHP變數
- 清華尹成帶你實戰GO案例(43)Go 可變長引數列表Go
- GO語言————6.3 傳遞變長引數Go
- Swift 呼叫 Objective-C 的可變引數函式SwiftObject函式
- 【Java】可變引數Java