/*================================================================
*
*tempnam() function
*
*
* ================================================================*/
#include<stdio.h>
int main( int argc , char *argv[] )
{
int n;
if( argc!=3 ){
printf( "usage: ./tempnam " );
//return;
}
printf( "%s", tempnam( argv[1][0] != ' '?argv[1]:NULL ,argv[2][0]!=' '?argv[2]:NULL ) );
printf( "strlen(argv1):%d\n" , strlen(argv[1]) );
printf( "strlen(argv2):%d\n" , strlen(argv[2]) );
//printf("%s\n", tempnam( NULL,NULL));
return 0;
}
|
在执行gcc tempnam.c -o tempnam
后执行./tempnam
就会出现segment fault !!原因是argv[1] argv[2]并没有值。
应该像这样执行才行:
$ ./a.out /home/sar TEMP specify both directory and prefix
/home/sar/TEMPsf00zi
$ ./a.out " " PFX use default directory: P_tmpdir
/tmp/PFXfBw7Gi
$ TMPDIR=/var/tmp ./a.out /usr/tmp " " use environment variable; no prefix
/var/tmp/file8fVYNi environment variable overrides directory
$ TMPDIR=/no/such/dir ./a.out /home/sar/tmp QQQ
/home/sar/tmp/QQQ98s8Ui invalid environment dire
|
阅读(1875) | 评论(0) | 转发(0) |