日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

system 函数被废除的替代方法

發(fā)布時(shí)間:2024/4/18 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 system 函数被废除的替代方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

做越獄應(yīng)用和插件開發(fā),經(jīng)常會(huì)調(diào)用 system 去執(zhí)行系統(tǒng)命令,早在 Xcode 7,使用 system 函數(shù)提示警告:

'system' is deprecated: first deprecated in iOS 8.0 - Use posix_spawn APIs installd

只是警告,還是可以正常編譯和使用,但是升級(jí)到 Xcode 9,system 函數(shù)就從 SDK 中移除了,不能再使用了,提示:

'system' is unavailable: not available on iOS

替代的方法一般有三種,第一種是使用 posix_spawn,代碼如下:

pid_t pid; char *argv[] = {"/bin/ls", //path"-al", //parameter1"/", //parameter2NULL };posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL);printf("pid=%d,child pid = %d\n",getpid(),pid);int stat; waitpid(pid,&stat,0); printf("stat is %d\n",stat);

第二種是使用 NSTask,代碼如下:

NSTask *task = [[NSTask alloc] init]; task.launchPath = @"/bin/ls"; task.arguments = [NSArray arrayWithObjects:@"-al",@"/",nil]; [task launch]; [task waitUntilExit];

NSTask.h 頭文件信息如下:

#import <Foundation/NSObject.h>@class NSString, NSArray, NSDictionary;@interface NSTask : NSObject// Create an NSTask which can be run at a later time // An NSTask can only be run once. Subsequent attempts to // run an NSTask will raise. // Upon task death a notification will be sent // { Name = NSTaskDidTerminateNotification; object = task; } //- (instancetype)init;// set parameters // these methods can only be done before a launch // if not set, use current // if not set, use current// set standard I/O channels; may be either an NSFileHandle or an NSPipe - (void)setStandardInput:(id)input; - (void)setStandardOutput:(id)output; - (void)setStandardError:(id)error;// get parameters @property (NS_NONATOMIC_IOSONLY, copy) NSString *launchPath; @property (NS_NONATOMIC_IOSONLY, copy) NSArray *arguments; @property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *environment; @property (NS_NONATOMIC_IOSONLY, copy) NSString *currentDirectoryPath;// get standard I/O channels; could be either an NSFileHandle or an NSPipe - (id)standardInput; - (id)standardOutput; - (id)standardError;// actions - (void)launch;- (void)interrupt; // Not always possible. Sends SIGINT. - (void)terminate; // Not always possible. Sends SIGTERM.@property (NS_NONATOMIC_IOSONLY, readonly) BOOL suspend; @property (NS_NONATOMIC_IOSONLY, readonly) BOOL resume;// status @property (NS_NONATOMIC_IOSONLY, readonly) int processIdentifier; @property (NS_NONATOMIC_IOSONLY, getter=isRunning, readonly) BOOL running;@property (NS_NONATOMIC_IOSONLY, readonly) int terminationStatus;@end@interface NSTask (NSTaskConveniences)+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments; // convenience; create and launch- (void)waitUntilExit; // poll the runLoop in defaultMode until task completes@endFOUNDATION_EXPORT NSString * const NSTaskDidTerminateNotification;

如果非要調(diào)用 system 函數(shù)不可,那就使用第三種方法,找到 system 函數(shù)地址直接調(diào)用,方法參見:?動(dòng)態(tài)調(diào)用函數(shù),具體代碼如下:

typedef int (*my_system) (const char *str); int call_system(const char *str){//動(dòng)態(tài)庫路徑char *dylib_path = "/usr/lib/libSystem.dylib";//打開動(dòng)態(tài)庫void *handle = dlopen(dylib_path, RTLD_GLOBAL | RTLD_NOW);if (handle == NULL) {//打開動(dòng)態(tài)庫出錯(cuò)fprintf(stderr, "%s\n", dlerror());} else {//獲取 system 地址my_system system = dlsym(handle, "system");//地址獲取成功則調(diào)用if (system) {int ret = system(str);return ret;}dlclose(handle); //關(guān)閉句柄}return -1; }

這樣 call_system 函數(shù)就相當(dāng)于 system 的功能了,替換即可。

原文地址:https://www.exchen.net/ios-hacker-system-%E5%87%BD%E6%95%B0%E8%A2%AB%E5%BA%9F%E9%99%A4%E7%9A%84%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%B3%95.html

總結(jié)

以上是生活随笔為你收集整理的system 函数被废除的替代方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。