可以放在符合语法的任意位置,我比较喜欢放在最后面,这样可以省去声明函数
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
size_t lines(const char *filename) {
struct stat sb;
if(lstat(filename, &sb) != 0) return 0;
if(sb.st_size == 0) return 0;
size_t count = 1;
int fd = open(filename, O_RDONLY);
char *buf = malloc(sb.st_size);
read(fd, buf, sb.st_size);
for(long i = 0; i < sb.st_size; ++i) {
if(buf[i] == '\n') ++count;
}
free(buf);
close(fd);
return count;
}
const char *suffix(const char *filename) {
return strrchr(filename, '.');
}
size_t count_lines(const char *path) {
size_t count = 0;
DIR *dirp = opendir(path);
if(!dirp) return 0;
struct dirent *de;
char *cwd = malloc(PATH_MAX); getcwd(cwd, PATH_MAX); chdir(path);
while((de = readdir(dirp))) {
if(!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
if(de->d_type == DT_DIR) {
count += count_lines(de->d_name);
}
const char *s = suffix(de->d_name);
if(!s || strcmp(s, ".c")) continue;
count += lines(de->d_name);
}
chdir(cwd); free(cwd);
closedir(dirp);
return count;
}
int main(void) {
printf("%lu\n", count_lines("../"));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
size_t lines(const char *filename);
const char *suffix(const char *filename);
size_t count_lines(const char *path);
int main(void) {
printf("%lu\n", count_lines("../"));
return 0;
}
size_t lines(const char *filename) {
struct stat sb;
if(lstat(filename, &sb) != 0) return 0;
if(sb.st_size == 0) return 0;
size_t count = 1;
int fd = open(filename, O_RDONLY);
char *buf = malloc(sb.st_size);
read(fd, buf, sb.st_size);
for(long i = 0; i < sb.st_size; ++i) {
if(buf[i] == '\n') ++count;
}
free(buf);
close(fd);
return count;
}
const char *suffix(const char *filename) {
return strrchr(filename, '.');
}
size_t count_lines(const char *path) {
size_t count = 0;
DIR *dirp = opendir(path);
if(!dirp) return 0;
struct dirent *de;
char *cwd = malloc(PATH_MAX); getcwd(cwd, PATH_MAX); chdir(path);
while((de = readdir(dirp))) {
if(!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
if(de->d_type == DT_DIR) {
count += count_lines(de->d_name);
}
const char *s = suffix(de->d_name);
if(!s || strcmp(s, ".c")) continue;
count += lines(de->d_name);
}
chdir(cwd); free(cwd);
closedir(dirp);
return count;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
size_t lines(const char *filename) {
struct stat sb;
if(lstat(filename, &sb) != 0) return 0;
if(sb.st_size == 0) return 0;
size_t count = 1;
int fd = open(filename, O_RDONLY);
char *buf = malloc(sb.st_size);
read(fd, buf, sb.st_size);
for(long i = 0; i < sb.st_size; ++i) {
if(buf[i] == '\n') ++count;
}
free(buf);
close(fd);
return count;
}
const char *suffix(const char *filename);
size_t count_lines(const char *path);
int main(void) {
printf("%lu\n", count_lines("../"));
return 0;
}
const char *suffix(const char *filename) {
return strrchr(filename, '.');
}
size_t count_lines(const char *path) {
size_t count = 0;
DIR *dirp = opendir(path);
if(!dirp) return 0;
struct dirent *de;
char *cwd = malloc(PATH_MAX); getcwd(cwd, PATH_MAX); chdir(path);
while((de = readdir(dirp))) {
if(!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
if(de->d_type == DT_DIR) {
count += count_lines(de->d_name);
}
const char *s = suffix(de->d_name);
if(!s || strcmp(s, ".c")) continue;
count += lines(de->d_name);
}
chdir(cwd); free(cwd);
closedir(dirp);
return count;
}
|