int main(int argc,char **argv) { int i,fd; unsigned char c,*ptr; struct stat stat; if(argc!=2) err_quit("usgae: shmread <name>"); fd=Shm_open(argv[1],O_RDONLY,FILE_MODE); Fstat(fd,&stat); ptr=Mmap(NULL,stat.st_size,PROT_READ,MAP_SHARED,fd,0); Close(fd); for(i=0;i<stat.st_size;i++) if((c=*ptr++)!=(i%256)) err_ret("ptr[%d] =%d",i,c); exit(0); }
int swap_straddr(char *src, char *dst) { char a[3]; int i, slen; char *crow, *tmp, *atom; int count = 0, offset = 0; slen = strlen(src); if (slen > DNS_MAX_HNAME_LEN) goto mlf_addr; tmp = src; for (i = 0; i < 4; i++) { count = 0; atom = a; while (*tmp && *tmp != '.') { if (count > 2) goto mlf_addr; *atom++ = *tmp++; count++; } if (!count) goto mlf_addr; crow = dst + slen - count - offset; strncpy(crow, a, count); offset += count; if (!(*tmp)) break; else { if (i == 3) goto mlf_addr; *(crow - 1) = '.'; offset++; tmp++; } } *(dst + slen) = 0; return 0; mlf_addr: debug(DBG_INSANE, "in swap_straddr: invalid address `%s`.\n", src); err_ret(ERR_DNSMDD, -1); }
/* * Descend through the hierachy, starting at "fullpath". * If "fullpath" is anything other than a directory, we lstat() it, * recursively for each name in the directory. */ static int dopath(Func *func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret, n; if (lstat(fullpath, &statbuf) < 0) /* stat error */ return (func(fullpath, &statbuf, FTW_NS)); if (S_ISDIR(statbuf.st_mode) == 0) /* not a directory */ return (func(fullpath, &statbuf, FTW_F)); /* * It's a directory. First call func() for the directory, * then process each filename in the directory. */ if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) return (ret); n = strlen(fullpath); if (n + NAME_MAX + 2 > pathlen) { /* expand path buffer */ pathlen *= 2; if ((realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } fullpath[n++] = '/'; fullpath[n] = 0; if ((dp = opendir(fullpath)) == NULL) /* cannot read directory */ return(func(fullpath, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; /* ignore dot and dot-dot */ strcpy(&fullpath[n], dirp->d_name); /* append after "/" */ if ((ret = dopath(func)) != 0) break; /* time to leave */ } fullpath[n-1] = 0; /* erase everything from slash onward */ if (closedir(dp) < 0) err_ret("Cannot close dir: %s", fullpath); return ret; }
/* * Descend through the hierarchy, starting at "fullpath". * If "fullpath" is anything other than a directory, we lstat() it, * call func(), and return. For a directory, we all ourself * recursively for each name in the directory. */ static int dopath(Myfunc* func) /* we return whatever func() returns */ { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; if (lstat(fullpath, &statbuf) < 0) /* stat error */ return(func(fullpath, &statbuf, FTW_NS)); if (S_ISDIR(statbuf.st_mode) == 0) /* not a directory */ return(func(fullpath, &statbuf, FTW_F)); /* * It's a directory. First call func() for the directory, * then process each filename in the directory. */ if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) return(ret); ptr = fullpath + strlen(fullpath); /* point to end of fullpath */ *ptr++ = '/'; *ptr = 0; if ((dp = opendir(fullpath)) == NULL) /* can't read directory */ return(func(fullpath, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; /* ignore dot and dot-dot */ strcpy(ptr, dirp->d_name); /* append name after slash */ if ((ret = dopath(func)) != 0) /* recursive */ break; /* time to leave */ } ptr[-1] = 0; /* erase everything from slash onward */ if (closedir(dp) < 0) err_ret("can't close directory %s", fullpath); return(ret); }
int main (int argc, char **argv) { int fd; int i; if (argc < 2) err_quit ("Usage: isastream file ..."); for (i = 1; i < argc; i++) { if ((fd = open (argv [i], O_RDONLY)) == -1) { err_ret ("Can't open %s", argv [i]); continue; } printf ("%s %s a STREAMS device\n", argv [i], (isastream (fd)) ? "is" : "is not"); } return (0); }
/* * Takes a label: is there a ptr? * Returns: * -1 is a malformed label is found * 0 if there's no pointer * <offset from start_pkt> if a pointer is found */ int getlblptr(char *buf) { uint16_t dlbl; char c[2]; memcpy(c, buf, 2); if (!LBL_PTR(*c)) /* No ptr */ return 0; if (LBL_PTR(*c) != LBL_PTR_MASK) { debug(DBG_INSANE, "In getlblptr: invalid octet %02x", (unsigned char) c[0]); err_ret(ERR_DNSMLO, -1); } (*c) &= LBL_PTR_OFF_MASK; memcpy(&dlbl, c, 2); dlbl = ntohs(dlbl); return dlbl; /* offset */ }
int delete_ntk_forward_chain(iptc_handle_t * t) { int res; res = iptc_is_chain(NTK_MARK_CHAIN, *t); if (!res) return 0; res = iptc_flush_entries(NTK_MARK_CHAIN, t); if (!res) goto cannot_delete; res = iptc_delete_chain(NTK_MARK_CHAIN, t); if (!res) goto cannot_delete; return 0; cannot_delete: error("In delete_ntk_forward_chain: -> %s", iptc_strerror(errno)); err_ret(ERR_NETDEL, -1); }
int main(int argc, char *argv[]) { int i; struct stat buf; for (i = 1; i < argc; i++) { printf("%s: ", argv[i]); if (stat(argv[i], &buf) < 0) { err_ret("stat error"); continue; } printf("dev = %d/%d", major(buf.st_dev), minor(buf.st_dev)); if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) { printf(" (%s) rdev = %d/%d", (S_ISCHR(buf.st_mode)) ? "character" : "block", major(buf.st_dev), minor(buf.st_dev)); } printf("/n"); } exit(0); }
/* * Disassembles a DNS qst_section_set. * Use the above function for each question section. * -1 on error. Number of bytes readed on success. * If -1 is returned, rcode ha sto be set to E_INTRPRT */ int d_qsts_u(char *start_buf, char *buf, dns_pkt * dp, int limit_len) { int offset = 0, res; int i, count; if (!(count = DP_QDCOUNT(dp))) return 0; /* No questions. */ for (i = 0; i < count; i++) { if ((res = d_qst_u(start_buf, buf + offset, dp, limit_len - offset)) == -1) { error(err_str); err_ret(ERR_DNSMDD, -1); } offset += res; } return offset; }
int main(int argc, char *argv[]) { int i; struct stat buf; char *ptr; for (i = 1; i < argc; i++) { if (stat(argv[i], &buf) < 0) { err_ret("lstat error"); continue; } if (S_ISREG(buf.st_mode) ) ptr = "regular"; else if (S_ISDIR(buf.st_mode) ) ptr = "directory"; printf("%s\n", ptr); } exit(0); }
int main(void){ char buf[MAXLINE]; pid_t pid; int status; printf("%% "); while(fgets(buf,MAXLINE,stdin)!=NULL){ buf[strlen(buf)-1]=0; if((pid=fork())<0) err_sys("fork error"); else if(pid==0){ execlp(buf,buf,(char*)0); err_ret("could not execute:%s",buf); exit(127); } if((pid=waitpid(pid,&status,0))<0) err_sys("waitpid error"); printf("%% "); } exit(0); }
int main(int argc, char const *argv[]) { int apipe[2], len; char buf[BUFSIZE]; if (pipe(apipe) == -1) err_ret("pipe error", ""); printf("Got a pipe. It is descriptors: %d, %d\n", apipe[0], apipe[1]); if ((fgets(buf, BUFSIZE, stdin)) != NULL) { write(apipe[1], buf, strlen(buf)); } len = read(apipe[0], buf, BUFSIZE); write(1, buf, len); return 0; }
int main(void) { STARTUPINFO si; PROCESS_INFORMATION pi; DWORD dwChildID = 0; char szBuff[MAXLINE]; // Initialize data ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); printf("%% "); /* print prompt (printf requires %% to print %) */ while (fgets(szBuff, MAXLINE, stdin) != NULL) { szBuff[strlen(szBuff) - 1] = 0; /* replace newline with null */ /* Start the child process */ if ( ! CreateProcess( NULL, // No module name (use command line) szBuff, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION struct ) { err_ret( "CreateProcess failed." ); } /* parent */ /* wait for normal termination of child process */ WaitForSingleObject(pi.hProcess, INFINITE); printf("%% "); } exit(0); }
int main(int argc, char **argv) { char *ptr, **pptr; char str[16]; /* ddd.ddd.ddd.ddd\0 */ struct hostent *hptr; while (--argc > 0) { ptr = *++argv; if ((hptr = gethostbyname(ptr)) == NULL) { err_msg("gethostbyname error for host: %s: %s", ptr, hstrerror(h_errno)); continue; } printf("official hostname: %s\n", hptr->h_name); for (pptr = hptr->h_aliases; *pptr != NULL; pptr++) printf("\talias: %s\n", *pptr); switch (hptr->h_addrtype) { case AF_INET: pptr = hptr->h_addr_list; for (; *pptr != NULL; pptr++) { printf("\taddress: %s\n", Inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str))); hptr = gethostbyaddr(*pptr, hptr->h_length, hptr->h_addrtype); if (hptr == NULL) err_msg("gethostbyaddr error: %s", hstrerror(h_errno)); else if (hptr ->h_name != NULL) printf("\tname: %s\n", hptr->h_name); else printf("\t(no hostname returned by gethostbyaddr)\n"); } break; default: err_ret("unknown address type"); break; } } exit(0); }
static int do_path(myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret, n; if (lstat(fullpath, &statbuf) < 0) return(func(fullpath, &statbuf, FTW_NS)); if (S_ISDIR(statbuf.st_mode) == 0) return(func(fullpath, &statbuf, FTW_F)); if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) return ret; n = strlen(fullpath); if (n + NAME_MAX + 2 > pathlen) { pathlen *= 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } fullpath[n++] = '/'; fullpath[n] = 0; if ((dp = opendir(fullpath)) == NULL) return(func(fullpath, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; strcpy(&fullpath[n], dirp->d_name); if ((ret = do_path(func)) != 0) break; } fullpath[n-1] = 0; if (closedir(dp) < 0) err_ret("cannot close directory %s", fullpath) ; return ret; }
int main (int argc,char **argv) { int i; struct stat buf; char *ptr; for(i=1;i<argc;i++) { printf("%s: \n \t",argv[i]); if(lstat(argv[i],&buf)<0) { err_ret("lstat error"); continue; } if(S_ISREG(buf.st_mode)) ptr="regular file"; else if(S_ISDIR(buf.st_mode)) ptr="directory file"; else if(S_ISCHR(buf.st_mode)) ptr="character special"; else if(S_ISBLK(buf.st_mode)) ptr="block special"; else if(S_ISFIFO(buf.st_mode)) ptr="fifo "; else if(S_ISLNK(buf.st_mode)) ptr="symbolic link"; else if(S_ISSOCK(buf.st_mode)) ptr="socket"; else ptr="** unknown mode **"; printf("%s\n",ptr); } exit(0); }
int CXSPAsyncSocket::SocketEpollDel(const int sockfd, const int event) { EPOLLEVENT epollevent; epollevent.data.fd = sockfd; epollevent.events = event; pthread_t thrid = pthread_self(); struct EpollWaitObject *pEwobj=NULL; if(m_ThreadId2Ewobj.Find(thrid, pEwobj) == 0) { int ret = epoll_ctl(pEwobj->epollId, EPOLL_CTL_DEL, sockfd, &epollevent); if(-1 == ret) { err_ret(GetLastError(ERROR_EPOLL_CTL)); return -1; } m_ThreadId2Ewobj.Set(thrid, pEwobj); SafeDecreaseEvent(); return ret; } return -2; }
int main(int argc, char **argv) { int i, fd; struct stat stat; unsigned char c, *ptr; if (argc != 2) err_quit("Usage: read <name>"); //open, get size, map fd = Shm_open(argv[1], O_RDONLY, FILE_MODE); Fstat(fd, &stat); ptr = Mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); Close(fd); //check teh ptr[0] = 0, ptr[1] = 1, etc. for (i = 0; i < stat.st_size; i++) if ((c = *ptr++) != (i % 256)) err_ret("ptr[%d] = %d", i, c); exit(0); }
int main(int argc, char *argv[]) { int fd, pid; if ((pid = fork()) == -1) err_ret("fork", ""); else if (pid == 0) { fd = open(argv[1], O_RDONLY); close(0); dup(fd); close(fd); execlp("sort", "sort", NULL); } else { wait(NULL); } return 0; }
static int dopath(Myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; if(lstat(fullpath, &statbuf) < 0) return (func(fullpath, &statbuf, FTW_NS)); if(S_ISDIR(statbuf.st_mode) == 0)//not a dir return (func(fullpath, &statbuf, FTW_F)); //dir if((ret = func(fullpath, &statbuf, FTW_D)) != 0) return (ret); //printf("%s_______fullpath_in_dopath\n", fullpath); ptr = fullpath + strlen(fullpath);//point to end of path *ptr++ = '/';//这里不需要了,fullpath=realpath,realpath后面有已经有'/' //printf("after_/_fullpath______%s\n", fullpath); *ptr = 0; //if can't read dir if ((dp = opendir(fullpath)) == NULL) return (func(fullpath, &statbuf, FTW_DNR)); //read while((dirp = readdir(dp)) != NULL) { if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; strcpy(ptr, dirp->d_name);//append dname after '/' if((ret = dopath(func)) != 0)//recursive 递归,循环 break;//time to leave } //ptr[-1] = 0;//erase everything from slash onwards ??? if(closedir(dp) < 0) err_ret("can't close dir %s", fullpath); return (ret); }
int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <filename>\n", argv[1]); exit(1); } int fd; fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE); write(fd, "abcdef", 6); struct stat statbuf; fstat(fd, &statbuf); fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID); tell_wait(); pid_t pid; pid = fork(); if (pid > 0) { write_lock(fd, 0, SEEK_SET, 0); tell_child(pid); waitpid(pid, NULL, 0); } else { wait_parent(); set_fl(fd, O_NONBLOCK); if (read_lock(fd, 0, SEEK_SET, 0) != -1) err_sys("child: read_lock successed"); printf("read_lock of already-locked region returns %d\n", errno); lseek(fd, 0, SEEK_SET); char buf[5]; if (read(fd, buf, 2) < 0) err_ret("read failed (mandatory locking works)"); else printf("read OK (no mandatory locking), buf = %2.2s\n", buf); } }
int main(void) { int i, size; char *path; if (chdir(MYHOME) < 0) err_sys("chdir error"); for (i = 0; i < DEPTH; i++) { if (mkdir(NAME, DIR_MODE) < 0) err_sys("mkdir failed, i = %d", i); if (chdir(NAME) < 0) err_sys("chdir failed, i = %d", i); } if (creat("afile", FILE_MODE) < 0) err_sys("creat error"); /* * The deep directory is created, with a file at the leaf. * Now let's try and obtain its pathname. */ path = path_alloc(&size); // printf ( "Path Size %ld\n", size ) ; for ( ; ; ) { if (getcwd(path, size) != NULL) break; else { err_ret("getcwd failed, size = %d", size); size += 100; if ( (path = realloc(path, size)) == NULL) err_sys("realloc error"); } } printf("length = %d\n%s\n", strlen(path), path); // printf("%s", path); exit(0); }
int main(void) { char buf[MAXLINE]; /* from apue.h */ pid_t pid; int status; printf("%% "); /* print prompt (printf requires %% to print %) */ /* 读入一行, 每一行命令会产生一个子进程用于执行 */ while (fgets(buf, MAXLINE, stdin) != NULL) { /* 去掉换行符 */ if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = 0; /* replace newline with null */ /* 执行读入的命令 */ /* fork创建一个子进程, 返回<0则表示fork发生了错误 */ if ((pid = fork()) < 0) { err_sys("fork error"); /* 对于子进程, fork返回的pid=0(父进程fork返回的pid>0) */ } else if (pid == 0) { /* child */ /* 调用execlp以执行从标准输入读入的命令 */ /* fork+exec组合, 是某些操作系统所称的产生(spawn)一个新的进程 */ execlp(buf, buf, (char *)0); err_ret("couldn't execute: %s", buf); /* 退出 */ exit(127); } /* 父进程, 等待子进程终止 */ /* pid为子进程id, status为子进程终止状态(用于判断其实如何终止的) */ /* parent */ if ((pid = waitpid(pid, &status, 0)) < 0) err_sys("waitpid error"); printf("%% "); } exit(0); }
int main(int argc, const char *argv[]) { struct sockaddr_in server_addr; char buf[MAXLINE]; int n, sock_fd; if(argc != 2) err_sys("Usage ./server server_addr msg"); //int socket_r(int domain, int type, int protocol); sock_fd = socket_r(AF_INET, SOCK_STREAM, 0); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; //int inet_pton(int af, const char *src, void *dst); inet_pton(AF_INET, argv[1], &server_addr.sin_addr); server_addr.sin_port = htons(SERVER_PORT); //void connect_r(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen, void *arg); connect_r(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr), (void *)argv[1]); //ssize_t write_r(int fd, const void *ptr, size_t nbytes); fprintf(stdout, "\n"); while(fgets(buf, MAXLINE, stdin) != NULL){ if(write_r(sock_fd, buf, strlen(buf)) == -1) err_ret("write_r error"); //supplemention //ssize_t read_r(int fd, void *ptr, size_t nbytes); n = read(sock_fd, buf, MAXLINE); if(n == 0) fprintf(stdout, "peer side(%s port %d) has been closed.\n", argv[1], ntohs(server_addr.sin_port)); else { fprintf(stdout, "responsed from server:\n"); write_r(STDOUT_FILENO, buf, n); //fprintf(stdout, "\n"); } } //void close_r(int fd); close_r(sock_fd); return 0; }
int mark_close() { iptc_handle_t t; int res; if (!clean_on_exit) { debug(DBG_NORMAL, "mark_close: cleaning is not my task."); return 0; } load_dump_rules(); res = table_init(MANGLE_TABLE, &t); if (res) goto reset_error; res = 0; res += delete_rule(&rr, &t); res += delete_rule(&fr, &t); if (death_loop_rule) { debug(DBG_INSANE, "In mark_close: I'm an IGW: deleting death loop rule."); res += delete_rule(&dr, &t); } if (res) goto reset_error; res = delete_ntk_forward_chain(&t); if (res) goto reset_error; res = commit_rules(&t); if (res) goto reset_error; debug(DBG_NORMAL, "Netfilter completely restored."); return 0; reset_error: error(err_str); loginfo("Netfilter was not restored. To clean, run:\n" "\tiptables -t mangle -F\n" "\tiptables -t mangle -X %s", NTK_MARK_CHAIN); err_ret(ERR_NETRST, -1); }
int main(int argc , char *argv[]) { int i ; struct stat buf ; char *ptr ; for(i = 1 ; i < argc ; ++i) { if(lstat(argv[i],&buf)<0) { err_ret("lstat error"); continue ; } if(S_ISREG(buf.st_mode)) ptr = "regular"; else if(S_ISDIR(buf.st_mode)) ptr = "directory"; else ptr = "** unknown mode **"; printf("%s\n",ptr); } return 0; }
int main (void) { char buf [MAXLINE];//定义数组存放标准输入的字符 pid_t pid ;//定义进程ID int status ; printf("%% ") ; while (fgets (buf,MAXLINE,stdin) != NULL){ //标准IO函数从标入命令 if (buf[strlen(buf)-1] =='\n') buf[strlen(buf)-1] = 0;//没搞明白 if ((pid = fork ()) < 0 ){ //创建子进程 err_sys ("fork error"); }else if (pid == 0 ){ //子进程返回值 execlp( buf, buf, (char *) 0) ;//子进程调用execle用来执行新的程序, ( char*) 0 err_ret ("couldnot execute :%s", buf); exit (127) ; } if (( pid = waitpid (pid , &status,0)) < 0) //子进程调用execle执行新程序,父进程等待子进程终止 err_sys("waitpid error"); printf ("%% "); } exit (0); }
int main(int argc, char **argv) { char *ptr, **pptr; char str[INET_ADDRSTRLEN]; struct hostent *hptr; while ( --argc > 0) { ptr = *++argv; /* It is not re-entrant. * Takes name, returns pointer to struct hostent, * that contains addr_list of binary IPv4 addresses */ if ( ( hptr = gethostbyname(ptr)) == NULL) { err_msg( "gethostbyname error for host: %s: %s", ptr, hstrerror( h_errno)); continue; } /* FQDN */ printf( "official hostname: %s\n", hptr->h_name); for ( pptr = hptr->h_aliases; *pptr != NULL; pptr++) printf( "\talias: %s\n", *pptr); switch ( hptr->h_addrtype) { case AF_INET: pptr = hptr->h_addr_list; for ( ; *pptr != NULL; pptr++) printf( "\taddress: %s\n", Inet_ntop( hptr->h_addrtype, *pptr, str, sizeof(str))); break; default: err_ret( "unknown address type"); break; } } exit(0); }
void pr_mask(const char *str){ sigset_t sigset; int errno_save; errno_save = errno; if(sigprocmask(0,NULL,&sigset)<0){ err_ret("sigprocmask error"); }else{ printf("%s",str); if(sigismember(&sigset,SIGINT)) printf(" SIGINT"); if(sigismember(&sigset,SIGQUIT)) printf(" SIGQUIT"); if(sigismember(&sigset,SIGUSR1)) printf(" SIGUSR1"); if(sigismember(&sigset,SIGALRM)) printf(" SIGALRM"); printf("\n"); } errno = errno_save; }
void push2tree(char *py, char *id) { if (strlen(id) != 24) { err_ret("bad id"); return; } py_tree *itree, *node; int py_len = strlen(py); for (int i = py_len - 1; i >= 0; i--) { itree = tree; for (int j = i; j < py_len; j++) { int idx = get_idx(py[j]); node = itree; itree = itree->next[idx]; if (itree == NULL) itree = node->next[idx] = make_node(); } itree->ids = add2list(itree->ids, id); } }