我有一个奇怪的问题。对于家庭作业,我正在创建自己的基本bash脚本,但是当运行execv()来运行我的cp命令时,它不起作用。它适用于ls和组,但不适用于cp。我搞乱了我的cp程序以隔离错误,它确实是execv命令。
func createAlertView() -> UIAlertController {
let view = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(250), height: CGFloat(100)))
let itemName = UITextField(frame: CGRect(x: CGFloat(10), y: CGFloat(0), width: CGFloat(252), height: CGFloat(25)))
itemName.borderStyle = .RoundedRect
itemName.placeholder = "Item Name"
itemName.keyboardAppearance = .Alert
itemName.delegate = self
view.addSubview(itemName)
let itemPrice = UITextField(frame: CGRect(x: CGFloat(10), y: CGFloat(30), width: CGFloat(252), height: CGFloat(25)))
itemPrice.placeholder = "Item Price"
itemPrice.borderStyle = .RoundedRect
itemPrice.keyboardAppearance = .Alert
itemPrice.delegate = self
view.addSubview(itemPrice)
let itemQuantity = UITextField(frame: CGRect(x: CGFloat(10), y: CGFloat(60), width: CGFloat(252), height: CGFloat(25)))
itemQuantity.placeholder = "Item Quantity"
itemQuantity.borderStyle = .RoundedRect
itemQuantity.keyboardAppearance = .Alert
itemQuantity.delegate = self
view.addSubview(itemQuantity)
let alertController = UIAlertController(title: "Add Item", message: nil, preferredStyle: .Alert)
alertController.view.addSubview(view)
return alertController
}
func createAlertViewItem(alertController: UIAlertController, itemName: String, itemStringPrice: String) {
let managedContext = self.bill.managedObjectContext
let entity = NSEntityDescription.entityForName("Item", inManagedObjectContext: managedContext!)
let newItem = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
if let itemName = alertController.textFields?.first?.text {
newItem.setValue(itemName, forKey: "name")
}
if let itemPriceString = alertController.textFields?[1].text {
let itemPriceNumber = Int(itemPriceString)
newItem.setValue(itemPriceNumber, forKey: "price")
}
do {
try managedContext!.save()
}
catch let error as NSError {
print("Core Data save failed: \(error)")
}
let currentItems = self.bill.mutableSetValueForKey("items")
currentItems.addObject(newItem)
self.tableView.reloadData()
}
@IBAction func addNewItem(sender: UIButton) {
let alertController = createAlertView()
let itemName = alertController.textFields![0].text
let itemPrice = alertController.textFields?[1].text
let itemQuantity: Int!
if alertController.textFields![2].text == "" {
itemQuantity = 0
} else {
itemQuantity = Int((alertController.textFields?[2].text)!)!
}
let okAction = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
if itemQuantity == 0 || itemQuantity == 1 {
print(2)
self.createAlertViewItem(alertController, itemName: itemName!, itemStringPrice: itemPrice!)
} else {
for _ in 0...itemQuantity {
print(3)
self.createAlertViewItem(alertController, itemName: itemName!, itemStringPrice: itemPrice!)
}
}
})
alertController.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
}
我觉得这个片段应该没问题。我的代码正确地访问了execv,它只是因为某些原因而没有执行它。 ls和groups工作正常,但cp没有。我的主要是调用输入
答案 0 :(得分:3)
我根据有限的信息猜测,作为参数发送的数组只包含您要发送给cp
的两件事之一。通过在调用cp
之前打印数组,验证是否将源和目标都发送到execv
。还要确保数组的最后一个元素是一个空指针。
答案 1 :(得分:2)
这对我有用:
char* args[] = { "/bin/cp", "/etc/passwd", "passwd-copy", 0 };
pid_t pid;
if(0>(pid=fork())){
perror(0);
return -1;
}
if(0==pid){
execv(args[0], args);
perror(0);
_exit(127);
}
siginfo_t info;
if(0>waitid(P_PID, pid, &info, WEXITED)){
perror(0);
return -1;
}
return info.si_status;