1. Си / Говнокод #11431

    +141

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    char **fpwd=(char**)malloc(160);
    int lstat(const char *path, struct stat *buf);
    int process_dir(const char *dir,struct stat *st1,ino_t cit);
    char *cdir=".";
    ino_t ci=0;
    struct stat cstat;
    int errors = 0;
    char *myname;
    int cnt=0;
    int main(int argc,char **argv){
      myname=argv[0];
      cdir=(char*)malloc(512);
      if(cdir==NULL){
        perror("Not sufficient memory");
        errors++;
        return -1;
      }
       memset(cdir,'\0',512);
      strcat(cdir,cdir);
      strcat(cdir,".");
      if(fpwd==NULL){
        fprintf(stderr, "%s: out of memory: %s\n",
      			myname, strerror(errno));
      		errors++;
     		return 0;
     }
    for(int i=0;i<=160;i++){
      *(fpwd+i)=(char*)malloc(1600);
     if(*(fpwd+i)==NULL){perror("Not sufficient memory");
     errors++;
     return -1;
     }
    }
    while(ci!=2){
      if(lstat(".",&cstat)==-1){
        fprintf(stderr, "%s: can't find file:%s %s\n",
     			myname, cdir, strerror(errno));
        errors++;
    	return -1;
      };
      ci=cstat.st_ino;
      if(process_dir("..",&cstat,ci)==-1){
            fprintf(stderr, "%s: can't process dir:%s %s\n",
     			myname, cdir, strerror(errno));
        errors++;
    	return -1;
      };
      int fd=open("..",O_RDONLY);
      if(fd<0){
       perror("open");
       errors++;return -1;
      }
        if(fchdir(fd)==-1){
          fprintf(stderr, "%s: can't chdir:%s\n",
     			myname, strerror(errno));
        errors++;return -1;
        };
        strcat(cdir,"/..");
    }
     for(int i=cnt-1;i>=0;i--){
        printf("/%s",fpwd[i]);
      }
      printf("\n");return 0;
    }
    int process_dir(const char *dir,struct stat *st1,ino_t cit/*,char **fpwd*/){
        DIR *dp;
        struct dirent *ent;
        struct stat *dstat;
        if ((dp = opendir(dir)) == NULL) {
    		fprintf(stderr, "%s: cannot open for reading: %s\n", dir, strerror(errno));errors++;return -1;
    	}
        if(lstat(dir,dstat)==-1){
    	    fprintf(stderr, "%s: can't find file:%s %s\n",
     			myname, cdir, strerror(errno));
    			errors++;
    	return -1;
          }
    	while ((ent = readdir(dp)) != NULL){
    	  if(ent->d_ino==cit && strcmp(ent->d_name,".")!=0 && strcmp(ent->d_name,"..")!=0){
    	    strcat(fpwd[cnt++],ent->d_name);
    	     if (closedir(dp) != 0) {
    		fprintf(stderr, "%s: closedir: %s\n", dir, strerror(errno));
    		errors++;
    		return 1;
    	      }
    	    return 0;
    	  }
    	}
    	if (closedir(dp) != 0) {
    		fprintf(stderr, "%s: closedir: %s\n", dir, strerror(errno));
    		errors++;
    		return 1;
    	}
    	return 0;
    }

    Запостил: AliceGoth, 17 Июля 2012

    Комментарии (0) RSS

    Добавить комментарий

    Ошибка компиляции комментария:
    1. Гости могут высказаться только во вторник, пятницу или субботу
    ava Семь раз отмерь — один отрежь, guest!
    А не использовать ли нам bbcode?
    • [b]жирный[/b] — жирный
    • [i]курсив[/i] — курсив
    • [u]подчеркнутый[/u] — подчеркнутый
    • [s]перечеркнутый[/s] — перечеркнутый
    • [blink]мигающий[/blink] — мигающий
    • [color=red]цвет[/color] — цвет (подробнее)
    • [size=20]размер[/size] — размер (подробнее)
    • [code=<language>]some code[/code] (подробнее)
    Проверочный код