我有一个简单的脚本,它在每个子文件夹的每个文件中搜索给定的字符串。它工作得很好,直到我相信我的 PHP 已更新(我不太确定是否是因为这个)。这是代码:
<?php
$limit = ini_get('memory_limit');
ini_set('memory_limit', -1);
ini_set('max_execution_time', 300);
function get_directory_content($directory){
global $search, $results;
$files = scandir($directory);
foreach ($files as $file) {
if ($file == "." || $file == "..") {
continue;
}
$is_file = false;
$path = realpath($directory . DIRECTORY_SEPARATOR . $file);
if (is_dir($path)) {
get_directory_content($path);
$is_file = true;
}
else{
$is_file = true;
}
if ($is_file) {
$content = file_get_contents($path);
}
if (stripos($content, $search) !== false) {
$obj = new stdClass();
$obj->dir = ($directory . DIRECTORY_SEPARATOR …Run Code Online (Sandbox Code Playgroud) 在"The C Programming Language"一书中,它说:
"当文件出现错误或文件结束时,库中的许多功能都会设置状态指示器.这些指示器可以显式设置和测试.此外,整数表达式
errno(声明<errno.h>)可能包含一个错误编号,提供有关最多的信息.最近的错误."
我在哪里可以看到这些功能的列表?
Windows 和 GNU/Linux 应用程序的常规返回值分别是什么。0 表示成功。但是应该在用户请求中止时使用什么。当我在 Windows 上中止时,它返回 3,但如果它不是 ERROR_PATH_NOT_FOUND,则该值不在系统错误代码列表中。GNU binutils 使用 1。从用户的角度来看,返回 GetLastError 或 errno 会很好,因为它们被记录在案,但这些似乎只涵盖较低级别的状态代码。我正在寻找一个表示“应用程序终止失败”的值
我受伤的原因是我想
exit(errcode)
Run Code Online (Sandbox Code Playgroud)
来自一个信号处理程序,它在打印有关它发生的位置的消息后捕获一些访问冲突/SIGSEGV(即编程错误)。然后错误代码应该可以从用户输入错误中识别出来。
我可以使用perror()或strerror()打印属于a的"人类可读"错误消息errno,但是如果我还要打印符号名称(例如" EAGAIN")的话errno呢?
任何方便的功能或宏来做到这一点?
更新:附上我最终编写的代码,基于下面接受的答案及其评论的想法:
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
int get_errno_name(char *buf, int buf_size) {
// Using the linux-only gawk instead of awk, because of the convenient
// match() functionality. For Posix portability, use a different recipe...
char cmd[] = "e= && " // errno to be inserted here (max digits = 6)
"echo '#include <errno.h>' | "
"gcc -dM -E - | " // optionally, use …Run Code Online (Sandbox Code Playgroud) 据说错误编号如EINVAL,ENOMEM等在errno.h中定义,但我在errno.h中找不到它们,我还搜索了/ usr/include下的一些目录,仍然无法找到它们.我可以在我的C代码中使用这些宏而没有任何问题.谁能告诉我他们在哪儿?
OverflowError在经过一些愚蠢的计算之后捕获Python ,我检查了错误args并看到它是一个包含整数作为其第一个坐标的元组.我假设这是某种错误号(errno).但是,我找不到任何文档或参考资料.
例:
try:
1e4**100
except OverflowError as ofe:
print ofe.args
## prints '(34, 'Numerical result out of range')'
Run Code Online (Sandbox Code Playgroud)
你知道34在这种情况下意味着什么吗?你知道这个例外的其他可能的错误号吗?
我见过类似的问题,但我无法解决这个错误。我和我的朋友正在制作一个聊天程序,但我们不断收到错误 ConnectionRefusedError: [Errno 61] 连接被拒绝 顺便说一下,我们在不同的网络上。这是我的服务器代码
import socket
def socket_create():
try:
global host
global port
global s
host = ''
port = 9999
s = socket.socket()
except socket.error as msg:
print("Socket creation error" + str(msg))
#Wait for client, Connect socket and port
def socket_bind():
try:
global host
global port
global s
print("Binding socket to port: " + str(port))
s.bind((host, port))
s.listen(5)
except socket.error as msg:
print("Socket binding error" + str(msg) + "\n" + "Retrying...")
socket_bind
#Accept connections (Establishes connection with …Run Code Online (Sandbox Code Playgroud) 目标
我正在尝试将zip文件写入python aws lambda的/ tmp文件夹中,因此我可以在压缩之前提取操作,并将其放入s3存储桶中。
问题
Os Errno30只读文件系统
在我的计算机上对该代码进行了本地测试,以确保在将文件上传到AWS之前,该文件将写入我的工作目录。这是我要使用的代码。
file = downloadFile() #This is api call that returns binary zip object
newFile = open('/tmp/myZip.zip','wb')
newFile.write(file)
extractAll('/tmp/myZip.zip')
Run Code Online (Sandbox Code Playgroud)
这是尝试提取zip文件的代码
def extractAll(self,source):
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
Run Code Online (Sandbox Code Playgroud)
这是痕迹
[Errno 30] Read-only file system: '/var/task/test-deploy': OSError
Traceback (most recent call last):
File "/var/task/web.py", line 31, in web
performAction(bb, eventBody)
File "/var/task/src/api/web.py", line 42, in performAction
zipHelper.extractAll('/tmp/myZip.zip')
File "/var/task/src/shared/utils/zipfilehelper.py", line 24, in extractAll
archive.extractall()
File "/var/lang/lib/python3.6/zipfile.py", line 1491, in extractall
self.extract(zipinfo, path, pwd)
File …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,其中大多数使用过的库函数在出错时返回-1并设置errno.程序的行为是这样的,如果发生错误它将退出.要确定确切的退出点和程序外部的错误(使用示例gdb),我想使用以下方法:
err = func_1(..arglist_1..);
if(err != 0)
{
perror("func(..arglist..)");
return ((1u << 8) | errno);
}
//..
//.. some more funcs
//..
err = func_n(..arglist_n..);
if(err != 0)
{
perror("func(..arglist_n..)");
return (((unsigned)n << 8) | errno);
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是安全的假设.
现实: 错误号被声明为extern int errno;内部errno.h
假设1:价值错误号总是小于255
假设2: 错误号总是正的.
根据定义的所有误差常数(EAGAIN等)errno.h,这些假设目前都是正确的.这些可以假设在未来也是如此吗?
PS:我不想依赖于perror()确定退出点.