用Visual Studio 2022开发Linux程序, 用ssh连接
函数单元测试
下载glibc解压到E:\library\GNU\glibc-2.38
mzh@DESKTOP-GITL67P:~$ sudo /etc/init.d/ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
Run:
Solution explorer
配置*.h头文件路径
* basefunc.h
#pragma once
#include
#ifdef __cplusplus
extern "C" {
#endif
#ifndef size_t
typedef unsigned long size_t;
#endif
#ifndef NULL
#define NULL (void *)0
#endif
/* void* bcl_memrchr(const void* s, char c_in, size_t n); */
char* bcl_dirname(char* path);
#ifdef __cplusplus
};
#endif
* basefunc.c
#include "basefunc.h"
#ifdef __cplusplus
extern "C" {
#endif
void* bcl_memrchr(const void* s, char c_in, long int n)
{
const char* p = s + n - 1;
for (; (char*)s != p; p--) {
if (0 == (p[0] ^ c_in)) { break; }
}
return (void*)p;
}
char* bcl_dirname(char* path)
{
static const char dot[] = ".";
char* last_slash;
/* Find last '/'. */
last_slash = path != NULL ? strrchr(path, '/') : NULL;
if (last_slash != NULL && last_slash != path && last_slash[1] == '\0') {
/* Determine whether all remaining characters are slashes. */
char* runp;
for (runp = last_slash; runp != path; --runp) {
if (runp[-1] != '/') { break; }
}
/* The '/' is the last character, we have to look further. */
if (runp != path) {
last_slash = bcl_memrchr(path, '/', runp - path);
}
}
if (last_slash != NULL) {
/* Determine whether all remaining characters are slashes. */
char* runp;
for (runp = last_slash; runp != path; --runp) {
if (runp[-1] != '/') { break; }
}
/* Terminate the path. */
if (runp == path) {
/* The last slash is the first character in the string. We have to
return "/". As a special case we have to return "//" if there
are exactly two slashes at the beginning of the string. See
XBD 4.10 Path Name Resolution for more information. */
if (last_slash == path + 1) { ++last_slash; }
else { last_slash = path + 1; }
}
else {
last_slash = runp;
}
last_slash[0] = '\0';
}
else {
/* This assignment is ill-designed but the XPG specs require to
return a string containing "." in any case no directory part is
found and so a static and constant string is required. */
path = (char*)dot;
}
return path;
}
#ifdef __cplusplus
};
#endif
* main.c
/* Download glibc http://mirrors.nju.edu.cn/gnu/libc/glibc-2.38.tar.gz */
#include
#include
#include /* struct timeval */
#include
#include "basefunc.h"
/*
struct timeval
{
long int tv_sec;
long int tv_usec;
};
*/
void bcl_udelay(long usec) {
struct timeval timeout;
timeout.tv_usec = usec % 1000000;
timeout.tv_sec = usec / 1000000;
select(1, NULL, NULL, NULL, &timeout);
}
static int
test(const char* input, const char* result)
{
int retval;
char* cp = calloc(strlen(input) + 1, sizeof(char));
strcpy(cp, input);
cp = bcl_dirname(cp);
retval = strcmp(cp, result);
if (retval) {
printf("dirname(\"%s\") should be \"%s\", but is \"%s\"\n",
input, result, cp);
}
return retval;
}
static int do_test(void) {
int result = 0;
/* These are the examples given in XPG4.2. */
result |= test("/usr/lib", "/usr");
result |= test("/usr/", "/");
result |= test("usr", ".");
result |= test("/", "/");
result |= test(".", ".");
result |= test("..", ".");
/* Some more tests. */
result |= test("/usr/lib/", "/usr");
result |= test("/usr", "/");
result |= test("a//", ".");
result |= test("a", ".");
result |= test("usr", "/");
result |= test("usr//", "/");
result |= test("//usr", "//");
result |= test("//usr//", "//");
result |= test("//", "//");
/* Other Unix implementations behave like this. */
result |= test("x///y", "x");
result |= test("x/y", "x");
return result != 0;
}
int main()
{
if (do_test()) {
printf("test FAILED.\n");
} else {
printf("All test PASSED.\n");
}
bcl_udelay(60000000);
return 0;
}
Build & Run
2个test case没有跑通过
* dirname.vcproj
Debug
ARM
Release
ARM
Debug
ARM64
Release
ARM64
Debug
x86
Release
x86
Debug
x64
Release
x64
{56eeb392-cfb2-4a99-ac64-deeb8b38efeb}
Linux
dirname
15.0
Linux
1.0
Generic
{D51BCBC9-82E9-4017-911E-C93873C4EA2B}
true
false
true
false
true
false
false
true
E:\library\GNU\glibc-2.38\include;$(IncludePath)
shellexec 1.0
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.15.90.1-microsoft-standard-WSL2 x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sun Sep 24 18:38:30 CST 2023
System load: 0.24 Processes: 221
Usage of /: 1.7% of 250.92GB Users logged in: 0
Memory usage: 9% IPv4 address for eth0: 192.168.131.239
Swap usage: 0%
239 updates can be applied immediately.
166 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable
New release '22.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Sun Sep 24 18:38:29 2023 from 192.168.128.1
mzh@DESKTOP-GITL67P:~$ =thread-group-added,id="i1"
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
Find the GDB manual and other documentation resources online at:
For help, type "help".
Type "apropos word" to search for commands related to "word".
=cmd-param-changed,param="pagination",value="off"
Stopped due to shared library event (no libraries added or removed)
Loaded '/lib64/ld-linux-x86-64.so.2'. Symbols loaded.
Stopped due to shared library event:
Inferior loaded /lib/x86_64-linux-gnu/libc.so.6
Loaded '/lib/x86_64-linux-gnu/libc.so.6'. Symbols loaded.
Breakpoint 1, main () at /home/mzh/projects/dirname/main.c:70
70 {
Program received signal SIGTRAP, Trace/breakpoint trap.
0x00007ffff7edbf7a in __GI___select (nfds=1, readfds=0x0, writefds=0x0, exceptfds=0x0, timeout=0x7fffffffea20) at ../sysdeps/unix/sysv/linux/select.c:41
1019kill
The thread 'dirname.out' (0x428) has exited with code 0 (0x0).
[Inferior 1 (process 1064) killed]
The program '' has exited with code 0 (0x0).