15AH, San Francisco

California, United States.

Send Your Mail At:

tianyingkejishe@sina.cn

Working Hours

Mon-Sat: 9.30am To 7.00pm

归档标题

Autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et dolore feugait.

月度归档 9月 26, 2023

【C语言】删除文件尾部N个字节

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc,char **argv) 
{ 
	char filename[128]="";
	
	strcpy(filename,argv[1]);
	printf("filename:%s\r\n",filename);
	
	FILE *fp = fopen(filename, "rb+"); 
	if (fp == NULL) 
	{
		printf("文件打开失败\n"); return -1; 
	} 
	int num_bytes = 10; // 要删除的字节数 
	fseek(fp, -num_bytes, SEEK_END); 
	ftruncate(fileno(fp), ftell(fp)); 
	fclose(fp); 
	return 0; 
}

【Openwrt】lua的cgi响应 lua中获取GET/POST数据

一、lua的cgi响应 lua中获取GET/POST数据:

  1. 安装uhttpd包(https需要加装“uhttpd-mod-tls”和“px5g”);
  2. 修改uhttpd配置文件“/etc/config/uhttpd”,将Lua作为“interpreter”;
  3. Lua脚本写到“/www/cgi-bin/”目录下,但是不带扩展名,且可执行;
  4. Fast-CGI要求Lua脚本必须首先使用“io.write()”输出“Content-type: text/html\n\n”
  5. “/www/cgi-bin/”目录下的脚本可以调用“/usr/lib/lua/”目录下的自定义模块;
  6. Lua环境已经就绪。

二、以“读取GET和POST数据”为例,全部使用默认值的情况下,如何具体实现:

第一步:使用“opkg update”更新“.ipk”包的源,然后使用“opkg install uhttpd”安装;

第二步:使用“vi /etc/config/uhttpd”,在“config uhttpd main”下添加一行

list interpreter '.lua=/usr/bin/lua'

第三步:建立目录“/www/cgi-bin”,并增加“+x”属性;

root@OpenWrt:~# mkdir -p /www/cgi-bin/
root@OpenWrt:~# chmod +x /www/cgi-bin/

建立响应文件,并增加“+x”属性;

root@OpenWrt:~# touch /www/cgi-bin/webservice
root@OpenWrt:~# chmod +x /www/cgi-bin/webservice

将文件“/www/cgi-bin/webservice”内容修改为:

#!/usr/bin/lua
local WebService = require 'WebService'
WebService.Run()

第四步:建立Lua模块文件(不再要求“+x”属性),并读取参数,返回响应:

root@OpenWrt:~# touch /usr/lib/lua/WebService.lua

将其内容修改为如下内容:

local WebService = {}
 
function WebService.Run()
    local client = os.getenv("REMOTE_ADDR")
    local GET = os.getenv("QUERY_STRING")
    local POST = nil
    local POSTLength = tonumber(os.getenv("CONTENT_LENGTH")) or 0
    if (POSTLength > 0) then
        POST = io.read(POSTLength)
        --POST = io.read("*a")
    end
 
    -- Fast-CGI+HTTP require HEADER
    -- enable cache
    --io.write("Content-type: text/html\n\n")
    -- disable cache, especially for Internet Explorer
    io.write("Content-type: text/html\nPragma: no-cache\n\n")
 
    local reply = string.format("Client %s said: url: [%s], data: [%s]\n", client or '-', GET or '-', POST or '-')
    io.write(reply)
end
 
return WebService

第五步:重启“uhttpd”服务(为了让更改的/etc/config/uhttpd生效):

root@OpenWrt:~# /etc/init.d/uhttpd restart

第六步:使用浏览器测试:

使用浏览器访问如下地址:

http://<openwrt_ipaddr>/cgi-bin/webservice?author=qige

这里假设OpenWrt开发板的IP为192.168.1.24:

http://192.168.1.1/cgi-bin/webservice?author=qige

最终效果如下图:

通过cUrl模拟POST提交

通过form表单POST提交的浏览器结果

注意:

  1. POST数据可以由curl、Python requests等进行模拟提交,也可以自制如:<form action=”/cgi-bin/webservice” method=”POST”>提交;
  2. 根据Fast-CGI要求,必须要先使用“io.write()”输出http协议头部;再根据http协议的要求,头部和内容之间必须使用“\n\n”来隔开。因此Lua脚本必须先输出头部“Content-type: text/html\n\n”,再输出其它内容;
  3. 如果“不需要缓存”,输出立即过期,则修改头部内容为“Content-type: text/html\nPragma: no-cache\n\n”;
  4. 让uhttpd开机自动启动,需要执行“/etc/init.d/uhttpd enable”;
  5. 需要将接收到的数据输出到串口,可以再安装一个“coreutils-stty”包用于控制串口波特率
root@OpenWrt:~# opkg install coreutils
root@OpenWrt:~# opkg install coreutils-stty

再将以下内容添加到Lua模块的适当位置,并调用此函数即可:

function WebService.uartWrite(msg)
    local uartConfig = 'stty -F /dev/ttyS0 raw speed 9600\n'
    os.execute(uartConfig)
 
    local cmd = string.format("echo '%s' > /dev/ttyS0\n", msg or '')
    os.execute(cmd)
end

【ffmpeg】error: #20: identifier “AVBitStreamFilterContext” is undefined

av_bitstream_filter_init 丢弃
老版本:

//声明:	
AVBitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb"); 
//使用
av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
//释放:
av_bitstream_filter_close(h264bsfc);  

新版本:

//声明  
AVBSFContext *bsf_ctx = nullptr;
const AVBitStreamFilter *pfilter = av_bsf_get_by_name("h264_mp4toannexb");
av_bsf_alloc(pfilter, &bsf_ctx);
//使用:
av_bsf_send_packet(bsf_ctx, &packet);
av_bsf_receive_packet(bsf_ctx, &packet);
//释放:
av_bsf_free(&bsf_ctx);