我正在使用 VM (virutalbox) 来运行附带的 LiveCD (Ubuntu 7.04),编写 Jon Erickson 的《黑客:剥削的艺术》的第二版。在第0x281节“文件访问”中,作者使用第82-84页的示例解释了通过文件描述符访问文件,以及open() close() read()和write()函数。
simplenote.c的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
void usage(char *prog_name,char *filename){
printf("Usage: %s < data to add to %s>\n",prog_name,filename);
exit(0);
}
void fatal(char *);
void *ec_malloc(unsigned int );
int main(int argc,char *argv[]){
int fd; //file descriptor
char *buffer,*datafile;
buffer = (char *)ec_malloc(100);
datafile = (char *)ec_malloc(20);
strcpy(datafile,"/tmp/notes");
if(argc < 2)
usage(argv[0],datafile);
strcpy(buffer,argv[1]);
printf("[DEBUG] buffer @ %p:\'%s'\n",buffer,buffer);
printf("[DEBUG] datafile @ %p:\'%s'\n",datafile,datafile);
strncat(buffer,"\n",1);//Add a newline on the end.
fd = open(datafile,O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR);
if(fd == -1)
fatal("in main() while opening file");
printf("[DEBUG] file descriptor is %d\n",fd);
//Writing data
if(write(fd,buffer,strlen(buffer)) == -1)
fatal("in main() while writing buffer to file");
//Closing file
if(close(fd) == -1)
fatal("in main() while closing file");
printf("Note has been saved.\n");
free(buffer);
free(datafile);
}
//A function to display an error message and then exit
void fatal(char *message){
char error_message[100];
strcpy(error_message,"[!!]Fatal Error");
strncat(error_message,message,83);
perror(error_message);
exit(-1);
}
//An error-checked malloc() wrapper function
void *ec_malloc(unsigned int size){
void *ptr;
ptr = malloc(size);
if(ptr == NULL)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
但是,当我在终端窗口中输入书中所述的以下说明时,它会返回以下错误消息:
reader@hacking:~/booksrc $ gcc -o simplenote simplenote.c
In file included from /usr/include/sys/stat.h:105, from simplenote.c:6:
/usr/include/bits/stat.h:70: error: field 'st_atim' has incomplete type
/usr/include/bits/stat.h:71: error: field 'st_mtim' has incomplete type
/usr/include/bits/stat.h:72: error: field 'st_ctim' has incomplete type
simplenote.c: In function 'main':
simplenote.c:35: error: 'O-WRONLY' undeclared (first use in this function)
simplenote.c:35: error: (Each undeclared identifier is reported only once
simplenote.c:35: error: for each function it appears in.)
simplenote.c:35: error: 'O_CREAT' undeclared (first use in this function)
simplenote.c:35: error: 'O_APPEND' undeclared (first use in this function)
这是 sys/stat.h 第 105 行:
#include <bits/stat.h>
这是 bits/stat.h 第 63-83 行:
#ifdef __USE_MISC
/* Nanosecond resolution timestamps are stored in a format
equivalent to 'struct timespec'. This is the type used
whenever possible but the Unix namespace rules do not allow the
identifier 'timespec' to appear in the <sys/stat.h> header.
Therefore we have to handle the use of this header in strictly
standard-compliant sources special. */
struct timespec st_atim; /* Time of last access. */
struct timespec st_mtim; /* Time of last modification. */
struct timespec st_ctim; /* Time of last status change. */
# define st_atime st_atim.tv_sec /* Backward compatibility */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
#else
__time_t st_atime; /* Time of last access. */
unsigned long int st_atimensec; /* Nscecs of last access. */
__time_t st_mtime; /* Time of last modification. */
unsigned long int st_mtimensec; /* Nsecs of last modification. */
__time_t st_ctime; /* Time of last status change. */
unsigned long int st_ctimensec; /* Nsecs of last status change. */
#endif
我想这可能对第一组问题有一定用处:
C++ system file bits/stat.h suddenly breaks with "error: field ‘st_atim’ has incomplete type"
/usr/include/time.h
cat time.h
在我的终端窗口中没有执行任何操作。
这是 simplenote.c 主函数第 1-6、34-35 行:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
// Opening the file
fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
我猜开放函数问题源于 fcntl.h ?
由于作者提供的错误代码,我似乎一直遇到问题。我不想一直依赖 stackoverflow 社区寻求帮助,那么您对新手在将来检查和解决这些问题有什么建议吗?
谢谢。
最佳答案
将选定的评论转化为半连贯的答案。
您可能应该显式启用 POSIX 定义。添加-D_XOPEN_SOURCE=700
到命令行,或 #define _XOPEN_SOURCE 700
在第一个#include
之前看看是否能解决任何问题。不过你不应该遇到这个问题; header 应该是独立的。
哦,但是 Ubuntu 7.04 已经过时了……你可能需要使用 600 而不是 700。它什么时候发布的(这本书什么时候出版的)?如果是 2009 年或更早版本,您可能需要旧版本 (600)。您看到这个错误仍然令人惊讶。您指定的命令行不包含通常会导致问题的选项(例如 -ansi -pedantic
或 -std=c99 -pedantic
)。您可以尝试使用 -std=gnu99
也;它可能会工作得更好。
您最近遇到了类似的问题 ( gcc -o stdlib.h syntax error c Hacking the Art of Exploitation )。你解决了吗?听起来好像 Live CD 上的编译系统不是自连贯的,或者您使用它的方式意味着它的行为不是自连贯的。你确定编译系统有效吗?看来是半废了。不知何故,它是否使用了错误的 header ?
I was able to resolve the previous problem by inserting
#include <stdint.h>
before#include <stdlib.h>
I will try the
-D_XOPEN_SOURCE=600
and get back to you. Something must be wrong with the compilation system.
嗯,您可能需要包含 <time.h>
(或者可能 <sys/time.h>
)在 <sys/stat.h>
之前,但是<sys/stat.h>
如果有效的话, header 已损坏。还有<stdlib.h>
如果您必须包含<stdint.h>
,则 header 已损坏在包含它之前。我想 Ubuntu 7.04 可能太旧了,你应该 #include <sys/types.h>
在许多这些 header 之前,但这仍然不是 <stdlib.h>
的借口;那应该是独立的。需要 POSIX 1997 #include <sys/types.h>
之前<sys/stat.h>
; POSIX 2004 没有。而且我认为 Ubuntu 7.04 并没有那么老。
但请注意,st_atim
成员(member)是新成员(member);它被添加到 POSIX 2008(因此也在 POSIX 2013 中)。这只是 st_atime
之前(并且 st_atime
现在是 st_atim.tv_sec
的宏)。
Including the
-D_XOPEN_SOURCE=600
dealt with the bits stat issue. Ubuntu 7.04 was released in 2007 and the 2nd edition of the book that I am using came out in 2008. Also, not sure if this is of use, but in another previous example that included both<stdio.h>
and<string.h>
(as opposed to only<stdio.h>
), the code would run fine without any intervention.
有趣……它会让你的生活变得有趣,但生活本来不需要变得有趣。 (像“愿你生活在有趣的时代”这样的中国咒语浮现在脑海中。)使用 -DXOPEN_SOURCE=600
在您的所有编辑中选择并祈祷吧;这很可能会解决您的大部分问题。考虑使用-std=gnu99
也可以,或者相反。幸运的话,其中一个或两个应该可以帮助您解决大多数问题。
关于c - stat.h 文件访问文件描述符 open() 黑客利用的艺术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31622726/