Advertisement
1fractal

Untitled

Sep 30th, 2020
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. # Thème 5 - système: Gestion de fichiers
  2.  
  3. ## Exo 5.1
  4.  
  5. `fopen` n'est pas une primitive systeme
  6. les fonctons de bibl passent par primitives syst. ex fopen apelle open.
  7. leur difference est que elles apportent des differentes nouvelles fonctionalités. Peuvent etre utile en terme de perf;
  8.  
  9. ## Exo 5.2
  10.  
  11. ```c++
  12. int faux (char *nom){
  13. FILE *fp ;
  14. int c ;
  15. fp = open (nom, "r") ;
  16. read (fp, &c, 1) ;
  17. fclose (fp) ;
  18. return c;
  19. }
  20. ```
  21.  
  22. - FILE\* est un pointeur de structure `FILE` or open renvoi un `int`.
  23. - `open` n'a pas "r" comme argument.
  24.  
  25. ## Exo 5.3/5.4
  26.  
  27. ```cpp
  28. void myCP(char * src, char * dest){
  29. int fd_src, fd_dest;
  30. //open files
  31. if((fd_src = open(src,O_RDONLY)) == -1){
  32. perror("Error: oped fd_src");
  33. exit(EXIT_FAILURE);
  34. }
  35. if((fd_dest = open(dest,O_WRONLY | O_CREAT | O_TRUNC,0666)) == -1){
  36. perror("Error: fd_src open");
  37. close(fd_src);
  38. exit(EXIT_FAILURE);
  39. }
  40.  
  41. //copy
  42. /**
  43. * @details size of bytes which will be copied | any impact on algo
  44. */
  45. uint size = 0;
  46. char buffer[BUF_SIZE];
  47. int bytes = BUF_SIZE;
  48. while ((bytes = read(fd_src,&buffer,bytes)) > 0) {
  49. size += bytes;
  50. int written;
  51. char* pbuffer = buffer;
  52. while ((written = write(fd_dest,pbuffer,bytes)) != bytes){
  53. bytes -= written;
  54. pbuffer += written;
  55. }
  56. }
  57. if(bytes == 0){
  58. printf("Copying ended successfully, copied [%d] of bytes", size);
  59. return;
  60. }
  61. if(bytes == -1){
  62. perror("Error: on copy > read");
  63. exit(EXIT_FAILURE);
  64. }
  65. return;
  66. }
  67.  
  68. ```
  69.  
  70. ## Exo 5.5
  71.  
  72. ```cpp
  73. void mygetchar(){
  74. char buffer;
  75. int bytes = 1;
  76. while ((bytes = read(STDIN_FILENO,&buffer,bytes)) > 0){
  77. int written = 0;
  78. char* pbuffer = buffer;
  79. while ((written = write(STDOUT_FILENO,&pbuffer,bytes)) != bytes){
  80. bytes -= written;
  81. pbuffer += written;
  82. }
  83. }
  84. }
  85.  
  86.  
  87. char mygetchar(){
  88. char buffer;
  89. int readed = 0;
  90. if((readed = read(STDIN_FILENO,&buffer,1)) >= 0){
  91. // dprintf(STDOUT_FILENO,"[%d : %c]\t",readed,buffer);
  92. switch (readed) {
  93. case 0:
  94. return EOF;
  95. break;
  96. default:
  97. return buffer;
  98. break;
  99. }
  100. }
  101. perror("Error on read");
  102. exit(EXIT_FAILURE);
  103. }
  104.  
  105.  
  106. int main() {
  107. char c;
  108. while((c = mygetchar()) != EOF ){
  109. write(STDIN_FILENO,&c,1);
  110. }
  111.  
  112. return 0;
  113. }
  114.  
  115. ```
  116.  
  117. ## Exo 5.6
  118.  
  119. ```c++
  120. char my_getChar(){
  121. struct buffer{
  122. char c_buf[1024];
  123. int buffer_size;
  124. int cur_pos;
  125. };
  126.  
  127. static struct buffer buffer = {0};
  128. char result = '\n';
  129. int read_bytes;
  130.  
  131. if(buffer.buffer_size == 0){
  132. if((read_bytes = read(STDIN_FILENO,&buffer.c_buf,1024 )) > 0){
  133. buffer.buffer_size = read_bytes;
  134. buffer.cur_pos = 0;
  135. std::cout << "Buffer_size : " << buffer.buffer_size << " Curr_pos : " << buffer.cur_pos << '\n';
  136. }
  137. }
  138. if(buffer.buffer_size > 0){
  139. result = buffer.c_buf[buffer.cur_pos];
  140. buffer.buffer_size--;
  141. buffer.cur_pos = (buffer.cur_pos+1) % 1024;
  142. return result;
  143. }
  144. return result;
  145.  
  146. }
  147.  
  148.  
  149.  
  150. ```
  151.  
  152. ## Exo 5.8
  153.  
  154. **Get group & user name**
  155.  
  156. ```c++
  157. #include<pwd.h>
  158. #include<grp.h>
  159. #include<sys/stat.h>
  160.  
  161. struct stat info;
  162. stat(filename,&info);
  163. struct passwd *pw = getpwuid(info.st_uid);
  164. struct group *gr = getgrgid(info.st_gid);
  165.  
  166. // if pw != 0, pw->pw_name contains the user name
  167. // if gr != 0, gr->gr_name contains the group name
  168.  
  169. ```
  170.  
  171. **st_mtime to string**
  172.  
  173. ```c++
  174. time_t t = mystat.st_mtime;
  175. struct tm lt;
  176. localtime_r(&t, &lt);
  177. char timbuf[80];
  178. strftime(timbuf, sizeof(timbuf), "%c", &lt);
  179.  
  180. // #################################################
  181.  
  182. struct tm *tm;
  183. char buf[200];
  184. /* convert time_t to broken-down time representation */
  185. tm = localtime(&t);
  186. /* format time days.month.year hour:minute:seconds */
  187. strftime(buf, sizeof(buf), "%d.%m.%Y %H:%M:%S", tm);
  188. printf("%s\n", buf);
  189.  
  190. ```
  191.  
  192. ```c++
  193.  
  194. void printLine(struct data *data) {
  195. struct group *g = getgrgid(data->gid);;
  196. struct passwd *passwd = getpwuid(data->uid);
  197. struct tm lt;
  198. localtime_r(&data->mtime,&lt);
  199. char timebuf[80];
  200. strftime(timebuf,sizeof(timebuf),"%c",&lt);
  201. printf("%c", data->ftype);
  202. for (int i = 0; i < 9; ++i) {
  203. printf("%c", data->accessmode[i]);
  204. }
  205. printf(" %d %s %s", data->links,(passwd)?passwd->pw_name: NULL,(g)?g->gr_name:NULL);
  206. printf(" %5d", data->size);
  207. printf(" %s",timebuf);
  208. printf(" %20s", data->name);
  209. printf("\n");
  210. }
  211.  
  212. void my_ls() {
  213.  
  214. DIR *dir;
  215. struct dirent *dent;
  216. struct stat stbuf;
  217.  
  218. dir = opendir(".");
  219. if (!dir) {
  220. perror("Error: opendir");
  221. exit(EXIT_FAILURE);
  222. }
  223.  
  224. struct data data = {0};
  225.  
  226. while ((dent = readdir(dir)) != NULL) {
  227. // remove . & ..
  228. if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
  229. continue;
  230. }
  231. if (stat(dent->d_name, &stbuf)) {
  232. closedir(dir);
  233. perror("Error: Stat");
  234. }
  235.  
  236. //file type
  237. data.ftype = "-d"[S_ISDIR(stbuf.st_mode)];
  238. //file perms
  239. data.uid = stbuf.st_uid;
  240. data.gid = stbuf.st_gid;
  241. data.links = stbuf.st_nlink;
  242. data.size = stbuf.st_size;
  243. {
  244. int perms[] = {S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IROTH};
  245. for (int i = 0; i < 9; ++i) {
  246. int index = i % 3;
  247. switch (index) {
  248. case 0:
  249. data.accessmode[i] = "-r"[(stbuf.st_mode & perms[i]) == perms[i]];
  250. break;
  251. case 1:
  252. data.accessmode[i] = "-w"[(stbuf.st_mode & perms[i]) == perms[i]];
  253. break;
  254. case 2:
  255. data.accessmode[i] = "-x"[(stbuf.st_mode & perms[i]) == perms[i]];
  256. break;
  257. default:
  258. data.accessmode[i] = '-';
  259. break;
  260. }
  261. }
  262. }
  263. data.mtime = stbuf.st_mtime;
  264. //file name
  265. data.name = dent->d_name;
  266.  
  267.  
  268. printLine(&data);
  269. // printf(":> %c %d %20s\n","df"[data.ftype],data.links,dent->d_name);
  270. }
  271. closedir(dir);
  272.  
  273.  
  274. }
  275.  
  276.  
  277. ```
  278.  
  279. ## Exo 5.9
  280.  
  281. ```c++
  282.  
  283. /*Exercice 5.9O
  284. * On désire implémenter une nouvelle version de la librairie standard d’entrées/sorties à l’aide des primitivessystèmes
  285. * .1. Donnez une définition du type FICHIER. N’oubliez de prévoir la bufferisation des entrées/sorties
  286. * .2. Programmez la fonctionmy_open, analogue àfopen. Pour simplifier, on ne considérera que les modesd’ouverture"r"et"w"
  287. * .3. Reprenez l’exercice 5.6 pour programmermy_getc, analogue à getc
  288. * .4. Programmez la fonctionmy_putc, analogue àputc(qui bufferise en sortie de la même manière quegetcbufferise en entrée)
  289. * .5. Programmez la fonctionmy_close, analogue àfclose
  290. * .6. Pour tester, écrivez une fonctionmainqui ouvre deux fichierstotoettataen lecture, puis qui lit enboucle 1 caractère dans chacun des deux fichiers et les affiche sur la sortie standard. Votre boucle s’ar-rêtera dès que vous rencontrez la première fin de fichier.
  291. */
  292.  
  293. typedef struct Fichier {
  294. int fd;
  295. int buff_size;
  296. int cur_buffPos;
  297. char buff[BUFF_SIZE];
  298.  
  299. char (*my_getchar)(Fichier *fichier);
  300. void (*my_putchar)(char ch,Fichier *fichier);
  301. } FICHIER;
  302.  
  303. void my_close(FICHIER *file) {
  304. close(file->fd);
  305. free(file);
  306. }
  307.  
  308. char my_getChar(FICHIER *file) {
  309. char value = '\n';
  310. //readi if buff_size == 0
  311. if (file->buff_size == 0) {
  312. if (int readSuccess = read(file->fd, file->buff, BUFF_SIZE) >= 0) {
  313. file->buff_size = readSuccess;
  314. file->cur_buffPos = 0;
  315. } else {
  316. perror("Error on read:");
  317. my_close(file);
  318. exit(EXIT_FAILURE);
  319. }
  320. }
  321. //return value at buff position;
  322. if (file->buff_size > 0) {
  323. value = file->buff[file->cur_buffPos];
  324. file->buff_size--;
  325. file->cur_buffPos++;
  326. return value;
  327. }
  328. return value;
  329. }
  330.  
  331. void my_putChar(char ch,FICHIER *file) {
  332. static bool isBuffer_full = false;
  333. if (isBuffer_full) {
  334.  
  335. if (int written = write(file->fd,&file->buff[file->cur_buffPos] , 1) == -1){
  336. perror("Error: write on file");
  337. my_close(file);
  338. exit(EXIT_FAILURE);
  339. }
  340. file->buff_size--;
  341. file->cur_buffPos++;
  342.  
  343. if(file->buff_size == 0){
  344. isBuffer_full = !isBuffer_full;
  345. }
  346. }
  347. if (!isBuffer_full) {
  348. //read
  349. file->cur_buffPos = 0;
  350. file->buff[file->cur_buffPos] = ch;
  351. file->buff_size++;
  352. if(file->buff_size == BUFF_SIZE){
  353. isBuffer_full = !isBuffer_full;
  354. return;
  355. }
  356. file->cur_buffPos++;
  357. }
  358. }
  359.  
  360. FICHIER *my_open(char *filePath, char mode) {
  361. if (!filePath) {
  362. perror("Error: file path name is NULL");
  363. exit(EXIT_FAILURE);
  364. }
  365. FICHIER *file = NULL;
  366. int fd = 0;
  367. switch (mode) {
  368. case 'r':
  369. fd = open(filePath, O_RDONLY);
  370. break;
  371. case 'w':
  372. fd = open(filePath, O_WRONLY | O_CREAT | O_TRUNC);
  373. break;
  374. default:
  375. printf("Unknown mode use :> r → for read; w → for write ");
  376. exit(EXIT_FAILURE);
  377. break;
  378. }
  379. file = (FICHIER *) calloc(1, sizeof(*file));
  380. if (!file) {
  381. close(fd);
  382. perror("Error: on allocating space for file");
  383. exit(EXIT_FAILURE);
  384. }
  385. if (fd == -1) {
  386. perror("Error: on opening file");
  387. free(file);
  388. exit(EXIT_FAILURE);
  389. }
  390. //struct init part
  391. file->fd = fd;
  392. file->buff_size = BUFF_SIZE;
  393. file->my_getchar = my_getChar;
  394. file->my_putchar = my_putChar;
  395. //end struct init part
  396. return file;
  397. }
  398.  
  399.  
  400. ```
  401.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement