*
* Created by Hajime Kurita
*/
+#include<sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
int rel2abs(const char *rel_path, char *abs_path);
int remove_path(const char *path);
int remove_not_file_char(const char *str, char *dst);
-int safe_strcat(char *dst, const char *append);
-int safe_strncat(char *dst, const char *append, const unsigned int len);
+int safe_strcat(char *dst, const char *append, size_t size);
int shmf(const char *file, char *shmfile);
#ifdef __cplusplus
int remove_path(const string path);
string remove_not_file_char(const string *str);
int reverse_char(char s[]);
- int safe_strcat(char *dst, const char *append);
- int safe_strncat(char *dst, const char *append, const unsigned int len);
+ int safe_strcat(char *dst, const char *append, size_t size);
string shmf(const string file);
};
#endif
all: lib1stclass.a libb1stclass.so lib1stclass_cxx.a lib1stclass_cxx.so t/test
-lib1stclass_cxx.a: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
- ar rvs lib1stclass_cxx.a host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+lib1stclass_cxx.a: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+ ar rvs lib1stclass_cxx.a host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
ranlib lib1stclass_cxx.a;
-lib1stclass_cxx.so: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
- g++ -shared -fPIC -o lib1stclass_cxx.so host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+lib1stclass_cxx.so: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+ g++ -shared -fPIC -o lib1stclass_cxx.so host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
-lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
- ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+ ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
ranlib lib1stclass.a;
-libb1stclass.so: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
- g++ -shared -fPIC -o lib1stclass.so host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+libb1stclass.so: host2ip.o rmkdir.o safe_strcat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+ g++ -shared -fPIC -o lib1stclass.so host2ip.o itoa.o rmkdir.o safe_strcat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
host2ip.o: host2ip.c
gcc -Wall -O3 -finline-functions -c host2ip.c;
safe_strcat_cxx.o: safe_strcat.c
g++ -Wall -O3 -finline-functions -o safe_strcat_cxx.o -c safe_strcat.c;
-safe_strncat.o: safe_strncat.c
- gcc -Wall -O3 -finline-functions -c safe_strncat.c;
-
-safe_strncat_cxx.o: safe_strncat.c
- g++ -Wall -O3 -finline-functions -o safe_strncat_cxx.o -c safe_strncat.c;
-
shmf.o: shmf.c
gcc -Wall -O3 -finline-functions -c shmf.c;
#ifdef __cplusplus
#include "1stclass.hpp"
+#include<iostream>a
#include<string>
string firstclass::host2ip(const string server_str){
char ip[32]="";
char *server=NULL;
try{
- server=new char[sizeof(char)*(server_str.length()+1)];
- server[0]='\0';
- safe_strcat(server, server_str.c_str());
+ server=new char[sizeof(char)*(server_str.length()+1)];
+ server[0]='\0';
+ safe_strcat(server, server_str.c_str(), sizeof(char)*(server_str.length()+1));
#else
-#include "lib1stclass.h"
+#include "1stclass.h"
int host2ip(const char *server, char *ip){
#endif
-
struct addrinfo hint, *res, *p;
int error=0;
char ipstr[INET6_ADDRSTRLEN];
#ifdef __cplusplus
delete[] server;
- firstclass::safe_strcat(ip, ipstr);
+ firstclass::safe_strcat(ip, ipstr, sizeof(ip));
string last_ip(ip);
return last_ip;
}
return "";
}
#else
- safe_strcat(ip, ipstr);
+ safe_strcat(ip, ipstr, sizeof(ip));
return 0;
#endif
}
try{
moji=new char[sizeof(char)*(in_moji.length()+1)];
moji[0]='\0';
- safe_strcat(moji, in_moji.c_str());
+ safe_strcat(moji, in_moji.c_str(), sizeof(moji));
#else
#include "1stclass.hpp"\r
\r
string firstclass::rel2abs(const string in_rel_path){\r
- char rel_path[1024]="";;\r
- safe_strcat(rel_path, in_rel_path.c_str());\r
- char abs_path[1024]="";\r
+ char rel_path[2048]="";;\r
+ safe_strcat(rel_path, in_rel_path.c_str(), sizeof(rel_path));\r
+ char abs_path[2048]="";\r
#else\r
#include "1stclass.h"\r
int rel2abs(const char *rel_path, char *abs_path){\r
#endif\r
if(rel_path[0]=='/'){\r
- safe_strcat(abs_path, rel_path);\r
+ safe_strcat(abs_path, rel_path, 2048);\r
}\r
else if(rel_path[0]=='.'){\r
char in_real[512]="";\r
- char tail[1024]="";\r
+ char tail[2048]="";\r
int found=0;\r
int found_i=0;\r
int i=0;\r
int tail_i=0;\r
- for(i=0;i<=1024;i++){\r
+ for(i=0;i<=2048;i++){\r
if(rel_path[i]=='\0'){\r
tail[tail_i]='\0';\r
break;\r
in_real[found_i-1]='\0';\r
}\r
\r
- char real_path[1024]="";\r
+ char real_path[2048]="";\r
realpath(in_real, real_path);\r
- safe_strcat(abs_path, real_path);\r
- safe_strcat(abs_path, "/");\r
- safe_strcat(abs_path, tail);\r
+ safe_strcat(abs_path, real_path, 2048);\r
+ safe_strcat(abs_path, "/", 2048);\r
+ safe_strcat(abs_path, tail, 2048);\r
}\r
else{\r
- char real_path[1024]="";\r
+ char real_path[2048]="";\r
realpath(".", real_path);\r
- safe_strcat(abs_path, real_path);\r
- safe_strcat(abs_path, "/");\r
- safe_strcat(abs_path, rel_path);\r
+ safe_strcat(abs_path, real_path, 2048);\r
+ safe_strcat(abs_path, "/", 2048);\r
+ safe_strcat(abs_path, rel_path, 2048);\r
}\r
\r
#ifdef __cplusplus\r
dst=new char[sizeof(char)*(in_str->length()+1)];
str[0]='\0';
dst[0]='\0';
- safe_strcat(str, in_str->c_str());
+ safe_strcat(str, in_str->c_str(), in_str->length()+1);
#else
#include "lib1stclass.h"
int remove_not_file_char(const char *str, char *dst){
try {
path=new char[sizeof(char)*(in_path.length()+1)];
path[0]='\0';
- safe_strcat(path, in_path.c_str());
+ safe_strcat(path, in_path.c_str(), in_path.length()+1);
#else
}
else{
char file[512]="";
- safe_strcat(file, path);
- safe_strcat(file, "/");
- safe_strcat(file, dp->d_name);
+ safe_strcat(file, path, sizeof(file));
+ safe_strcat(file, "/", sizeof(file));
+ safe_strcat(file, dp->d_name, sizeof(file));
remove(file);
}
}
int firstclass::rmkdir(const string in_dir){
char abs_dir[2048]="";
string abs_dir_str=rel2abs(in_dir);
- safe_strcat(abs_dir, abs_dir_str.c_str());
+ safe_strcat(abs_dir, abs_dir_str.c_str(), sizeof(abs_dir));
#else
#include "1stclass.h"
int rmkdir(const char *check_dir){
if(mkdir(abs_dir, 0755)==0){
}
else{
- safe_strcat(moto, abs_dir);
+ safe_strcat(moto, abs_dir, sizeof(moto));
token_p=strtok(moto, "/");
- safe_strcat(dir_path, "/");
- safe_strcat(dir_path, token_p);
+ safe_strcat(dir_path, "/", sizeof(dir_path));
+ safe_strcat(dir_path, token_p, sizeof(dir_path));
if(stat(dir_path, &st)!=0){
if(mkdir(dir_path, 0755)==0){
}
}
}
while((token_p=strtok(NULL,"/"))!=NULL){
- safe_strcat(dir_path, "/");
- safe_strcat(dir_path, token_p);
+ safe_strcat(dir_path, "/", sizeof(dir_path));
+ safe_strcat(dir_path, token_p, sizeof(dir_path));
if(stat(dir_path, &st)!=0){
if(mkdir(dir_path, 0755)==0){
}
*
* Created by Hajime Kurita
*/
-#include <stdio.h>
+#include <sys/types.h>
#include <string.h>
#ifdef __cplusplus
#include "1stclass.hpp"
-int firstclass::safe_strcat(char *dst, const char *append){
+int firstclass::safe_strcat(char *dst, const char *src, size_t size){
#else
-#include "lib1stclass.h"
-int safe_strcat(char *dst, const char *append){
+#include "1stclass.h"
+int safe_strcat(char *dst, const char *src, size_t size){
#endif
- if( sizeof(append) >sizeof(dst) ){
- printf("\"%s\" is too long for this system.\n", append);
+ char *d = dst;
+ const char *s = src;
+ size_t n = size;
+ size_t dlen;
+
+ while (n-- != 0 && *d != '\0'){
+ d++;
+ }
+ dlen = d - dst;
+ n = size - dlen;
+
+ if (n == 0){
return 1;
}
- else{
- strcat(dst, append);
+
+ while (*s != '\0') {
+ if (n != 1) {
+ *d++ = *s;
+ n--;
+ }
+ s++;
}
+ *d = '\0';
return 0;
}
+++ /dev/null
-/*
-* See the file LICENSE for redistribution information.
-*
-* Copyright: 2010- 1stclass.co.jp. All rights reserved.
-*
-* Created by Hajime Kurita
-*/
-#include <stdio.h>
-#include <string.h>
-
-#ifdef __cplusplus
-#include "1stclass.hpp"
-int firstclass::safe_strncat(char *dst, const char *append, const unsigned int len){
-#else
-#include "1stclass.h"
-int safe_strncat(char *dst, const char *append, const unsigned int len){
-#endif
- if( strlen(dst)>len ){
- printf("\"%s\" is too long for this system.\n", append);
- return 1;
- }
- else{
- strncat(dst, append, len);
- }
- return 0;
-}
#include "1stclass.hpp"
string firstclass::shmf(const string in_file){
const char *file=in_file.c_str();
- char shm_file[1024]="";
+ char shm_file[2048]="";
#else
-#include "lib1stclass.h"
+#include "1stclass.h"
int shmf(const char *file, char *shm_file){
#endif
- char tmp_file[1024]="";
+ char tmp_file[2048]="";
int i;
- safe_strcat(tmp_file, file);
- for(i=0;i<1024;i++){
+ safe_strcat(tmp_file, file, sizeof(tmp_file));
+ for(i=0;i<2048;i++){
if(tmp_file[i] == '/'){
tmp_file[i]='_';
}
}
}
- safe_strcat(shm_file, "/dev/shm/");
- safe_strcat(shm_file, tmp_file);
+ safe_strcat(shm_file, "/dev/shm/", 2048);
+ safe_strcat(shm_file, tmp_file, 2048);
#ifdef __cplusplus
string result(shm_file);
void tester::test(){
firstclass fst;
struct stat st;
-// is_num: 数字かどうかの判別
+// is_num:
// Judge whether this is number or not
+// 数字かどうかの判別
if(fst.is_num("2") && fst.is_num("test")==0){
cout << "is_num/C++: ok "<< endl;
ok++;
bad++;
}
-// ホスト名からIPを得る
+// host2ip
// Get IP from host name
+// ホスト名からIPを得る
string server="www.yahoo.co.jp";
string ip=fst.host2ip(server);
if( (ip.at(0) - '1'>=0) && (ip.at(0) - '1'<=9) ){
bad++;
}
-// 相対パスから絶対パスを得る
+// rel2abs:
// Get absolute path from relative path
+// 相対パスから絶対パスを得る
string rel_path="../lib1stclass/1/2/3";
string abs_path=fst.rel2abs(rel_path);
if( abs_path.find("/")==0 && abs_path.find("lib1stclass/1/2/3")!=string::npos){
ok++;
}
else{
- cout << "rel2abs/C: bad" << endl;
+ cout << "rel2abs/C: bad" << "/" << abs_path_char << endl;
bad++;
}
cout << "rel2abs 3/C: bad" << endl;
bad++;
}
-
-// ディレクトリを再帰的に作る
+
+// rmkdir:
// mkdir recursively
+// ディレクトリを再帰的に作る
fst.rmkdir("test_dir/depth2");
if(stat("test_dir/depth2", &st)==0){
cout << "rmkdir/C++: ok" << endl;
ok++;
-// パス全体を消去(ディレクトリを再帰的に消去)
+// remove_path:
// Remove path completely
+// パス全体を消去(ディレクトリを再帰的に消去)
fst.remove_path("test_dir");
if(stat("test_dir", &st)!=0){
cout << "remove_path/C++: ok" << endl;
bad++;
}
-// int型をchar型に変換
+// itoa
// Convert int to char
+// int型をchar型に変換
int integer=12345;
char itoa_str[6]="";
bad++;
}
-// 文字を逆並びにする
+// reverse_char:
// Reverse char
+// charを逆並びにする
char example1[]="1234";
fst.reverse_char(example1);
if (strcmp(example1,"4321") == 0){
bad++;
}
-
-// ファイルシステムに使えないアスキー文字を「_」に変換
+// remove_not_file_char:
// Convert ascii characters which cannot be used for file name into "_"
+// ファイルシステムに使えないアスキー文字を「_」に変換
string not_file_str="bk\\sl/sm:as*qt?dq\"lt<rt>vt|";
string file_str=fst.remove_not_file_char(¬_file_str);
if (file_str=="bk_sl_sm_as_qt_dq_lt_rt_vt_"){
bad++;
}
+// shmf:
+// Create temporary file on /dev/shm/
// /dev/shm/上への一時ファイル生成
string file="/tmp/test.txt";
if(fst.shmf(file)=="/dev/shm/_tmp_test.txt"){
cout << "shmf/C: bad" << endl;
bad++;
}
+
+// safe_strcat:
+// Safe strcat
+// 安全なstrcat
+ char a1[20]="";
+ char b1[]="12345";
+ fst.safe_strcat(a1, b1, sizeof(a1));
+ if(strcmp(a1,b1)==0){
+ cout << "safe_strcat 1/C++: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "safe_strcat 1/C++: bad" << endl;
+ bad++;
+ }
+
+ char a2[20]="";
+ char b2[]="12345";
+ safe_strcat(a2, b2, sizeof(a2));
+ if(strcmp(a2,b2)==0){
+ cout << "safe_strcat 2/C: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "safe_strcat 2/C: bad" << endl;
+ bad++;
+ }
+
+ char a3[2]="";
+ char b3[]="12345";
+ fst.safe_strcat(a3, b3, sizeof(a3));
+ if(strcmp(a3,"1")==0){
+ cout << "safe_strcat 3/C++: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "safe_strcat 3/C++: bad" << endl;
+ bad++;
+ }
+
+ char a4[2]="";
+ char b4[]="12345";
+ safe_strcat(a4, b4, sizeof(a4));
+ if(strcmp(a4,"1")==0){
+ cout << "safe_strcat 4/C: ok" << endl;
+ ok++;
+ }
+ else{
+ cout << "safe_strcat 4/C: bad" << endl;
+ bad++;
+ }
}
void tester::report(){