--- /dev/null
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include "lib1stclass.h"
+
+int host2ip(const char *server, char *ip){
+ struct addrinfo hint, *res, *p;
+ int error=0;
+ char ipstr[INET6_ADDRSTRLEN];
+
+ memset(&hint, 0, sizeof(hint));
+ hint.ai_family = PF_INET;
+ hint.ai_socktype = SOCK_STREAM;
+
+ error = getaddrinfo(server, "http", &hint, &res);
+
+ if(error){
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
+ return 1;
+ }
+
+ for(p = res;p != NULL; p = p->ai_next) {
+ void *addr;
+ char *ipver;
+
+ if (p->ai_family == AF_INET) { // IPv4
+ struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
+ addr = &(ipv4->sin_addr);
+ ipver = "IPv4";
+ } else { // IPv6
+ struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
+ addr = &(ipv6->sin6_addr);
+ ipver = "IPv6";
+ }
+
+ // convert the IP to a string and print it:
+ inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
+ }
+ freeaddrinfo(res); // free the linked list
+ safe_strcat(ip, ipstr);
+ return 0;
+}
--- /dev/null
+int is_num(const char *moji){
+ int i;
+ int is_num=0;
+ for(i=0; moji[i]; i++){
+ if( (moji[i] < '0') || (moji[i] > '9') ){
+ return 0;
+ }
+ else{
+ if(is_num==0){
+ is_num++;
+ }
+ }
+ }
+ return is_num;
+}
--- /dev/null
+#include <string.h>
+
+void reverse(char s[]){
+ int c, i, j;
+
+ for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
+ c = s[i];
+ s[i] = s[j];
+ s[j] = c;
+ }
+}
+
+void itoa(int n, char s[]){
+ int i, sign;
+ if ((sign = n) < 0) /* record sign */
+ n = -n; /* make n positive */
+ i = 0;
+ do { /* generate digits in reverse order */
+ s[i++] = n % 10 + '0'; /* get next digit */
+ } while ((n /= 10) > 0); /* delete it */
+ if (sign < 0)
+ s[i++] = '-';
+ s[i] = '\0';
+ reverse(s);
+}
--- /dev/null
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int host2ip(const char *server, char *ip);
+int is_num(const char *moji);
+void itoa(int n, char s[]);
+int rmkdir(const char *check_dir);
+int remove_path(const char *path);
+int remove_special_char(const char *str, char *dst);
+int safe_strcat(char *dst, const char *append);
+int safe_strncat(char *dst, const char *append, const int len);
+int shmf(const char *file, char *shmfile);
+
+#ifdef __cplusplus
+};
+#endif
--- /dev/null
+.PHONY: all
+all: lib1stclass.a
+
+lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_special_char.o lib1stclass.h
+ ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_special_char.o;
+ ranlib lib1stclass.a;
+
+host2ip.o: host2ip.c
+ gcc -Wall -c host2ip.c;
+
+itoa.o: itoa.c
+ gcc -Wall -c itoa.c
+
+remove_special_char.o: remove_special_char.c;
+ gcc -Wall -c remove_special_char.c;
+
+remove_path.o: remove_path.c
+ gcc -Wall -c remove_path.c;
+
+rmkdir.o: rmkdir.c
+ gcc -Wall -c rmkdir.c;
+
+safe_strcat.o: safe_strcat.c
+ gcc -Wall -c safe_strcat.c;
+
+safe_strncat.o: safe_strncat.c
+ gcc -Wall -c safe_strncat.c;
+
+is_num.o: is_num.c
+ gcc -Wall -c is_num.c;
+
+.PHONY: install
+install:
+ mkdir -p /usr/local/lib;
+ cp lib1stclass.a /usr/local/lib;
+ cp lib1stclass.h /usr/local/include;
+
+.PHONY: clean
+clean:
+ rm *.o -f *.o;
+ rm lib1stclass.a -f lib1stclass.a;
+
--- /dev/null
+#include <stdio.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <string.h>
+#include "lib1stclass.h"
+int remove_path(const char *path){
+ DIR *dirp;
+ struct dirent *dp;
+ if((dirp = opendir(path))==NULL){
+ return 0;
+ }
+ else{
+ while( (dp = readdir(dirp)) != NULL){
+ if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name,"..")){
+ }
+ else{
+ char file[512]="";
+ safe_strcat(file, path);
+ safe_strcat(file, "/");
+ safe_strcat(file, dp->d_name);
+ remove(file);
+ }
+ }
+ closedir(dirp);
+ rmdir(path);
+ }
+ return 0;
+}
--- /dev/null
+#include<string.h>
+#include<stdio.h>
+#include "lib1stclass.h"
+
+int remove_special_char(const char *str, char *dst){
+ int zero=0;
+ if(sizeof(dst)<sizeof(str)){
+ printf("Bad parameter for remove_special_character\n");
+ return 1;
+ }
+ if((strrchr(str, '\0'))==NULL){
+ printf("\\0 is missing for the input parameter of remove_special_character\n");
+ return 1;
+ }
+ for( ; *str!='\0'; str++, dst++){
+ if(*str == '/' || *str == ':' || *str == '?' || *str == '%'){
+ *dst='_';
+ }
+ else if(*str == '\0'){
+ *dst=*str;
+ zero++;
+ break;
+ }
+ else{
+ *dst=*str;
+ }
+ }
+ *dst='\0';
+ return 0;
+}
--- /dev/null
+#include <stdio.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <string.h>
+
+int rmkdir(const char *check_dir){
+ struct stat st;
+ char *token_p=NULL;
+ char dir_path[512]="";
+ char moto[512]="";
+ if(stat(check_dir, &st)!=0){
+ if(mkdir(check_dir, 0755)==0){
+ }
+ else{
+ safe_strcat(moto, check_dir);
+ token_p=strtok(moto, "/");
+ safe_strcat(dir_path, "/");
+ safe_strcat(dir_path, token_p);
+ if(stat(dir_path, &st)!=0){
+ if(mkdir(dir_path, 0755)==0){
+ }
+ else{
+ return 1;
+ }
+ }
+ while((token_p=strtok(NULL,"/"))!=NULL){
+ safe_strcat(dir_path, "/");
+ safe_strcat(dir_path, token_p);
+ if(stat(dir_path, &st)!=0){
+ if(mkdir(dir_path, 0755)==0){
+ }
+ else{
+ return 1;
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+#include "lib1stclass.h"
+
+int safe_strcat(char *dst, const char *append){
+ if( sizeof(append) >sizeof(dst) ){
+ printf("\"%s\" is too long for this system.\n", append);
+ return 1;
+ }
+ else{
+ strcat(dst, append);
+ }
+ return 0;
+}
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+
+int safe_strncat(char *dst, const char *append, const int len){
+ if( strlen(dst)>len ){
+ printf("\"%s\" is too long for this system.\n", append);
+ return 1;
+ }
+ else{
+ strncat(dst, append, len);
+ }
+ return 0;
+}
--- /dev/null
+#include <string.h>
+
+char *shmf(const char *file, char *shmfile){
+ char tmp_file[1024]="";
+ int i;
+ safe_strcat(tmp_file, file);
+ for(i=0;i<1024;i++){
+ if(tmp_file[i] == '/'){
+ tmp_file[i]='_';
+ }
+ else if(tmp_file[i] == '\0'){
+ break;
+ }
+ }
+ safe_strcat(shmfile, "/dev/shm/");
+ safe_strcat(shmfile, tmp_file);
+ return tmp_file;
+}