본문 바로가기

UNIX_LINUX_C_C++

날짜,pthread,도메인

/*

strftime, strptime.

get_ip_cls

*/

#include <time.h>
int main(){
char buf[200] = "";
time_t now = 0;
struct tm *t = NULL;

//strftime.
now = time(NULL);
t = localtime(&now);
strftime(buf, 20, "%Y/%m/%d %H:%M:%S", t);
puts(buf);

//strptime.
strptime(buf, "%Y/%m/%d %H:%M:%S", &t);
sleep(3);
strftime(buf, 20, "%Y/%m/%d %H:%M:%S", t);
puts(buf);

return 0;
}

/*

날짜 관련 함수 예제

*/


#include "/home/jinyedge/lib/c/edgelib.h"

//-----------------
int main(void){
time_t now = 0;
struct tm *t = NULL;

/*오늘 날짜*/
now = time(NULL);
t = localtime(&now);
printf("%d-%02d-%02d\n" , t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);

/*일주일전 날짜*/
now = time(NULL) - (86400 * 7);
t = localtime(&now);
printf("%d-%02d-%02d\n" , t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);

/*한달전 날짜*/
now = time(NULL) - (86400 * 30);
t = localtime(&now);
printf("%d-%02d-%02d\n" , t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
}


/*

get_ip_cls

*/

//-----------------
char get_ip_cls(char *ip){
int i = 0, a = 0;
char buf[4] = "";
for(i = 0; i < strlen(ip); i++){
if(ip[i] == '.'){
buf[i] = '\0';
break;
}
buf[i] = ip[i];
}

a = atoi(buf);
if(a >= 0 && a <= 127){
return 'a';
}
else if(a >= 128 && a <= 191){
return 'b';
}
else if(a >= 192 && a <= 223){
return 'c';
}
else if(a >= 224 && a <= 239){
return 'd';
}
else{
return 'e';
}
}

//-----------------
int main(void){
printf("%c\n", get_ip_cls("192.168.10.10.1"));
return 0;
}

/*

ip_sub

*/

#include "/home/jinyedge/lib/c/edgelib.h"
//-----------------
unsigned long ip_sub(char *ip, char *ip2){
int i = 0, ip_no_arr[4] = {0, 0, 0, 0}, ip_no_arr2[4] = {0, 0, 0, 0}, k = 0;
unsigned long sum = 0;
List *list = NULL;

/*Get ip_no_arr*/
list = split(ip, ".");
for(i = 0; i < 4; i++){
ip_no_arr[i] = atoi(list->arr[i]);
}
list_free(list);

/*Get ip_no_arr2*/
list = split(ip2, ".");
for(i = 0; i < 4; i++){
ip_no_arr2[i] = atoi(list->arr[i]);
}
list_free(list);

/*Get res*/
sum = 0;
for(i = 0; i < 4; i++){
k = ip_no_arr[i] - ip_no_arr2[i];
if(i == 0){
sum += k * 255 * 255 * 255;
}
else if(i == 1){
sum += k * 255 * 255;
}
else if(i == 2){
sum += k * 255;
}
else if(i == 3){
sum += k;
}
}
return sum;
}

//-----------------
int main(void){
unsigned long res = 0;
char *ip = "192.168.10.1";
char *ip2 = "192.165.1.1";

res = ip_sub(ip, ip2);
printf("%u\n", res);
}

/*

get_time, get_date.
번호: 76 / 작성자: jinyedge / 등록일: 2004-06-27 22:13:05 / 조회: 115

*/

#include <stdio.h>
#include <time.h>

//-----------------------
void get_time(char buf[]){
time_t now = 0;
struct tm *t = NULL;

now = time(NULL);
t = localtime(&now);
sprintf(buf, "%d/%02d/%02d %2d:%02d:%02d"
, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
}

//-----------------------
void get_date(char buf[]){
time_t now = 0;
struct tm *t = NULL;

now = time(NULL);
t = localtime(&now);
sprintf(buf, "%d/%02d/%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
}

//-----------------------
int main(void){
char buf[1024];

get_date(buf);
puts(buf);

get_time(buf);
puts(buf);

return 0;
}

/*

프로세스 읽기.
번호: 75 / 작성자: jinyedge / 등록일: 2004-06-27 21:58:10 / 조회: 141

*/

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

#define BUF_SIZE 8192

//-----------------
int main(void){
FILE *fp = NULL;
char buf[BUF_SIZE];
long rcnt = 0;

if((fp = popen("ps -aux | grep '^root'", "r")) == NULL){return -1;}
rcnt = fread(buf, sizeof(char), BUF_SIZE, fp);
pclose(fp);

printf("Read count: %d\n%s\n", rcnt, buf);

return 0;
}

/*

get_kw_q_part.
번호: 73 / 작성자: jinyedge / 등록일: 2003-08-24 19:22:44 / 조회: 166

*/

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

/*------------*/
void get_kw_q_part(char *arr[], int arr_len, char buf[]){
int i = 0;

//If there's no arg.
if(arr_len == 0){
strcpy(buf, "");
return;
}

//If there's only one arg.
if(arr_len == 1){
sprintf(buf, "kw = '%s'", arr[0]);
return;
}

//Append the args into buf.
sprintf(buf, "kw in('%s'", arr[0]);
for(i = 1; i < arr_len; i++){
sprintf(buf, "%s, '%s'", buf, arr[i]);
}
strcat(buf, ")");
}

/*------------*/
int main(void){
char buf[1024];
char *arr[] = {"123", "456", "789"};

get_kw_q_part(arr, 1, buf);
puts(buf);

get_kw_q_part(arr, 3, buf);
puts(buf);

return 0;
}

/*

New substr functions.
번호: 72 / 작성자: jinyedge / 등록일: 2003-04-09 12:05:13 / 조회: 214

I realized I made some mistakes when I was writing substr functions using c.
I think it was due to my lack of understanding of strncpy function. A few days
ago I started studying c again to acquire some kind of certification. And it made
me to realize my mistake which I made when I wrote substr functions that have
taken important role in my library so far. So I wrote new version of my substr
functions.
*/


/*-----------------------*/
/*스트링 자르기*/
void substr(char *line, int offset, int len, char buf[]){
if(len < 1 || offset >= (int)strlen(line)){
strcpy(buf, "");
return;
}

strncpy(buf, line + offset, len);
buf[len] = '\0';
}

/*-----------------------*/
/*스트링 자르기*/
void substr_nolen(char *line, int offset, char buf[]){
int str_len = 0, len = 0;

str_len = (int)strlen(line);
if(offset >= str_len){
strcpy(buf, "");
return;
}

len = str_len - offset;
strncpy(buf, line + offset, len);
buf[len] = '\0';
}

/*-----------------------*/
char * psubstr(char *line, int offset, int len){/*스트링 자르기(스트링 포인터를 리턴한다.)*/
int str_len = 0;
char *res = NULL;

str_len = (int)strlen(line);
if(len < 1 || offset >= str_len){return NULL;}

/*Memory Size*/
if((offset + len) > str_len){len = str_len - offset;}

res = (char *)malloc(sizeof(char) * (len + 1));
strncpy(res, line + offset, len);
res[len] = '\0';

return res;
}

/*-----------------------*/
char * psubstr_nolen(char *line, int offset){/*스트링 자르기(스트링 포인터를 리턴한다.)*/
int str_len = 0, len = 0;
char *res = NULL;

str_len = (int)strlen(line);
if(offset >= str_len){return NULL;}

len = str_len - offset;

/*Memory Size*/
res = (char *)malloc(sizeof(char) * (len + 1));

strncpy(res, line + offset, len);
res[len] = '\0';

return res;
}

replace, replace_i
번호: 63 / 작성자: jinyedge / 등록일: 2002-05-28 13:01:14 / 조회: 192
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

/*-----------------------*/
void str_lower(char *line, char buf[]){/*스트링의 문자를 모두 소문자로 만든다.*/
int i, len;

len = strlen(line);
for(i = 0; i < len; i++){
if(isupper(line[i])){buf[i] = line[i] + 32;}
else{buf[i] = line[i];}
}
buf[len] = '\0';
}

/*-----------------------*/
char * substr2(char *line, int offset, int len){/*스트링 자르기(스트링 포인터를 리턴한다.)*/
int i, str_len;
char *res;

str_len = (int)strlen(line);
if(len < 1 || offset >= str_len){return NULL;}

/*Memory Size*/
if((offset + len) > str_len){len = str_len - offset;}

for(i = 0; i < offset; i++){
line++;
}

res = (char *)malloc(sizeof(char) * (len + 1));
strncpy(res, line, len);
res[len] = '\0';

return res;
}

/*-----------------------*/
int str_index(char *line, char *kword, int offset){/*서브스트링의 위치를 리턴한다.*/
int pos;
char *temp;

if(offset >= (int)strlen(line)){
return -1;
}

temp = substr2(line, offset, strlen(line) - offset);
pos = strstr(temp, kword) - temp + offset;
free(temp);

if(pos < 0){return -1;}

return pos;
}

/*-----------------------*/
int find_char(char *line, char delim, int offset){/*문자의 위치를 리턴한다.*/
int i, len;
len = strlen(line);
for(i = offset; i < len; i++){
if(line[i] == delim){return i;}
}
return -1;
}

/*-----------------------*/
void replace_by_string(char *line, char *pattern, char *replace, char buf[]){
int offset, stop, plen, len;
char *temp;

strcpy(buf, "");
if(line == NULL || (strcmp(line, "") == 0)){return;}

len = strlen(line);
plen = strlen(pattern);

offset = 0;
while(offset < len){
stop = str_index(line, pattern, offset);
if(stop < 0){break;}
temp = substr2(line, offset, stop - offset);
if(temp){//if temp != null
strcat(buf, temp);
free(temp);
}
strcat(buf, replace);

offset = stop + plen;
}
temp = substr2(line, offset, len - offset);
if(temp){
strcat(buf, temp);
free(temp);
}
}

/*-----------------------*/
void replace_by_char(char *line, char pattern, char *replace, char buf[]){
int offset, stop, len;
char *temp;

strcpy(buf, "");
if(line == NULL || (strcmp(line, "") == 0)){return;}

len = strlen(line);
offset = 0;
while(offset < len){
stop = find_char(line, pattern, offset);
if(stop < 0){break;}
temp = substr2(line, offset, stop - offset);
if(temp){//if temp != null
strcat(buf, temp);
free(temp);
}
strcat(buf, replace);

offset = stop + 1;
}
temp = substr2(line, offset, len - offset);
if(temp){
strcat(buf, temp);
free(temp);
}
}

/*-----------------------*/
void replace(char *line, char *pattern, char *replace, char buf[]){
if(strlen(pattern) == 1){
replace_by_char(line, pattern[0], replace, buf);
return;
}
replace_by_string(line, pattern, replace, buf);
}

/*-----------------------*/
void replace_by_string_i(char *line, char *pattern, char *replace, char buf[]){
int offset, stop, plen, len;
char *temp, *line_lower, *pattern_lower;

strcpy(buf, "");
if(line == NULL || (strcmp(line, "") == 0)){return;}

len = strlen(line);
plen = strlen(pattern);

/*스트링 복사*/
line_lower = (char *)malloc(sizeof(char) * (len + 1));
pattern_lower = (char *)malloc(sizeof(char) * (plen + 1));

/*str_lower*/
str_lower(line, line_lower);
str_lower(pattern, pattern_lower);

offset = 0;
while(offset < len){
/*str_index의 변수로 사본을 넘긴다.*/
stop = str_index(line_lower, pattern_lower, offset);
if(stop < 0){break;}

/*substr2의 변수로 원본을 넘긴다.*/
temp = substr2(line, offset, stop - offset);
if(temp){//if temp != null
strcat(buf, temp);
free(temp);
}
strcat(buf, replace);

offset = stop + plen;
}
temp = substr2(line, offset, len - offset);
if(temp){
strcat(buf, temp);
free(temp);
}
}

/*-----------------------*/
void replace_by_char_i(char *line, char pattern, char *replace, char buf[]){
int offset, stop, len;
char *temp, *line_lower, pattern_lower;

strcpy(buf, "");
if(line == NULL || (strcmp(line, "") == 0)){return;}

len = strlen(line);

/*스트링 복사*/
line_lower = (char *)malloc(sizeof(char) * (len + 1));
pattern_lower = pattern;

/*str_lower*/
str_lower(line, line_lower);
if(isupper(pattern)){pattern_lower += 32;}

offset = 0;
while(offset < len){
/*find_char의 변수로 사본을 넘긴다.*/
stop = find_char(line_lower, pattern_lower, offset);
if(stop < 0){break;}

/*substr2의 변수로 원본을 넘긴다.*/
temp = substr2(line, offset, stop - offset);
if(temp){//if temp != null
strcat(buf, temp);
free(temp);
}
strcat(buf, replace);

offset = stop + 1;
}
temp = substr2(line, offset, len - offset);
if(temp){
strcat(buf, temp);
free(temp);
}
}

/*-----------------------*/
void replace_i(char *line, char *pattern, char *replace, char buf[]){
if(strlen(pattern) == 1){
replace_by_char_i(line, pattern[0], replace, buf);
return;
}
replace_by_string_i(line, pattern, replace, buf);
}

//------------------------
int main(){
char *line = "kkk:ooo:ppp";
char buf[1024];
int i = 0;
clock_t start_time;

start_time = clock();

for(i = 0; i < 10; i++){
replace(line, ":", "+", buf);
puts(buf);
}

printf("Elapsed time: %2.2f\n", (double)(clock() - start_time) / CLOCKS_PER_SEC);
return 0;
}

/*

simple sorting.

번호: 71 / 작성자: jinyedge / 등록일: 2003-04-09 02:09:03 / 조회: 198

I suddenly needed a simple sorting algorithm which I can remember easily. And
I think this code is enough for the purpose. I don't know what kind of sorting algorithm
is this. But I think I saw this algorithm in some book which I can't remember the
title.
*/


#include <stdio.h>
void main(){
int i, k, min_idx, temp;
int arr[] = {24, 87, 34, 765, 55, 87, 3, 75, 44, 44, 1, 44, 99, 12, 575};

for(i = 0; i < 15; i++){
min_idx = i;
for(k = i + 1; k < 15; k++){
if(arr[k] < arr[min_idx]){min_idx = k;}
}

temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}

for(i = 0; i < 15; i++){
printf("%d\n", arr[i]);
}
}

도메인 - > 주소.
번호: 70 / 작성자: jinyedge / 등록일: 2003-01-08 12:46:35 / 조회: 328
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>

//-----------------
int main(int argc, char *argv[]){
hostent *he = NULL;

he = gethostbyname("www.yahoo.com");
printf("%s\n", inet_ntoa(*((struct in_addr *)he->h_addr)));
return 0;
}

실행시간 측정.
번호: 67 / 작성자: jinyedge / 등록일: 2002-08-28 09:06:06 / 조회: 366
#include <time.h>
int main(void){
int i;
clock_t start_time;

start_time = clock();

for(i = 0; i < 1000; i++){
printf("test...\n");
}

printf("Elapsed time: %2.2f\n", (double)(clock() - start_time) / CLOCKS_PER_SEC);
}

pthread - mutex
번호: 65 / 작성자: jinyedge / 등록일: 2002-06-21 07:58:26 / 조회: 254
/*컴파일 하려면 gcc -lpthread*/
#include <stdio.h>
#include <pthread.h>

/*-----------------*/
typedef struct _THR_ARG{
int *shr;
pthread_mutex_t *lock;
} THR_ARG;

/*-----------------*/
void * my_thr(void *arg){
int i;
THR_ARG *thr_arg = (THR_ARG *)arg;

/*lock*/
pthread_mutex_lock(thr_arg->lock);

*(thr_arg->shr) += 1;
usleep(10);
printf("%d\n", *(thr_arg->shr));

/*unlock*/
pthread_mutex_unlock(thr_arg->lock);

return NULL;
}

/*-----------------*/
int main(void){
int i, shr;
pthread_t thr[10];
pthread_mutex_t lock;
THR_ARG thr_arg;

shr = 0;
/*init lock*/
pthread_mutex_init(&lock, NULL);

thr_arg.shr = &shr;
thr_arg.lock = &lock;

/*create thread*/
for(i = 0; i < 10; i++){
if(pthread_create(&thr[i], NULL, my_thr, &thr_arg) != 0){puts("thread create..");}
}

/*wating for thread to end*/
for(i = 0; i < 10; i++){
if(pthread_join(thr[i], NULL) != 0){puts("thread join..");}
}

return 0;
}

pthread - create, join
번호: 64 / 작성자: jinyedge / 등록일: 2002-06-21 06:26:39 / 조회: 213
/*컴파일 하려면 gcc -lpthread*/
#include <stdio.h>
#include <pthread.h>

/*-----------------*/
void * print_num(void *arg){
int i = 0;

for(i = 0; i < 10; i++){
printf("1: %d\n", i);
sleep(3);
}

return NULL;
}

/*-----------------*/
void * print_num2(void *arg){
int i = 0;

for(i = 18; i < 28; i++){
printf("2: %d\n", i);
sleep(1);
}

return NULL;
}

/*-----------------*/
int main(void){
pthread_t thr;
pthread_t thr2;

/*create thread*/
if(pthread_create(&thr, NULL, print_num, (void *)NULL) != 0){puts("thread create..");}
if(pthread_create(&thr2, NULL, print_num2, (void *)NULL) != 0){puts("thread create..");}

/*wating for thread to end*/
if(pthread_join(thr, NULL) != 0){puts("thread join..");}
if(pthread_join(thr2, NULL) != 0){puts("thread join..");}

return 0;
}

get_open_file, get_save_file
번호: 62 / 작성자: jinyedge / 등록일: 2002-05-13 10:31:02 / 조회: 191
/*-----------------------*/
void get_open_file(HWND hWnd, const char *filter, char buf[]){
OPENFILENAME OFN;
char path[MAX_PATH] = "";

memset(&OFN, 0, sizeof(OPENFILENAME));
OFN.lStructSize = sizeof(OPENFILENAME);
OFN.hwndOwner = hWnd;
OFN.lpstrFilter= filter;
OFN.lpstrFile = path;
OFN.nMaxFile = MAX_PATH;
GetOpenFileName(&OFN);
strcpy(buf, path);
}

/*-----------------------*/
void get_save_file(HWND hWnd, const char *filter, char buf[]){
OPENFILENAME OFN;
char path[MAX_PATH] = "";

memset(&OFN, 0, sizeof(OPENFILENAME));
OFN.lStructSize = sizeof(OPENFILENAME);
OFN.hwndOwner = hWnd;
OFN.lpstrFilter= filter;
OFN.lpstrFile = path;
OFN.nMaxFile = MAX_PATH;
GetSaveFileName(&OFN);
strcpy(buf, path);
}