lovelove工作

This commit is contained in:
lzy
2024-05-13 17:20:11 +08:00
parent 3594812944
commit 1b796adfc3
8 changed files with 566 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TIMESTRSIZE 128
/**
* @brief 找出100天以后是哪一天
* @details
*
* @param argc
* @param argv
*
* @return int
*/
int main(int argc, char **argv)
{
time_t stamp;
struct tm *tm;
char timestr[TIMESTRSIZE];
time(&stamp);
tm = localtime(&stamp);
strftime(timestr, TIMESTRSIZE, "Now: %Y-%m-%d", tm);
puts(timestr);
tm->tm_mday += 100;
// mktime 会先调整tm溢出的情况
mktime(tm);
strftime(timestr, TIMESTRSIZE, "100 days later: %Y-%m-%d", tm);
puts(timestr);
exit(0);
}

View File

@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
static void f1(void)
{
puts("f1() is working!");
}
static void f2(void)
{
puts("f2() is working!");
}
static void f3(void)
{
puts("f3() is working!");
}
int main(int argc, char **argv)
{
puts("Begin!");
atexit(f1);
atexit(f2);
atexit(f3);
puts("End!");
exit(0);
}
// out:
// *[main][~/Linux-C-Notes/C13-Linux系统编程/fs]$ ./atexit
// Begin!
// End!
// f3() is working!
// f2() is working!
// f1() is working!

View File

@@ -0,0 +1,46 @@
#include <crypt.h>
#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/**
* @brief 加密输入的密码并与shadow文件中的密码进行比较
* @details
* !!! compile: gcc check.c -o check -lcrypt
* !!! 需要root权限运行
*
* @param argc
* @param argv [in] 用户名
*
* @return int
*/
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage:...\n");
exit(1);
}
char *input_pass;
struct spwd *shadowline;
char *crypt_pass;
//* getpass()函数在输入密码时不会回显密码
input_pass = getpass("PassWord: ");
//* getspnam()函数通过用户名获取shadow文件中的密码
shadowline = getspnam(argv[1]);
//* 加密输入的密码
crypt_pass = crypt(input_pass, shadowline->sp_pwdp);
if (0 == strcmp(crypt_pass, shadowline->sp_pwdp))
puts("OK!");
else
puts("Error!");
exit(0);
}

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("Hello, World!\n");
return 0;
/*
* echo $?
* 0
*/
}

View File

@@ -0,0 +1,109 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define TIMESTRSIZE 1024
#define FMTSTRSIZE 1024
/**
* @brief mydate
* @details
* Usage:
* mydate [-H 12|24] [-y 2|4] [-m] [-d] [-M] [-S]
* Options:
* -y: year 2位,4位
* -m: month
* -d: day
* -H: hour 12小时制,24小时制
* -M: minute
* -S: second
*
* @param argc
* @param argv
*
* @return int
*/
int main(int argc, char **argv)
{
time_t stamp;
struct tm *tm;
char timestr[TIMESTRSIZE];
int c;
char fmtstr[FMTSTRSIZE];
fmtstr[0] = '\0';
FILE *fp = stdout;
time(&stamp);
tm = localtime(&stamp);
while (1)
{
//* 选项传参
// c = getopt(argc, argv, "H:y:MSmd");
//* 加'-',接收非选项传参
c = getopt(argc, argv, "-H:y:MSmd");
if (c < 0)
break;
switch (c)
{
//* 非选项传参返回1
case 1:
if (stdout == fp) //! 先入为主
{
fp = fopen(argv[optind - 1], "w");
if (NULL == fp)
{
perror("fopen()");
fp = stdout;
}
}
break;
case 'H':
if (0 == strcmp(optarg, "12"))
strncat(fmtstr, "%I(%P) ", FMTSTRSIZE - 1);
else if (0 == strcmp(optarg, "24"))
strncat(fmtstr, "%H ", FMTSTRSIZE - 1);
else
fprintf(stderr,
"Usage: %s [-H 12|24] [-y 2|4] [-m] [-d] [-M] [-S]\n",
argv[0]);
break;
case 'M': strncat(fmtstr, "%M ", FMTSTRSIZE - 1); break;
case 'S': strncat(fmtstr, "%S ", FMTSTRSIZE - 1); break;
case 'y':
if (0 == strcmp(optarg, "2"))
strncat(fmtstr, "%y ", FMTSTRSIZE - 1);
else if (0 == strcmp(optarg, "4"))
strncat(fmtstr, "%Y ", FMTSTRSIZE - 1);
else
fprintf(stderr,
"Usage: %s [-H 12|24] [-y 2|4] [-m] [-d] [-M] [-S]\n",
argv[0]);
break;
case 'm': strncat(fmtstr, "%m ", FMTSTRSIZE - 1); break;
case 'd': strncat(fmtstr, "%d ", FMTSTRSIZE - 1); break;
default: break;
}
}
strncat(fmtstr, "\n", FMTSTRSIZE - 1);
strftime(timestr, TIMESTRSIZE, fmtstr, tm);
fputs(timestr, fp);
if (fp != stdout)
fclose(fp);
exit(0);
}

View File

@@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define FNAME "time.log"
#define BUFSIZE 1024
/**
* @brief 往文件里面输出时间戳
* @details
* 实例格式2019-12-12 12:12:12
*
* @param argc
* @param argv
*
* @return int
*/
int main(int argc, char **argv)
{
FILE *fp;
time_t stamp;
struct tm *tm;
char buf[BUFSIZE];
int count = 0;
fp = fopen(FNAME, "a+");
if (fp == NULL)
{
perror("fopen()");
exit(1);
}
while (fgets(buf, BUFSIZE, fp) != NULL)
count++;
while (1)
{
stamp = time(NULL);
tm = localtime(&stamp);
if (NULL == tm)
{
perror("localtime()");
exit(1);
}
strftime(buf, BUFSIZE, "%Y-%m-%d %H:%M:%S", tm);
fprintf(fp, "%-4d%s\n", ++count, buf);
/*
* 加了sleep(1)之后就没有输出?
* 全缓冲
* 因为缓冲区没有满,没有刷新到文件里面
*/
fflush(fp);
sleep(1);
}
fclose(fp);
exit(0);
}
//! 最终效果
// *[main][~/Linux-C-Notes/C13-Linux系统编程/fs]$ ./test
// ^C
// *[main][~/Linux-C-Notes/C13-Linux系统编程/fs]$ cat time.log
// 1 2024-05-11 21:53:26
// 2 2024-05-11 21:53:27
// 3 2024-05-11 21:53:28
// 4 2024-05-11 21:53:29
// 5 2024-05-11 21:53:30
// *[main][~/Linux-C-Notes/C13-Linux系统编程/fs]$ ./test
// ^C
// *[main][~/Linux-C-Notes/C13-Linux系统编程/fs]$ cat time.log
// 1 2024-05-11 21:53:26
// 2 2024-05-11 21:53:27
// 3 2024-05-11 21:53:28
// 4 2024-05-11 21:53:29
// 5 2024-05-11 21:53:30
// 6 2024-05-11 21:53:35
// 7 2024-05-11 21:53:36
// 8 2024-05-11 21:53:37
// 9 2024-05-11 21:53:38

View File

@@ -0,0 +1,29 @@
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
/**
* @brief 用user id获取用户名
* @details
*
* @param argc
* @param argv
*
* @return int
*/
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: %s <user id>\n", argv[0]);
exit(1);
}
struct passwd *pwdline;
pwdline = getpwuid(atoi(argv[1]));
puts(pwdline->pw_name);
exit(0);
}