peri mkstemp

Vasilis Vasaitis mod at hal.csd.auth.gr
Sun Feb 7 22:50:29 EET 1999


  Prosekse ta lathi sou...

On Sun, Feb 07, 1999 at 02:22:47PM +0000, D.A.Glynos wrote:
> re gmt giati to parakatw den douleuei?
> Egrapsa 5-6 grammes kwdika gia na ftiaxnei ena temporary file :
> 
> #include <unistd.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/stat.h>
> 
> main(){
> int fd;
> char *k;
> 
>         k= (char *) malloc(sizeof(k));
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Katarxin to casting se (char *) einai peritto, afou i malloc epistrefei
void *. I isodinami entoli einai:

          k = malloc(4);

afou sta perissotera 32-bita sistimata oi pointers exoun megethos 4 bytes.
Diladi edo desmeueis 4 bytes mnimis sta opoia deixnei o k.

>         k="/ergasia/temp/koukouXXXXXX";
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Edo anatheteis ston k tin dieuthinsi autou tou constant character
string. I dieuthinsi pou perieixe prin (auti pou pire apo tin malloc)
xanetai, ta 4 bytes auta menoun orfana kai na ena memory leak. Tespa...
mikro to kako.

>         umask(022);
>         fd=mkstemp(k);
          ^^^^^^^^^^^^^^

  Otan tora kaleis tin mkstemp me parametro to k, auti poli logika
ipothetei oti mporei na metavalei to string sto opoio deixnei to k. Omos
to k deixnei se read-only thesi mnimis, opote pairneis ena segmentation
fault pou einai olo diko sou.

>         write(fd,"patates",sizeof("patates"));
>         close(fd);
>         printf("%i",fd);
>         free(k);
> }

  As min katigoroume loipon tin libc gia kathe provlima. Oi pointers einai
ipouloi kai tha prepei na eksikiotheis kala mazi tous prin arxiseis na
tous xrisimopoieis me anesi.

  Pantos to provlima linetai eukola. Alakse tis dio protes grammes pou
simeiosa me:

        k = strdup("/ergasia/temp/koukouXXXXXX");

Min ksexaseis stin arxi tou programmatos:

#include <string.h>


Vasilis Vasaitis
vvas at egnatia.ee.auth.gr

--
====================================================================
Gia boithia (h na diagrafhte) e-mail sto majordomo at hellug.gr
Ta archives tis listas einai sto http://lists.hellug.gr
prin steilete kapoia erothsh psakte mipos exei hdh apanththei.
Gia opoiodipote problima stilte e-mail ston owner-linux-greek-users
====================================================================



More information about the Linux-greek-users mailing list